Skip to content

Commit 51593d9

Browse files
author
Jun
committed
add optional encoded param to puppeteer_screenshot for base64 output
1 parent 2490245 commit 51593d9

2 files changed

Lines changed: 7 additions & 51 deletions

File tree

src/puppeteer/README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,7 @@ A Model Context Protocol server that provides browser automation capabilities us
2222
- `selector` (string, optional): CSS selector for element to screenshot
2323
- `width` (number, optional, default: 800): Screenshot width
2424
- `height` (number, optional, default: 600): Screenshot height
25-
26-
- **puppeteer_screenshot_encoded**
27-
- Captures a screenshot of the entire page or a specific element and return it as a base64-encoded data URI.
28-
- Inputs:
29-
- `name` (string, required): Name for the screenshot
30-
- `selector` (string, optional): CSS selector for element to screenshot
31-
- `width` (number, optional, default: 800): Screenshot width
32-
- `height` (number, optional, default: 600): Screenshot height
25+
- `encoded` (boolean, optional): If true, capture the screenshot as a base64-encoded data URI (as text) instead of binary image content. Default false.
3326

3427
- **puppeteer_click**
3528

src/puppeteer/index.ts

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ async function handleToolCall(name: string, args: any): Promise<CallToolResult>
242242
case "puppeteer_screenshot": {
243243
const width = args.width ?? 800;
244244
const height = args.height ?? 600;
245+
const encoded = args.encoded ?? false;
245246
await page.setViewport({ width, height });
246247

247248
const screenshot = await (args.selector ?
@@ -269,52 +270,14 @@ async function handleToolCall(name: string, args: any): Promise<CallToolResult>
269270
type: "text",
270271
text: `Screenshot '${args.name}' taken at ${width}x${height}`,
271272
} as TextContent,
272-
{
273+
encoded ? ({
274+
type: "text",
275+
text: `data:image/png;base64,${screenshot}`,
276+
} as TextContent) : ({
273277
type: "image",
274278
data: screenshot,
275279
mimeType: "image/png",
276-
} as ImageContent,
277-
],
278-
isError: false,
279-
};
280-
}
281-
282-
case "puppeteer_screenshot_encoded": {
283-
const width = args.width ?? 800;
284-
const height = args.height ?? 600;
285-
await page.setViewport({ width, height });
286-
287-
const screenshot = await (args.selector
288-
? (await page.$(args.selector))?.screenshot({ encoding: "base64" })
289-
: page.screenshot({ encoding: "base64", fullPage: false }));
290-
291-
if (!screenshot) {
292-
return {
293-
content: [
294-
{
295-
type: "text",
296-
text: args.selector ? `Element not found: ${args.selector}` : "Screenshot failed",
297-
},
298-
],
299-
isError: true,
300-
};
301-
}
302-
303-
screenshots.set(args.name, screenshot as string);
304-
server.notification({
305-
method: "notifications/resources/list_changed",
306-
});
307-
308-
return {
309-
content: [
310-
{
311-
type: "text",
312-
text: `Screenshot '${args.name}' taken at ${width}x${height}`,
313-
} as TextContent,
314-
{
315-
type: "text",
316-
text: `data:image/png;base64,${screenshot}`,
317-
} as TextContent,
280+
} as ImageContent),
318281
],
319282
isError: false,
320283
};

0 commit comments

Comments
 (0)