Skip to content

Commit 6d090cf

Browse files
committed
feat: add default screenshot format configuration and update related tools
1 parent 9fa46ee commit 6d090cf

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

README.md

Lines changed: 29 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 jpeg.
388+
- **Type:** string
389+
- **Choices:** `png`, `jpeg`, `webp`
390+
- **Default:** `jpeg`
391+
386392
<!-- END AUTO GENERATED OPTIONS -->
387393

388394
Pass them via the `args` property in the JSON configuration. For example:
@@ -424,6 +430,29 @@ 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 JPEG. You can change it to PNG 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=png"
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+
> JPEG is the default format as it typically produces smaller file sizes than PNG, which improves performance when working with screenshots. WebP offers the best compression while maintaining quality. Use PNG if you need lossless screenshots.
455+
427456
You can also run `npx chrome-devtools-mcp@latest --help` to see all available configuration options.
428457

429458
## 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 ?? 'jpeg';
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+
'Use JPEG as the default screenshot format',
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export const screenshot = defineTool({
2222
schema: {
2323
format: zod
2424
.enum(['png', 'jpeg', 'webp'])
25-
.default('png')
26-
.describe('Type of format to save the screenshot as. Default is "png"'),
25+
.optional()
26+
.describe('Type of format to save the screenshot as. If not specified, uses the configured default format.'),
2727
quality: zod
2828
.number()
2929
.min(0)
@@ -63,8 +63,11 @@ export const screenshot = defineTool({
6363
pageOrHandle = context.getSelectedPage();
6464
}
6565

66+
// Use configured default format if not specified in request
67+
const format = request.params.format ?? context.getDefaultScreenshotFormat();
68+
6669
const screenshot = await pageOrHandle.screenshot({
67-
type: request.params.format,
70+
type: format,
6871
fullPage: request.params.fullPage,
6972
quality: request.params.quality,
7073
optimizeForSpeed: true, // Bonus: optimize encoding for speed
@@ -87,6 +90,7 @@ export const screenshot = defineTool({
8790
// Detect the actual format of the screenshot data
8891
// Puppeteer may not always return the requested format
8992
const actualFormat = detectImageFormat(screenshot);
93+
console.error(`[DEBUG] Requested format: ${format}, Detected format: ${actualFormat}`);
9094

9195
if (request.params.filePath) {
9296
const file = await context.saveFile(screenshot, request.params.filePath);

0 commit comments

Comments
 (0)