modelcontextprotocol / modelcontextprotocol/python-sdk
MCPServer handlers should raise exceptions, not return error objects
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.3k
- Forks
- 4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 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 asToolError, 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/exceptat the MCPServer layer — exceptions propagate to the low-level server
Low-level server handlers:
_handle_request()catchesMCPError→ sends its.erroras a JSON-RPC error- Any other
Exception→ErrorData(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
- 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.
- Low-level server users should return
ErrorDataexplicitly when they want to control the JSON-RPC error response, since they operate at the protocol level. - 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with _handle_call_tool() in src/mcp/server/mcpserver/server.py and _handle_request() in src/mcp/server/lowlevel/server.py, comparing their exception paths and response construction. The work is done when high-level handlers can signal errors by raising, low-level handlers can explicitly return ErrorData, and unhandled exceptions at either layer produce well-formed JSON-RPC errors without leaking internal details.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend-api-design
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100