Skip to content

feat: downsample get_screen MCP images to save tokens - #35

Merged
trmquang93 merged 2 commits into
mainfrom
feature/mcp-downsample-screen-images
Apr 17, 2026
Merged

feat: downsample get_screen MCP images to save tokens#35
trmquang93 merged 2 commits into
mainfrom
feature/mcp-downsample-screen-images

Conversation

@trmquang93

@trmquang93 trmquang93 commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add imageMaxWidth parameter (default 400 px) to the MCP get_screen tool; when stored svgContent exists and the native width exceeds the target, re-render a fresh PNG from the SVG via @resvg/resvg-js so get_screen calls cost a fraction of the tokens.
  • Pass imageMaxWidth: 0 to 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_screen call 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

imageMaxWidth currently only affects screens stored as PNG data URIs (i.e. every Satori-rendered screen). The svg+xml branch (rare, imported-flow path) continues to rasterize at the SVG's intrinsic size as before.

Test plan

  • Sanity: rendered an iPhone 15 Pro screen via SatoriRenderer, then re-rendered the SVG at 400 px via Resvg -- PNG shrinks to roughly 36% of the full-size bytes.
  • End-to-end through the MCP handler:
    • 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).
    • Screen with svgContent cleared -> 786 px PNG passthrough.
  • Regression: list_screens, create_screen, update_screen_image, and batch_create_screens responses unchanged.

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.
@trmquang93
trmquang93 merged commit 61e9516 into main Apr 17, 2026
1 check passed
@github-actions
github-actions Bot deleted the feature/mcp-downsample-screen-images branch April 17, 2026 01:35
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