Skip to content

fix: honor cancellation in the input-required loop - #730

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-input-required-cancellation
Dec 1, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/client-input-required-cancellation

Conversation

@yorzi

@yorzi yorzi commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Cancelling a request during an input_required round now stops further input handlers and wakes the backoff wait.

Fixes #727

Motivation and Context

The client looks at the cancellation: token only when it sends a request. Between requests, the remaining on_elicitation, on_sampling and on_roots handlers of the round run after a cancel, and the backoff of a requestState-only result is slept to its end.

The token is now checked before each round and before each handler. With a token, the backoff waits on a condition variable that the cancel signals, against a monotonic deadline, and its callback is removed when the wait ends. Re-issuing the request then raises CancelledError before anything is sent. Without a token, sleep is used as before. No notifications/cancelled is sent in these cases, because no request is in flight.

How Has This Been Tested?

  • Automated unit / integration tests
  • Tested manually
  • Tested in a real application

New tests in test/mcp/client_test.rb:

  • A cancel in the first of two handlers does not run the second.
  • A cancel after a response takes precedence over the round limit.
  • A cancel wakes a backoff wait. The test cancels once the wait has been entered, so it does not depend on timing.
  • A backoff that ends normally removes its callback from the token.

All four fail on main, and reverting any one part of the change fails one of them. bundle exec rake test (2,812 tests, 10,194 assertions on Ruby 4.0.9; also run on 2.7), bundle exec rake rubocop, bundle exec yard --no-output and bundle exec rake conformance:test (baseline) pass locally.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • 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 this behavior)

Additional context

#707 covers the server's legacy input_required shim and does not overlap.

## Motivation and Context

The client looks at the `cancellation:` token only when it sends a request. Between requests, the remaining
`on_elicitation`, `on_sampling` and `on_roots` handlers of the round run after a cancel, and the backoff of a
`requestState`-only result is slept to its end.

The token is now checked before each round and before each handler. With a token, the backoff waits on a condition
variable that the cancel signals, against a monotonic deadline, and its callback is removed when the wait ends.
Re-issuing the request then raises `CancelledError` before anything is sent. Without a token, `sleep` is used as
before. No `notifications/cancelled` is sent in these cases, because no request is in flight.

## How Has This Been Tested?

New tests in `test/mcp/client_test.rb`:

- A cancel in the first of two handlers does not run the second.
- A cancel after a response takes precedence over the round limit.
- A cancel wakes a backoff wait. The test cancels once the wait has been entered, so it does not depend on timing.
- A backoff that ends normally removes its callback from the token.

All four fail on `main`, and reverting any one part of the change fails one of them.

## Breaking Changes

None.

Fixes modelcontextprotocol#727.
@koic
koic merged commit f4b9eae into modelcontextprotocol:main Dec 1, 2026
13 checks passed
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.

Cancelling during an input_required round still runs the remaining input handlers

2 participants