browserbase_screenshot
Capture a screenshot of the current webpage to visually confirm your location or state during browser control, ideal for verifying actions or debugging.
Instructions
Takes a screenshot of the current page. Use this tool to learn where you are on the page when controlling the browser with Stagehand. Only use this tool when the other tools are not sufficient to get the information you need.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | No | The name of the screenshot |
Implementation Reference
- src/tools/snapshot.ts:361-378 (schema)Zod schema defining the input for the browserbase_take_screenshot tool, including options for raw format, element description, and reference.const screenshotSchema = z.object({ raw: z .boolean() .optional() .describe( "Whether to return without compression (PNG). Default is false (JPEG)." ), element: z .string() .optional() .describe("Human-readable element description."), ref: z .string() .optional() .describe("Exact target element reference from the page snapshot.") }); type ScreenshotInput = z.infer<typeof screenshotSchema>;
- src/tools/snapshot.ts:382-386 (registration)Tool registration metadata specifying the name 'browserbase_take_screenshot', description, and reference to the input schema.schema: { name: "browserbase_take_screenshot", description: `Take a screenshot of the current page or element using ref.`, inputSchema: screenshotSchema, },
- src/tools/snapshot.ts:387-492 (handler)Core handler logic for executing the screenshot: validates input, gets page/locator, configures screenshot options, generates code snippet, performs screenshot via Playwright, and returns image as base64 or empty based on config.handle: async ( context: Context, params: ScreenshotInput ): Promise<ToolResult> => { if (!!params.element !== !!params.ref) { throw new Error("Both element and ref must be provided or neither."); } const page = await context.getActivePage(); if (!page) { throw new Error("No active page found for screenshot"); } // Conditionally get snapshot only if ref is provided let pageSnapshot: PageSnapshot | null = null; if (params.ref) { pageSnapshot = context.snapshotOrDie(); } const fileType = params.raw ? "png" : "jpeg"; const fileName = await outputFile( context.config, `screenshot-${Date.now()}.${fileType}` ); const baseOptions: PageScreenshotOptions = { scale: "css", timeout: 15000, // Kept existing timeout }; let options: PageScreenshotOptions; if (fileType === "jpeg") { options = { ...baseOptions, type: "jpeg", quality: 50, // Quality is only for jpeg path: fileName, }; } else { options = { ...baseOptions, type: "png", path: fileName, }; } const isElementScreenshot = params.element && params.ref; const code: string[] = []; code.push( `// Screenshot ${ isElementScreenshot ? params.element : "viewport" } and save it as ${fileName}` ); // Conditionally get locator only if ref and snapshot are available const locator = params.ref && pageSnapshot ? pageSnapshot.refLocator(params.ref) : null; // Use JSON.stringify for code generation as javascript.formatObject is not available const optionsForCode = { ...options }; // delete optionsForCode.path; // Path is an internal detail for saving, not usually part of the "command" log if (locator) { code.push( `// await page.${await generateLocator( locator )}.screenshot(${JSON.stringify(optionsForCode)});` ); } else { code.push(`// await page.screenshot(${JSON.stringify(optionsForCode)});`); } const action = async (): Promise<ToolActionResult> => { // Access config via context.config const includeBase64 = !context.config.tools?.browserbase_take_screenshot?.omitBase64; // Use the page directly for full page screenshots if locator is null const screenshotBuffer = locator ? await locator.screenshot(options) : await page.screenshot(options); if (includeBase64) { const rawBase64 = screenshotBuffer.toString("base64"); return { content: [ { type: "image", format: fileType, // format might be redundant if mimeType is present, but kept for now mimeType: fileType === "png" ? `image/png` : `image/jpeg`, data: rawBase64, }, ], }; } else { // If base64 is not included, return an empty content array return { content: [] }; } }; return { code, action, captureSnapshot: true, waitForNetwork: false, }; },
- src/index.ts:84-106 (registration)Registers the browserbase_take_screenshot tool (and all others) to the MCP server using a loop over imported tools, calling server.tool with the tool's name, description, schema shape, and a wrapped executor that runs context.run(tool, params).tools.forEach(tool => { if (tool.schema.inputSchema instanceof z.ZodObject) { server.tool( tool.schema.name, tool.schema.description, tool.schema.inputSchema.shape, async (params: z.infer<typeof tool.schema.inputSchema>) => { try { const result = await context.run(tool, params); return result; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); process.stderr.write(`[Smithery Error] ${new Date().toISOString()} Error running tool ${tool.schema.name}: ${errorMessage}\n`); throw new Error(`Failed to run tool '${tool.schema.name}': ${errorMessage}`); } } ); } else { console.warn( `Tool "${tool.schema.name}" has an input schema that is not a ZodObject. Schema type: ${tool.schema.inputSchema.constructor.name}` ); } });