Skip to content

fix(server): bound preview_snapshot metadata sent to the agent - #9505

Open
ylcn91 wants to merge 2 commits into
pingdotgg:mainfrom
ylcn91:fix/preview-snapshot-metadata-budget
Open

fix(server): bound preview_snapshot metadata sent to the agent#9505
ylcn91 wants to merge 2 commits into
pingdotgg:mainfrom
ylcn91:fix/preview-snapshot-metadata-budget

Conversation

@ylcn91

@ylcn91 ylcn91 commented Sep 3, 2026

Copy link
Copy Markdown

What Changed

packages/contracts/src/previewAutomation.ts: PreviewAutomationSnapshot gains an optional truncation field (originalBytes, omitted, trimmed) so a reduced result stays inside the tool's advertised output schema.

apps/server/src/mcp/McpHttpServer.ts: the preview_snapshot metadata handed to the agent is bounded at 100 KB by boundPreviewSnapshotMetadata. Over the limit, the title and URL are first capped at 2,048 characters (they are identifiers, and a data: URL can be the whole overrun by itself), then fields go least useful first: the accessibility tree, then the network, console, and action logs (only when they hold anything), then the visible text is cut short, and only then the interactive elements, which carry the locators the other tools need. The screenshot rides beside the metadata as an image and is never reduced. A truncated result adds a plain-language text block saying what was omitted or cut and pointing at preview_evaluate.

apps/server/src/mcp/toolkits/preview/tools.ts: the tool description says the metadata is bounded and where to look.

Tests: the reduction order and its bookkeeping on synthetic snapshots (including multi-byte text), and the tool result through the broker: bounded JSON, image kept, note present, text equal to structuredContent.

Why

Fixes #7410.

The snapshot forwarded Chrome's full accessibility tree with no limit. On content-heavy pages that alone ran to hundreds of kilobytes per tool result, enough to crowd out the thread's context; the reporter saw a later turn fail with an internal provider error once such a result entered the conversation. The producer already caps visible text (20k characters) and interactive elements (200), so the tree is the only unbounded field, and the limit is placed at the MCP boundary so every provider is covered. 100 KB sits above what the producer's own caps add up to on an ordinary page, so a snapshot without the tree is normally sent whole.

The marker lives in the contract rather than as an extra key because MCP clients may validate structuredContent against the tool's output schema, which rejects unknown properties.

#7408 proposed a fix for the same issue earlier (64 KB limit, drop the tree then fall back to a compact summary) and has not moved since mid-August. This one differs in two ways that seemed worth a separate PR rather than a comment: the truncation marker is declared in the output schema instead of added as an undeclared key, and the reduction is an ordered ladder that keeps the interactive elements for as long as possible. Happy to fold either PR into the other if a maintainer prefers.

Model and harness: Claude Fable 5.1 in Claude Code.

Note

Bound preview_snapshot metadata to 100,000 bytes in McpHttpServer

  • Adds boundPreviewSnapshotMetadata in McpHttpServer.ts to limit preview_snapshot JSON metadata to 100,000 bytes (PREVIEW_SNAPSHOT_METADATA_MAX_BYTES).
  • Oversized metadata is reduced in a fixed order: cap titles and URLs, omit the accessibility tree and diagnostics, shorten visible text, and omit interactive elements last.
  • Updates the PreviewAutomationSnapshot contract in previewAutomation.ts with an optional truncation object. The MCP response now includes an explanatory text note when metadata is reduced.
  • Risk: Behavioral change - preview_snapshot now returns truncated metadata and an extra text item when the byte budget is exceeded. Callers must check the truncation object to detect omitted fields.

Macroscope summarized d7633d8.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Sep 3, 2026
Comment thread apps/server/src/mcp/McpHttpServer.ts
@macroscopeapp

macroscopeapp Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Would Approve

Macroscope's review found this PR approvable — This is a focused, well-tested guardrail bug fix that bounds oversized preview metadata while preserving normal snapshots, screenshots, and interactive elements. The follow-up caps oversized URLs and titles with regression coverage; the supplied unresolved high-severity bound concern remains an independent policy block.

Not approved because:

  • 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity

Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more.

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

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: preview_snapshot can exhaust agent context with unbounded metadata

1 participant