Skip to content

fix: send no error response to a cancelled request - #697

Open
yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-cancelled-exception-suppression
Open

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/mcp-cancelled-exception-suppression

Conversation

@yorzi

@yorzi yorzi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

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 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.

On main (c2ce3c1), a tool held in flight, cancelled, then raising RuntimeError:

Expected {jsonrpc: "2.0", id: 7, error: {code: -32603, message: "Internal error", data: "Internal error calling tool fails_after_cancel"}} to be nil.

The same tool in a session terminated with DELETE, JSON response mode:

Expected: 202
  Actual: 200

Server#handle_request checks the cancellation only on the result path (lib/mcp/server.rb:874). Its OAuth::InsufficientScopeError, RequestHandlerError and StandardError branches now do the same after reporting the exception: the request still reaches exception_reporter and its instrumentation records cancelled: true, but no error response is sent. call_tool wraps a tool's error in RequestHandlerError, 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#_onrequest checks abortController.signal.aborted in its error handler as well as its result handler (packages/core-internal/src/shared/protocol.ts at 5ee54ee).

The Cancellation docs page now says this.

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 gets no response (the RequestHandlerError branch, through call_tool).
  • A custom method raising an ordinary error gets no response (the StandardError branch).
  • A custom method raising RequestHandlerError gets no response.
  • A tool failing require_scopes! gets no response (the OAuth::InsufficientScopeError branch).
  • The error is still reported to exception_reporter, and the instrumentation records cancelled: true with the reason.
  • An uncancelled tool that raises still gets its -32603 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 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), and bundle 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

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

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

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#pop without a timeout and nothing newer than Ruby 2.7.

## 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`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant