Skip to content

Commit c5de8bd

Browse files
author
Jun
committed
Add a Puppeteer tool to capture screenshot as raw base64 text
1 parent 0d12ab3 commit c5de8bd

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/puppeteer/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ A Model Context Protocol server that provides browser automation capabilities us
2121
- `width` (number, optional, default: 800): Screenshot width
2222
- `height` (number, optional, default: 600): Screenshot height
2323

24+
- **puppeteer_screenshot_encoded**
25+
- Captures a screenshot of the entire page or a specific element and return it as a base64-encoded data URI.
26+
- Inputs:
27+
- `name` (string, required): Name for the screenshot
28+
- `selector` (string, optional): CSS selector for element to screenshot
29+
- `width` (number, optional, default: 800): Screenshot width
30+
- `height` (number, optional, default: 600): Screenshot height
31+
2432
- **puppeteer_click**
2533
- Click elements on the page
2634
- Input: `selector` (string): CSS selector for element to click

src/puppeteer/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ const TOOLS: Tool[] = [
4343
required: ["name"],
4444
},
4545
},
46+
{
47+
name: "puppeteer_screenshot_encoded",
48+
description: "Take a screenshot of the current page or a specific element and return it as a base64-encoded data URI",
49+
inputSchema: {
50+
type: "object",
51+
properties: {
52+
name: { type: "string", description: "Name for the screenshot" },
53+
selector: { type: "string", description: "CSS selector for element to screenshot" },
54+
width: { type: "number", description: "Width in pixels (default: 800)" },
55+
height: { type: "number", description: "Height in pixels (default: 600)" },
56+
},
57+
required: ["name"],
58+
},
59+
},
4660
{
4761
name: "puppeteer_click",
4862
description: "Click an element on the page",
@@ -265,6 +279,47 @@ async function handleToolCall(name: string, args: any): Promise<CallToolResult>
265279
};
266280
}
267281

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,
318+
],
319+
isError: false,
320+
};
321+
}
322+
268323
case "puppeteer_click":
269324
try {
270325
await page.click(args.selector);

0 commit comments

Comments
 (0)