take-screenshot
Capture screenshots of web pages for performance analysis, supporting full-page captures to document website appearance during testing.
Instructions
Takes a screenshot of the currently open page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | URL of the website you want to capture | |
| fullPage | No | If true, captures a screenshot of the entire page |
Implementation Reference
- src/index.ts:452-503 (handler)The handler function that executes the take-screenshot tool. It navigates to the given URL, takes a JPEG screenshot (full page if specified), saves it to the screenshots directory, closes the browser, and returns text messages with the save path and the base64-encoded image.async ({ url, fullPage }) => { try { // Automatically launch browser and navigate to URL await navigateToUrl(url); const screenshot = await page!.screenshot({ fullPage, type: "jpeg", quality: 80 }); // Create directory for screenshots if it doesn't exist const screenshotsDir = path.join(__dirname, "../screenshots"); if (!existsSync(screenshotsDir)) { mkdirSync(screenshotsDir, { recursive: true }); } // Save screenshot const screenshotPath = path.join(screenshotsDir, `screenshot-${Date.now()}.jpg`); writeFileSync(screenshotPath, screenshot); // Close browser after taking screenshot await closeBrowser(); return { content: [ { type: "text" as const, text: `Screenshot captured. ${fullPage ? "(Full page)" : ""}`, }, { type: "text" as const, text: `Saved to: ${screenshotPath}`, }, { type: "image" as const, data: screenshot.toString("base64"), mimeType: "image/jpeg", }, ], }; } catch (error) { // Close browser even when an error occurs await closeBrowser(); return { content: [ { type: "text" as const, text: `An error occurred while taking screenshot: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
- src/index.ts:448-451 (schema)Zod schema defining the input parameters for the take-screenshot tool: 'url' (required URL string) and 'fullPage' (optional boolean, defaults to false).{ url: z.string().url().describe("URL of the website you want to capture"), fullPage: z.boolean().default(false).describe("If true, captures a screenshot of the entire page"), },
- src/index.ts:445-504 (registration)Registers the 'take-screenshot' tool with the MCP server using server.tool(), specifying the name, description, input schema, and handler function.server.tool( "take-screenshot", "Takes a screenshot of the currently open page", { url: z.string().url().describe("URL of the website you want to capture"), fullPage: z.boolean().default(false).describe("If true, captures a screenshot of the entire page"), }, async ({ url, fullPage }) => { try { // Automatically launch browser and navigate to URL await navigateToUrl(url); const screenshot = await page!.screenshot({ fullPage, type: "jpeg", quality: 80 }); // Create directory for screenshots if it doesn't exist const screenshotsDir = path.join(__dirname, "../screenshots"); if (!existsSync(screenshotsDir)) { mkdirSync(screenshotsDir, { recursive: true }); } // Save screenshot const screenshotPath = path.join(screenshotsDir, `screenshot-${Date.now()}.jpg`); writeFileSync(screenshotPath, screenshot); // Close browser after taking screenshot await closeBrowser(); return { content: [ { type: "text" as const, text: `Screenshot captured. ${fullPage ? "(Full page)" : ""}`, }, { type: "text" as const, text: `Saved to: ${screenshotPath}`, }, { type: "image" as const, data: screenshot.toString("base64"), mimeType: "image/jpeg", }, ], }; } catch (error) { // Close browser even when an error occurs await closeBrowser(); return { content: [ { type: "text" as const, text: `An error occurred while taking screenshot: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } } );