feat(mcpserver): let ToolError carry content for is_error results#2984
Open
RaidLZ wants to merge 1 commit into
Open
feat(mcpserver): let ToolError carry content for is_error results#2984RaidLZ wants to merge 1 commit into
RaidLZ wants to merge 1 commit into
Conversation
Tool authors can now raise ToolError(content=[...]) to return a CallToolResult with is_error=True that carries arbitrary content (e.g. an image or embedded resource) instead of only the error message as text. A plain ToolError behaves exactly as before. content is typed as list[Any] rather than list[ContentBlock] because exceptions.py is imported during mcp package initialization, before mcp.types is importable - referencing that type would create a circular import. Closes modelcontextprotocol#348
723770d to
9b6b9af
Compare
|
Good enhancement! Letting ToolError carry structured content (images, embedded resources) is useful for rich error responses. The circular import avoidance note is a smart documentation touch. |
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
Closes #348.
Tool authors currently have no way to return a
CallToolResultwithis_error=Truethat carries non-text content — raising an exception only surfaces the message as text. This implements the approach @Kludex suggested in the issue: giveToolErroran optionalcontentfield that is translated to the error result internally.What changed
ToolErroraccepts an optional keywordcontent: list[ContentBlock] | None. When set, it becomes theCallToolResult.content; otherwise behavior is unchanged (the message is returned as text).Tool.run) preservescontentwhen it wraps a raisedToolError, so the content survives to the result._handle_call_toolreturns the attached content for an errored result when present.A plain
ToolError("...")behaves exactly as before — this is purely additive and non-breaking.Tests / checks
ToolError../scripts/testpasses at 100% coverage;pyrightandruffare clean.Disclosure
Developed with AI assistance. I've reviewed the change and can explain every line.