Repository navigation
Conversation
## Motivation and Context When a handler returns `input_required` on the legacy wire, `Server#handle_request` checks the request's cancellation once and then hands the result to `run_legacy_input_required_shim`, which sends each fulfilment request, re-runs the handler and returns its result with no further check. A cancellation that arrives after the client answered a leg, or while the handler re-runs, is therefore not honored: the handler runs again, further legs are still sent to the client, and the final result is returned. Over Streamable HTTP the result is written to the request's SSE stream after the client's `notifications/cancelled`, although the re-run handler itself sees `server_context.cancelled?` true. A request the shim is not involved in is suppressed in the same situation. The shim now checks the request's cancellation before each leg, before re-running the handler, and before returning its result. A cancelled request raises `CancelledError`, which reaches the rescue a cancelled nested request already reaches, so no response is sent and the cancellation is recorded in the instrumentation. Uncancelled replays, the round cap, capability checks and the first-writer-wins handling of a nested response are unchanged. ## How Has This Been Tested? New tests in `test/mcp/server_input_required_legacy_shim_test.rb`, with a fixture transport that cancels the originating request right after answering a leg: a request cancelled once its fulfilment request is answered does not re-run its handler, gets no response, and records the cancellation and its reason; a request cancelled between the two fulfilment requests of one round does not send the second; a request cancelled while its handler re-runs gets no response, for `tools/call`, `prompts/get` and `resources/read`. All three fail against the previous library, and dropping any one of the three checks fails one of them. ## Breaking Changes None. A legacy `input_required` request cancelled during its replay is no longer answered. Fixes modelcontextprotocol#706.
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 legacy
input_requiredrequest cancelled after the client answered a fulfilment leg, or while its handler re-runs, no longer gets a response.Fixes #706
Motivation and Context
When a handler returns
input_requiredon the legacy wire,Server#handle_requestchecks the request's cancellation once (lib/mcp/server.rb:884) and then hands the result torun_legacy_input_required_shim(:1090), which sends each fulfilment request, re-runs the handler, and returns its result (:1127) with no further check. A cancellation that arrives after the client answered a leg, or while the handler re-runs, is not honored: the handler runs again, remaining legs are still sent, and the final result is returned. A request the shim is not involved in is suppressed in the same situation (#697).On
main(00868f7), overStreamableHTTPTransportin SSE mode, with the client answering the elicitation and then sendingnotifications/cancelledwhile the re-run handler is held:With this change:
The shim now checks the request's cancellation before each leg, before re-running the handler, and before returning its result. A cancelled request raises
CancelledError, which reaches the rescue that a cancelled nested request already reaches, so no response is sent and the cancellation is recorded in the instrumentation. A cancellation during the wait for a leg's answer was already handled by the bundled transports' nested-request hook and is unchanged, as are uncancelled replays, the round cap, capability checks, and a nested answer that wins its race with a cancellation.How Has This Been Tested?
New tests in
test/mcp/server_input_required_legacy_shim_test.rb, with a fixture transport that cancels the originating request right after answering a leg:tools/call,prompts/getandresources/read.All three fail against the previous library. Dropping any one of the three checks fails one of them.
bundle exec rake test,bundle exec rake rubocopandbundle exec rake conformance:test(baseline) pass locally on Ruby 4.0.9.Breaking Changes
None. A legacy
input_requiredrequest cancelled during its replay is no longer answered.Types of changes
Checklist
Additional context
This touches the same method as #696 (passing
auth_infoto the re-run) and merges cleanly with it; the two changes are independent.