take-screenshot
Capture screenshots of web pages for any URL. Specify if full-page capture is needed. Supports website documentation, analysis, and testing.
Instructions
Takes a screenshot of the currently open page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fullPage | No | If true, captures a screenshot of the entire page | |
| url | Yes | URL of the website you want to capture |
Implementation Reference
- src/index.ts:452-503 (handler)The handler function for the 'take-screenshot' tool. It navigates to the provided URL, captures a screenshot (full page optional), saves it to a file, and returns the image data along with file path.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:449-451 (schema)Zod schema defining the input parameters for the 'take-screenshot' tool: url (required string URL) and fullPage (optional boolean).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)Registration of the 'take-screenshot' tool on the MCP server using server.tool(name, description, schema, handler).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, }; } } );
- src/index.ts:56-64 (helper)Helper function used by the tool to launch browser if needed, get a page, and navigate to the specified URL.async function navigateToUrl(url: string) { try { const page = await getPage(); await page.goto(url, { waitUntil: "load" }); return page; } catch (error) { throw error; } }