Skip to content

Record how big each MCP tool result is in tool-call analytics - #9687

Open
3mdistal wants to merge 3 commits into
mainfrom
alice/mcp-tool-call-result-size
Open

3mdistal wants to merge 3 commits into
mainfrom
alice/mcp-tool-call-result-size

Conversation

@3mdistal

Copy link
Copy Markdown
Contributor

Why

MCP hosts cut or move a big tool result before the model sees it. Measured on Nov 18 with a small probe server:

  • Claude Code 2.1.308 saves a result over 50,000 characters to a file.
  • Codex 0.169.0 keeps about 10,000 tokens from the start and end.
  • OpenCode 2.3.0 keeps the first 50,000 characters.

Since #7110, read-only actions return their whole JSON, so a large read now reaches the model cut short. Nobody knows yet which reads get that big, or how often: $mcp_tool_call records the tool, how long it took and whether it failed, but nothing about the size of what it returned. This PR adds the size, so the reads that need paging can be picked from data instead of guessed.

What changes

  • $mcp_tool_call gets two numbers:

    • $mcp_response_text_chars: characters in the result's text blocks.
    • $mcp_response_structured_chars: characters in the result's structured content (structuredContent) written out as JSON. It's left out when the result has none.

    They're counted apart because hosts read different parts. Claude Code and OpenCode show the model only the structured content when a result has one, and Codex shows both, so a read costs a Codex user about double. Images and other block types aren't counted.

  • The tools/call handler measures the result it's about to return, after every early return, with the new mcpToolResultSize() in mcp/analytics.ts, and hands trackMcpToolCall only the two numbers. The result itself never reaches the analytics call, so the specs that check a ticket or the caller's text stays out of analytics still hold.

  • The tracking skill lists the two properties and says why they're separate.

  • direct-action-call.spec.ts pins the analytics payload of every call it makes, so its 41 snapshots gain the size lines, each with an oracle-change note.

Risk

  • Privacy: only lengths leave the server. $mcp_response is still never sent, and $mcp_parameters stays opt-in.
  • Cost: one extra JSON.stringify of the structured content per call, which the SDK serializes anyway to send it. A value JSON can't hold (a BigInt, a cycle) now fails in the handler rather than in the transport. The call failed either way.
  • Names: PostHog's MCP analytics vocabulary, which these events follow, has no size property, so the two names are ours, alongside our own $mcp_listed_tool_count and $mcp_vendor_source.

Verification

On a fresh Linux machine, with this branch on main at 21c90bd:

  • Core's pnpm typecheck and the workspace pnpm typecheck pass.
  • Core's src/mcp specs pass (3,016 tests). They include a new end-to-end check that the event's two numbers equal the text and structured content the client received, and that the result's text never appears in the event.
  • direct-action-call.spec.ts passed two more runs after its snapshot update, so the sizes don't drift between runs.
  • pnpm guards passes all 112 checks, and oxfmt and oxlint pass on the changed files.

Review focus

  • Is the split into text and structured content right, or would one total serve the follow-up just as well?
  • mcpToolResultSize runs on every call, including when MCP analytics are off. Worth skipping then?

Follow-ups

  • Once about a week of events is in, list the reads whose results pass about 40,000 characters and add paging to each, in separate PRs.

🤖 Generated with Claude Code

$mcp_tool_call gains $mcp_response_text_chars (characters in the result's
text blocks) and $mcp_response_structured_chars (its structured content as
JSON, when present). Hosts cut or move a result past about 40,000 characters
and read different parts of it, so the two are counted apart. The handler
measures the result and passes only the numbers to analytics.
@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Adds separate text and structured-content lengths to $mcp_tool_call.

  • MCP tool-call analytics show how large each result part is.

3mdistal acknowledges the extra serialization cost, measurement when analytics are disabled, and earlier failure for values JSON cannot hold.

Reviews (1) · Last reviewed commit: "test(core): read the tool-call event wit..." · Reviewed by Greptile

@builder-io-integration builder-io-integration 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.

Builder reviewed your changes and has a few items to flag 🟡

Review Details

Code Review Summary

PR #9687 adds separate character counts for MCP tool-result text blocks and serialized structuredContent, emits them as numeric $mcp_tool_call properties, documents the fields, and updates coverage/snapshots. This is standard-risk framework analytics work. The split is useful because clients consume the two result parts differently, and the handler sends only lengths—not payload content—to tracking. Tests exercise mixed result content and assert the result text is absent from analytics. The changeset is included.

Findings

  • 🟡 Avoidable serialization when analytics are disabled — Both review agents found that result sizing runs before trackMcpToolCall checks whether analytics are enabled; one rated it medium and the other low. This can stringify large structured results even when the measurement will be discarded.
  • Snapshot audit trail overwritten — One agent noted the update replaces existing oracle-change explanations with the new result-size explanation in several places, losing the earlier snapshot-change rationale. Keep both reasons where both changes apply.

No blocking issue was identified. 🧪 Browser testing: Skipped — PR only modifies backend, docs, changeset, and tests; no UI impact.

@github-actions

Copy link
Copy Markdown
Contributor

Visual recap — generation failed

Recap authentication failed — PLAN_RECAP_TOKEN was rejected; it may be expired, revoked, or minted for another Plans app. Personal token: run npx @agent-native/core@latest connect <app-url> --client codex, then npx @agent-native/recap-cli@latest recap setup. Org service token: an owner/admin can mint a 10-year replacement with npx -y @agent-native/core@latest connect <app-url> --service-token <name> --ttl-days 3650. Update this repo's secret.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant