[错误]: MCPTool 只读取 content[0].text,因此多块或图像的 MCP 结果会失败或丢失文本
作者: sxh313创建于 2026年9月28日更新于 2026年9月28日
发生了什么事? MCPTool.execute 只读取 output.content[0] 并只读取其 .text,因此任何 MCP 服务器如果返回超过一个内容块,或者首先返回非文本块,则会直接失败或默默丢失数据: 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是一个内容块列表,在mcpSDK 中,此框架安装了ContentBlock类,它有五个成员 —TextContent、ImageContent、AudioContent、ResourceLink、EmbeddedResource— 其中只有TextContent具有.text(ImageContent/AudioContent具有data+mimeType,EmbeddedResource具有其文本在.resource.text中)。因此,块 0 的.text是一个假设,而不是协议的属性。然后,被吞噬的except Exception 将崩溃报告为如果 *服务器* 失败了(Error running mcp tool: …),这就是为什么这看起来像是一个 MCP 问题而不是一个 Trae 问题。 ### 重现 真实的 mcp.types对象,一个返回它们的虚拟客户端,来自main的MCPTool(e839e55)。没有服务器,没有网络: single text block代码=0 输出='found 3 matches' 错误=Nonetwo text blocks代码=0 输出='found 3 matches' 错误=Nonescreenshot (image first, text second)代码=-1 输出=None 错误="Error running mcp tool: 'ImageContent' object has no attribute 'text'"embedded text resource代码=-1 输出=None 错误="Error running mcp tool: 'EmbeddedResource' object has no attribute 'text'"no content at all代码=-1 输出=None 错误='Error running mcp tool: list index out of range'error carrying an image代码=-1 输出=None 错误="Error running mcp tool: 'ImageContent' object has no attribute 'text'" 两种不同的失败模式: **硬失败** — 位于第一个位置的图像 / 音频 / 嵌入式资源块引发了AttributeError,而空内容列表引发了 IndexError。两者都变为 error_code=-1,因此代理被告知工具调用失败,而服务器则正常回复。 **默默截断** — 返回两个文本块的结果为 'found 3 matches',并丢弃了 'in 2 files'。这种情况更糟糕,因为没有任何东西报告它:模型只是接收到一半的答案。 这是可以通过 repo 自带的配置实现的: trae_config.yaml.example:11-17(在 main中)允许使用playwright MCP 服务器(npx @playwright/[email protected]`),其截图工具返回图像内容。图像还是文本通知先返回是由…
内容来源: bytedance/trae-agent