Skip to content

Commit ced4f9a

Browse files
aberemia24OrKoN
authored andcommitted
feat(screenshot): add WebP format support with quality parameter
Add WebP format to screenshot tool, providing superior compression compared to JPEG. Also fixes bug where saveTemporaryFile always saved files with .png extension regardless of format.
1 parent a9fc863 commit ced4f9a

4 files changed

Lines changed: 17 additions & 10 deletions

File tree

docs/tool-reference.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,14 @@ so returned values have to JSON-serializable.
306306

307307
**Parameters:**
308308

309+
<<<<<<< HEAD
309310
- **filePath** (string) _(optional)_: The absolute path, or a path relative to the current working directory, to save the screenshot to instead of attaching it to the response.
310311
- **format** (enum: "png", "jpeg") _(optional)_: Type of format to save the screenshot as. Default is "png"
312+
=======
313+
- **format** (enum: "png", "jpeg", "webp") _(optional)_: Type of format to save the screenshot as. Default is "png"
314+
>>>>>>> 9b0e51e (feat(screenshot): add WebP format support with quality parameter)
311315
- **fullPage** (boolean) _(optional)_: If set to true takes a screenshot of the full page instead of the currently visible viewport. Incompatible with uid.
312-
- **quality** (number) _(optional)_: Compression quality for JPEG format (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.
316+
- **quality** (number) _(optional)_: Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.
313317
- **uid** (string) _(optional)_: The uid of an element on the page from the page content snapshot. If omitted takes a pages screenshot.
314318

315319
---

src/McpContext.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,20 @@ export class McpContext implements Context {
340340

341341
async saveTemporaryFile(
342342
data: Uint8Array<ArrayBufferLike>,
343-
mimeType: 'image/png' | 'image/jpeg',
343+
mimeType: 'image/png' | 'image/jpeg' | 'image/webp',
344344
): Promise<{filename: string}> {
345345
try {
346346
const dir = await fs.mkdtemp(
347347
path.join(os.tmpdir(), 'chrome-devtools-mcp-'),
348348
);
349-
const filename = path.join(
350-
dir,
351-
mimeType == 'image/png' ? `screenshot.png` : 'screenshot.jpg',
352-
);
353-
await fs.writeFile(path.join(dir, `screenshot.png`), data);
349+
const ext =
350+
mimeType === 'image/png'
351+
? 'png'
352+
: mimeType === 'image/jpeg'
353+
? 'jpg'
354+
: 'webp';
355+
const filename = path.join(dir, `screenshot.${ext}`);
356+
await fs.writeFile(filename, data);
354357
return {filename};
355358
} catch (err) {
356359
this.logger(err);

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export type Context = Readonly<{
7272
setCpuThrottlingRate(rate: number): void;
7373
saveTemporaryFile(
7474
data: Uint8Array<ArrayBufferLike>,
75-
mimeType: 'image/png' | 'image/jpeg',
75+
mimeType: 'image/png' | 'image/jpeg' | 'image/webp',
7676
): Promise<{filename: string}>;
7777
waitForEventsAfterAction(action: () => Promise<unknown>): Promise<void>;
7878
}>;

src/tools/screenshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const screenshot = defineTool({
2121
},
2222
schema: {
2323
format: z
24-
.enum(['png', 'jpeg'])
24+
.enum(['png', 'jpeg', 'webp'])
2525
.default('png')
2626
.describe('Type of format to save the screenshot as. Default is "png"'),
2727
quality: z
@@ -30,7 +30,7 @@ export const screenshot = defineTool({
3030
.max(100)
3131
.optional()
3232
.describe(
33-
'Compression quality for JPEG format (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.',
33+
'Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.',
3434
),
3535
uid: z
3636
.string()

0 commit comments

Comments
 (0)