Class FlowSseClient
java.lang.Object
io.modelcontextprotocol.client.transport.FlowSseClient
A Server-Sent Events (SSE) client implementation using Java's Flow API for reactive
stream processing. This client establishes a connection to an SSE endpoint and
processes the incoming event stream, parsing SSE-formatted messages into structured
events.
The client supports standard SSE event fields including:
- event - The event type (defaults to "message" if not specified)
- id - The event ID
- data - The event payload data
Events are delivered to a provided FlowSseClient.SseEventHandler which can process events and
handle any errors that occur during the connection.
- Author:
- Christian Tzolov
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord class representing a Server-Sent Event with its standard fields.static interfaceInterface for handling SSE events and errors. -
Constructor Summary
ConstructorsConstructorDescriptionFlowSseClient(HttpClient httpClient) Creates a new FlowSseClient with the specified HTTP client.FlowSseClient(HttpClient httpClient, HttpRequest.Builder requestBuilder) Creates a new FlowSseClient with the specified HTTP client and request builder. -
Method Summary
Modifier and TypeMethodDescriptionvoidsubscribe(String url, FlowSseClient.SseEventHandler eventHandler) Subscribes to an SSE endpoint and processes the event stream.
-
Constructor Details
-
FlowSseClient
Creates a new FlowSseClient with the specified HTTP client.- Parameters:
httpClient- theHttpClientinstance to use for SSE connections
-
FlowSseClient
Creates a new FlowSseClient with the specified HTTP client and request builder.- Parameters:
httpClient- theHttpClientinstance to use for SSE connectionsrequestBuilder- theHttpRequest.Builderto use for SSE requests
-
-
Method Details
-
subscribe
Subscribes to an SSE endpoint and processes the event stream.This method establishes a connection to the specified URL and begins processing the SSE stream. Events are parsed and delivered to the provided event handler. The connection remains active until either an error occurs or the server closes the connection.
- Parameters:
url- the SSE endpoint URL to connect toeventHandler- the handler that will receive SSE events and error notifications- Throws:
RuntimeException- if the connection fails with a non-200 status code
-