modelcontextprotocol / modelcontextprotocol/python-sdk

In-process callback failures break tool calls and notification sends

Aperta
#3,434 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

Initial Checks

  • I confirm that I am using the newest release of the 2.x line.
  • I confirm that I searched the issue tracker before opening this issue.

Release line

2.x (current stable)

Description

Client(server) uses DirectDispatcher. If a progress callback raises, that exception crosses back into server tool execution, so a tool that otherwise returns successfully fails with UnexpectedToolError. DirectDispatcher also lets an inbound notification handler exception escape back to the sender's notify() call.

The stream-backed JSONRPCDispatcher logs and contains both callback failures. I expected the in-process path to keep the same boundary: log the callback failure, but let the protocol operation continue.

I have a small patch and regression tests for both paths and would like to fix this if maintainers want an outside pull request.

I used GitHub Copilot CLI to inspect the dispatcher paths, write the patch, and draft this report. I verified the reproduction and tests.

Example Code

import anyio

from mcp import Client
from mcp.server.mcpserver import Context, MCPServer

server = MCPServer("callback-repro")


@server.tool()
async def work(context: Context) -> str:
    await context.report_progress(1, 1)
    return "done"


async def broken_progress(progress: float, total: float | None, message: str | None) -> None:
    raise RuntimeError("consumer failed")


async def main() -> None:
    async with Client(server, mode="2026-07-28") as client:
        result = await client.call_tool("work", progress_callback=broken_progress)
        print(result.content[0].text)


anyio.run(main)

Current main ends with:

UnexpectedToolError: Error executing tool work

The tool should print done. The callback failure should still be logged.

Verification

With only the regression tests applied to clean main:

uv run --frozen pytest tests/shared/test_dispatcher.py \
  -k 'progress_callback_exception_does_not_fail_request or notification_handler_exception_does_not_reach_sender' -q
# 2 failed, 2 passed. Both DirectDispatcher cases failed.

With the two callback guards applied:

uv run --frozen pytest tests/shared/test_dispatcher.py \
  -k 'progress_callback_exception_does_not_fail_request or notification_handler_exception_does_not_reach_sender' -q
# 4 passed

Python & MCP Python SDK

Python 3.12.13
mcp 2.1.1
mcp 2.1.2.dev4+d060b36e at d060b36e1d095ef6e93e07ba5d59bb69b2ad449a
macOS

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 nei percorsi di DirectDispatcher e confronta la gestione dei fallimenti dei callback con quella di JSONRPCDispatcher basato su stream. Esegui tests/shared/test_dispatcher.py con i due casi -k indicati per riprodurre i fallimenti. Il lavoro è completato quando entrambi i casi di DirectDispatcher hanno esito positivo, le chiamate agli strumenti riuscite e gli invii delle notifiche continuano a funzionare e i fallimenti dei callback vengono registrati.

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

Valutazione

Stack tecnologico
python
Ambito
backend-api-design
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
84/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.