Class SSETransport

java.lang.Object
com.tnsai.mcp.client.SSETransport
All Implemented Interfaces:
McpTransport

public class SSETransport extends Object implements McpTransport
Server-Sent Events (SSE) transport implementation for MCP. Provides real-time streaming support with automatic reconnection, event type handling, and proper resource management.
 ┌─────────────────────────────────────────────────────────────────────────┐
 │                    SSE Transport Architecture                           │
 └─────────────────────────────────────────────────────────────────────────┘

   TnsAI Agent           SSETransport                MCP Server (SSE)
       │                     │                            │
       │  connect()          │                            │
       │ ───────────────────►│                            │
       │                     │  GET /sse                  │
       │                     │  Accept: text/event-stream │
       │                     │ ─────────────────────────► │
       │                     │                            │
       │                     │      SSE Stream Open       │
       │                     │ ◄───────────────────────── │
       │                     │                            │
       │  send(message)      │                            │
       │ ───────────────────►│  POST /message             │
       │                     │ ─────────────────────────► │
       │                     │                            │
       │                     │  event: message            │
       │                     │  data: {...}               │
       │  onMessage(data)    │ ◄───────────────────────── │
       │ ◄───────────────────│                            │
       │                     │                            │
       │                     │  event: error              │
       │                     │  data: {...}               │
       │  onError(err)       │ ◄───────────────────────── │
       │ ◄───────────────────│                            │
       │                     │                            │
 

Features

  • Real-time streaming with HTTP/2 support
  • Automatic reconnection with exponential backoff
  • Event type handling (message, error, ping)
  • Last-Event-ID support for resumable streams
  • Proper resource cleanup and thread management

Usage

 SSETransport transport = SSETransport.builder()
     .sseEndpoint("https://api.example.com/sse")
     .messageEndpoint("https://api.example.com/message")
     .apiKey("your-api-key")
     .build();

 McpClient client = new McpClient(transport);
 client.connect();
 
See Also:
  • Method Details

    • builder

      public static SSETransport.Builder builder()
      Creates a new SSETransport builder.
      Returns:
      a new Builder instance
    • withApiKey

      public static SSETransport withApiKey(String sseEndpoint, String messageEndpoint, String apiKey)
      Creates an SSETransport with API key authentication.
      Parameters:
      sseEndpoint - the SSE endpoint URL
      messageEndpoint - the message endpoint URL
      apiKey - the API key for authentication
      Returns:
      a new SSETransport instance
    • connect

      public void connect() throws IOException
      Description copied from interface: McpTransport
      Connects to the MCP server.
      Specified by:
      connect in interface McpTransport
      Throws:
      IOException - if connection fails
    • send

      public void send(String message) throws IOException
      Description copied from interface: McpTransport
      Sends a message to the MCP server.
      Specified by:
      send in interface McpTransport
      Parameters:
      message - the message to send (JSON-RPC request)
      Throws:
      IOException - if sending fails
    • setOnMessage

      public void setOnMessage(Consumer<String> messageConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for incoming messages from the MCP server.
      Specified by:
      setOnMessage in interface McpTransport
      Parameters:
      messageConsumer - the consumer for incoming messages (JSON-RPC response/notification)
    • setOnError

      public void setOnError(Consumer<Throwable> errorConsumer)
      Description copied from interface: McpTransport
      Sets a consumer for transport errors.
      Specified by:
      setOnError in interface McpTransport
      Parameters:
      errorConsumer - the consumer for transport errors
    • disconnect

      public void disconnect() throws IOException
      Description copied from interface: McpTransport
      Disconnects from the MCP server.
      Specified by:
      disconnect in interface McpTransport
      Throws:
      IOException - if disconnection fails
    • isConnected

      public boolean isConnected()
      Description copied from interface: McpTransport
      Checks if the transport is currently connected.
      Specified by:
      isConnected in interface McpTransport
      Returns:
      true if connected, false otherwise
    • getSseEndpoint

      public String getSseEndpoint()
      Returns the configured SSE endpoint.
      Returns:
      the SSE endpoint URL
    • getMessageEndpoint

      public String getMessageEndpoint()
      Returns the message endpoint (may be dynamic).
      Returns:
      the message endpoint URL
    • getLastEventId

      public String getLastEventId()
      Returns the last event ID received.
      Returns:
      the last event ID, or null if none received