Skip to content

fix(mcp): contain malformed tool results - #4666

Merged
Re-bin merged 3 commits into
HKUDS:mainfrom
Yuxin-Lou:fix-mcp-result-containment
Jul 4, 2026
Merged

Re-bin merged 3 commits into
HKUDS:mainfrom
Yuxin-Lou:fix-mcp-result-containment

Conversation

@Yuxin-Lou

Copy link
Copy Markdown
Contributor

fix #4652

Summary

  • contain exceptions raised while rendering malformed MCP tool results
  • mark MCP timeouts, internal cancellations, retry failures, and execution exceptions as structured tool errors. This is the main difference from PR fix: contain malformed mcp tool results #4660 .

Changes

  • wrap MCP result rendering in a local exception boundary
  • convert malformed responses to ToolResult.error
  • convert timeout, server-side cancellation, transient retry failure, and generic MCP exceptions to ToolResult.error
  • continue propagating genuine external CancelledError signals
  • add regression coverage for malformed empty content and registry-level correction hints

@hamb1y

hamb1y commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

yours is way better :)

@chengyongru chengyongru added priority: p0 Urgent blocker: security, data loss, startup/core outage, or severe regression without workaround. priority: p1 Important user-impacting issue or project priority with bounded impact or a reasonable workaround. bug Something isn't working and removed priority: p0 Urgent blocker: security, data loss, startup/core outage, or severe regression without workaround. labels Jul 3, 2026
maintainer edit: treat transient MCP stream failures as dead sessions so the existing reconnect handler can refresh the session before retrying. Also cover retry failure as a structured tool error.
@chengyongru

Copy link
Copy Markdown
Collaborator

Maintainer edit pushed: transient MCP stream failures such as closed streams now use the existing reconnect handler before retrying, so a stale MCP session can be refreshed without requiring a process restart. I also added regression coverage for reconnect success and retry failure returning a structured tool error.

maintainer edit: update the existing transient retry tests for reconnect-first behavior and keep structured retry-failure coverage in the focused MCP transient suite.
@chengyongru

Copy link
Copy Markdown
Collaborator

Follow-up pushed: aligned the existing transient retry regression test with the reconnect-first behavior, and kept retry-exhaustion covered as a structured ToolResult.error. GitHub Actions are now green on the latest head.

@Re-bin Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This fixes the MCP tool-result failure path cleanly: malformed MCP content, timeouts, cancellations, and retry exhaustion now surface as structured tool errors instead of successful-looking text. The reconnect follow-up also keeps transient closed-stream failures on the existing MCP recovery path.

Validated locally:

  • python -m pytest tests/tools/test_mcp_tool.py tests/agent/test_mcp_transient_retry.py -q
  • python -m ruff check nanobot/agent/tools/mcp.py tests/agent/test_mcp_transient_retry.py tests/tools/test_mcp_tool.py

GitHub Actions is green across the matrix.

@Re-bin
Re-bin merged commit 614ea86 into HKUDS:main Jul 4, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority: p1 Important user-impacting issue or project priority with bounded impact or a reasonable workaround.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nanobot process crashes directly when MCP tool call exception

4 participants