Class HttpClientSseClientTransport

java.lang.Object
io.modelcontextprotocol.client.transport.HttpClientSseClientTransport
All Implemented Interfaces:
McpClientTransport, McpTransport

public class HttpClientSseClientTransport extends Object implements McpClientTransport
Server-Sent Events (SSE) implementation of the McpTransport that follows the MCP HTTP with SSE transport specification, using Java's HttpClient.

This transport implementation establishes a bidirectional communication channel between client and server using SSE for server-to-client messages and HTTP POST requests for client-to-server messages. The transport:

  • Establishes an SSE connection to receive server messages
  • Handles endpoint discovery through SSE events
  • Manages message serialization/deserialization using Jackson
  • Provides graceful connection termination

The transport supports two types of SSE events:

  • 'endpoint' - Contains the URL for sending client messages
  • 'message' - Contains JSON-RPC message payload
Author:
Christian Tzolov
See Also:
  • Field Details

    • objectMapper

      protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
      JSON object mapper for message serialization/deserialization
    • messageEndpointSink

      protected final reactor.core.publisher.Sinks.One<String> messageEndpointSink
      Sink for managing the message endpoint URI provided by the server. Stores the most recent endpoint URI and makes it available for outbound message processing.
  • Constructor Details

    • HttpClientSseClientTransport

      @Deprecated(forRemoval=true) public HttpClientSseClientTransport(String baseUri)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder(String) instead. This constructor will be removed in future versions.
      Creates a new transport instance with default HTTP client and object mapper.
      Parameters:
      baseUri - the base URI of the MCP server
    • HttpClientSseClientTransport

      @Deprecated(forRemoval=true) public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, String baseUri, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder(String) instead. This constructor will be removed in future versions.
      Creates a new transport instance with custom HTTP client builder and object mapper.
      Parameters:
      clientBuilder - the HTTP client builder to use
      baseUri - the base URI of the MCP server
      objectMapper - the object mapper for JSON serialization/deserialization
      Throws:
      IllegalArgumentException - if objectMapper or clientBuilder is null
    • HttpClientSseClientTransport

      @Deprecated(forRemoval=true) public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, String baseUri, String sseEndpoint, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder(String) instead. This constructor will be removed in future versions.
      Creates a new transport instance with custom HTTP client builder and object mapper.
      Parameters:
      clientBuilder - the HTTP client builder to use
      baseUri - the base URI of the MCP server
      sseEndpoint - the SSE endpoint path
      objectMapper - the object mapper for JSON serialization/deserialization
      Throws:
      IllegalArgumentException - if objectMapper or clientBuilder is null
    • HttpClientSseClientTransport

      @Deprecated(forRemoval=true) public HttpClientSseClientTransport(HttpClient.Builder clientBuilder, HttpRequest.Builder requestBuilder, String baseUri, String sseEndpoint, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder(String) instead. This constructor will be removed in future versions.
      Creates a new transport instance with custom HTTP client builder, object mapper, and headers.
      Parameters:
      clientBuilder - the HTTP client builder to use
      requestBuilder - the HTTP request builder to use
      baseUri - the base URI of the MCP server
      sseEndpoint - the SSE endpoint path
      objectMapper - the object mapper for JSON serialization/deserialization
      Throws:
      IllegalArgumentException - if objectMapper, clientBuilder, or headers is null
  • Method Details

    • protocolVersions

      public List<String> protocolVersions()
      Specified by:
      protocolVersions in interface McpTransport
    • builder

      public static HttpClientSseClientTransport.Builder builder(String baseUri)
      Creates a new builder for HttpClientSseClientTransport.
      Parameters:
      baseUri - the base URI of the MCP server
      Returns:
      a new builder instance
    • connect

      public reactor.core.publisher.Mono<Void> connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>,reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler)
      Description copied from interface: McpClientTransport
      Used to register the incoming messages' handler and potentially (eagerly) connect to the server.
      Specified by:
      connect in interface McpClientTransport
      Parameters:
      handler - a transformer for incoming messages
      Returns:
      a Mono that terminates upon successful client setup. It can mean establishing a connection (which can be later disposed) but it doesn't have to, depending on the transport type. The successful termination of the returned Mono simply means the client can now be used. An error can be retried according to the application requirements.
    • sendMessage

      public reactor.core.publisher.Mono<Void> sendMessage(McpSchema.JSONRPCMessage message)
      Sends a JSON-RPC message to the server.

      This method waits for the message endpoint to be discovered before sending the message. The message is serialized to JSON and sent as an HTTP POST request.

      Specified by:
      sendMessage in interface McpTransport
      Parameters:
      message - the JSON-RPC message to send
      Returns:
      a Mono that completes when the message is sent
      Throws:
      McpError - if the message endpoint is not available or the wait times out
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Gracefully closes the transport connection.

      Sets the closing flag and disposes of the SSE subscription. This prevents new messages from being sent and allows ongoing operations to complete.

      Specified by:
      closeGracefully in interface McpTransport
      Returns:
      a Mono that completes when the closing process is initiated
    • unmarshalFrom

      public <T> T unmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      Unmarshal data to the specified type using the configured object mapper.
      Specified by:
      unmarshalFrom in interface McpTransport
      Type Parameters:
      T - the target type
      Parameters:
      data - the data to unmarshal
      typeRef - the type reference for the target type
      Returns:
      the unmarshalled object