From 12e72a96c4a54529872d6da0646bdc31c908ee25 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 28 Apr 2025 17:21:23 -0700 Subject: [PATCH] chore: allow configuring screenshot tool (#286) Fixes: https://github.com/microsoft/playwright-mcp/issues/277 --- config.d.ts | 17 +++++++++++++++++ src/tools/snapshot.ts | 5 +++-- tests/screenshot.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/config.d.ts b/config.d.ts index d08af7d..5a0a6d7 100644 --- a/config.d.ts +++ b/config.d.ts @@ -85,4 +85,21 @@ export type Config = { * The directory to save output files. */ outputDir?: string; + + /** + * Configuration for specific tools. + */ + tools?: { + /** + * Configuration for the browser_take_screenshot tool. + */ + browser_take_screenshot?: { + + /** + * Whether to disable base64-encoded image responses to the clients that + * don't support binary data or prefer to save on tokens. + */ + omitBase64?: boolean; + } + } }; diff --git a/src/tools/snapshot.ts b/src/tools/snapshot.ts index 8f87511..9571c2a 100644 --- a/src/tools/snapshot.ts +++ b/src/tools/snapshot.ts @@ -244,14 +244,15 @@ const screenshot = defineTool({ else code.push(`await page.screenshot(${javascript.formatObject(options)});`); + const includeBase64 = !context.config.tools?.browser_take_screenshot?.omitBase64; const action = async () => { const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options); return { - content: [{ + content: includeBase64 ? [{ type: 'image' as 'image', data: screenshot.toString('base64'), mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg', - }] + }] : [] }; }; diff --git a/tests/screenshot.spec.ts b/tests/screenshot.spec.ts index 5a94249..0e59837 100644 --- a/tests/screenshot.spec.ts +++ b/tests/screenshot.spec.ts @@ -93,3 +93,39 @@ test('browser_take_screenshot (outputDir)', async ({ startClient }, testInfo) => expect(fs.existsSync(outputDir)).toBeTruthy(); expect([...fs.readdirSync(outputDir)]).toHaveLength(1); }); + +test('browser_take_screenshot (omitBase64)', async ({ startClient }) => { + const client = await startClient({ + config: { + tools: { + browser_take_screenshot: { + omitBase64: true, + }, + }, + }, + }); + + expect(await client.callTool({ + name: 'browser_navigate', + arguments: { + url: 'data:text/html,TitleHello, world!', + }, + })).toContainTextContent(`Navigate to data:text/html`); + + await client.callTool({ + name: 'browser_take_screenshot', + arguments: {}, + }); + + expect(await client.callTool({ + name: 'browser_take_screenshot', + arguments: {}, + })).toEqual({ + content: [ + { + text: expect.stringContaining(`Screenshot viewport and save it as`), + type: 'text', + }, + ], + }); +});