modelcontextprotocol / modelcontextprotocol/python-sdk

[v2] Expose request cancellation on MCPServer tool Context

Aperta
#3,389 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

v2
Lingua principale
Python
Stelle
24.3k
Fork
4k
Merge medio
1g 1h
PR unite (30g)
31

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da mcp.server.mcpserver.MCPServer e dal suo mcp.server.mcpserver.context.Context iniettato, quindi confronta come mcp.shared.context.BaseContext espone cancel_requested e come notifications/cancelled viene memorizzato nel contesto di dispatch per richiesta. Il lavoro è completato quando gli handler degli strumenti di alto livello possono usare un evento di cancellazione supportato senza accedere agli interni privati della sessione o del dispatch, con copertura per la riproduzione segnalata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
api, backend
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.