[Bug]: MCPTool reads only content[0].text, so multi-block or image MCP results fail or lose text
What happened?
MCPTool.execute reads only output.content[0] and only its .text, so any MCP server that answers with more than one content block, or with a non-text block first, either fails outright or silently loses data:
trae_agent/tools/mcp_tool.py:49-58
async def execute(self, arguments: ToolCallArguments) -> ToolExecResult:
try:
output = await self.client.call_tool(self.get_name(), arguments)
if output.isError:
return ToolExecResult(output=None, error=output.content[0].text)
else:
return ToolExecResult(output=output.content[0].text)
except Exception as e:
return ToolExecResult(error=f"Error running mcp tool: {e}", error_code=-1)CallToolResult.content is a list of content blocks, and in the mcp SDK this box has installed the union ContentBlock has five members — TextContent, ImageContent, AudioContent, ResourceLink, EmbeddedResource — of which only TextContent carries .text (ImageContent/AudioContent carry data + mimeType, EmbeddedResource carries its text at .resource.text). So .text on block 0 is an assumption, not a property of the protocol.
The swallowed except Exception then reports the crash as if the server failed (Error running mcp tool: …), which is why this reads as an MCP problem instead of a Trae one.
Reproduction
Real mcp.types objects, a fake client returning them, MCPTool from main (e839e55). No server, no network:
single text block code=0 output='found 3 matches' error=None
two text blocks code=0 output='found 3 matches' error=None
screenshot (image first, text second) code=-1 output=None error="Error running mcp tool: 'ImageContent' object has no attribute 'text'"
embedded text resource code=-1 output=None error="Error running mcp tool: 'EmbeddedResource' object has no attribute 'text'"
no content at all code=-1 output=None error='Error running mcp tool: list index out of range'
error carrying an image code=-1 output=None error="Error running mcp tool: 'ImageContent' object has no attribute 'text'"Two distinct failure modes:
- Hard failure — an image / audio / embedded-resource block in first position raises
AttributeError, and an empty content list raisesIndexError. Both becomeerror_code=-1, so the agent is told the tool call failed when the server answered fine. - Silent truncation — a two-text-block result returned
'found 3 matches'and dropped'in 2 files'. This one is worse, because nothing reports it: the model just receives half the answer.
This is reachable with the repo's own shipped configuration: trae_config.yaml.example:11-17 (on main) allows the playwright MCP server (npx @playwright/[email protected]), whose screenshot tool answers with image content. Whether the image or a text notice comes back first is up to the server, and either way today's code loses something — image-first errors out, text-first drops the image.
What did you expect to happen?
Every block the server sent should reach the model: text blocks verbatim, non-text blocks as an explicit marker of what was there instead of vanishing, and an empty result described as empty rather than crashing.
Direction
Flatten the content list instead of indexing it — one helper that walks the blocks and keeps each block's meaning. That leaves the isError branch alone except for using the same flattening.
Two nearby defects are already claimed and deliberately not folded in here: the missing error_code=-1 on the isError return is open PR #429's change, and the get_parameters crash on omitted JSON Schema fields is #476 / #475. Neither touches the content[0] indexing — of the six open PRs that modify mcp_tool.py (#429 #434 #435 #436 #437 #476), only #429 has an added line containing content[0], and it keeps the indexing while adding error_code=-1.
Possibly downstream of this, but not verified as the same cause: #368 reports antv mcp-server-chart being addable yet unusable in conversation.
I'll take this, with tests that build real mcp.types content blocks.
Source: bytedance/trae-agent