modelcontextprotocol / modelcontextprotocol/python-sdk
[v2] Expose request cancellation on MCPServer tool Context
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
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
- Install
mcp==2.1.0. - Register an async tool with
MCPServerthat accepts its injectedContext. - Attempt to access
ctx.cancel_requested; the attribute is absent. - 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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 mcp.server.mcpserver.MCPServer 及其注入的 mcp.server.mcpserver.context.Context 開始,接著比較 mcp.shared.context.BaseContext 如何公開 cancel_requested,以及 notifications/cancelled 如何儲存在每個請求的 dispatch context 中。完成的標準是:高階工具處理器可以使用受支援的取消事件,而不必存取私有的 session 或 dispatch 內部,並且涵蓋已回報的重現案例。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- api, backend
- Issue 類型
- 功能
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 活躍
- 描述清晰度
- 基本清楚
- 新手友好度
- 68/100