Support notifications/cancelled per MCP specification#332
Open
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Support notifications/cancelled per MCP specification#332koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
notifications/cancelled per MCP specification#332koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
## Motivation and Context The MCP specification defines `notifications/cancelled` so either party can stop a previously-issued in-flight request. The Ruby SDK declared the method constant but had no receive logic, no in-flight tracking, and no way for handlers to participate in cancellation; cancellation notifications fell through to the unsupported-method path. This PR implements the **server-side** half of the spec. The server stops processing the targeted request cooperatively and suppresses its JSON-RPC response, matching the Python SDK's anyio `CancelScope` and the TypeScript SDK's `RequestHandlerExtra.signal`. Cancellation is observable by every user-overridable request handler: - `Tool.call` (tools/call) - prompt templates (prompts/get) - blocks registered via `resources_read_handler` / `completion_handler` / `resources_subscribe_handler` / `resources_unsubscribe_handler` / `define_custom_method` Each handler opts in by declaring a `server_context:` keyword and polls `server_context.cancelled?` or calls `server_context.raise_if_cancelled!` (raising `MCP::CancelledError`) inside long-running work. Handlers that keep their existing `|params|` (or `|args|`) signature continue to work unchanged. On `StreamableHTTPTransport`, cancelling a parent `tools/call` automatically cancels n ested server-to-client requests (`sampling/createMessage`, `elicitation/create`); the nested `send_request` raises `MCP::CancelledError` and a cancel notification is routed to the peer on the parent's POST response stream. `StdioTransport` is single-threaded and blocks on `$stdin.gets`, so it deliberately does not propagate nested cancellation. Tools running on stdio still observe cancellation between calls via `server_context.cancelled?`. The design is cooperative-only (no `Thread#raise`) because preemptive cancellation is unsafe across Rack adapters and arbitrary handler code. The `initialize` request is never cancellable, satisfying the spec rule that it MUST NOT be cancelled. Unknown / completed / duplicate cancel notifications are silently ignored per the spec. `MCP::Client#cancel` (an equivalent that aborts the calling thread's synchronous wait) is deferred to a follow-up PR. Ref: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation ## How Has This Been Tested? `test/mcp/cancellation_test.rb` covers the `MCP::Cancellation` token contract. `test/mcp/server_cancellation_test.rb` covers end-to-end cancellation: - A handler spins on `cancelled?` in a background thread; the test sends `notifications/cancelled` and asserts the handler observed cancellation and the JSON-RPC response was suppressed. Each user-overridable handler type (tool, prompt template, resources/read, completion, custom method) has its own regression test. - `initialize` is not cancellable; unknown / duplicate / late-after-completion cancels are silently ignored. - The `reason` propagates into the `cancellation_reason` instrumentation field. - Custom transports implementing only the abstract `(method, params = nil)` contract keep working; the new kwargs (`session_id:`, `related_request_id:`, `parent_cancellation:`, `server_session:`) are silently dropped when the transport's signature does not declare them. `StreamableHTTPTransport` tests cover nested cancellation, hook deregistration after normal completion (so a late parent cancel does not emit a stray `notifications/cancelled`), and the first-writer-wins race when a real response and a cancel arrive concurrently. ## Breaking Change None. `MCP::ServerContext#initialize` gains an optional `cancellation:` keyword (defaults to `nil`), and `StreamableHTTPTransport#send_request` / `#send_notification` gain optional cancellation kwargs. The abstract `Transport` base class and `StdioTransport` keep their existing `(method, params = nil)` signatures, and custom transports following those signatures continue to work. `StreamableHTTPTransport` now dispatches client-originated notifications through `ServerSession#handle_json` before returning 202; previously these were accepted without dispatch, but the existing handlers for `notifications/initialized` and `notifications/progress` were already no-ops, so no user-visible effect is expected. Adding `server_context:` to a handler block is strictly opt-in.
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.
Motivation and Context
The MCP specification defines
notifications/cancelledso either party can stop a previously-issued in-flight request. The Ruby SDK declared the method constant but had no receive logic, no in-flight tracking, and no way for handlers to participate in cancellation; cancellation notifications fell through to the unsupported-method path.This PR implements the server-side half of the spec. The server stops processing the targeted request cooperatively and suppresses its JSON-RPC response, matching the Python SDK's anyio
CancelScopeand the TypeScript SDK'sRequestHandlerExtra.signal. Cancellation is observable by every user-overridable request handler:Tool.call(tools/call)resources_read_handler/completion_handler/resources_subscribe_handler/resources_unsubscribe_handler/define_custom_methodEach handler opts in by declaring a
server_context:keyword and pollsserver_context.cancelled?or callsserver_context.raise_if_cancelled!(raisingMCP::CancelledError) inside long-running work. Handlers that keep their existing|params|(or|args|) signature continue to work unchanged.On
StreamableHTTPTransport, cancelling a parenttools/callautomatically cancels n ested server-to-client requests (sampling/createMessage,elicitation/create); the nestedsend_requestraisesMCP::CancelledErrorand a cancel notification is routed to the peer on the parent's POST response stream.StdioTransportis single-threaded and blocks on$stdin.gets, so it deliberately does not propagate nested cancellation. Tools running on stdio still observe cancellation between calls viaserver_context.cancelled?.The design is cooperative-only (no
Thread#raise) because preemptive cancellation is unsafe across Rack adapters and arbitrary handler code. Theinitializerequest is never cancellable, satisfying the spec rule that it MUST NOT be cancelled. Unknown / completed / duplicate cancel notifications are silently ignored per the spec.MCP::Client#cancel(an equivalent that aborts the calling thread's synchronous wait) is deferred to a follow-up PR.Ref: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
How Has This Been Tested?
test/mcp/cancellation_test.rbcovers theMCP::Cancellationtoken contract.test/mcp/server_cancellation_test.rbcovers end-to-end cancellation:cancelled?in a background thread; the test sendsnotifications/cancelledand asserts the handler observed cancellation and the JSON-RPC response was suppressed. Each user-overridable handler type (tool, prompt template, resources/read, completion, custom method) has its own regression test.initializeis not cancellable; unknown / duplicate / late-after-completion cancels are silently ignored.reasonpropagates into thecancellation_reasoninstrumentation field.(method, params = nil)contract keep working; the new kwargs (session_id:,related_request_id:,parent_cancellation:,server_session:) are silently dropped when the transport's signature does not declare them.StreamableHTTPTransporttests cover nested cancellation, hook deregistration after normal completion (so a late parent cancel does not emit a straynotifications/cancelled), and the first-writer-wins race when a real response and a cancel arrive concurrently.Breaking Change
None.
MCP::ServerContext#initializegains an optionalcancellation:keyword (defaults tonil), andStreamableHTTPTransport#send_request/#send_notificationgain optional cancellation kwargs. The abstractTransportbase class andStdioTransportkeep their existing(method, params = nil)signatures, and custom transports following those signatures continue to work.StreamableHTTPTransportnow dispatches client-originated notifications throughServerSession#handle_jsonbefore returning 202; previously these were accepted without dispatch, but the existing handlers fornotifications/initializedandnotifications/progresswere already no-ops, so no user-visible effect is expected. Addingserver_context:to a handler block is strictly opt-in.Types of changes
Checklist