modelcontextprotocol / modelcontextprotocol/python-sdk

In-process callback failures break tool calls and notification sends

Offen
#3,434 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

v2
Vorherrschende Sprache
Python
Sterne
24.3k
Forks
4k
Ø Merge
1 T. 1 Std.
Gemergte PRs (30 T.)
31

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in den DirectDispatcher-Pfaden und vergleiche deren Behandlung von Callback-Fehlern mit dem stream-basierten JSONRPCDispatcher. Führe tests/shared/test_dispatcher.py mit den beiden genannten -k-Fällen aus, um die Fehler zu reproduzieren. Die Aufgabe ist abgeschlossen, wenn beide DirectDispatcher-Fälle bestehen, erfolgreiche Tool-Aufrufe und das Senden von Benachrichtigungen weiterhin funktionieren und Callback-Fehler protokolliert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend-api-design
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Aktiv
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
84/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.