feat: downsample get_screen MCP images to save tokens - #35
Merged
Conversation
Add an `imageMaxWidth` parameter (default 400 px) to the `get_screen` tool. When the stored screen has `svgContent` and its width exceeds `imageMaxWidth`, re-render a fresh PNG from the SVG via @resvg/resvg-js instead of returning the full-resolution Retina PNG. Pass `imageMaxWidth: 0` to disable resizing. The `svg+xml` image branch now honors the same parameter, and both resvg call sites share a `renderSvgToPngBase64` helper. Rasterizing straight from the stored vector is sharper than post-resizing a 2x PNG, and reuses an existing dependency without pulling in a new image library.
Drop the `renderSvgToPngBase64` helper and `DEFAULT_IMAGE_MAX_WIDTH` constant; inline the single resvg call at the PNG-branch downsample site. Revert the `svg+xml` branch to its original `mode: "original"` form — `imageMaxWidth` is only meaningful for Satori-rendered screens, which always store PNG data URIs, so applying it to imported SVG screens was outside the PR's scope and required an awkward `(imageWidth || 400)` fallback. No behavior change for the PNG branch: the four-case E2E (400 / 200 / 0 / no-svg) still yields 400, 200, 786, 786 px respectively.
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.
Summary
imageMaxWidthparameter (default 400 px) to the MCPget_screentool; when storedsvgContentexists and the native width exceeds the target, re-render a fresh PNG from the SVG via@resvg/resvg-jssoget_screencalls cost a fraction of the tokens.imageMaxWidth: 0to opt back into the original full-resolution Retina PNG; SVG-less screens pass through unchanged.Why
Stored screens are Retina-scaled (e.g. 786x1704 for iPhone 15 Pro, up to 2064x2752 for iPad Pro 13"), so every
get_screencall forwarded ~100-300 KB of base64 and thousands of image tokens. For tasks like "describe this screen" or "find the login button," a ~400 px thumbnail is plenty. Rasterizing straight from the stored vector is sharper than post-resizing a 2x PNG and reuses an existing dependency.Scope note
imageMaxWidthcurrently only affects screens stored as PNG data URIs (i.e. every Satori-rendered screen). Thesvg+xmlbranch (rare, imported-flow path) continues to rasterize at the SVG's intrinsic size as before.Test plan
SatoriRenderer, then re-rendered the SVG at 400 px via Resvg -- PNG shrinks to roughly 36% of the full-size bytes.includeImage: true(default) -> 400 px PNG (b64 len 4380).imageMaxWidth: 200-> 200 px PNG (b64 len 1740).imageMaxWidth: 0-> 786 px PNG (b64 len 12088).svgContentcleared -> 786 px PNG passthrough.list_screens,create_screen,update_screen_image, andbatch_create_screensresponses unchanged.