modelcontextprotocol / modelcontextprotocol/python-sdk

[v2] Expose request cancellation on MCPServer tool Context

Abierto
#3,389 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

v2
Lenguaje dominante
Python
Estrellas
24.3k
Forks
4k
Merge medio
1 d 1 h
PR fusionados (30 d)
31

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en mcp.server.mcpserver.MCPServer y su mcp.server.mcpserver.context.Context inyectado; luego compara cómo mcp.shared.context.BaseContext expone cancel_requested y cómo se almacena notifications/cancelled en el contexto de dispatch por solicitud. Se considera terminado cuando los handlers de herramientas de alto nivel pueden usar un evento de cancelación compatible sin acceder a los detalles internos privados de la sesión o del dispatch, y existe cobertura para la reproducción reportada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
api, backend
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Activo
Claridad
Bastante claro
Aptitud para principiantes
68/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.