modelcontextprotocol / modelcontextprotocol/python-sdk

[v2] Expose request cancellation on MCPServer tool Context

オープン
#3,389 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

v2
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
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

  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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

mcp.server.mcpserver.MCPServer と、そこに注入される mcp.server.mcpserver.context.Context から始め、mcp.shared.context.BaseContext が cancel_requested をどのように公開しているか、また notifications/cancelled がリクエストごとの dispatch コンテキストにどのように格納されているかを比較します。高レベルのツールハンドラーが、プライベートなセッション内部や dispatch 内部にアクセスせずに、サポートされたキャンセルイベントを使用でき、報告された再現例のカバレッジがある状態を完了とします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api, backend
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。