Class HttpClientTelemetrySseClientTransport

java.lang.Object
org.nasdanika.ai.mcp.HttpClientTelemetrySseClientTransport
All Implemented Interfaces:
io.modelcontextprotocol.spec.McpClientTransport, io.modelcontextprotocol.spec.McpTransport

public class HttpClientTelemetrySseClientTransport extends Object implements io.modelcontextprotocol.spec.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, Pavel Vlasov
See Also:
  • McpTransport
  • McpClientTransport
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected com.fasterxml.jackson.databind.ObjectMapper
    JSON object mapper for message serialization/deserialization
  • Constructor Summary

    Constructors
    Constructor
    Description
    HttpClientTelemetrySseClientTransport(String baseUri, io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator, BiConsumer<String,Long> durationConsumer)
    Creates a new transport instance with default HTTP client and object mapper.
    HttpClientTelemetrySseClientTransport(HttpClient.Builder clientBuilder, String baseUri, com.fasterxml.jackson.databind.ObjectMapper objectMapper, io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator, BiConsumer<String,Long> durationConsumer)
    Creates a new transport instance with custom HTTP client builder and object mapper.
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Mono<Void>
    Gracefully closes the transport connection.
    reactor.core.publisher.Mono<Void>
    connect(Function<reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage>,reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage>> handler)
    Establishes the SSE connection with the server and sets up message handling.
     
    reactor.core.publisher.Mono<Void>
    sendMessage(io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage message)
    Sends a JSON-RPC message to the server.
    <T> T
    unmarshalFrom(Object data, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
    Unmarshals 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, wait

    Methods inherited from interface io.modelcontextprotocol.spec.McpTransport

    close
  • Field Details

    • objectMapper

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

    • HttpClientTelemetrySseClientTransport

      public HttpClientTelemetrySseClientTransport(String baseUri, io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator, BiConsumer<String,Long> durationConsumer)
      Creates a new transport instance with default HTTP client and object mapper.
      Parameters:
      baseUri - the base URI of the MCP server
      tracer - If not null, creates a span for sendMessage HTTP request. Pass null when using TelemetryMcpClientTransportFilter to avoid two nested sendMessage spans
    • HttpClientTelemetrySseClientTransport

      public HttpClientTelemetrySseClientTransport(HttpClient.Builder clientBuilder, String baseUri, com.fasterxml.jackson.databind.ObjectMapper objectMapper, io.opentelemetry.api.trace.Tracer tracer, io.opentelemetry.context.propagation.TextMapPropagator propagator, BiConsumer<String,Long> durationConsumer)
      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
  • Method Details

    • connect

      public reactor.core.publisher.Mono<Void> connect(Function<reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage>,reactor.core.publisher.Mono<io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage>> handler)
      Establishes the SSE connection with the server and sets up message handling.

      This method:

      • Initiates the SSE connection
      • Handles endpoint discovery events
      • Processes incoming JSON-RPC messages
      Specified by:
      connect in interface io.modelcontextprotocol.spec.McpClientTransport
      Parameters:
      handler - the function to process received JSON-RPC messages
      Returns:
      a Mono that completes when the connection is established
    • sendMessage

      public reactor.core.publisher.Mono<Void> sendMessage(io.modelcontextprotocol.spec.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 io.modelcontextprotocol.spec.McpTransport
      Parameters:
      message - the JSON-RPC message to send
      Returns:
      a Mono that completes when the message is sent
      Throws:
      io.modelcontextprotocol.spec.McpError - if the message endpoint is not available or the wait times out
    • getHttpRequestBuilder

    • closeGracefully

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

      Sets the closing flag and cancels any pending connection future. This prevents new messages from being sent and allows ongoing operations to complete.

      Specified by:
      closeGracefully in interface io.modelcontextprotocol.spec.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)
      Unmarshals data to the specified type using the configured object mapper.
      Specified by:
      unmarshalFrom in interface io.modelcontextprotocol.spec.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