Skip to content

fix: honor cancellation throughout the legacy input_required shim - #707

Open
yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/legacy-input-required-cancellation
Open

yorzi wants to merge 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/legacy-input-required-cancellation

Conversation

@yorzi

@yorzi yorzi commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

A legacy input_required request 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_required on the legacy wire, Server#handle_request checks the request's cancellation once (lib/mcp/server.rb:884) and then hands the result to run_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), over StreamableHTTPTransport in SSE mode, with the client answering the elicitation and then sending notifications/cancelled while the re-run handler is held:

runs=[:first, :rerun, [:rerun_saw_cancelled, true]] cancel_status=202
stream after cancel: ["data: {\"jsonrpc\":\"2.0\",\"id\":5,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"Hello, Koichi!\"}],\"isError\":false}}\n"]

With this change:

runs=[:first, :rerun, [:rerun_saw_cancelled, true]] cancel_status=202
stream after cancel: []

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:

  • 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 a 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. Dropping any one of the three checks fails one of them. bundle exec rake test, bundle exec rake rubocop and bundle exec rake conformance:test (baseline) pass locally on Ruby 4.0.9.

Breaking Changes

None. A legacy input_required request cancelled during its replay is no longer answered.

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 (none needed: the Cancellation page already describes suppression, which now holds for the shim too)

Additional context

This touches the same method as #696 (passing auth_info to the re-run) and merges cleanly with it; the two changes are independent.

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

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.

A legacy input_required request cancelled during its replay still gets its result

1 participant