Support is_error flag for tool results#2566
Open
zuhabul wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Support is_error flag for tool results#2566zuhabul wants to merge 1 commit intomodelcontextprotocol:mainfrom
zuhabul wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…protocol#348)\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR aims to let tool implementations explicitly mark a tool call result as an error (via CallToolResult.is_error) even when returning non-text content (e.g., images/audio), by supporting a 3-tuple return shape (unstructured_content, structured_content, is_error) and documenting the behavior in the migration guide.
Changes:
- Extend
MCPServer._handle_call_tool()to interpret 3-tuples as(content, structured_content, is_error)and setCallToolResult.is_erroraccordingly. - Add migration documentation describing the new 3-tuple tool return shape and the alternative of returning
CallToolResultdirectly.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/mcp/server/mcpserver/server.py |
Adds tuple parsing logic intended to propagate an is_error flag into CallToolResult. |
docs/migration.md |
Documents the new 3-tuple return option for marking non-text tool results as errors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+319
to
335
| if isinstance(result, tuple): | ||
| # Support either (unstructured_content, structured_content) or | ||
| # (unstructured_content, structured_content, is_error). The third element, | ||
| # if present, controls the CallToolResult.is_error flag. | ||
| if len(result) == 2: | ||
| unstructured_content, structured_content = result | ||
| is_error = False | ||
| elif len(result) == 3: | ||
| unstructured_content, structured_content, is_error = result | ||
| else: | ||
| # Fallback: treat as a sequence of content blocks | ||
| return CallToolResult(content=list(result)) | ||
| return CallToolResult( | ||
| content=list(unstructured_content), # type: ignore[arg-type] | ||
| structured_content=structured_content, # type: ignore[arg-type] | ||
| is_error=bool(is_error), | ||
| ) |
Comment on lines
+328
to
+334
| else: | ||
| # Fallback: treat as a sequence of content blocks | ||
| return CallToolResult(content=list(result)) | ||
| return CallToolResult( | ||
| content=list(unstructured_content), # type: ignore[arg-type] | ||
| structured_content=structured_content, # type: ignore[arg-type] | ||
| is_error=bool(is_error), |
Comment on lines
331
to
332
| return CallToolResult( | ||
| content=list(unstructured_content), # type: ignore[arg-type] |
Comment on lines
+1141
to
+1147
| Tools may now return a 3-tuple: (unstructured_content, structured_content, is_error). | ||
| If the third element is True, the resulting CallToolResult sent to clients will have | ||
| is_error=True. This allows returning non-text content (images, audio) while still | ||
| indicating the tool execution failed or produced an error state. | ||
|
|
||
| Alternatively, tools may return a full `CallToolResult` instance directly to control | ||
| is_error and other fields explicitly. |
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.
Fixes #348 — allow tools to return (unstructured_content, structured_content, is_error) so non-text results (e.g., images) can be marked as errors. Also document the behavior in docs/migration.md.