browser_take_screenshot
Capture screenshots of web pages or specific elements using Playwright MCP's browser automation. Save images in PNG or JPEG format for documentation, testing, or visual reference purposes.
Instructions
Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| type | No | Image format for the screenshot. Default is png. | png |
| filename | No | File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified. Prefer relative file names to stay within the output directory. | |
| element | No | Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too. | |
| ref | No | Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too. | |
| fullPage | No | When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots. |
Implementation Reference
- src/tools/screenshot.ts:48-85 (handler)The main handler function for the 'browser_take_screenshot' tool. It captures a screenshot of the current page viewport or a specific element using Playwright, handles options like format (PNG/JPEG), quality, saves to a file, and returns base64-encoded image content if supported by the client.handle: async (context, params) => { const tab = context.currentTabOrDie(); const snapshot = tab.snapshotOrDie(); const fileType = params.raw ? 'png' : 'jpeg'; const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`); const options: playwright.PageScreenshotOptions = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName }; const isElementScreenshot = params.element && params.ref; const code = [ `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`, ]; const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null; if (locator) code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`); else code.push(`await page.screenshot(${javascript.formatObject(options)});`); const includeBase64 = context.clientSupportsImages(); const action = async () => { const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options); return { content: includeBase64 ? [{ type: 'image' as 'image', data: screenshot.toString('base64'), mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg', }] : [] }; }; return { code, action, captureSnapshot: true, waitForNetwork: false, }; }
- src/tools/screenshot.ts:26-36 (schema)Zod schema defining the input parameters for the tool: raw (boolean for PNG without compression), filename (optional string), element and ref (paired strings for element screenshot with validation refinement).const screenshotSchema = z.object({ raw: z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'), filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'), element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'), ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'), }).refine(data => { return !!data.element === !!data.ref; }, { message: 'Both element and ref must be provided or neither.', path: ['ref', 'element'] });
- src/tools/screenshot.ts:38-86 (registration)Tool registration via defineTool call, which combines the schema, name 'browser_take_screenshot', description, and handler into a tool object, exported for inclusion in the tools list.const screenshot = defineTool({ capability: 'core', schema: { name: 'browser_take_screenshot', title: 'Take a screenshot', description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`, inputSchema: screenshotSchema, type: 'readOnly', }, handle: async (context, params) => { const tab = context.currentTabOrDie(); const snapshot = tab.snapshotOrDie(); const fileType = params.raw ? 'png' : 'jpeg'; const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`); const options: playwright.PageScreenshotOptions = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName }; const isElementScreenshot = params.element && params.ref; const code = [ `// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`, ]; const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null; if (locator) code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`); else code.push(`await page.screenshot(${javascript.formatObject(options)});`); const includeBase64 = context.clientSupportsImages(); const action = async () => { const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options); return { content: includeBase64 ? [{ type: 'image' as 'image', data: screenshot.toString('base64'), mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg', }] : [] }; }; return { code, action, captureSnapshot: true, waitForNetwork: false, }; } });
- src/tools.ts:36-52 (registration)The screenshot tool is included in the snapshotTools array (spread from its module export), which serves as the primary list of available tools for the MCP server.export const snapshotTools: Tool<any>[] = [ ...common(true), ...console, ...dialogs(true), ...files(true), ...install, ...keyboard(true), ...navigate(true), ...network, ...pdf, ...screenshot, ...snapshot, ...tabs(true), ...testing, ...video, ...wait(true), ];
- src/connection.ts:29-33 (registration)Final registration to MCP server: snapshotTools selected as allTools based on config, filtered by capabilities, and handled in ListTools and CallTool request handlers.export function createConnection(config: FullConfig, browserContextFactory: BrowserContextFactory): Connection { const allTools = config.vision ? visionTools : snapshotTools; const tools = allTools.filter(tool => !config.capabilities || tool.capability === 'core' || config.capabilities.includes(tool.capability)); validateConfig(config); const context = new Context(tools, config, browserContextFactory);