Skip to content

fix(mcp): return compact text for untyped tools and bound settle removals - #4246

Merged
thymikee merged 3 commits into
mainfrom
mcp-compact-structured
Nov 6, 2026
Merged

thymikee merged 3 commits into
mainfrom
mcp-compact-structured

Conversation

@thymikee

@thymikee thymikee commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

  • MCP: a tool without an outputSchema (snapshot, open, get, …) now returns only its compact text on success. It returns structuredContent only when the caller asks for machine output (mcpOutputFormat: json, a non-default responseLevel, or includeCost). Typed tools and error results keep structuredContent (ADR 0012). The AI SDK adapter reads results as data, so it asks the executor for structuredContent: 'always'.
    Reason: Claude Code 2.1.317 shows the model only structuredContent when both are present, so untyped tools put the raw result (float rects, pids, native class names, log paths) into context instead of the compact text. Codex 0.166 shows only the text.
  • Settle: the settled diff keeps at most 8 removed lines; the summary keeps the true count and truncated says lines were dropped. Added lines, which carry the new refs, keep priority.
  • Both changes are bundled on purpose: they are the two largest model-visible payloads measured in the same Claude Code replay, and each is small.
  • Output-economy: refs in a text-only MCP result now count; baseline regenerated (MCP snapshot sample 1239 → 228 bytes, refs 4 → 4).
  • Docs: agent-setup.md MCP section says when structuredContent is returned.
  • 14 files. Behavior change: scripts reading structuredContent from untyped MCP tools must pass mcpOutputFormat: "json".

Validation

  • Tested SHA: f3eeb38b58e531406dbbba3c4284692fc82d1c25
  • pnpm check:affected --run --head f3eeb38b58e531406dbbba3c4284692fc82d1c25: passed (exactCommit).
  • The AI SDK test fails when the structuredContent: 'always' option is removed.
  • Claude Code replay of simulator-captured results through the real executor: snapshot -i 10,280 → 1,248 chars, open 1,220 → 168 chars model-visible. A captured cross-screen press --settle goes 3,824 → 2,794 bytes with the new cap.
  • Not tested: Cursor, VS Code, opencode; a live device run with this build (captures came from 0.21.31).

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.06 MB 6.06 MB +219 B
Package (unpacked) 6.06 MB 6.06 MB +219 B
Package (download) 1.83 MB 1.83 MB +66 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.7 ms 23.5 ms -0.2 ms
CLI --help 47.3 ms 41.6 ms -5.7 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 11 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread src/mcp/command-tools.ts Resolved
Comment thread test/output-economy/economy-metrics.ts Resolved
Comment thread website/docs/docs/agent-setup.md Resolved
Comment thread src/mcp/tool-control-fields.ts Resolved
@apex-sentinel-staging

apex-sentinel-staging Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Apex Sentinel

Verdict: approve

Since the last review: 1 new · 0 still open · 0 no longer raised

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/command-runtime/src/commands/interaction/runtime/settle.ts:427 🆕 Settle removal cap makes truncated: true on small, fully shown diffs

Reviewed with this repository's own Sentinel configuration (0a1d37cb5359).

Reply under an inline comment with @apex-sentinel-staging note ... or @apex-sentinel-staging pivot ... to re-check that finding.
Comment @apex-sentinel-staging review on this conversation to run a full review.

Earlier reviews

Review of 510c03a8 · 05.11.2026 19:36 UTC

Apex Sentinel

Verdict: approve

Since the last review: 0 new · 0 still open · 0 no longer raised

No findings.

Reviewed with this repository's own Sentinel configuration (e6ac165961de).

Review of 163dfaed · 05.11.2026 19:26 UTC

Apex Sentinel

Verdict: block

1 finding(s): 1 blocker.

Required fixes

  • AI SDK adapter now returns an empty object for every untyped tool call (snapshot, open, get)

🔴 Blocker (1)

Where Finding
src/ai-sdk/index.ts:123 AI SDK adapter now returns an empty object for every untyped tool call (snapshot, open, get)

