Repository navigation
Conversation
## Motivation and Context A request cancelled with `notifications/cancelled`, or by the termination of its Streamable HTTP session, gets no response when its handler returns or raises `MCP::CancelledError`. When the handler raises any other error after the cancellation, it still gets a JSON-RPC error. A tool that fails while it winds down (a closed connection, a failed cleanup) reports `-32603` for a request the client already abandoned, and in JSON response mode the POST of a session terminated with `DELETE` is answered 200 with that error instead of 202 with no body. `Server#handle_request` checks the cancellation only on the result path. Its `OAuth::InsufficientScopeError`, `RequestHandlerError` and `StandardError` branches now do the same after reporting the exception: a cancelled request still reaches `exception_reporter` and its instrumentation records `cancelled: true`, but no error response is sent. The cancellation spec says a receiver SHOULD "Not send a response for the cancelled request", and the TypeScript SDK drops the error the same way, checking `abortController.signal.aborted` in its error handler as well as its result handler. ## How Has This Been Tested? New tests in `test/mcp/server_cancellation_test.rb` hold a handler with `Queue` barriers, cancel its request, and then let it raise. A tool raising an ordinary error, a custom method raising an ordinary error, a custom method raising `RequestHandlerError`, and a tool failing `require_scopes!` each get no response. The error is still reported, and the instrumentation records the cancellation. An uncancelled tool that raises still gets its error. A new test in `test/mcp/server/transports/streamable_http_transport_test.rb` terminates the session with `DELETE` while a tool is held and then lets it raise: its POST is answered 202 with no body. The suppression tests fail against the previous library, and dropping the check from any one rescue branch fails the matching test. ## Breaking Changes None to the API. A handler error raised after cancellation is no longer sent to the client. It still reaches `exception_reporter`.
6 of 9 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A cancelled request no longer gets a JSON-RPC error when its handler raises after the cancellation. The error still reaches
exception_reporter.Motivation and Context
A request cancelled with
notifications/cancelled, or by the termination of its Streamable HTTP session (#692), gets no response when its handler returns or raisesMCP::CancelledError. When the handler raises any other error after the cancellation, it still gets a JSON-RPC error. A tool that fails while it winds down (a closed connection, a failed cleanup) reports-32603for a request the client already abandoned, and in JSON response mode the POST of a session terminated withDELETEis answered 200 with that error instead of 202 with no body.On
main(c2ce3c1), a tool held in flight, cancelled, then raisingRuntimeError:The same tool in a session terminated with
DELETE, JSON response mode:Server#handle_requestchecks the cancellation only on the result path (lib/mcp/server.rb:874). ItsOAuth::InsufficientScopeError,RequestHandlerErrorandStandardErrorbranches now do the same after reporting the exception: the request still reachesexception_reporterand its instrumentation recordscancelled: true, but no error response is sent.call_toolwraps a tool's error inRequestHandlerError, so tool errors take the second branch.The cancellation spec says a receiver SHOULD "Not send a response for the cancelled request", and the TypeScript SDK drops the error the same way:
Protocol#_onrequestchecksabortController.signal.abortedin its error handler as well as its result handler (packages/core-internal/src/shared/protocol.tsat5ee54ee).The Cancellation docs page now says this.
How Has This Been Tested?
New tests in
test/mcp/server_cancellation_test.rbhold a handler withQueuebarriers, cancel its request, and then let it raise:RequestHandlerErrorbranch, throughcall_tool).StandardErrorbranch).RequestHandlerErrorgets no response.require_scopes!gets no response (theOAuth::InsufficientScopeErrorbranch).exception_reporter, and the instrumentation recordscancelled: truewith the reason.-32603error.A new test in
test/mcp/server/transports/streamable_http_transport_test.rbterminates the session withDELETEwhile a tool is held and then lets it raise: its POST is answered 202 with no body.The suppression tests fail against
main, and dropping the check from any one rescue branch, or the instrumentation record, fails the matching test. On Ruby 3.4.4:bundle exec rake test(2721 runs, 0 failures, 3 skips),bundle exec rake rubocop(212 files, no offenses), andbundle exec rake conformance:test(baseline check passed for server and client).Breaking Changes
None to the API. A handler error raised after its request is cancelled is no longer sent to the client. It still reaches
exception_reporter.Types of changes
Checklist
Additional context
An error raised after a request completed and was answered is unaffected: the cancellation then arrives too late and is ignored, as before. Only Ruby 3.4.4 was run locally; CI covers the rest of the matrix. The tests use
Queue#popwithout a timeout and nothing newer than Ruby 2.7.