Skip to content

fix(mcp): tool results are duplicated when a server mirrors structuredContent as compact JSON - #169135

Closed
jipeng6036-del wants to merge 1 commit into
openclaw:mainfrom
jipeng6036-del:fix/169121-mcp-compact-json-mirror
Closed

jipeng6036-del wants to merge 1 commit into
openclaw:mainfrom
jipeng6036-del:fix/169121-mcp-compact-json-mirror

Conversation

@jipeng6036-del

@jipeng6036-del jipeng6036-del commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #169121

What Problem This Solves

Fixes: an MCP tool result is sent to the model twice when the server mirrors structuredContent as 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.ts and node-host bounding in src/node-host/invoke-mcp-result.ts) only dropped a text block that was byte-identical to JSON.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

  • New tests: src/agents/mcp-content.test.ts (pretty/compact/reordered mirrors dropped; different JSON, truncated JSON and guidance text kept) and src/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.
  • Related MCP suites (code-mode.*, agent-bundle-mcp-tools.request-boundary, node-plugin-tools, node-host/mcp.recovery) pass. agent-bundle-mcp-runtime.test.ts has 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 --check and oxlint on the touched files pass. I did not run the full pnpm build && pnpm check && pnpm test.
  • Repro from the issue (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 agent run; 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 with count:3, and recovery guidance.

Observed through the shipped routes:

  • Gateway node.invoke / mcp.tools.call.v1: payload retains structuredContent; its text blocks are exactly the different JSON and Recovery guidance: retry with a smaller query. Both mirrors are removed.
  • Gateway agent -> agent.wait -> chat.history: the MCP tool result contains exactly three text blocks: one structuredContent: 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 qaRuntime build 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 full pnpm build && pnpm check && pnpm test gap remains as stated above.

This change was prepared with an AI coding agent.

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
@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Oct 11, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 11, 2026
@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

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
Relationship: superseded
Canonical: #169180
Summary: The merged replacement explicitly carries this complete patch and closes the original duplication report.

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 details

Best 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:

  • Peter Steinberger: Raw commit 407c8cd adds src/node-host/invoke-mcp-result.ts:14 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 407c8cdb3440; files: src/node-host/invoke-mcp-result.ts)
  • wangmiao0668000666: Raw commit 452c808 adds src/agents/mcp-content.ts:111 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 452c808d5b1b; files: src/agents/mcp-content.ts)

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.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MCP structuredContent projection duplicates payload when server's text mirror is compact JSON

1 participant