Class WebFluxSseServerTransportProvider
java.lang.Object
io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider
- All Implemented Interfaces:
McpServerTransportProvider,McpServerTransportProviderBase
Server-side implementation of the MCP (Model Context Protocol) HTTP transport using
Server-Sent Events (SSE). This implementation provides a bidirectional communication
channel between MCP clients and servers using HTTP POST for client-to-server messages
and SSE for server-to-client messages.
Key features:
- Implements the
McpServerTransportProviderinterface that allows managingMcpServerSessioninstances and enabling their communication with theMcpServerTransportabstraction. - Uses WebFlux for non-blocking request handling and SSE support
- Maintains client sessions for reliable message delivery
- Supports graceful shutdown with session cleanup
- Thread-safe message broadcasting to multiple clients
The transport sets up two main endpoints:
- SSE endpoint (/sse) - For establishing SSE connections with clients
- Message endpoint (configurable) - For receiving JSON-RPC messages from clients
This implementation is thread-safe and can handle multiple concurrent client
connections. It uses ConcurrentHashMap for session management and Project
Reactor's non-blocking APIs for message processing and delivery.
- Author:
- Christian Tzolov, Alexandros Pappas, Dariusz Jędrzejczyk
- See Also:
-
McpServerTransportServerSentEvent
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating instances ofWebFluxSseServerTransportProvider. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringDefault SSE endpoint path as specified by the MCP transport specification.static final StringEvent type for sending the message endpoint URI to clients.static final StringEvent type for JSON-RPC messages sent through the SSE connection. -
Constructor Summary
ConstructorsConstructorDescriptionWebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint) Deprecated.WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint, String sseEndpoint) Deprecated.Use the builderbuilder()instead for better configuration options.WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String baseUrl, String messageEndpoint, String sseEndpoint) Deprecated.Use the builderbuilder()instead for better configuration options.WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String baseUrl, String messageEndpoint, String sseEndpoint, Duration keepAliveInterval) Deprecated.Use the builderbuilder()instead for better configuration options. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()reactor.core.publisher.Mono<Void>Initiates a graceful shutdown of all the sessions.org.springframework.web.reactive.function.server.RouterFunction<?>Returns the WebFlux router function that defines the transport's HTTP endpoints.reactor.core.publisher.Mono<Void>notifyClients(String method, Object params) Broadcasts a JSON-RPC message to all connected clients through their SSE connections.voidsetSessionFactory(McpServerSession.Factory sessionFactory) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.modelcontextprotocol.spec.McpServerTransportProviderBase
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:
-
DEFAULT_SSE_ENDPOINT
Default SSE endpoint path as specified by the MCP transport specification.- See Also:
-
DEFAULT_BASE_URL
- See Also:
-
-
Constructor Details
-
WebFluxSseServerTransportProvider
@Deprecated public WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint) Deprecated.Use the builderbuilder()instead for better configuration options.Constructs a new WebFlux SSE server transport provider instance with the default SSE endpoint.- Parameters:
objectMapper- The ObjectMapper to use for JSON serialization/deserialization of MCP messages. Must not be null.messageEndpoint- The endpoint URI where clients should send their JSON-RPC messages. This endpoint will be communicated to clients during SSE connection setup. Must not be null.- Throws:
IllegalArgumentException- if either parameter is null
-
WebFluxSseServerTransportProvider
@Deprecated public WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String messageEndpoint, String sseEndpoint) Deprecated.Use the builderbuilder()instead for better configuration options.Constructs a new WebFlux SSE server transport provider instance.- Parameters:
objectMapper- The ObjectMapper to use for JSON serialization/deserialization of MCP messages. Must not be null.messageEndpoint- The endpoint URI where clients should send their JSON-RPC messages. This endpoint will be communicated to clients during SSE connection setup. Must not be null.- Throws:
IllegalArgumentException- if either parameter is null
-
WebFluxSseServerTransportProvider
@Deprecated public WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String baseUrl, String messageEndpoint, String sseEndpoint) Deprecated.Use the builderbuilder()instead for better configuration options.Constructs a new WebFlux SSE server transport provider instance.- Parameters:
objectMapper- The ObjectMapper to use for JSON serialization/deserialization of MCP messages. Must not be null.baseUrl- webflux message base pathmessageEndpoint- The endpoint URI where clients should send their JSON-RPC messages. This endpoint will be communicated to clients during SSE connection setup. Must not be null.- Throws:
IllegalArgumentException- if either parameter is null
-
WebFluxSseServerTransportProvider
@Deprecated public WebFluxSseServerTransportProvider(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String baseUrl, String messageEndpoint, String sseEndpoint, Duration keepAliveInterval) Deprecated.Use the builderbuilder()instead for better configuration options.Constructs a new WebFlux SSE server transport provider instance.- Parameters:
objectMapper- The ObjectMapper to use for JSON serialization/deserialization of MCP messages. Must not be null.baseUrl- webflux message base pathmessageEndpoint- The endpoint URI where clients should send their JSON-RPC messages. This endpoint will be communicated to clients during SSE connection setup. Must not be null.sseEndpoint- The SSE endpoint path. Must not be null.keepAliveInterval- The interval for sending keep-alive pings to clients.- Throws:
IllegalArgumentException- if either parameter is null
-
-
Method Details
-
protocolVersion
- Specified by:
protocolVersionin interfaceMcpServerTransportProviderBase
-
setSessionFactory
- Specified by:
setSessionFactoryin interfaceMcpServerTransportProvider
-
notifyClients
Broadcasts a JSON-RPC message to all connected clients through their SSE connections. The message is serialized to JSON and sent as a server-sent event to each active session.The method:
- Serializes the message to JSON
- Creates a server-sent event with the message data
- Attempts to send the event to all active sessions
- Tracks and reports any delivery failures
- Specified by:
notifyClientsin interfaceMcpServerTransportProviderBase- Parameters:
method- The JSON-RPC method to send to clientsparams- The method parameters to send to clients- Returns:
- A Mono that completes when the message has been sent to all sessions, or errors if any session fails to receive the message
-
closeGracefully
Initiates a graceful shutdown of all the sessions. This method ensures all active sessions are properly closed and cleaned up.- Specified by:
closeGracefullyin interfaceMcpServerTransportProviderBase- Returns:
- A Mono that completes when all sessions have been closed
-
getRouterFunction
public org.springframework.web.reactive.function.server.RouterFunction<?> getRouterFunction()Returns the WebFlux router function that defines the transport's HTTP endpoints. This router function should be integrated into the application's web configuration.The router function defines two endpoints:
- GET {sseEndpoint} - For establishing SSE connections
- POST {messageEndpoint} - For receiving client messages
- Returns:
- The configured
RouterFunctionfor handling HTTP requests
-
builder
-
builder()instead for better configuration options.