Stop answering cancelled requests

Merged
MA
4mo ago
+464-204

When a peer sent notifications/cancelled for an in-flight request, the dispatcher interrupted the handler and then answered the request anyway with a synthesized error, {"code": 0, "message": "Request cancelled"} — a v1 carry-over pinned as a spec divergence. The 2026-07-28 transport rules say a cancelled request MUST NOT be answered (and 0 isn't a valid JSON-RPC code), so a cancelled request now settles with no response at all: no result if the handler runs to completion, no error if it fails. This matches typescript-sdk and csharp-sdk, which both suppress the write.

Motivation and Context

Removing the frame exposed that it was doing a second job on the 2025-era streamable HTTP wire, where a request's POST stays open until its response arrives — the error frame was what let the POST complete, so simply suppressing it would park the POST until session end. The dispatcher now signals the transport when a request settles unanswered (on_request_unanswered on the inbound message metadata), and the legacy transport ends the request's stream on it: empty 202 Accepted in JSON-response mode, a cleanly-ended event stream in SSE mode.

For the same reason, the client transport now releases an abandoned request's own POST at every era (not just 2026-07-28): otherwise the stream sits parked, or — against an event-store server — resumes via Last-Event-ID for an answer that will never exist. At 2025 the cancellation frame is still POSTed; only the lingering stream goes.

How Has This Been Tested?

Full suite (./scripts/test, 100% coverage, strict-no-cover) plus end-to-end runs: a stdio server subprocess driven with hand-typed JSON-RPC (cancel → silence, session healthy; unknown-id / completed-request / double cancel all silent); a live uvicorn streamable-HTTP server in both response modes (cancelled call's POST completes at cancel time — 202 empty / clean SSE end — where main returns the code-0 body); and an event-store server with client abandon-by-timeout (no Last-Event-ID reconnect).

Breaking Changes

Callers of the built-in Client are unaffected — abandon and timeout never waited on the error frame. A caller that hand-sends notifications/cancelled while still awaiting the call now waits instead of receiving the code-0 error; see docs/migration.md.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

No conformance-suite scenario covers "no response after cancel" yet; worth an issue there. Spec-internal note: the transport pages say MUST NOT while basic/patterns/cancellation still says SHOULD for the same behaviour.

AI Disclaimer