modelcontextprotocol / modelcontextprotocol/python-sdk

MCPServer handlers should raise exceptions, not return error objects

Aperta
#2,153 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Problem

The current error handling across the two server layers is inconsistent and confusing for users. As noted by Marcelo, the experience should be more like Starlette's raise HTTPException pattern.

Current behavior

MCPServer (high-level) tool handlers:

  • Raising any exception → caught by Tool.run(), re-wrapped as ToolError, then caught by _handle_call_tool()CallToolResult(isError=True) (a JSON-RPC success response)
  • Raising MCPError → re-raised past _handle_call_tool() → becomes a JSON-RPC error response
  • Returning CallToolResult(isError=True) directly → also works
  • Resource/prompt handlers have no try/except at the MCPServer layer — exceptions propagate to the low-level server

Low-level server handlers:

  • _handle_request() catches MCPError → sends its .error as a JSON-RPC error
  • Any other ExceptionErrorData(code=0, message=str(err)) → JSON-RPC error with non-standard code

Users need to understand the difference between ToolError, MCPError, CallToolResult(isError=True), and plain exceptions — each produces different behavior depending on which layer catches it.

Desired behavior
  1. MCPServer users should raise exceptions to signal errors — the framework converts them to the appropriate protocol response. No need to construct and return error result objects.
  2. Low-level server users should return ErrorData explicitly when they want to control the JSON-RPC error response, since they operate at the protocol level.
  3. Unhandled exceptions at either layer should be caught gracefully by the framework and returned as a well-formed JSON-RPC error, without leaking internal details to the client.
Related issues
  • #1742 — Introduce typed error classes with metadata (covers error taxonomy but not the raise-vs-return layering)
  • #698 — Tool.run should not reveal exception value to the client (security concern with current behavior)
  • #396 — Inconsistent Exception Handling in @app.call_tool (older, narrower scope)
  • #1788 — Extensible pattern for protocol flow-control exceptions

AI Disclaimer

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 con _handle_call_tool() in src/mcp/server/mcpserver/server.py e _handle_request() in src/mcp/server/lowlevel/server.py, confrontando i rispettivi percorsi delle eccezioni e la costruzione delle risposte. Il lavoro è completato quando i gestori di alto livello possono segnalare gli errori sollevando eccezioni, i gestori di basso livello possono restituire esplicitamente ErrorData e le eccezioni non gestite a entrambi i livelli producono errori JSON-RPC ben formati senza esporre dettagli interni.

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

Valutazione

Stack tecnologico
python
Ambito
api, backend-api-design
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.