Skip to content

Commit 8c76d72

Browse files
committed
feat(screenshot): add CLI options to cap screenshot size at the source
Adds opt-in CLI flags so operators can cap the size of screenshots returned by `take_screenshot` before they are embedded in the MCP response. Addresses two related symptoms reported when MCP clients display screenshots inline: 1. The hosted LLM API rejects images exceeding its per-image dimension limits (e.g. Anthropic's 8000x8000 px / 2000x2000 px when >20 images are in the same request). 2. After many captures the cumulative base64 payload pushes the request over the per-call body size limit. Both can be mitigated at the source by reducing format/quality and downscaling the capture. New CLI flags (all opt-in, no behavior change when unset): - --screenshot-format <jpeg|png|webp>: override the default format used by take_screenshot when the caller does not specify one. - --screenshot-quality <0-100>: override the default JPEG/WebP quality when the caller does not specify one. Ignored for PNG. - --screenshot-max-width <px>: downscale screenshots wider than this before they are returned. - --screenshot-max-height <px>: downscale screenshots taller than this before they are returned. Combines with --screenshot-max-width; the smaller scale wins so both bounds are respected while preserving aspect ratio. Resizing leverages Puppeteer's clip.scale (CDP Page.captureScreenshot) so no new dependencies are introduced. Source dimensions are computed per capture mode: - viewport: page.viewport() - full page: document.documentElement.scrollWidth/scrollHeight via page.evaluate() - element (uid): elementHandle.boundingBox() For element and full-page captures with a downscale clip, the call is routed through page.screenshot({clip}) so the scale parameter applies. captureBeyondViewport is left to Puppeteer's default (true when a clip is set), which preserves correct behavior for elements below the fold and for full-page captures. Design notes: - Aligned with the "Reference over Value" principle in docs/design-principles.md: the existing 2 MB threshold still routes oversized screenshots to a temporary file. This change only reduces the size of the inline base64 fallback path, which the principles document calls out as an acceptable exception when MCP clients display images natively. - Fully opt-in: when no flags are set, take_screenshot returns the exact same bytes as before. No breaking change. - The MCP server hardcodes no LLM-specific size limits — operators pick the values that match their client/model combination. This keeps the maintenance surface minimal as model limits evolve and is intended as a complement to, not a replacement for, fixes in the MCP client itself. - Compares against CSS pixels (page.viewport()), not raw bitmap pixels, so HiDPI emulation behaves predictably from the user's perspective. Tests added (6 new): - honors screenshotFormat default from CLI args - keeps "png" as default format when no CLI override is set - downscales viewport screenshot when screenshotMaxWidth is set - downscales using the smaller scale when both max-width and max-height are set - does not resize when source is smaller than the max bounds - downscales full page screenshot when screenshotMaxWidth is set Refs #879
1 parent 2e039c0 commit 8c76d72

5 files changed

Lines changed: 476 additions & 102 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,23 @@ The Chrome DevTools MCP server supports the following configuration option:
675675
- **Type:** boolean
676676
- **Default:** `true`
677677

678+
- **`--screenshotFormat`/ `--screenshot-format`**
679+
Override the default output format used by take_screenshot when the caller does not specify one. JPEG and WebP are ~3-5x smaller than PNG, which helps reduce context size in AI conversations. Unset preserves the existing default ("png").
680+
- **Type:** string
681+
- **Choices:** `jpeg`, `png`, `webp`
682+
683+
- **`--screenshotQuality`/ `--screenshot-quality`**
684+
Override the default compression quality (0-100) used by take_screenshot for JPEG and WebP when the caller does not specify one. Lower values mean smaller files. Ignored for PNG. Unset preserves the Puppeteer default.
685+
- **Type:** number
686+
687+
- **`--screenshotMaxWidth`/ `--screenshot-max-width`**
688+
Maximum width in pixels for screenshots. If the captured image is wider, it is downscaled (preserving aspect ratio) before being returned. Reduces context size in AI conversations. Unset means no resize.
689+
- **Type:** number
690+
691+
- **`--screenshotMaxHeight`/ `--screenshot-max-height`**
692+
Maximum height in pixels for screenshots. If the captured image is taller, it is downscaled (preserving aspect ratio) before being returned. Can be combined with --screenshot-max-width; the smaller scale factor wins. Unset means no resize.
693+
- **Type:** number
694+
678695
- **`--slim`**
679696
Exposes a "slim" set of 3 tools covering navigation, script execution and screenshots only. Useful for basic browser tasks.
680697
- **Type:** boolean

src/bin/chrome-devtools-mcp-cli-options.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,60 @@ export const cliOptions = {
264264
hidden: true,
265265
describe: 'Include watchdog PID in Clearcut request headers (for testing).',
266266
},
267+
screenshotFormat: {
268+
type: 'string',
269+
description:
270+
'Override the default output format used by take_screenshot when the caller does not specify one. JPEG and WebP are ~3-5x smaller than PNG, which helps reduce context size in AI conversations. Unset preserves the existing default ("png").',
271+
choices: ['jpeg', 'png', 'webp'] as const,
272+
},
273+
screenshotQuality: {
274+
type: 'number',
275+
description:
276+
'Override the default compression quality (0-100) used by take_screenshot for JPEG and WebP when the caller does not specify one. Lower values mean smaller files. Ignored for PNG. Unset preserves the Puppeteer default.',
277+
coerce: (value: number | undefined) => {
278+
if (value === undefined) {
279+
return;
280+
}
281+
if (!Number.isInteger(value) || value < 0 || value > 100) {
282+
throw new Error(
283+
`Invalid screenshotQuality ${value}. Expected an integer between 0 and 100.`,
284+
);
285+
}
286+
return value;
287+
},
288+
},
289+
screenshotMaxWidth: {
290+
type: 'number',
291+
description:
292+
'Maximum width in pixels for screenshots. If the captured image is wider, it is downscaled (preserving aspect ratio) before being returned. Reduces context size in AI conversations. Unset means no resize.',
293+
coerce: (value: number | undefined) => {
294+
if (value === undefined) {
295+
return;
296+
}
297+
if (!Number.isInteger(value) || value <= 0) {
298+
throw new Error(
299+
`Invalid screenshotMaxWidth ${value}. Expected a positive integer.`,
300+
);
301+
}
302+
return value;
303+
},
304+
},
305+
screenshotMaxHeight: {
306+
type: 'number',
307+
description:
308+
'Maximum height in pixels for screenshots. If the captured image is taller, it is downscaled (preserving aspect ratio) before being returned. Can be combined with --screenshot-max-width; the smaller scale factor wins. Unset means no resize.',
309+
coerce: (value: number | undefined) => {
310+
if (value === undefined) {
311+
return;
312+
}
313+
if (!Number.isInteger(value) || value <= 0) {
314+
throw new Error(
315+
`Invalid screenshotMaxHeight ${value}. Expected a positive integer.`,
316+
);
317+
}
318+
return value;
319+
},
320+
},
267321
slim: {
268322
type: 'boolean',
269323
describe:

src/telemetry/flag_usage_metrics.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,31 @@
295295
{
296296
"name": "category_experimental_third_party",
297297
"flagType": "boolean"
298+
},
299+
{
300+
"name": "screenshot_format_present",
301+
"flagType": "boolean"
302+
},
303+
{
304+
"name": "screenshot_format",
305+
"flagType": "enum",
306+
"choices": [
307+
"SCREENSHOT_FORMAT_UNSPECIFIED",
308+
"SCREENSHOT_FORMAT_JPEG",
309+
"SCREENSHOT_FORMAT_PNG",
310+
"SCREENSHOT_FORMAT_WEBP"
311+
]
312+
},
313+
{
314+
"name": "screenshot_quality_present",
315+
"flagType": "boolean"
316+
},
317+
{
318+
"name": "screenshot_max_width_present",
319+
"flagType": "boolean"
320+
},
321+
{
322+
"name": "screenshot_max_height_present",
323+
"flagType": "boolean"
298324
}
299325
]

0 commit comments

Comments
 (0)