@@ -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