Class HttpClientStreamableHttpTransport

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

public class HttpClientStreamableHttpTransport extends Object implements McpClientTransport
An implementation of the Streamable HTTP protocol as defined by the 2025-03-26 version of the MCP specification.

The transport is capable of resumability and reconnects. It reacts to transport-level session invalidation and will propagate appropriate exceptions to the higher level abstraction layer when needed in order to allow proper state management. The implementation handles servers that are stateful and provide session meta information, but can also communicate with stateless servers that do not provide a session identifier and do not support SSE streams.

This implementation does not handle backwards compatibility with the "HTTP with SSE" transport. In order to communicate over the phased-out 2024-11-05 protocol, use HttpClientSseClientTransport or WebFluxSseClientTransport.

Author:
Christian Tzolov
See Also:
  • Field Details

    • NOT_FOUND

      public static int NOT_FOUND
    • METHOD_NOT_ALLOWED

      public static int METHOD_NOT_ALLOWED
    • BAD_REQUEST

      public static int BAD_REQUEST
  • Method Details

    • protocolVersions

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

      public static HttpClientStreamableHttpTransport.Builder builder(String baseUri)
    • 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.
    • setExceptionHandler

      public void setExceptionHandler(Consumer<Throwable> handler)
      Description copied from interface: McpClientTransport
      Sets the exception handler for exceptions raised on the transport layer.
      Specified by:
      setExceptionHandler in interface McpClientTransport
      Parameters:
      handler - Allows reacting to transport level exceptions by the higher layers
    • closeGracefully

      public reactor.core.publisher.Mono<Void> closeGracefully()
      Description copied from interface: McpTransport
      Closes the transport connection and releases any associated resources asynchronously.
      Specified by:
      closeGracefully in interface McpTransport
      Returns:
      a reactor.core.publisher.Mono<Void> that completes when the connection has been closed.
    • toString

      public String toString(McpSchema.JSONRPCMessage message)
    • sendMessage

      public reactor.core.publisher.Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage)
      Description copied from interface: McpTransport
      Sends a message to the peer asynchronously.

      This method handles the transmission of messages to the server in an asynchronous manner. Messages are sent in JSON-RPC format as specified by the MCP protocol.

      Specified by:
      sendMessage in interface McpTransport
      Parameters:
      sentMessage - the McpSchema.JSONRPCMessage to be sent to the server
      Returns:
      a reactor.core.publisher.Mono<Void> that completes when the message has been sent
    • unmarshalFrom

      public <T> T unmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
      Description copied from interface: McpTransport
      Unmarshals the given data into an object of the specified type.
      Specified by:
      unmarshalFrom in interface McpTransport
      Type Parameters:
      T - the type of the object to unmarshal
      Parameters:
      data - the data to unmarshal
      typeRef - the type reference for the object to unmarshal
      Returns:
      the unmarshalled object