modelcontextprotocol / modelcontextprotocol/python-sdk
MCPServer handlers should raise exceptions, not return error objects
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 24.3k
- フォーク
- 4k
- 平均マージ
- 1日 1時間
- マージ済み PR(30日)
- 31
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
src/mcp/server/mcpserver/server.py の _handle_call_tool() と src/mcp/server/lowlevel/server.py の _handle_request() から始め、それぞれの例外経路とレスポンス構築を比較します。高レベルのハンドラーが例外を raise してエラーを通知でき、低レベルのハンドラーが明示的に ErrorData を返せ、どちらの層でも未処理の例外が内部の詳細を漏らさず、適切な形式の JSON-RPC エラーを生成できるようになれば作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- api, backend-api-design
- issue の種類
- リファクタリング
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100