Package com.tnsai.mcp.client
Class StreamableHttpTransport
java.lang.Object
com.tnsai.mcp.client.StreamableHttpTransport
- All Implemented Interfaces:
McpTransport
Streamable HTTP transport implementation for MCP (spec 2025-03-26).
Unlike the legacy SSE transport which uses separate SSE and message endpoints, Streamable HTTP uses a single endpoint for all communication via POST. Responses can be either JSON (application/json) or SSE (text/event-stream).
┌─────────────────────────────────────────────────────────────────────────┐
│ Streamable HTTP Transport Architecture │
└─────────────────────────────────────────────────────────────────────────┘
TnsAI Agent StreamableHttpTransport MCP Server
│ │ │
│ connect() │ │
│ ───────────────────►│ │
│ │ POST /mcp (initialize) │
│ │ ────────────────────────►│
│ │ │
│ │ Mcp-Session-Id: abc123 │
│ │ JSON or SSE response │
│ │ ◄────────────────────────│
│ │ │
│ send(message) │ │
│ ───────────────────►│ POST /mcp │
│ │ Mcp-Session-Id: abc123 │
│ │ ────────────────────────►│
│ │ │
│ │ JSON or SSE response │
│ onMessage(data) │ ◄────────────────────────│
│ ◄───────────────────│ │
│ │ │
│ │ GET /mcp (SSE stream) │
│ │ Mcp-Session-Id: abc123 │
│ │ ────────────────────────►│
│ │ │
│ │ SSE notifications │
│ onMessage(data) │ ◄────────────────────────│
│ ◄───────────────────│ │
│ │ │
│ disconnect() │ DELETE /mcp │
│ ───────────────────►│ Mcp-Session-Id: abc123 │
│ │ ────────────────────────►│
│ │ │
Usage
StreamableHttpTransport transport = StreamableHttpTransport.builder()
.endpoint("https://api.example.com/mcp")
.apiKey("your-api-key")
.build();
McpClient client = new McpClient(transport);
client.connect();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for StreamableHttpTransport.static classThrown when the server returns 404 indicating the session has expired. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a new StreamableHttpTransport builder.voidconnect()Connects to the MCP server.voidDisconnects from the MCP server.Returns the configured endpoint.Returns the last event ID received.Returns the current session ID.booleanChecks if the transport is currently connected.booleanReturns whether the notification stream is currently active.voidOpens a long-lived GET request to receive server-initiated notifications via SSE.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 StreamableHttpTransportwithApiKey(String endpoint, String apiKey) Creates a StreamableHttpTransport with API key authentication.
-
Method Details
-
builder
Creates a new StreamableHttpTransport builder.- Returns:
- a new Builder instance
-
withApiKey
Creates a StreamableHttpTransport with API key authentication.- Parameters:
endpoint- the MCP server endpoint URLapiKey- the API key for authentication- Returns:
- a new StreamableHttpTransport 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
-
openNotificationStream
Opens a long-lived GET request to receive server-initiated notifications via SSE.This runs in a background thread and delivers notifications through the
setOnMessage(Consumer)callback. Requires an established session (i.e., a successful initialize must have been sent first).- Throws:
IOException- if the transport is not connected or no session exists
-
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
-
getEndpoint
Returns the configured endpoint.- Returns:
- the endpoint URL
-
getSessionId
Returns the current session ID.- Returns:
- the session ID, or null if no session established
-
getLastEventId
Returns the last event ID received.- Returns:
- the last event ID, or null if none received
-
isNotificationStreamActive
public boolean isNotificationStreamActive()Returns whether the notification stream is currently active.- Returns:
- true if the GET notification stream is running
-