Add custom content support to ToolError for isError responses#1824
Open
hanzili wants to merge 5 commits intomodelcontextprotocol:mainfrom
Open
Add custom content support to ToolError for isError responses#1824hanzili wants to merge 5 commits intomodelcontextprotocol:mainfrom
hanzili wants to merge 5 commits intomodelcontextprotocol:mainfrom
Conversation
ToolError can now accept an optional `content` parameter to return arbitrary content blocks (TextContent, ImageContent, etc.) with isError=True, enabling rich error responses beyond plain text. Changes: - Add `content` parameter to ToolError exception class - Handle ToolError with custom content in lowlevel server - Re-raise ToolError in FastMCP's Tool.run to preserve content - Add comprehensive tests for both Server and FastMCP APIs Github-Issue: modelcontextprotocol#348
The lowlevel server's call_tool decorator has both a ToolError handler and a generic Exception handler. This test ensures the generic Exception path is covered for 100% code coverage.
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
This PR addresses #348 by adding an optional
contentparameter toToolError, allowing tools to return arbitrary content blocks (images, multiple text blocks, etc.) withisError=True.Problem: Previously,
ToolErroronly accepted a string message, which was converted to a singleTextContent. There was no way to return rich error content like images or structured data with the error flag set.Solution: Following the approach suggested in the issue discussion,
ToolErrornow accepts an optionalcontentparameter:Changes
src/mcp/server/fastmcp/exceptions.py: Add optionalcontentparameter toToolError.__init__src/mcp/server/lowlevel/server.py: HandleToolErrorwith custom content in the low-level server'scall_tooldecoratorsrc/mcp/server/fastmcp/tools/base.py: Re-raiseToolErroras-is inTool.run()to preserve custom content (without this,ToolErrorwould be caught by the genericexcept Exceptionand wrapped in a newToolError, losing thecontent)Test Plan
Added tests in
tests/issues/test_348_tool_error_content.pythat verify:ToolError("message")still works as before (backwards compatibility)ToolError("msg", content=[...])returns the custom content withisError=TrueServerand high-levelFastMCPAPIsCloses #348