Skip to content

Commit d112e44

Browse files
committed
Document when client disconnects triggers a cancellation
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 99fefaf commit d112e44

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/McpAsyncResponse.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public abstract class McpAsyncResponse<O> {
2121
private McpAsyncResponse() {
2222
}
2323

24+
/**
25+
* Answer with {@code result}. Over HTTP a disconnect is only noticed when something
26+
* is written, so a request answered this way cannot be cancelled there; work that
27+
* should stop when the client goes away belongs in a {@link #streaming} response. See
28+
* {@code HttpServletMcpTransport}.
29+
*/
2430
public static <O> McpAsyncResponse<O> result(O result) {
2531
return new Result<>(result);
2632
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/McpSyncResponse.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public abstract class McpSyncResponse<O> {
1818
private McpSyncResponse() {
1919
}
2020

21+
/**
22+
* Answer with {@code result}. Sync handler code is never interrupted, so a cancelled
23+
* request still runs to completion; cancellation only drops its output. Over HTTP a
24+
* request answered this way does not even notice the client disconnecting. See
25+
* {@code HttpServletMcpTransport}.
26+
*/
2127
public static <O> McpSyncResponse<O> result(O result) {
2228
return new Result<>(result);
2329
}

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/transport/HttpServletMcpTransport.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
* served on the container thread, so thread-locals set by servlet filters are visible to
6161
* sync handlers; {@code subscriptions/listen} is served asynchronously.
6262
* <p>
63+
* The MCP specification treats a client disconnect as cancellation of its request, but
64+
* the servlet API only reveals a disconnect when a write fails. A handler answering with
65+
* a plain result writes nothing until it is done, so it always runs to completion and its
66+
* result goes nowhere. A streaming response notices at its next message or keep-alive
67+
* (see {@link Builder#keepAliveInterval}) and cancels its body. Sync handler code is
68+
* blocking and is never interrupted, so a sync body keeps running until it returns; only
69+
* its output is dropped. Work that must stop when the client goes away belongs in an
70+
* async streaming response.
71+
* <p>
6372
* Authentication and authorization, including {@code 401}/{@code 403} challenges, belong
6473
* in servlet filters in front of this servlet. Filters may decide on the
6574
* {@code Mcp-Method} and {@code Mcp-Name} headers: a request whose headers disagree with
@@ -555,7 +564,8 @@ public Builder httpHeaderValidator(ServerHttpHeaderValidator httpHeaderValidator
555564
/**
556565
* How often an open event stream gets an SSE comment, keeping intermediaries from
557566
* closing it and detecting clients that went away. {@code null} disables
558-
* keep-alives. Defaults to 30 seconds.
567+
* keep-alives; a quiet stream then only notices a disconnect when its next
568+
* message fails to write. Defaults to 30 seconds.
559569
*/
560570
public Builder keepAliveInterval(Duration keepAliveInterval) {
561571
Assert.isTrue(keepAliveInterval == null || !(keepAliveInterval.isNegative() || keepAliveInterval.isZero()),

0 commit comments

Comments
 (0)