modelcontextprotocol / modelcontextprotocol/python-sdk

MCPServer handlers should raise exceptions, not return error objects

Ouverte
#2,153 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

enhancement v2
Langage dominant
Python
Étoiles
24.3k
Forks
4k
Merge moyen
1 j 1 h
PR mergées (30 j)
31

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par _handle_call_tool() dans src/mcp/server/mcpserver/server.py et _handle_request() dans src/mcp/server/lowlevel/server.py, en comparant leurs chemins d’exception et la construction des réponses. Le travail est terminé lorsque les gestionnaires de haut niveau peuvent signaler des erreurs en levant des exceptions, que les gestionnaires de bas niveau peuvent retourner explicitement ErrorData et que les exceptions non gérées à l’une ou l’autre couche produisent des erreurs JSON-RPC correctement formées sans divulguer de détails internes.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
api, backend-api-design
Type d'issue
Refactorisation
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.