MCPServer handlers should raise exceptions, not return error objects
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- api, backend-api-design
Hướng nghiên cứu
Bắt đầu với _handle_call_tool() trong src/mcp/server/mcpserver/server.py và _handle_request() trong src/mcp/server/lowlevel/server.py, so sánh các luồng xử lý exception và cách xây dựng response của chúng. Công việc hoàn tất khi các handler cấp cao có thể báo hiệu lỗi bằng cách raise, các handler cấp thấp có thể trả về ErrorData một cách tường minh, và các exception chưa được xử lý ở bất kỳ tầng nào cũng tạo ra các lỗi JSON-RPC đúng định dạng mà không làm lộ thông tin nội bộ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 24.3k
- Fork
- 4k
- Merge trung bình
- 1 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 31
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/python-sdk
-
Streamable HTTP client logs a WARNING for valid 202 Accepted on session termination (DELETE) Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
modelcontextprotocol/python-sdk#3546 · 4 bình luận ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
modelcontextprotocol/python-sdk#3545 · 1 bình luận ·
-
v1 v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
modelcontextprotocol/python-sdk#3508 · 2 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
modelcontextprotocol/python-sdk#3504 ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/python-sdk#3492 · 1 bình luận ·
Tất cả issue của modelcontextprotocol/python-sdk
Issue tương tự
-
link-check link-check:sphinx-theme
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100