Class WebFluxSseServerTransportProvider

java.lang.Object
io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider
All Implemented Interfaces:
io.modelcontextprotocol.spec.McpServerTransportProvider, io.modelcontextprotocol.spec.McpServerTransportProviderBase

public class WebFluxSseServerTransportProvider extends Object implements io.modelcontextprotocol.spec.McpServerTransportProvider
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 McpServerTransportProvider interface that allows managing McpServerSession instances and enabling their communication with the McpServerTransport abstraction.
  • 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:
  • McpServerTransport
  • ServerSentEvent
  • Field Details

    • MESSAGE_EVENT_TYPE

      public static final String MESSAGE_EVENT_TYPE
      Event type for JSON-RPC messages sent through the SSE connection.
      See Also:
    • ENDPOINT_EVENT_TYPE

      public static final String ENDPOINT_EVENT_TYPE
      Event type for sending the message endpoint URI to clients.
      See Also:
    • DEFAULT_SSE_ENDPOINT

      public static final String DEFAULT_SSE_ENDPOINT
      Default SSE endpoint path as specified by the MCP transport specification.
      See Also:
    • SESSION_ID

      public static final String SESSION_ID
      See Also:
    • DEFAULT_BASE_URL

      public static final String DEFAULT_BASE_URL
      See Also:
  • Method Details

    • protocolVersions

      public List<String> protocolVersions()
      Specified by:
      protocolVersions in interface io.modelcontextprotocol.spec.McpServerTransportProviderBase
    • setSessionFactory

      public void setSessionFactory(io.modelcontextprotocol.spec.McpServerSession.Factory sessionFactory)
      Specified by:
      setSessionFactory in interface io.modelcontextprotocol.spec.McpServerTransportProvider
    • notifyClients

      public reactor.core.publisher.Mono<Void> notifyClients(String method, Object params)
      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:
      notifyClients in interface io.modelcontextprotocol.spec.McpServerTransportProviderBase
      Parameters:
      method - The JSON-RPC method to send to clients
      params - 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

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Initiates a graceful shutdown of all the sessions. This method ensures all active sessions are properly closed and cleaned up.
      Specified by:
      closeGracefully in interface io.modelcontextprotocol.spec.McpServerTransportProviderBase
      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 RouterFunction for handling HTTP requests
    • builder