Repository navigation
fix(mcp): tool results are duplicated when a server mirrors structuredContent as compact JSON - #169135
fix(mcp): tool results are duplicated when a server mirrors structuredContent as compact JSON#169135jipeng6036-del wants to merge 1 commit into
Conversation
MCP servers that mirror structuredContent as compact JSON (or with a different key order) had that text block sent to the model next to the structuredContent block, doubling the payload. Treat a text block as the mirror when it is complete JSON for the same value, in both the agent projection and the node-host bounding. Other text, including JSON for a different value, is still kept. Fixes openclaw#169121
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Thanks for the context here. I did a careful shell check against current Close this PR: the explicitly linked merged replacement carries its complete patch onto current main, preserving contributor credit. I found the merged PR that appears to have closed this: #169180: fix(mcp): tool results are duplicated when a server mirrors structuredContent as compact JSON. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. So I’m closing this as already implemented rather than keeping a duplicate issue open. Review detailsBest possible solution: Retain the credited fix already on main and close this redundant PR. Do we have a high-confidence way to reproduce the issue? The original failure is explained by the base's exact pretty-JSON comparison and the official-release report; current main contains the complete repair, and no target code was executed during this review. Is this the best way to solve the issue? The shared complete-JSON matcher repairs both existing result boundaries while preserving distinct values, recovery guidance, payload priorities, and depth-failure handling. Security review: Security review cleared: The patch changes result deduplication without adding permissions, dependencies, execution paths, or authority transfers. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning medium; reviewed against c8e7f1339f81; fix evidence: merged PR #169180, commit 5270b0374a53, main fix timestamp 2026-10-11T19:33:45Z. |
Closes #169121
What Problem This Solves
Fixes: an MCP tool result is sent to the model twice when the server mirrors
structuredContentas compact (or differently ordered) JSON instead of the exact pretty-printed form.User Impact
User impact: tool results from such MCP servers no longer double context usage or get spilled to files just because of the duplicate. Recovery text and JSON for a different value are still kept.
Why This Change Was Made
Both mirror filters (agent projection in
src/agents/mcp-content.tsand node-host bounding insrc/node-host/invoke-mcp-result.ts) only dropped a text block that was byte-identical toJSON.stringify(structuredContent, null, 2). They now share a small matcher that keeps that fast path and otherwise treats a text block as the mirror only when it parses as complete JSON with the same stable-stringified value. Parse failures (including too-deep input) keep the block.Evidence
src/agents/mcp-content.test.ts(pretty/compact/reordered mirrors dropped; different JSON, truncated JSON and guidance text kept) andsrc/node-host/invoke-mcp-result.test.ts(compact/reordered mirrors dropped, other text kept). The compact/reordered cases fail on main and pass with the change.pnpm exec vitest run src/agents/mcp-content.test.ts src/node-host/invoke-mcp-result.test.ts src/node-host/invoke.mcp.test.ts: 15/15 pass.code-mode.*,agent-bundle-mcp-tools.request-boundary,node-plugin-tools,node-host/mcp.recovery) pass.agent-bundle-mcp-runtime.test.tshas 32 failures in my sandbox both on main and with this change (stdio server startup in my environment), so I could not use it as signal; CI should cover it.pnpm tsgo:core,oxfmt --checkandoxlinton the touched files pass. I did not run the fullpnpm build && pnpm check && pnpm test.projectMcpCallToolResult/boundMcpToolResultPayload), blocks before → after: compact 2 → 1 and 1 → 0, reordered 2 → 1 and 1 → 0; different JSON and plain text unchanged.Configured Gateway and node-host process proof
Isolated process proof run passed on Ubuntu 24.04 with Node 24.16.0 and pnpm 12.9.0. The verification branch adds only the process fixture and workflow; its production sources are unchanged from this PR's
d7ef91f29d85a0594855b863351301f0a531454d.The fixture starts a real Gateway, pairs a real node host, and uses an SDK stdio MCP server. A local mock provider drives a Gateway
agentrun; no external model service or live user state is used. One MCP response contains compact and reordered mirrors of{"count":2,"nested":{"ready":true,"name":"synthetic"}}, different JSON withcount:3, and recovery guidance.Observed through the shipped routes:
node.invoke/mcp.tools.call.v1: payload retainsstructuredContent; its text blocks are exactly the different JSON andRecovery guidance: retry with a smaller query.Both mirrors are removed.agent->agent.wait->chat.history: the MCP tool result contains exactly three text blocks: onestructuredContent:rendering, the different JSON, and the same guidance. Both mirror blocks are absent.The run's terminal output includes the synthetic result trace. The selected test passed (1/1); runner wall time was 191.49 seconds (Vitest 186.63 seconds). Frozen dependency installation and the shipped
qaRuntimebuild also passed. This is a configured stdio process proof with a mock provider, not live-provider or all-transport coverage. A process-level run of the original baseline has not been performed; the focused before/after test evidence above remains separate. The fullpnpm build && pnpm check && pnpm testgap remains as stated above.This change was prepared with an AI coding agent.