Repository navigation
fix(mcp): report non-text error results without their payload - #8327
Conversation
When an MCP tool returns isError with no text content, the local conversion fell back to stringifying the LangChain content blocks. That put the whole base64 payload into the ToolException message, and the middleware's 500-character cap still left a base64 prefix in the model-visible ToolMessage and the checkpoint. Describe the blocks by kind and MIME type instead; text results keep their existing message unchanged. Applies to stdio tools, which use this conversion; sse/http tools keep the adapter's conversion (tracked separately).
willem-bd
left a comment
There was a problem hiding this comment.
Verified the stdio fix for nonempty binary payloads. The inline comment covers a remaining zero-byte file case reproduced through the tool wrapper and agent checkpoint.
| if call_tool_result.isError: | ||
| error_parts = [item["text"] for item in lc_content if isinstance(item, dict) and item.get("type") == "text"] | ||
| raise ToolException("\n".join(error_parts) if error_parts else str(lc_content)) | ||
| if error_parts: | ||
| raise ToolException("\n".join(error_parts)) | ||
| # No text block to report: describe the payload shapes instead of | ||
| # stringifying the blocks, which would put image/file base64 into the | ||
| # model-visible error message. | ||
| described = _describe_non_text_result(lc_content) |
There was a problem hiding this comment.
[P3] Handle zero-byte files before constructing LangChain content blocks
The isError branch runs after content conversion. A zero-byte embedded file has an empty base64 string, which create_file_block() rejects first. I reproduced this through the stdio wrapper and agent checkpoint. The model received ValueError: Must provide one of: url, base64, or file_id even when the server supplied a useful error message. This behavior also occurs on the parent commit. Handle error results before constructing binary blocks so this case gets the intended fallback and preserves any server diagnostic.
There was a problem hiding this comment.
Fixed in 3f6cff723: on error results, zero-byte embedded file/image payloads are skipped before create_file_block/create_image_block and their shapes are named in the no-text fallback, so the server's text diagnostic is preserved. Added three regressions (zero-byte file + server text, zero-byte file only, zero-byte image); all three fail on the parent with ValueError: Must provide one of: url, base64, or file_id and pass now. Audio behavior is unchanged (still raises NotImplementedError). MCP test files: 17 passed; ruff check/format clean.
An error result whose embedded file or image had a zero-byte payload hit
create_file_block/create_image_block before the isError branch, so the
model saw ValueError('Must provide one of: url, base64, or file_id')
instead of the server's diagnostic. Skip empty binary payloads on error
results, name their shape in the no-text fallback, and keep normal
results unchanged (audio still raises NotImplementedError).
Why
An MCP tool that reports
isError=Truewith no text content (an image-only or file-only error result) produced aToolExceptionbuilt fromstr(lc_content). The LangChain content blocks embed the whole base64 payload, so a 100 KB image became a ~133,000-character exception. The model-visibleToolMessageis capped at 500 characters, but that prefix still carried ~450 base64 characters into the model context and the checkpoint.backend/packages/harness/deerflow/mcp/AGENTS.mdstates the invariant: "Never put base64 payloads in text."This fixes the local conversion that stdio MCP tools use.
sse/httptools keeplangchain-mcp-adapters' own conversion, which has the samestr(tool_content)fallback and is tracked separately in #8326.What changed
_convert_call_tool_resultstill joins the result's text blocks for the error message. When there are none, it describes the blocks by kind and MIME type (image (image/png),file (application/pdf)) instead of stringifying them: "MCP tool returned an error result without text content (image (image/png)); the payload is not shown." An empty error result reports only that no text was sent.mcp/AGENTS.mddocuments the conversion's behavior next to the existing payload rule, including that sse/http still uses the adapter conversion.Surface area
None of the template's boxes apply: this changes the harness's local MCP tool-result conversion in
backend/packages/harness/deerflow/mcp/tools.py, not a frontend/API/graph/prompt shape.Bug fix verification
backend/tests/test_mcp_error_non_text_content.pycovers an image-only error result, a file-only (EmbeddedResource blob) error result, an empty error result, and a text+image error result.mainand green on this branch? yes — on the parent the first three fail because the message contains the full base64 payload / block reprs (image-onlymessage was ~133,000 characters); the text+image case passes on both because text results keep their message.Validation
cd backend && make test→ 4 shards, 41,060 passed; the only failures are environment-only and reproduce on the parent: 3test_extension_manager.pygit-source tests fail whenuvclones the test's local git server in this sandbox, andtest_managed_provider_presets.py::...[anthropic]picks up this shell'sANTHROPIC_AUTH_TOKEN; it passes with that variable unset.cd backend && make test-blocking-io→ 479 passed, 1 skippedpytest backend/tests/test_mcp*.py -q→ 1622 passed, 31 skippedruff check/ruff format --checkon the changed files → cleanAI assistance
Tool(s) used: Claude Code, operating the BlueX888 GitHub account autonomously.
How you used it: An AI agent found the defect in a code audit, reproduced the base64 leak, wrote the failing tests first, implemented the fix, and had an independent reviewer agent verify the red/green result and the end-to-end tool-error path; that review also found the sse/http gap now filed as #8326. No human reviewed this diff before it was opened.
The account owner has authorized autonomous contributions, but has not personally reviewed this diff, so the human-review box above is intentionally left unchecked.