Repository navigation
fix(mcp): tool results are duplicated when a server mirrors structuredContent as compact JSON - #169180
Conversation
|
🦞👀 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. |
|
Codex review: needs maintainer review before merge. What this changesThis PR removes compact and reordered JSON mirrors of structured MCP results while preserving distinct JSON and recovery guidance. Example: An MCP server returns structuredContent {"answer":42}, text '{"answer":42}', and 'authentication expired; run login'.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this PR open: it fixes a current MCP duplication bug with a focused shared matcher, and no actionable defect was found. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherGateway MCP projection converts server results into model content, while node-host bounding removes redundant content before applying transport limits; both boundaries now share the JSON mirror matcher. flowchart TD
A[MCP server result] --> B[Gateway MCP execution]
A --> C[Node host payload bounding]
C --> D[Node invoke response]
D --> E[Shared result projection]
B --> E
F[JSON mirror matcher] --> C
F --> E
E --> G[Model tool result]
Technical reviewBest possible solution: Land the shared complete-JSON comparison at both existing boundaries while preserving independent content, payload priorities, and the original contributor's credit. Do we have a high-confidence way to reproduce the issue? Current-main source deterministically retains compact JSON beside structured output, and the reporter supplies official-release before/after traces; no target code was executed during this review. Is this the best way to solve the issue? The shared matcher fixes the cause at both existing boundaries, compares complete JSON values, and preserves overlapping text that may contain recovery guidance. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 4e3818bab0fe. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +33, Tests +55. Total +88 across 5 files. Added test files: 0. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes: No label changes. Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (1 earlier review cycle)
Reviewed October 11, 2026, 3:28 PM ET / 19:28 UTC (Revision 2). |
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
e237858 to
c75580d
Compare
…openclaw#169180) 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 Co-authored-by: jipeng6036-del <[email protected]>
…openclaw#169180) 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 Co-authored-by: jipeng6036-del <[email protected]> (cherry picked from commit 5270b03) Co-authored-by: RomneyDa <[email protected]>
Closes #169121
Related: #169135
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
This carries #169135 forward with the real-behavior proof ClawSweeper requested there. The code is @jipeng6036-del's commit, unchanged and with its authorship kept, rebased on current
main. I reported #169121 and had prepared an equivalent fix when #169135 appeared. That PR is blocked only on proof, and this is the author's first PR here, so I'm adding the proof in a PR body that I can update. If you'd rather land #169135 once its author copies the evidence below, please close this one.The change itself: 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 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 keep the block.Evidence
Real behavior through the shipped Gateway and node-host flows. I used a disposable Gateway and a paired headless node (
openclaw node run) on loopback. The model was a local OpenAI-compatible mock that calls the MCP tool and logs the exact tool-result text the agent sends back to it. The MCP server is a stdio fixture that returns:structuredContent:{"answer":42,"items":[1,2],"query":"proof"}{"answer":41}, and the guidance textauthentication expired; run loginThe same tool was configured on both paths: Gateway
mcp.servers.mirror, reached asmirror__lookup, and nodenodeHost.mcp.servers.nodemirror, reached asnodemirror_lookupthroughmcp.tools.call.v1. The turns ran withopenclaw agent --agent main --message "Call the lookup tool." --json.Runtime used and its limit. The runtime was the official
[email protected]npm package. "Before" is that package unmodified. "After" applies this PR's change to the two compiled chunks:mcp-content-*.mjsfor the agent projection, and the chunk withboundMcpToolResultPayloadfor node-host bounding. I could not buildmainhere, because tsdown needs about 4.7 GB of RAM and this host runs production agents. Betweenv2026.10.1and this PR's base,src/agents/mcp-content.tsis identical.src/node-host/invoke-mcp-result.tsdiffers only in unrelated byte-limit checks.Tool result received by the model (
\nshown as line breaks):Gateway MCP (
mirror__lookup)Before (2026.10.1):
After (with this change):
Node-hosted MCP (
nodemirror_lookupvianode.invoke)Before (2026.10.1, node and Gateway unmodified): same as Gateway before, with both mirrors present.
Node-host change only (Gateway projection still unmodified): both mirrors are gone. This isolates
boundMcpToolResultPayload.After (full change on node and Gateway): identical to the output above.
On both paths, the compact and reordered mirrors are removed, and the distinct JSON and recovery guidance remain.
Tests on this branch:
pnpm exec vitest run src/agents/mcp-content.test.ts src/node-host/invoke-mcp-result.test.ts src/node-host/invoke.mcp.test.tspasses 3 files and 15 tests.This PR body and the proof harness were prepared with an AI coding agent.