Class HttpServletSseServerTransport
- All Implemented Interfaces:
McpTransport,ServerMcpTransport,jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable
The transport handles two types of endpoints:
- SSE endpoint (/sse) - Establishes a long-lived connection for server-to-client events
- Message endpoint (configurable) - Handles client-to-server message requests
Features:
- Asynchronous message handling using Servlet 6.0 async support
- Session management for multiple client connections
- Graceful shutdown support
- Error handling and response formatting
- Author:
- Christian Tzolov, Alexandros Pappas
- See Also:
-
ServerMcpTransportHttpServlet- Serialized Form
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringDefault endpoint path for SSE connectionsstatic final StringEvent type for endpoint informationstatic final Stringstatic final StringEvent type for regular messagesstatic final StringFields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Constructor Summary
ConstructorsConstructorDescriptionHttpServletSseServerTransport(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint) Creates a new HttpServletSseServerTransport instance with the default SSE endpoint.HttpServletSseServerTransport(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint, String sseEndpoint) Creates a new HttpServletSseServerTransport instance with a custom SSE endpoint. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the transport.reactor.core.publisher.Mono<Void>Initiates a graceful shutdown of the transport.reactor.core.publisher.Mono<Void>connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Sets up the message handler for processing client requests.voiddestroy()Cleans up resources when the servlet is being destroyed.protected voiddoGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles GET requests to establish SSE connections.protected voiddoPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Handles POST requests for client messages.reactor.core.publisher.Mono<Void>sendMessage(McpSchema.JSONRPCMessage message) Broadcasts a message to all connected clients.<T> TunmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Unmarshals data from one type to another using the object mapper.Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPatch, doPut, doTrace, getLastModified, init, isSensitiveHeader, service, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
UTF_8
- See Also:
-
APPLICATION_JSON
- See Also:
-
FAILED_TO_SEND_ERROR_RESPONSE
- See Also:
-
DEFAULT_SSE_ENDPOINT
Default endpoint path for SSE connections- See Also:
-
MESSAGE_EVENT_TYPE
Event type for regular messages- See Also:
-
ENDPOINT_EVENT_TYPE
Event type for endpoint information- See Also:
-
-
Constructor Details
-
HttpServletSseServerTransport
public HttpServletSseServerTransport(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint, String sseEndpoint) Creates a new HttpServletSseServerTransport instance with a custom SSE endpoint.- Parameters:
objectMapper- The JSON object mapper to use for message serialization/deserializationmessageEndpoint- The endpoint path where clients will send their messagessseEndpoint- The endpoint path where clients will establish SSE connections
-
HttpServletSseServerTransport
public HttpServletSseServerTransport(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint) Creates a new HttpServletSseServerTransport instance with the default SSE endpoint.- Parameters:
objectMapper- The JSON object mapper to use for message serialization/deserializationmessageEndpoint- The endpoint path where clients will send their messages
-
-
Method Details
-
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.This method sets up a new SSE connection when a client connects to the SSE endpoint. It configures the response headers for SSE, creates a new session, and sends the initial endpoint information to the client.
- 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 client messages.This method processes incoming messages from clients, routes them through the connect handler if configured, and sends back the appropriate response. It handles error cases and formats error responses according to the MCP specification.
- Overrides:
doPostin 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
-
connect
public reactor.core.publisher.Mono<Void> connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Sets up the message handler for processing client requests.- Specified by:
connectin interfaceMcpTransport- Parameters:
handler- The function to process incoming messages and produce responses- Returns:
- A Mono that completes when the handler is set up
-
sendMessage
Broadcasts a message to all connected clients.This method serializes the message and sends it to all active client sessions. If a client is disconnected, its session is removed.
- Specified by:
sendMessagein interfaceMcpTransport- Parameters:
message- The message to broadcast- Returns:
- A Mono that completes when the message has been sent to all clients
-
close
public void close()Closes the transport.This implementation delegates to the super class's close method.
- Specified by:
closein interfaceMcpTransport
-
unmarshalFrom
Unmarshals data from one type to another using the object mapper.- Specified by:
unmarshalFromin interfaceMcpTransport- Type Parameters:
T- The target type- Parameters:
data- The source datatypeRef- The type reference for the target type- Returns:
- The unmarshaled data
-
closeGracefully
Initiates a graceful shutdown of the transport.This method marks the transport as closing and closes all active client sessions. New connection attempts will be rejected during shutdown.
- Specified by:
closeGracefullyin interfaceMcpTransport- Returns:
- A Mono that completes when all sessions have been closed
-
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
-