Class HttpClientSseClientTransport
java.lang.Object
io.modelcontextprotocol.client.transport.HttpClientSseClientTransport
- All Implemented Interfaces:
McpClientTransport,McpTransport
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:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected McpJsonMapperJSON mapper for message serialization/deserializationprotected final reactor.core.publisher.Sinks.One<String>Sink for managing the message endpoint URI provided by the server. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new builder forHttpClientSseClientTransport.reactor.core.publisher.Mono<Void>Gracefully closes the transport connection.reactor.core.publisher.Mono<Void>connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Used to register the incoming messages' handler and potentially (eagerly) connect to the server.reactor.core.publisher.Mono<Void>sendMessage(McpSchema.JSONRPCMessage message) Sends a JSON-RPC message to the server.<T> TunmarshalFrom(Object data, TypeRef<T> typeRef) Unmarshal data to the specified type using the configured object mapper.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.modelcontextprotocol.spec.McpClientTransport
setExceptionHandlerMethods inherited from interface io.modelcontextprotocol.spec.McpTransport
close
-
Field Details
-
jsonMapper
JSON mapper for message serialization/deserialization -
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.
-
-
Method Details
-
protocolVersions
- Specified by:
protocolVersionsin interfaceMcpTransport
-
builder
Creates a new builder forHttpClientSseClientTransport.- 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:McpClientTransportUsed to register the incoming messages' handler and potentially (eagerly) connect to the server.- Specified by:
connectin interfaceMcpClientTransport- Parameters:
handler- a transformer for incoming messages- Returns:
- a
Monothat 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 returnedMonosimply means the client can now be used. An error can be retried according to the application requirements.
-
sendMessage
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:
sendMessagein interfaceMcpTransport- 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
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:
closeGracefullyin interfaceMcpTransport- Returns:
- a Mono that completes when the closing process is initiated
-
unmarshalFrom
Unmarshal data to the specified type using the configured object mapper.- Specified by:
unmarshalFromin interfaceMcpTransport- Type Parameters:
T- the target type- Parameters:
data- the data to unmarshaltypeRef- the type reference for the target type- Returns:
- the unmarshalled object
-