Skip to content

Commit 8704863

Browse files
committed
Add documentation
1 parent 6da69b4 commit 8704863

File tree

12 files changed

+435
-267
lines changed

12 files changed

+435
-267
lines changed

Diff for: mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/server/transport/WebFluxSseServerTransportProvider.java

+15-53
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
* <p>
3939
* Key features:
4040
* <ul>
41-
* <li>Implements the {@link ServerMcpTransport} interface for MCP server transport
42-
* functionality</li>
41+
* <li>Implements the {@link McpServerTransportProvider} interface that allows managing
42+
* {@link McpServerSession} instances and enabling their communication with the
43+
* {@link McpServerTransport} abstraction.</li>
4344
* <li>Uses WebFlux for non-blocking request handling and SSE support</li>
4445
* <li>Maintains client sessions for reliable message delivery</li>
4546
* <li>Supports graceful shutdown with session cleanup</li>
@@ -55,12 +56,13 @@
5556
*
5657
* <p>
5758
* This implementation is thread-safe and can handle multiple concurrent client
58-
* connections. It uses {@link ConcurrentHashMap} for session management and Reactor's
59-
* {@link Sinks} for thread-safe message broadcasting.
59+
* connections. It uses {@link ConcurrentHashMap} for session management and Project
60+
* Reactor's non-blocking APIs for message processing and delivery.
6061
*
6162
* @author Christian Tzolov
6263
* @author Alexandros Pappas
63-
* @see ServerMcpTransport
64+
* @author Dariusz Jędrzejczyk
65+
* @see McpServerTransport
6466
* @see ServerSentEvent
6567
*/
6668
public class WebFluxSseServerTransportProvider implements McpServerTransportProvider {
@@ -103,7 +105,7 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
103105
private volatile boolean isClosing = false;
104106

105107
/**
106-
* Constructs a new WebFlux SSE server transport instance.
108+
* Constructs a new WebFlux SSE server transport provider instance.
107109
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
108110
* of MCP messages. Must not be null.
109111
* @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
@@ -126,8 +128,8 @@ public WebFluxSseServerTransportProvider(ObjectMapper objectMapper, String messa
126128
}
127129

128130
/**
129-
* Constructs a new WebFlux SSE server transport instance with the default SSE
130-
* endpoint.
131+
* Constructs a new WebFlux SSE server transport provider instance with the default
132+
* SSE endpoint.
131133
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
132134
* of MCP messages. Must not be null.
133135
* @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
@@ -179,8 +181,10 @@ public Mono<Void> notifyClients(String method, Map<String, Object> params) {
179181
.then();
180182
}
181183

184+
// FIXME: This javadoc makes claims about using isClosing flag but it's not actually
185+
// doing that.
182186
/**
183-
* Initiates a graceful shutdown of the transport. This method ensures all active
187+
* Initiates a graceful shutdown of all the sessions. This method ensures all active
184188
* sessions are properly closed and cleaned up.
185189
*
186190
* <p>
@@ -220,18 +224,8 @@ public RouterFunction<?> getRouterFunction() {
220224
/**
221225
* Handles new SSE connection requests from clients. Creates a new session for each
222226
* connection and sets up the SSE event stream.
223-
*
224-
* <p>
225-
* The handler performs the following steps:
226-
* <ul>
227-
* <li>Generates a unique session ID</li>
228-
* <li>Creates a new ClientSession instance</li>
229-
* <li>Sends the message endpoint URI as an initial event</li>
230-
* <li>Sets up message forwarding for the session</li>
231-
* <li>Handles connection cleanup on completion or errors</li>
232-
* </ul>
233227
* @param request The incoming server request
234-
* @return A response with the SSE event stream
228+
* @return A Mono which emits a response with the SSE event stream
235229
*/
236230
private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
237231
if (isClosing) {
@@ -275,7 +269,7 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
275269
* <li>Handles various error conditions with appropriate error responses</li>
276270
* </ul>
277271
* @param request The incoming server request containing the JSON-RPC message
278-
* @return A response indicating the message processing result
272+
* @return A Mono emitting the response indicating the message processing result
279273
*/
280274
private Mono<ServerResponse> handleMessage(ServerRequest request) {
281275
if (isClosing) {
@@ -307,38 +301,6 @@ private Mono<ServerResponse> handleMessage(ServerRequest request) {
307301
});
308302
}
309303

310-
/*
311-
* Current:
312-
*
313-
* framework layer: var transport = new WebFluxSseServerTransport(objectMapper,
314-
* "/mcp", "/sse"); McpServer.async(ServerMcpTransport transport)
315-
*
316-
* client connects -> WebFluxSseServerTransport creates a: - var sessionTransport =
317-
* WebFluxMcpSessionTransport - ServerMcpSession(sessionId, sessionTransport)
318-
*
319-
* WebFluxSseServerTransport IS_A ServerMcpTransport IS_A McpTransport
320-
* WebFluxMcpSessionTransport IS_A ServerMcpSessionTransport IS_A McpTransport
321-
*
322-
* McpTransport contains connect() which should be removed ClientMcpTransport should
323-
* have connect() ServerMcpTransport should have setSessionFactory()
324-
*
325-
* Possible Future: var transportProvider = new
326-
* WebFluxSseServerTransport(objectMapper, "/mcp", "/sse"); WebFluxSseServerTransport
327-
* IS_A ServerMcpTransportProvider ? ServerMcpTransportProvider creates
328-
* ServerMcpTransport
329-
*
330-
* // disadvantage - too much breaks, e.g. McpServer.async(ServerMcpTransportProvider
331-
* transportProvider)
332-
*
333-
* // advantage
334-
*
335-
* ClientMcpTransport and ServerMcpTransport BOTH represent 1:1 relationship
336-
*
337-
*
338-
*
339-
*
340-
*/
341-
342304
private class WebFluxMcpSessionTransport implements McpServerTransport {
343305

344306
private final FluxSink<ServerSentEvent<?>> sink;

0 commit comments

Comments
 (0)