Skip to content

Fix #348: Allow ToolError to carry custom content and is_error#2708

Open
Stoobyy wants to merge 1 commit into
modelcontextprotocol:mainfrom
Stoobyy:fix/issue-348-iserror-content
Open

Fix #348: Allow ToolError to carry custom content and is_error#2708
Stoobyy wants to merge 1 commit into
modelcontextprotocol:mainfrom
Stoobyy:fix/issue-348-iserror-content

Conversation

@Stoobyy
Copy link
Copy Markdown

@Stoobyy Stoobyy commented May 28, 2026

Why

Closes #348. There is currently no way to set isError=True when returning non-text content from tool calls (e.g., images, mixed content). The only way to get isError=True is to let an unhandled exception propagate, which always produces a single TextContent block.

What changed

  • ToolError now accepts content and is_error keyword arguments
  • Tool.run() re-raises ToolError instead of wrapping it
  • _handle_call_tool catches ToolError before the generic exception handler, constructing a CallToolResult with the caller's custom content and is_error value

Usage

from mcp.server.mcpserver.exceptions import ToolError
from mcp.types import TextContent, ImageContent

@server.tool()
def my_tool():
    raise ToolError(
        "Processing failed",
        content=[
            TextContent(type="text", text="Detailed error message"),
            ImageContent(type="image", data="base64...", mimeType="image/png"),
        ],
    )

Validation

  • All 91 existing tests pass
  • 2 new tests added: ToolError with custom content, ToolError with default content

@Stoobyy Stoobyy force-pushed the fix/issue-348-iserror-content branch from ad6c854 to 1be32b4 Compare May 28, 2026 17:49
… and is_error

Adds `content` and `is_error` parameters to ToolError so tool functions
can return arbitrary content blocks with isError=True (e.g., images,
structured data alongside error status).

- exceptions.py: ToolError gains content and is_error parameters
- tools/base.py: Re-raise ToolError instead of wrapping it
- server.py: Catch ToolError, construct CallToolResult with proper content
- tests: 2 new tests covering ToolError with custom content and default

Closes modelcontextprotocol#348

Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
@Stoobyy Stoobyy force-pushed the fix/issue-348-iserror-content branch from 1be32b4 to f879ec6 Compare May 28, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No way to set isError=True for arbitrary tool result content

1 participant