Class HttpServletStreamableServerTransportProvider
java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
io.modelcontextprotocol.server.transport.HttpServletStreamableServerTransportProvider
- All Implemented Interfaces:
McpServerTransportProviderBase,McpStreamableServerTransportProvider,jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable
@WebServlet(asyncSupported=true)
public class HttpServletStreamableServerTransportProvider
extends jakarta.servlet.http.HttpServlet
implements McpStreamableServerTransportProvider
Server-side implementation of the Model Context Protocol (MCP) streamable transport
layer using HTTP with Server-Sent Events (SSE) through HttpServlet. This implementation
provides a bridge between synchronous HttpServlet operations and reactive programming
patterns to maintain compatibility with the reactive transport interface.
This is the HttpServlet equivalent of
io.modelcontextprotocol.server.transport.WebMvcStreamableServerTransportProvider
for the core MCP module, providing streamable HTTP transport functionality without
Spring dependencies.
- Author:
- Zachary German, Christian Tzolov, Dariusz Jędrzejczyk
- See Also:
-
McpStreamableServerTransportProviderHttpServlet- Serialized Form
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating instances ofHttpServletStreamableServerTransportProvider. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringEvent type for sending the message endpoint URI to clients.static final Stringstatic final StringEvent type for JSON-RPC messages sent through the SSE connection.static final Stringstatic final StringFields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Method Summary
Modifier and TypeMethodDescriptionbuilder()reactor.core.publisher.Mono<Void>Initiates a graceful shutdown of the transport.voiddestroy()Cleans up resources when the servlet is being destroyed.protected voiddoDelete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles DELETE requests for session deletion.protected voiddoGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles GET requests to establish SSE connections and message replay.protected voiddoPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles POST requests for incoming JSON-RPC messages from clients.reactor.core.publisher.Mono<Void>notifyClients(String method, Object params) Broadcasts a notification to all connected clients through their SSE connections.Returns the protocol version supported by this transport provider.voidresponseError(jakarta.servlet.http.HttpServletResponse response, int httpCode, McpError mcpError) voidsetSessionFactory(McpStreamableServerSession.Factory sessionFactory) Sets the session factory that will be used to create sessions for new clients.Methods inherited from class jakarta.servlet.http.HttpServlet
doHead, doOptions, doPatch, doPut, doTrace, getLastModified, init, isSensitiveHeader, service, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, logMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.modelcontextprotocol.spec.McpStreamableServerTransportProvider
close
-
Field Details
-
MESSAGE_EVENT_TYPE
Event type for JSON-RPC messages sent through the SSE connection.- See Also:
-
ENDPOINT_EVENT_TYPE
Event type for sending the message endpoint URI to clients.- See Also:
-
UTF_8
- See Also:
-
APPLICATION_JSON
- See Also:
-
TEXT_EVENT_STREAM
- See Also:
-
FAILED_TO_SEND_ERROR_RESPONSE
- See Also:
-
-
Method Details
-
protocolVersion
Description copied from interface:McpServerTransportProviderBaseReturns the protocol version supported by this transport provider.- Specified by:
protocolVersionin interfaceMcpServerTransportProviderBase- Returns:
- the protocol version as a string
-
setSessionFactory
Description copied from interface:McpStreamableServerTransportProviderSets the session factory that will be used to create sessions for new clients. An implementation of the MCP server MUST call this method before any MCP interactions take place.- Specified by:
setSessionFactoryin interfaceMcpStreamableServerTransportProvider- Parameters:
sessionFactory- the session factory to be used for initiating client sessions
-
notifyClients
Broadcasts a notification to all connected clients through their SSE connections. If any errors occur during sending to a particular client, they are logged but don't prevent sending to other clients.- Specified by:
notifyClientsin interfaceMcpServerTransportProviderBase- Specified by:
notifyClientsin interfaceMcpStreamableServerTransportProvider- Parameters:
method- The method name for the notificationparams- The parameters for the notification- Returns:
- A Mono that completes when the broadcast attempt is finished
- See Also:
-
closeGracefully
Initiates a graceful shutdown of the transport.- Specified by:
closeGracefullyin interfaceMcpServerTransportProviderBase- Specified by:
closeGracefullyin interfaceMcpStreamableServerTransportProvider- Returns:
- A Mono that completes when all cleanup operations are finished
-
doGet
protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException Handles GET requests to establish SSE connections and message replay.- Overrides:
doGetin classjakarta.servlet.http.HttpServlet- Parameters:
request- The HTTP servlet requestresponse- The HTTP servlet response- Throws:
jakarta.servlet.ServletException- If a servlet-specific error occursIOException- If an I/O error occurs
-
doPost
protected void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException Handles POST requests for incoming JSON-RPC messages from clients.- Overrides:
doPostin classjakarta.servlet.http.HttpServlet- Parameters:
request- The HTTP servlet request containing the JSON-RPC messageresponse- The HTTP servlet response- Throws:
jakarta.servlet.ServletException- If a servlet-specific error occursIOException- If an I/O error occurs
-
doDelete
protected void doDelete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException Handles DELETE requests for session deletion.- Overrides:
doDeletein classjakarta.servlet.http.HttpServlet- Parameters:
request- The HTTP servlet requestresponse- The HTTP servlet response- Throws:
jakarta.servlet.ServletException- If a servlet-specific error occursIOException- If an I/O error occurs
-
responseError
public void responseError(jakarta.servlet.http.HttpServletResponse response, int httpCode, McpError mcpError) throws IOException - Throws:
IOException
-
destroy
public void destroy()Cleans up resources when the servlet is being destroyed.This method ensures a graceful shutdown by closing all client connections before calling the parent's destroy method.
- Specified by:
destroyin interfacejakarta.servlet.Servlet- Overrides:
destroyin classjakarta.servlet.GenericServlet
-
builder
-