Class WebFluxSseClientTransport
java.lang.Object
io.modelcontextprotocol.client.transport.WebFluxSseClientTransport
- All Implemented Interfaces:
McpClientTransport,McpTransport
Server-Sent Events (SSE) implementation of the
McpTransport that follows the MCP HTTP with SSE
transport specification.
This transport establishes a bidirectional communication channel where:
- Inbound messages are received through an SSE connection from the server
- Outbound messages are sent via HTTP POST requests to a server-provided endpoint
The message flow follows these steps:
- The client establishes an SSE connection to the server's /sse endpoint
- The server sends an 'endpoint' event containing the URI for sending messages
WebClient for HTTP communications and supports JSON
serialization/deserialization of messages.- Author:
- Christian Tzolov
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final reactor.core.publisher.Sinks.One<String>Sink for managing the message endpoint URI provided by the server.protected com.fasterxml.jackson.databind.ObjectMapperObjectMapper for serializing outbound messages and deserializing inbound messages. -
Constructor Summary
ConstructorsConstructorDescriptionWebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder) Constructs a new SseClientTransport with the specified WebClient builder.WebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructs a new SseClientTransport with the specified WebClient builder and ObjectMapper.WebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, com.fasterxml.jackson.databind.ObjectMapper objectMapper, String sseEndpoint) Constructs a new SseClientTransport with the specified WebClient builder and ObjectMapper. -
Method Summary
Modifier and TypeMethodDescriptionbuilder(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder) Creates a new builder forWebFluxSseClientTransport.reactor.core.publisher.Mono<Void>Implements graceful shutdown of the transport.reactor.core.publisher.Mono<Void>connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Establishes a connection to the MCP server using Server-Sent Events (SSE).protected reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<String>>Initializes and starts the inbound SSE event processing.reactor.core.publisher.Mono<Void>sendMessage(McpSchema.JSONRPCMessage message) Sends a JSON-RPC message to the server using the endpoint provided during connection.<T> TunmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Unmarshalls data from a generic Object into the specified type using the configured ObjectMapper.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
-
objectMapper
protected com.fasterxml.jackson.databind.ObjectMapper objectMapperObjectMapper for serializing outbound messages and deserializing inbound messages. Handles conversion between JSON-RPC messages and their string representation. -
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
-
WebFluxSseClientTransport
public WebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder) Constructs a new SseClientTransport with the specified WebClient builder. Uses a default ObjectMapper instance for JSON processing.- Parameters:
webClientBuilder- the WebClient.Builder to use for creating the WebClient instance- Throws:
IllegalArgumentException- if webClientBuilder is null
-
WebFluxSseClientTransport
public WebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Constructs a new SseClientTransport with the specified WebClient builder and ObjectMapper. Initializes both inbound and outbound message processing pipelines.- Parameters:
webClientBuilder- the WebClient.Builder to use for creating the WebClient instanceobjectMapper- the ObjectMapper to use for JSON processing- Throws:
IllegalArgumentException- if either parameter is null
-
WebFluxSseClientTransport
public WebFluxSseClientTransport(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder, com.fasterxml.jackson.databind.ObjectMapper objectMapper, String sseEndpoint) Constructs a new SseClientTransport with the specified WebClient builder and ObjectMapper. Initializes both inbound and outbound message processing pipelines.- Parameters:
webClientBuilder- the WebClient.Builder to use for creating the WebClient instanceobjectMapper- the ObjectMapper to use for JSON processingsseEndpoint- the SSE endpoint URI to use for establishing the connection- Throws:
IllegalArgumentException- if either parameter is null
-
-
Method Details
-
protocolVersion
- Specified by:
protocolVersionin interfaceMcpTransport
-
connect
public reactor.core.publisher.Mono<Void> connect(Function<reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>, reactor.core.publisher.Mono<McpSchema.JSONRPCMessage>> handler) Establishes a connection to the MCP server using Server-Sent Events (SSE). This method initiates the SSE connection and sets up the message processing pipeline.The connection process follows these steps:
- Establishes an SSE connection to the server's /sse endpoint
- Waits for the server to send an 'endpoint' event with the message posting URI
- Sets up message handling for incoming JSON-RPC messages
The connection is considered established only after receiving the endpoint event from the server.
- Specified by:
connectin interfaceMcpClientTransport- Parameters:
handler- a function that processes incoming JSON-RPC messages and returns responses- Returns:
- a Mono that completes when the connection is fully established
- Throws:
McpError- if there's an error processing SSE events or if an unrecognized event type is received
-
sendMessage
Sends a JSON-RPC message to the server using the endpoint provided during connection.Messages are sent via HTTP POST requests to the server-provided endpoint URI. The message is serialized to JSON before transmission. If the transport is in the process of closing, the message send operation is skipped gracefully.
- Specified by:
sendMessagein interfaceMcpTransport- Parameters:
message- the JSON-RPC message to send- Returns:
- a Mono that completes when the message has been sent successfully
- Throws:
RuntimeException- if message serialization fails
-
eventStream
protected reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<String>> eventStream()Initializes and starts the inbound SSE event processing. Establishes the SSE connection and sets up event handling for both message and endpoint events. Includes automatic retry logic for handling transient connection failures. -
closeGracefully
Implements graceful shutdown of the transport. Cleans up all resources including subscriptions and schedulers. Ensures orderly shutdown of both inbound and outbound message processing.- Specified by:
closeGracefullyin interfaceMcpTransport- Returns:
- a Mono that completes when shutdown is finished
-
unmarshalFrom
Unmarshalls data from a generic Object into the specified type using the configured ObjectMapper.This method is particularly useful when working with JSON-RPC parameters or result objects that need to be converted to specific Java types. It leverages Jackson's type conversion capabilities to handle complex object structures.
- Specified by:
unmarshalFromin interfaceMcpTransport- Type Parameters:
T- the target type to convert the data into- Parameters:
data- the source object to converttypeRef- the TypeReference describing the target type- Returns:
- the unmarshalled object of type T
- Throws:
IllegalArgumentException- if the conversion cannot be performed
-
builder
public static WebFluxSseClientTransport.Builder builder(org.springframework.web.reactive.function.client.WebClient.Builder webClientBuilder) Creates a new builder forWebFluxSseClientTransport.- Parameters:
webClientBuilder- the WebClient.Builder to use for creating the WebClient instance- Returns:
- a new builder instance
-