python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

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

Open ezyang opened this issue 1 year ago • 8 comments

There appears to be no way to manually toggle isError=True for non-text content. For example, you can't return an image with isError=True. I noticed this when implementing a proxy using mcp.tool that recursively dispatches to call_tool. Returning content is fine, but there's no good way to make use of isError, unless you have a single string in which case you can raise an Exception and get isError toggled that way.

ezyang avatar Mar 23 '25 08:03 ezyang

I think the best experience here would be to raise a specific exception that has some content inside, and then we could translate to isError internally.

Kludex avatar Oct 03 '25 10:10 Kludex

Hi @Kludex, I implemented your suggested approach in #1824 - adding an optional content parameter to ToolError that gets translated to isError=True internally.

Happy to close if you're already working on this or prefer a different approach!

hanzili avatar Jan 04 '26 03:01 hanzili

I'd like to take this. I see #1824 has been open since January with merge conflicts and no maintainer review, so I'll submit a fresh PR rebased on current main with the same maintainer-endorsed approach (adding optional content to ToolError).

Context: I maintain mcp-assert, an MCP server testing tool. We scanned 54 MCP servers and found this exact isError gap causing real-world issues, so this fix is something I care about getting right.

blackwell-systems avatar Apr 27 '26 16:04 blackwell-systems

I've opened #2984 implementing the approach @Kludex suggested here: an optional content field on ToolError that gets translated to is_error=True internally, so a tool can return an image (or any content) on an error result.

I rebased fresh on current main since #1824 has been idle with conflicts. The change is purely additive — a plain ToolError("...") behaves exactly as before, only the new content-bearing case differs. ~50 lines, full suite green at 100% coverage, pyright/ruff clean.

Could I be assigned? Happy to adjust if you'd prefer a different shape.

Disclosure: developed with AI assistance; I've reviewed the change and can speak to every line.

RaidLZ avatar Jun 26 '26 07:06 RaidLZ

I have implemented a fix for this issue in PR #3370. The fix adds:

  1. CallToolResult.create_error() classmethod for convenient error results with non-text content
  2. Auto-conversion of SDK Image/Audio helpers to wire content types via model_validator
  3. Updated convert_result to handle helpers in CallToolResult.content

Usage:

from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.utilities.types import Image
from mcp.types import CallToolResult

@mcp.tool()
async def my_tool() -> CallToolResult:
    img = Image(data=b"...", format="png")
    return CallToolResult.create_error(content=[img])

@mcp.tool()
async def structured_error() -> CallToolResult:
    return CallToolResult.create_error(
        content=[TextContent(type="text", text="Invalid input")],
        structured_content={"error_code": "INVALID_INPUT", "details": {"field": "email"}},
    )

All 5,747 existing tests pass + 3 new tests added. The PR was auto-closed due to the assignment requirement - happy to discuss or make adjustments!

mukktinaadh avatar Aug 23 '26 20:08 mukktinaadh

ran into this building an mcp server that proxies postgres queries. when a query fails i want to return the partial result set alongside the error so the model can adjust its query, but right now ToolError only lets me send back a string. ended up having to return is_error=False with an error message stuffed into the text content which is... not great, the model doesn't always treat it as an error.

i took a stab at this on my fork (branch feat/tool-error-content). basically just added an optional content kwarg to ToolError that flows through to CallToolResult when present, falls back to the existing str-wrapping when it's None. the tricky part was making sure content survives the re-raise in Tool.run() where it prepends the tool name to the message, without that the content just gets dropped silently.

happy to push more commits on #3404 if it gets reopened, or just leaving this here in case the approach is useful context for whoever picks it up.

SushantTusharJoshi avatar Aug 27 '26 14:08 SushantTusharJoshi

Confirming this is still reproducible on current main: a tool failure always comes back as a single TextContent with is_error=True (server.py _handle_call_tool), and ToolError takes no content kwarg, so non-text content can never ride along on an error result.

Real-world impact from my side: I run proxy-style MCP servers (recursively dispatching to downstream tools), where a failed call often still holds useful content — e.g. a chart image or partial result the model needs to see as an error. Today the only option is stuffing text into an is_error=False result, which models don't reliably treat as failure.

I'd implement @Kludex's suggested shape — optional content on ToolError, translated to is_error=True internally, surviving the re-raise in Tool.run() — keeping plain ToolError("...") behavior identical. Happy to take this if you'll assign it.

Disclosure: drafted with AI assistance; I've reproduced this locally and stand behind it.

milangeorge2000 avatar Sep 28 '26 07:09 milangeorge2000

Hey @dsp-ant, I'd love to take a shot at this issue and submit a PR to allow setting isError=True natively for arbitrary tool result content blocks. Could you please assign this to me?

shesamridhi avatar Oct 12 '26 10:10 shesamridhi