Package com.tnsai.mcp.client
Class SSETransport
java.lang.Object
com.tnsai.mcp.client.SSETransport
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic SSETransport.Builderbuilder()Creates a new SSETransport builder.voidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the last event ID received.Returns the message endpoint (may be dynamic).Returns the configured SSE endpoint.booleanChecks if the transport is currently connected.voidSends a message to the MCP server.voidsetOnError(Consumer<Throwable> errorConsumer) Sets a consumer for transport errors.voidsetOnMessage(Consumer<String> messageConsumer) Sets a consumer for incoming messages from the MCP server.static SSETransportwithApiKey(String sseEndpoint, String messageEndpoint, String apiKey) Creates an SSETransport with API key authentication.
-
Method Details
-
builder
Creates a new SSETransport builder.- Returns:
- a new Builder instance
-
withApiKey
Creates an SSETransport with API key authentication.- Parameters:
sseEndpoint- the SSE endpoint URLmessageEndpoint- the message endpoint URLapiKey- the API key for authentication- Returns:
- a new SSETransport instance
-
connect
Description copied from interface:McpTransportConnects to the MCP server.- Specified by:
connectin interfaceMcpTransport- Throws:
IOException- if connection fails
-
send
Description copied from interface:McpTransportSends a message to the MCP server.- Specified by:
sendin interfaceMcpTransport- Parameters:
message- the message to send (JSON-RPC request)- Throws:
IOException- if sending fails
-
setOnMessage
Description copied from interface:McpTransportSets a consumer for incoming messages from the MCP server.- Specified by:
setOnMessagein interfaceMcpTransport- Parameters:
messageConsumer- the consumer for incoming messages (JSON-RPC response/notification)
-
setOnError
Description copied from interface:McpTransportSets a consumer for transport errors.- Specified by:
setOnErrorin interfaceMcpTransport- Parameters:
errorConsumer- the consumer for transport errors
-
disconnect
Description copied from interface:McpTransportDisconnects from the MCP server.- Specified by:
disconnectin interfaceMcpTransport- Throws:
IOException- if disconnection fails
-
isConnected
public boolean isConnected()Description copied from interface:McpTransportChecks if the transport is currently connected.- Specified by:
isConnectedin interfaceMcpTransport- Returns:
- true if connected, false otherwise
-
getSseEndpoint
Returns the configured SSE endpoint.- Returns:
- the SSE endpoint URL
-
getMessageEndpoint
Returns the message endpoint (may be dynamic).- Returns:
- the message endpoint URL
-
getLastEventId
Returns the last event ID received.- Returns:
- the last event ID, or null if none received
-