mobile_take_screenshot
Capture screenshots from mobile devices to analyze on-screen content and identify interactive elements for mobile automation tasks.
Instructions
Take a screenshot of the mobile device. Use this to understand what's on screen, if you need to press an element that is available through view hierarchy then you must list elements on screen instead. Do not cache this result.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| device | Yes | The device identifier to use. Use mobile_list_available_devices to find which devices are available to you. |
Implementation Reference
- src/server.ts:530-579 (handler)Handler function that executes the mobile_take_screenshot tool: retrieves Robot for device, captures screenshot, processes (validate PNG, optional resize/JPEG), base64 encodes, returns image content.async ({ device }) => { try { const robot = getRobotFromDevice(device); const screenSize = await robot.getScreenSize(); let screenshot = await robot.getScreenshot(); let mimeType = "image/png"; // validate we received a png, will throw exception otherwise const image = new PNG(screenshot); const pngSize = image.getDimensions(); if (pngSize.width <= 0 || pngSize.height <= 0) { throw new ActionableError("Screenshot is invalid. Please try again."); } if (isScalingAvailable()) { trace("Image scaling is available, resizing screenshot"); const image = Image.fromBuffer(screenshot); const beforeSize = screenshot.length; screenshot = image.resize(Math.floor(pngSize.width / screenSize.scale)) .jpeg({ quality: 75 }) .toBuffer(); const afterSize = screenshot.length; trace(`Screenshot resized from ${beforeSize} bytes to ${afterSize} bytes`); mimeType = "image/jpeg"; } const screenshot64 = screenshot.toString("base64"); trace(`Screenshot taken: ${screenshot.length} bytes`); posthog("tool_invoked", { "ToolName": "mobile_take_screenshot", "ScreenshotFilesize": screenshot64.length, "ScreenshotMimeType": mimeType, "ScreenshotWidth": pngSize.width, "ScreenshotHeight": pngSize.height, }).then(); return { content: [{ type: "image", data: screenshot64, mimeType }] }; } catch (err: any) { error(`Error taking screenshot: ${err.message} ${err.stack}`); return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true, }; } }
- src/server.ts:523-528 (schema)Tool metadata including title, description, and input schema (device ID).{ title: "Take Screenshot", description: "Take a screenshot of the mobile device. Use this to understand what's on screen, if you need to press an element that is available through view hierarchy then you must list elements on screen instead. Do not cache this result.", inputSchema: { device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you.") }
- src/server.ts:521-580 (registration)Registration of the mobile_take_screenshot tool using server.registerTool, including schema and inline handler.server.registerTool( "mobile_take_screenshot", { title: "Take Screenshot", description: "Take a screenshot of the mobile device. Use this to understand what's on screen, if you need to press an element that is available through view hierarchy then you must list elements on screen instead. Do not cache this result.", inputSchema: { device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you.") } }, async ({ device }) => { try { const robot = getRobotFromDevice(device); const screenSize = await robot.getScreenSize(); let screenshot = await robot.getScreenshot(); let mimeType = "image/png"; // validate we received a png, will throw exception otherwise const image = new PNG(screenshot); const pngSize = image.getDimensions(); if (pngSize.width <= 0 || pngSize.height <= 0) { throw new ActionableError("Screenshot is invalid. Please try again."); } if (isScalingAvailable()) { trace("Image scaling is available, resizing screenshot"); const image = Image.fromBuffer(screenshot); const beforeSize = screenshot.length; screenshot = image.resize(Math.floor(pngSize.width / screenSize.scale)) .jpeg({ quality: 75 }) .toBuffer(); const afterSize = screenshot.length; trace(`Screenshot resized from ${beforeSize} bytes to ${afterSize} bytes`); mimeType = "image/jpeg"; } const screenshot64 = screenshot.toString("base64"); trace(`Screenshot taken: ${screenshot.length} bytes`); posthog("tool_invoked", { "ToolName": "mobile_take_screenshot", "ScreenshotFilesize": screenshot64.length, "ScreenshotMimeType": mimeType, "ScreenshotWidth": pngSize.width, "ScreenshotHeight": pngSize.height, }).then(); return { content: [{ type: "image", data: screenshot64, mimeType }] }; } catch (err: any) { error(`Error taking screenshot: ${err.message} ${err.stack}`); return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true, }; } } );