Move the documentation that had grown to 3000+ lines in README.md onto https://ruby.sdk.modelcontextprotocol.io and slim the README down to a quick start and feature overview, following the Python SDK layout: - Add 21 server pages as the docs/_server/ collection (overview plus transports, discovery, tools, prompts, resources, roots, sampling, elicitation, multi round-trip results, notifications, notification subscriptions, cancellation, progress, ping, completions, logging, pagination, server context, configuration, and custom methods), 8 client pages as the docs/_client/ collection (overview, transports, lifecycle, multi round-trip results, cancellation, ping, pagination, and authorization), and 3 extension pages as the docs/_extensions/ collection (overview, capability extensions, and MCP Apps, following the Extensions Overview recommendation that SDK documentation list the supported extensions) - one page per topic, ordered to match the sidebar of the 2026-07-28 specification, with the client-side APIs (pinging, cancelling, and paginating from MCP::Client) documented under Building Clients - Add top-level Examples and Protocol Versions pages after Installation, linking the runnable examples in examples/ and summarizing the supported protocol versions, the era model, and client negotiation - Render the three sections as just-the-docs collections, which list every page beneath a plain category heading in the sidebar with no folding, and serve every page at an extensionless URL under /server/, /client/, and /extensions/, matching the sibling SDK documentation sites - Replace docs/building-servers.md and docs/building-clients.md with redirects to the new section overview pages via jekyll-redirect-from, redirect the previously published /installation.html to its extensionless URL, fold their content that was missing from README.md into the new pages, and update the Tool argument keys reference comment in lib/mcp/server.rb to the relocated Tools page - Reduce README.md to the badges, a compact feature overview in the Python SDK style, installation instructions, a stdio server and client quick start, and a License section, using absolute URLs for the remaining repository links so they resolve on rubygems.org - Adapt formatting where GitHub rendering habits break on the site: convert the numbered "three ways to define" lists into headings, since kramdown restarts numbering at 1 when code blocks split list items, convert GitHub-style alerts into just-the-docs callouts with the SEP-2260 server-to-client association note raised to a red warning, merge the duplicated Exception Reporting and Configuration Block Data sections into the Configuration page, and relocate the Streamable HTTP settings that were nested under the Logging section into the Transports page - Refresh the migrated content against the current implementation: correct stale claims and broken samples the README carried, note on each session-era feature how it relates to the modern lifecycle of MCP 2026-07-28, and point spec links at the latest revision, keeping deliberate 2025-11-25 pins for pages the modern revision removed - Style the site after the Rails API documentation palette (red links and accents on neutral surfaces) with matching light and dark color schemes, center the sidebar and content as one block, add a Previous/Next footer pager following the sidebar order, open external links in a new tab, and serve the MCP logo as the favicon Every code block and heading from the previous README was verified to have a home in the new docs pages or the slimmed README before the reduction; a few samples were corrected rather than copied, as noted above.
137 lines
5.1 KiB
Markdown
137 lines
5.1 KiB
Markdown
---
|
|
layout: default
|
|
title: Cancellation
|
|
nav_order: 13
|
|
---
|
|
|
|
# Cancellation
|
|
|
|
The MCP Ruby SDK supports server-side handling of
|
|
the [MCP `notifications/cancelled` utility](https://modelcontextprotocol.io/specification/latest/basic/patterns/cancellation).
|
|
When a client sends `notifications/cancelled` for an in-flight request, the server stops
|
|
processing cooperatively and suppresses the JSON-RPC response for that request.
|
|
|
|
Cancellation is cooperative: the SDK does not forcibly terminate tool code. Instead,
|
|
a `MCP::Cancellation` token is threaded through [`server_context`](/server/server-context/), and long-running tools
|
|
poll it to exit early. When a tool returns after cancellation has been observed,
|
|
the server suppresses the JSON-RPC response, matching the spec. The `initialize` request
|
|
is never cancellable per the spec.
|
|
|
|
{: .note }
|
|
> Cancellation by notification belongs to the handshake lifecycle, where the session correlates
|
|
> `notifications/cancelled` with the in-flight request it targets. Requests of the
|
|
> [modern lifecycle](/server/discovery/#the-stateless-modern-lifecycle) are sessionless single POST exchanges, so a separately
|
|
> POSTed cancel notification cannot reach them; a modern client abandons a request by closing
|
|
> the connection instead.
|
|
|
|
## Handlers that Check for Cancellation
|
|
|
|
Any handler that opts in to `server_context:` - tools (`Tool.call`), prompt templates,
|
|
`resources_read_handler`, `resources_list_handler`, `completion_handler`, `resources_subscribe_handler`,
|
|
`resources_unsubscribe_handler`, and [`define_custom_method`](/server/custom-methods/) blocks - receives
|
|
an `MCP::ServerContext` wired to the in-flight request's cancellation token.
|
|
Handlers check `cancelled?` in their work loop, or call `raise_if_cancelled!` to raise
|
|
`MCP::CancelledError` at a safe point:
|
|
|
|
```ruby
|
|
class LongRunningTool < MCP::Tool
|
|
description "A tool that supports cancellation"
|
|
input_schema(properties: { count: { type: "integer" } }, required: ["count"])
|
|
|
|
def self.call(count:, server_context:)
|
|
count.times do |i|
|
|
# Exit early if the client has sent `notifications/cancelled`.
|
|
break if server_context.cancelled?
|
|
|
|
do_work(i)
|
|
end
|
|
|
|
MCP::Tool::Response.new([{ type: "text", text: "Done" }])
|
|
end
|
|
end
|
|
```
|
|
|
|
Alternatively, raise at the next safe point with `raise_if_cancelled!`:
|
|
|
|
```ruby
|
|
def self.call(count:, server_context:)
|
|
count.times do |i|
|
|
server_context.raise_if_cancelled!
|
|
|
|
do_work(i)
|
|
end
|
|
|
|
MCP::Tool::Response.new([{ type: "text", text: "Done" }])
|
|
end
|
|
```
|
|
|
|
When a handler observes cancellation (either by returning early with `cancelled?` or
|
|
by raising `MCP::CancelledError` via `raise_if_cancelled!`), the server drops the response and
|
|
no JSON-RPC result is sent to the client.
|
|
|
|
The same pattern works for other handler types:
|
|
|
|
```ruby
|
|
# resources/read
|
|
server.resources_read_handler do |params, server_context:|
|
|
server_context.raise_if_cancelled!
|
|
# read the resource
|
|
end
|
|
|
|
# completion/complete
|
|
server.completion_handler do |params, server_context:|
|
|
server_context.raise_if_cancelled!
|
|
# compute completions
|
|
end
|
|
|
|
# custom method
|
|
server.define_custom_method(method_name: "custom/slow") do |params, server_context:|
|
|
server_context.raise_if_cancelled!
|
|
# do work
|
|
end
|
|
|
|
# prompts (via Prompt subclass)
|
|
class SlowPrompt < MCP::Prompt
|
|
prompt_name "slow_prompt"
|
|
|
|
def self.template(args, server_context:)
|
|
server_context.raise_if_cancelled!
|
|
MCP::Prompt::Result.new(messages: [])
|
|
end
|
|
end
|
|
```
|
|
|
|
Handlers that do not declare a `server_context:` keyword continue to work unchanged -
|
|
the opt-in detection only wraps the context when the block signature asks for it.
|
|
|
|
## Nested Server-to-Client Requests Are Cancelled Automatically
|
|
|
|
When a tool handler is waiting on a nested server-to-client request
|
|
(`server_context.create_sampling_message`, `create_form_elicitation`, or
|
|
`create_url_elicitation`), cancelling the parent tool call automatically raises
|
|
`MCP::CancelledError` from the nested call, so the tool does not need to wrap it
|
|
in its own `cancelled?` checks:
|
|
|
|
```ruby
|
|
def self.call(server_context:)
|
|
result = server_context.create_sampling_message(messages: messages, max_tokens: 100)
|
|
# If the parent tools/call is cancelled while waiting above, MCP::CancelledError
|
|
# is raised here and the tool can let it propagate or clean up as needed.
|
|
MCP::Tool::Response.new([{ type: "text", text: result[:content][:text] }])
|
|
rescue MCP::CancelledError
|
|
# Optional: run cleanup. Re-raising (or letting it propagate) is fine; the server
|
|
# will still suppress the JSON-RPC response per the MCP spec.
|
|
raise
|
|
end
|
|
```
|
|
|
|
Nested cancellation propagation is supported on `StreamableHTTPTransport` only.
|
|
`StdioTransport` is single-threaded and blocks on `$stdin.gets`, so a nested
|
|
`server_context.create_sampling_message` inside a tool runs to completion even if
|
|
the parent `tools/call` is cancelled. The parent tool itself still observes cancellation
|
|
via `server_context.cancelled?` between nested calls.
|
|
|
|
## Client Side
|
|
|
|
Cancelling a request the client has issued (the `cancellation:` keyword, wire-order guarantees,
|
|
and custom transport requirements) is documented on the client [Cancellation](/client/cancellation/) page.
|