Skip to main content
Glama
devskido

Playwright MCP Server

by devskido

playwright_screenshot

Capture web page screenshots or specific elements for documentation, testing, or content capture using browser automation.

Instructions

Take a screenshot of the current page or a specific element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesName for the screenshot
selectorNoCSS selector for element to screenshot
widthNoWidth in pixels (default: 800)
heightNoHeight in pixels (default: 600)
storeBase64NoStore screenshot in base64 format (default: true)
fullPageNoStore screenshot of the entire page (default: false)
savePngNoSave screenshot as PNG file (default: false)
downloadsDirNoCustom downloads directory path (default: user's Downloads folder)

Implementation Reference

  • ScreenshotTool class with execute method that performs the screenshot using Playwright's page.screenshot, saves to file and optionally stores base64
    export class ScreenshotTool extends BrowserToolBase {
      private screenshots = new Map<string, string>();
    
      /**
       * Execute the screenshot tool
       */
      async execute(args: any, context: ToolContext): Promise<ToolResponse> {
        return this.safeExecute(context, async (page) => {
          const screenshotOptions: any = {
            type: args.type || "png",
            fullPage: !!args.fullPage
          };
    
          if (args.selector) {
            const element = await page.$(args.selector);
            if (!element) {
              return {
                content: [{
                  type: "text",
                  text: `Element not found: ${args.selector}`,
                }],
                isError: true
              };
            }
            screenshotOptions.element = element;
          }
    
          // Generate output path
          const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
          const filename = `${args.name || 'screenshot'}-${timestamp}.png`;
          const downloadsDir = args.downloadsDir || defaultDownloadsPath;
    
          if (!fs.existsSync(downloadsDir)) {
            fs.mkdirSync(downloadsDir, { recursive: true });
          }
    
          const outputPath = path.join(downloadsDir, filename);
          screenshotOptions.path = outputPath;
    
          const screenshot = await page.screenshot(screenshotOptions);
          const base64Screenshot = screenshot.toString('base64');
    
          const messages = [`Screenshot saved to: ${path.relative(process.cwd(), outputPath)}`];
    
          // Handle base64 storage
          if (args.storeBase64 !== false) {
            this.screenshots.set(args.name || 'screenshot', base64Screenshot);
            this.server.notification({
              method: "notifications/resources/list_changed",
            });
    
            messages.push(`Screenshot also stored in memory with name: '${args.name || 'screenshot'}'`);
          }
    
          return createSuccessResponse(messages);
        });
      }
    
      /**
       * Get all stored screenshots
       */
      getScreenshots(): Map<string, string> {
        return this.screenshots;
      }
    } 
  • Tool definition including name, description, and input schema for 'playwright_screenshot'
      name: "playwright_screenshot",
      description: "Take a screenshot of the current page or a specific element",
      inputSchema: {
        type: "object",
        properties: {
          name: { type: "string", description: "Name for the screenshot" },
          selector: { type: "string", description: "CSS selector for element to screenshot" },
          width: { type: "number", description: "Width in pixels (default: 800)" },
          height: { type: "number", description: "Height in pixels (default: 600)" },
          storeBase64: { type: "boolean", description: "Store screenshot in base64 format (default: true)" },
          fullPage: { type: "boolean", description: "Store screenshot of the entire page (default: false)" },
          savePng: { type: "boolean", description: "Save screenshot as PNG file (default: false)" },
          downloadsDir: { type: "string", description: "Custom downloads directory path (default: user's Downloads folder)" },
        },
        required: ["name"],
      },
    },
  • Dispatch in handleToolCall switch statement that calls the screenshot tool handler
    case "playwright_screenshot":
      return await screenshotTool.execute(args, context);
  • Instantiation of ScreenshotTool instance
    if (!screenshotTool) screenshotTool = new ScreenshotTool(server);
  • Inclusion in BROWSER_TOOLS array to trigger browser initialization
    "playwright_screenshot",

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/devskido/customed-playwright'

If you have feedback or need assistance with the MCP directory API, please join our Discord server