feat: add token optimization parameters (maxLength, selector, image resizing)#833
feat: add token optimization parameters (maxLength, selector, image resizing)#833semikolon wants to merge 5 commits intoChromeDevTools:mainfrom
Conversation
Inspired by fast-playwright-mcp, this adds foundation for token-efficient responses: - Add expectation.ts with schema for controlling response content - includeSnapshot, includeConsole, includeNetwork, includeTabs - snapshotOptions (selector, maxLength, verbose) - imageOptions (quality, maxWidth, maxHeight, format) - Tool-specific defaults for optimal token usage - Add maxLength truncation to SnapshotFormatter - Truncates snapshot output with notice when limit exceeded - Useful for token efficiency on large pages - Expose maxLength parameter in take_snapshot tool - Users can now limit snapshot size directly This is Phase 1 of token optimization - the infrastructure layer. Future phases can integrate the expectation schema into tool handlers and add image processing utilities. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Document the fork's token optimization features: - Snapshot truncation via maxLength parameter - Future enhancement infrastructure (expectation schema) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ation Phase 2 of token optimization implementation: Snapshot CSS selector filtering: - Added `selector` parameter to take_snapshot tool - Filters snapshot to only include subtree matching CSS selector - Uses CDP DOM.describeNode to map elements to accessibility nodes - Falls back to full snapshot with warning if selector doesn't match Screenshot image resizing: - Added `maxWidth` and `maxHeight` parameters to take_screenshot - Images resized maintaining aspect ratio using sharp library - Logs compression ratio when resizing occurs - Added sharp as production dependency Code changes: - src/utils/image-processor.ts: New utility for image processing - src/McpResponse.ts: Added #filterSnapshotBySelector private method - src/tools/screenshot.ts: Integrated image resizing - src/tools/snapshot.ts: Added selector parameter - src/tools/ToolDefinition.ts: Extended SnapshotParams with selector Documentation: - Updated README with full token optimization documentation - Added parameter reference table for all optimization options Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
- Added "Browser MCP Selection" section with fork recommendation - Updated comparison table to include CDT Fork column - Removed outdated "Both: Doesn't exist yet" - it now exists! - Removed redundant Playwright GPU config (use --autoConnect instead) - Updated fast-playwright-mcp section to reference the fork Fork: https://github.com/semikolon/chrome-devtools-mcp PR: ChromeDevTools/chrome-devtools-mcp#833 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add maxLength truncation tests to snapshotFormatter.test.ts - Add new image-processor.test.ts with tests for resize and format conversion - Use sharp to generate valid test images (more reliable than manual PNG bytes) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- maxWidth, maxHeight for take_screenshot - maxLength, selector for take_snapshot - Format fixes from prettier Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| .describe( | ||
| 'The absolute path, or a path relative to the current working directory, to save the snapshot to instead of attaching it to the response.', | ||
| ), | ||
| maxLength: zod |
There was a problem hiding this comment.
max length does not seem like a good parameter for tree structures.
|
|
||
| // Apply selector filtering if specified | ||
| if (textSnapshot && this.#snapshotParams.selector) { | ||
| const filteredSnapshot = await this.#filterSnapshotBySelector( |
There was a problem hiding this comment.
I think this might not work as expected, we would need more tests for different scenarios of getting filtered snapshots and using uids from the current and the past snapshots.
| return null; | ||
| } | ||
|
|
||
| // Get the backendNodeId via CDP |
There was a problem hiding this comment.
I believe backendNodeId is a public API in puppeteer https://pptr.dev/api/puppeteer.elementhandle.backendnodeid
| * Process an image with optional resizing and format conversion. | ||
| * Returns the processed image buffer and metadata. | ||
| */ | ||
| export async function processImage( |
There was a problem hiding this comment.
We should not include image processing in the same PR as the snapshot work. Additionally, I do not think we plan to have any image processing capabilities right now.
Summary
Adds token optimization features inspired by fast-playwright-mcp, addressing concerns raised in #340, #726, and #716.
Changes
Snapshot optimization
maxLengthparameter - truncates snapshot with notice when exceededselectorparameter - limits snapshot to CSS selector subtree (addresses Accessibility tree/snapshot for a specific DOM element, including whole hierarchy below it (custom "root" element) #716)Screenshot optimization
maxWidth/maxHeightparameters - resizes images maintaining aspect ratiosharplibrary for image processingFiles changed
src/tools/snapshot.ts- added maxLength, selector paramssrc/tools/screenshot.ts- added maxWidth, maxHeight paramssrc/utils/image-processor.ts- new image processing utilitysrc/McpResponse.ts- selector filtering logicsrc/tools/ToolDefinition.ts- extended SnapshotParamsToken savings
These features allow agents to dramatically reduce context usage on large pages by: