Skip to content

Commit e6fb135

Browse files
committed
feat: add default screenshot format configuration and update related functionality
1 parent 6f9182f commit e6fb135

File tree

7 files changed

+68
-5
lines changed

7 files changed

+68
-5
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ The Chrome DevTools MCP server supports the following configuration option:
383383
- **Type:** boolean
384384
- **Default:** `true`
385385

386+
- **`--screenshot-format`**
387+
Default image format for screenshots. Options: png, jpeg, webp. Default is png.
388+
- **Type:** string
389+
- **Choices:** `png`, `jpeg`, `webp`
390+
- **Default:** `png`
391+
386392
<!-- END AUTO GENERATED OPTIONS -->
387393

388394
Pass them via the `args` property in the JSON configuration. For example:
@@ -424,6 +430,36 @@ You can connect directly to a Chrome WebSocket endpoint and include custom heade
424430

425431
To get the WebSocket endpoint from a running Chrome instance, visit `http://127.0.0.1:9222/json/version` and look for the `webSocketDebuggerUrl` field.
426432

433+
### Configuring default screenshot format
434+
435+
You can set a default image format for all screenshots using the `--screenshot-format` option. The default is PNG. You can change it to JPEG or WebP if needed:
436+
437+
```json
438+
{
439+
"mcpServers": {
440+
"chrome-devtools": {
441+
"command": "npx",
442+
"args": [
443+
"chrome-devtools-mcp@latest",
444+
"--screenshot-format=jpeg"
445+
]
446+
}
447+
}
448+
}
449+
```
450+
451+
When configured, the `take_screenshot` tool will use this format by default unless explicitly overridden by passing a `format` parameter. Supported formats are `png`, `jpeg`, and `webp`.
452+
453+
> [!TIP]
454+
> PNG is the default format as it provides lossless screenshots. JPEG typically produces smaller file sizes than PNG, which improves performance when working with screenshots. WebP offers the best compression while maintaining quality.
455+
456+
> [!NOTE]
457+
> **Claude Code users**: If you experience issues with screenshots not displaying correctly, you can work around this by:
458+
> 1. Setting the default format to JPEG in your configuration (recommended): Add `--screenshot-format=jpeg` to your MCP server args
459+
> 2. Explicitly passing `jpeg` as the format parameter in all `take_screenshot()` calls: `take_screenshot(format="jpeg")`
460+
>
461+
> This workaround is needed until the issue is resolved on Claude Code's side.
462+
427463
You can also run `npx chrome-devtools-mcp@latest --help` to see all available configuration options.
428464

429465
## Concepts

src/McpContext.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ interface McpContextOptions {
4949
experimentalDevToolsDebugging: boolean;
5050
// Whether all page-like targets are exposed as pages.
5151
experimentalIncludeAllPages?: boolean;
52+
// Default screenshot format.
53+
screenshotFormat?: 'png' | 'jpeg' | 'webp';
5254
}
5355

5456
const DEFAULT_TIMEOUT = 5_000;
@@ -292,6 +294,10 @@ export class McpContext implements Context {
292294
this.#dialog = undefined;
293295
}
294296

297+
getDefaultScreenshotFormat(): 'png' | 'jpeg' | 'webp' {
298+
return this.#options.screenshotFormat ?? 'png';
299+
}
300+
295301
getSelectedPage(): Page {
296302
const page = this.#selectedPage;
297303
if (!page) {

src/cli.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ export const cliOptions = {
160160
default: true,
161161
describe: 'Set to false to exclude tools related to network.',
162162
},
163+
screenshotFormat: {
164+
type: 'string',
165+
describe:
166+
'Default image format for screenshots. Options: png, jpeg, webp. Default is png.',
167+
choices: ['png', 'jpeg', 'webp'] as const,
168+
default: 'png',
169+
},
163170
} satisfies Record<string, YargsOptions>;
164171

165172
export function parseArguments(version: string, argv = process.argv) {
@@ -212,6 +219,10 @@ export function parseArguments(version: string, argv = process.argv) {
212219
'Disable tools in the performance category',
213220
],
214221
['$0 --no-category-network', 'Disable tools in the network category'],
222+
[
223+
'$0 --screenshot-format jpeg',
224+
'Set default screenshot format to JPEG (overrides PNG default)',
225+
],
215226
]);
216227

217228
return yargsInstance

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ async function getContext(): Promise<McpContext> {
8686
context = await McpContext.from(browser, logger, {
8787
experimentalDevToolsDebugging: devtools,
8888
experimentalIncludeAllPages: args.experimentalIncludeAllPages,
89+
screenshotFormat: args.screenshotFormat as 'png' | 'jpeg' | 'webp',
8990
});
9091
}
9192
return context;

src/tools/ToolDefinition.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export type Context = Readonly<{
120120
* Returns a reqid for a cdpRequestId.
121121
*/
122122
resolveCdpElementId(cdpBackendNodeId: number): string | undefined;
123+
/**
124+
* Returns the configured default screenshot format.
125+
*/
126+
getDefaultScreenshotFormat(): 'png' | 'jpeg' | 'webp';
123127
}>;
124128

125129
export function defineTool<Schema extends zod.ZodRawShape>(

src/tools/screenshot.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export const screenshot = defineTool({
2121
schema: {
2222
format: zod
2323
.enum(['png', 'jpeg', 'webp'])
24-
.default('png')
25-
.describe('Type of format to save the screenshot as. Default is "png"'),
24+
.optional()
25+
.describe('Type of format to save the screenshot as. If not specified, uses the configured default format.'),
2626
quality: zod
2727
.number()
2828
.min(0)
@@ -62,8 +62,11 @@ export const screenshot = defineTool({
6262
pageOrHandle = context.getSelectedPage();
6363
}
6464

65+
// Use configured default format if not specified in request
66+
const format = request.params.format ?? context.getDefaultScreenshotFormat();
67+
6568
const screenshot = await pageOrHandle.screenshot({
66-
type: request.params.format,
69+
type: format,
6770
fullPage: request.params.fullPage,
6871
quality: request.params.quality,
6972
optimizeForSpeed: true, // Bonus: optimize encoding for speed
@@ -89,12 +92,12 @@ export const screenshot = defineTool({
8992
} else if (screenshot.length >= 2_000_000) {
9093
const {filename} = await context.saveTemporaryFile(
9194
screenshot,
92-
`image/${request.params.format}`,
95+
`image/${format}`,
9396
);
9497
response.appendResponseLine(`Saved screenshot to ${filename}.`);
9598
} else {
9699
response.attachImage({
97-
mimeType: `image/${request.params.format}`,
100+
mimeType: `image/${format}`,
98101
data: Buffer.from(screenshot).toString('base64'),
99102
});
100103
}

tests/cli.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('cli args parsing', () => {
1616
categoryPerformance: true,
1717
'category-network': true,
1818
categoryNetwork: true,
19+
'screenshot-format': 'png',
20+
screenshotFormat: 'png',
1921
};
2022

2123
it('parses with default args', async () => {

0 commit comments

Comments
 (0)