Skip to content

fix(mcp): mark errored MCP tool results as failed (set error_code=-1) - #429

Open
nankingjing wants to merge 2 commits into
bytedance:mainfrom
nankingjing:fix-mcp-tool-error-code
Open

fix(mcp): mark errored MCP tool results as failed (set error_code=-1)#429
nankingjing wants to merge 2 commits into
bytedance:mainfrom
nankingjing:fix-mcp-tool-error-code

Conversation

@nankingjing

Copy link
Copy Markdown

Problem

MCPTool.execute (trae_agent/tools/mcp_tool.py) does not propagate a failure code when an MCP server returns an error result:

output = await self.client.call_tool(self.get_name(), arguments)
if output.isError:
    return ToolExecResult(output=None, error=output.content[0].text)   # error_code defaults to 0
else:
    return ToolExecResult(output=output.content[0].text)

ToolExecResult.error_code defaults to 0 (see trae_agent/tools/base.py). ToolExecutor.execute_tool_call derives success from that field:

success=tool_exec_result.error_code == 0,

So when an MCP tool call reports isError == True, the resulting ToolResult.success is True. The failure is silently treated as a success:

  • The Anthropic client sends the result back with is_error=not successis_error=False, telling the model the tool succeeded.
  • Failure-reflection logic (BaseAgent.reflect_on_result, which keys on tool_result.success) never fires.
  • The trajectory records the failed step as success: true.

Note the except branch in the same method already sets error_code=-1, and the existing test_execute_exception asserts it — the isError branch was simply missing the same signal.

Fix

Set error_code=-1 in the isError branch so failures are propagated consistently with the exception branch.

Verification

  • python -m py_compile on both changed files: passed.
  • Reproduced end-to-end against the real MCPTool + ToolExecutor code with a stubbed client:
    • Before: an isError result yields ToolResult.success == True (bug).
    • After: it yields ToolResult.success == False; the success path still yields success == True.
  • Extended the existing test_execute_failure unit test to assert error_code == -1. (Full pytest suite not run here — repo targets Python 3.12+ and the local interpreter is 3.11; verification used a typing.override shim.)

@nankingjing

Copy link
Copy Markdown
Author

Reviewed — the error_code fix correctly signals failures to the agent loop, and the test assertion covers it. Ready for review.

@nankingjing

Copy link
Copy Markdown
Author

@chao-peng ready for review. Fixes MCP errored results not signaling failure to the agent loop by setting error_code=-1. One regression test covers the assertion.

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.

1 participant