2 findings below minor not shown (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (e6ac165961de).

@thymikee

thymikee commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

At 510c03a, the change reads correctly: untyped tools return compact text unless machine output is asked for, and typed tools and error results keep structuredContent. All 19 checks pass and there are no conflicts. One thing before merge: the one-line change that keeps the ai-sdk adapter returning structured results has no test that fails without it.

createAgentDeviceTools passes structuredContent: 'always', and nothing checks that. command-tools-structured-content.test.ts calls the executor directly with the option, and src/ai-sdk/__tests__/index.test.ts mocks createCommandToolExecutor with a factory that ignores its argument (lines 27-38). If a refactor drops the option, every untyped ai-sdk tool (snapshot, open, get, find) returns {} from execute and all tests stay green, which is the regression the inline thread reported. Could the mocked factory record its argument and assert { structuredContent: 'always' }, or one untyped tool run through createAgentDeviceTools with the real executor and a stub runCommand? Please confirm the test goes red when the option is removed.

Not blocking, take or leave:

  • the new gate test covers only mcpOutputFormat: 'json'; cases for responseLevel: 'digest', includeCost: true and a throwing snapshot that keeps structuredContent.code would pin the other opt-ins and the error path;
  • the settle change drops the <= MAX_SETTLE_DIFF_LINES early return, so a case with 9 removals (cut to 8, summary.removals 9) and one with 8 (not cut) would cover the newly truncated range, and the 8 kept removals follow tree position, so the acted-on element can fall in the dropped ones;
  • the longer mcpOutputFormat description in tool-control-fields.ts is advertised in every tool's schema, so it could stay short with the rule in the setup docs;
  • the PR body names 163dfae and 11 files while the head is 510c03a with 12, and the settle bound and the MCP gate are independent, so a note that they are bundled on purpose (or a split) would help.

The four earlier inline threads no longer apply at this head and can be resolved; the adapter one is fixed in code, and the test above is what keeps it fixed.

@thymikee

thymikee commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Fixed at f3eeb38.

  • Blocking: the mocked createCommandToolExecutor in src/ai-sdk/__tests__/index.test.ts now records its argument, and a new test asserts { structuredContent: 'always' }. I removed the option from src/ai-sdk/index.ts and the test went red (1 failed / 7 passed), then restored it.
  • Opt-ins and the error path: command-tools-structured-content.test.ts now covers responseLevel: 'digest' and includeCost: true on an untyped tool, and a throwing snapshot that keeps structuredContent.code.
  • Settle range: new settle-removal-cap.test.ts covers 8 removals (all kept, not truncated) and 9 (8 kept, summary.removals 9, truncated). I kept tree order for the 8 kept removals. A dropped removal is still counted, and the added lines carry the refs the caller acts on next, so I did not add a rule that prefers the acted-on element.
  • mcpOutputFormat description is short again; the full rule lives in agent-setup.md.
  • PR body: head SHA, file count, and a note that the two changes are bundled on purpose.

The four earlier inline threads were already resolved.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Apex Sentinel: 1 new finding on f3eeb38. The verdict and the full list are in the summary comment.

@thymikee

thymikee commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

At f3eeb38 the earlier findings are fixed and I found no new problems. The AI SDK adapter now builds its executor with structuredContent: 'always', with a test that goes red without it, and the output-economy metrics no longer count refs twice. The docs name the opt-ins and the error shape. All 19 checks pass and there are no conflicts.

The settle-removal claim in r4491331147 does not apply: with 9 removals the cap of 8 is exceeded, so 9 lines are kept and truncated is correct, and your tests cover the 8- and 9-removal cases.

Third-party MCP clients that read structuredContent from untyped tools will see the change you already call out in the PR body.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 6, 2026
@thymikee
thymikee merged commit 7b5b92d into main Nov 6, 2026
19 checks passed
@thymikee
thymikee deleted the mcp-compact-structured branch November 6, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant