feat(mcp): add compress option to browser_snapshot to collapse repeated ARIA nodes#41396
Closed
Josef-Le wants to merge 1 commit into
Closed
feat(mcp): add compress option to browser_snapshot to collapse repeated ARIA nodes#41396Josef-Le wants to merge 1 commit into
compress option to browser_snapshot to collapse repeated ARIA nodes#41396Josef-Le wants to merge 1 commit into
Conversation
…nodes Adds a `compress?: boolean` parameter to the `browser_snapshot` MCP tool. When set, a two-pass algorithm collapses repeated structural patterns in the ARIA snapshot YAML — keeping the first 10 occurrences of any pattern that appears more than 100 times — and emits a trailing note explaining how to enumerate the full list via browser_evaluate(). This targets pages with large lists, data grids, or navigation menus where the snapshot can grow to thousands of lines. On a 150-item GitHub issues page the output shrinks from ~1 800 lines to ~80 lines while preserving all interactive elements (buttons, inputs, links) unconditionally. Adds ariaCompression.ts with the pure compression function, and four tests covering the happy path, passthrough on small lists, interactive-element preservation, and the compress: false escape hatch.
Author
|
@microsoft-github-policy-service agree |
Member
|
Closing while discussion takes place in the issue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #41395
Summary
Adds a
compress?: booleanparameter to thebrowser_snapshotMCP tool. When set, a two-pass algorithm collapses repeated structural patterns in the ARIA snapshot YAML — keeping the first 10 occurrences of any pattern that appears more than 100 times — and emits a trailing note explaining how to enumerate the full list viabrowser_evaluate().Motivation
On pages with large lists, data grids, or autocomplete menus,
browser_snapshotcan return thousands of lines. A GitHub issues page with 100 open issues produces ~1 800 lines; a spreadsheet with 500 rows produces ~6 000 lines. The vast majority is structurally identical — only the text and[ref=eN]differ. This wastes tokens and can cause the model to miss important elements buried in the noise.Algorithm
Two-pass, O(n) time and space:
Pre-scan (safety gate): Normalise every line to a structural signature by stripping
[ref=eN], accessible names, and bare numbers. Count(indent, signature)pairs. Only proceed if the maximum count exceedsFIRE_THRESHOLD = 100. This prevents false positives on diverse pages (dashboards, forms, settings pages).Compression pass: Walk the YAML line by line; keep the first
KEEP_N = 10occurrences of any repeated pattern; collapse the rest along with their descendant subtrees. Lines matching interactive roles (button,input,link,checkbox,menuitem, …) are unconditionally kept.Trailing note: Informs the model how many lines were removed and suggests
browser_evaluate()for full enumeration.Example
Page with 150
<li>elements:Files changed
packages/playwright-core/src/tools/backend/ariaCompression.tspackages/playwright-core/src/tools/backend/snapshot.tscompresstobrowser_snapshotzod schemapackages/playwright-core/src/tools/backend/response.tscompressthroughsetIncludeFullSnapshot()tests/mcp/snapshot-compression.spec.tscompress: falseescape hatchDesign notes
browser_evaluate().Testing
The 4 tests in
tests/mcp/snapshot-compression.spec.tscover:compress: truecollapses a 150-item list to the first 10 itemscompress: falsereturns the full snapshot unchangedcompress: truedoes not fire on a 50-item list (belowFIRE_THRESHOLD)compress: truepreserves all 150 buttons (interactive-role guard)Algorithm correctness was also verified with a standalone Node.js harness (18 assertions, all pass) prior to integration.
This PR is opened as a draft pending maintainer triage of issue #41395.