modelcontextprotocol / modelcontextprotocol/python-sdk

[v2] Expose request cancellation on MCPServer tool Context

Open
#3,389 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v2
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 1h
Merged PRs (30d)
31

Description

What happened?

With mcp==2.1.0, a tool registered through the high-level mcp.server.mcpserver.MCPServer API receives mcp.server.mcpserver.Context. That context does not expose a public cancel_requested event, although the lower-level mcp.shared.context.BaseContext does.

from mcp.server.mcpserver.context import Context as MCPServerContext
from mcp.shared.context import BaseContext

assert not hasattr(MCPServerContext, "cancel_requested")
assert hasattr(BaseContext, "cancel_requested")

The server dispatcher processes notifications/cancelled and holds the event in its per-request dispatch context, but normal tool handlers cannot cooperatively observe it through the supported high-level context API. A handler that owns cleanup-sensitive work (for example, a bounded child process) must either rely only on task interruption or traverse private session/dispatch internals to relay cancellation.

Expected behavior

Expose the current request's cancellation event on the high-level tool context (for example, await ctx.cancel_requested.wait()), or document an equivalent supported API. It should allow a handler to trigger its own cooperative cleanup without using private SDK attributes.

If this is already addressed in a later release, a version pointer would also resolve the report; the reproduction above is against 2.1.0.

Reproduction steps

  1. Install mcp==2.1.0.
  2. Register an async tool with MCPServer that accepts its injected Context.
  3. Attempt to access ctx.cancel_requested; the attribute is absent.
  4. Compare with BaseContext.cancel_requested, which is present.

SDK version

mcp==2.1.0 (Python 3.12)

Area

Server / high-level MCPServer tool context API.

AI assistance disclosure: drafted with AI assistance; the reporter is responsible for this report.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at mcp.server.mcpserver.MCPServer and its injected mcp.server.mcpserver.context.Context, then compare how mcp.shared.context.BaseContext exposes cancel_requested and how notifications/cancelled is stored in the per-request dispatch context. Done means high-level tool handlers can use a supported cancellation event without private session or dispatch internals, with coverage for the reported reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.