refactor: consistent raise-based error handling in MCPServer handlers#2252
Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
Implements the pattern described in modelcontextprotocol#2153: MCPServer users should raise exceptions and the framework converts them to appropriate protocol responses, without leaking internal details. Changes: - ToolError raised by user → CallToolResult(is_error=True) with user's message - ResourceError raised by user → MCPError (JSON-RPC error) with user's message - PromptError (new) raised by user → MCPError (JSON-RPC error) with user's message - MCPError → re-raised as-is for protocol-level errors - Unexpected exceptions → sanitized message (no internal detail leakage) Files modified: - exceptions.py: Add PromptError class - tools/base.py: Let ToolError/MCPError pass through Tool.run() without wrapping - resources/types.py: Let ResourceError/MCPError pass through FunctionResource.read() - prompts/base.py: Let PromptError/MCPError pass through Prompt.render() - prompts/manager.py: Raise PromptError instead of ValueError for unknown prompts - server.py: Add handler-level error handling to _handle_read_resource() and _handle_get_prompt(), explicit ToolError catch in _handle_call_tool() - __init__.py: Export ToolError, ResourceError, PromptError from package Closes modelcontextprotocol#2153 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the pattern described in #2153: MCPServer users should raise exceptions to signal errors, and the framework converts them to appropriate protocol responses — without leaking internal details to clients.
Changes
Error propagation — let user-raised exceptions pass through:
Tool.run():ToolErrorandMCPErrornow pass through without re-wrapping (previously all exceptions were blindly wrapped asToolError)FunctionResource.read():ResourceErrorandMCPErrorpass throughPrompt.render():PromptError(new) andMCPErrorpass throughHandler-level error conversion — framework catches and converts:
_handle_call_tool(): Explicitly catchesToolError→CallToolResult(is_error=True); unexpected exceptions get a sanitized message_handle_read_resource(): CatchesResourceError→MCPError(INVALID_PARAMS); unexpected exceptions →MCPError(INTERNAL_ERROR)with sanitized message_handle_get_prompt(): CatchesPromptError→MCPError(INVALID_PARAMS); unexpected exceptions →MCPError(INTERNAL_ERROR)with sanitized messageNew
PromptErrorclass for symmetry withToolErrorandResourceError.Security improvement (#698): Unexpected exceptions no longer leak internal details (e.g., database connection strings, file paths) to the client. Only user-raised
ToolError/ResourceError/PromptErrormessages reach the client.User-facing behavior
Test plan
ToolError,ResourceError,PromptErrormessages reach client correctly🤖 Generated with Claude Code