screenshot
Capture screenshots of Tauri desktop application windows for automated testing and UI inspection. Specify which window to capture or use the currently focused window.
Instructions
Take screenshot
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| window | No | Window label (default: focused window) |
Implementation Reference
- Main screenshot implementation with platform-specific logic. On macOS, uses the screencapture CLI tool to capture a specific window by ID without requiring Screen Recording permissions. On other platforms, delegates to the native screenshot command via the socket connection. Returns base64-encoded image data with mime type and dimensions.async screenshot(options?: { window?: string }): Promise<{ data: string; mimeType: string; width: number; height: number }> { // On macOS, try screencapture CLI first (no Screen Recording permission needed) // If it fails, fall through to native (xcap → html2canvas) if (os.platform() === 'darwin') { try { return await this.screenshotMacOS(options); } catch { // screencapture CLI failed, fall through to native } } return this.screenshotNative(options); } private async screenshotMacOS(options?: { window?: string }): Promise<{ data: string; mimeType: string; width: number; height: number }> { // Get window ID from Tauri app const params: Record<string, unknown> = {}; if (options?.window) params.window = options.window; const windowInfo = await this.sendCommand('get_window_id', params) as { window_id: number; pid: number }; const windowId = windowInfo.window_id; // Create temp file for screenshot const tmpFile = path.join(os.tmpdir(), `tauri-mcp-screenshot-${Date.now()}.png`); try { // Use screencapture command with window ID // -l<windowid>: capture specific window // -x: no sound // -o: no shadow await execFileAsync('screencapture', [ `-l${windowId}`, '-x', '-o', tmpFile ]); // Read the file and convert to base64 const imageBuffer = fs.readFileSync(tmpFile); const base64Data = imageBuffer.toString('base64'); // Get image dimensions (basic PNG header parsing) // PNG dimensions are at bytes 16-23 (width: 16-19, height: 20-23) let width = 0; let height = 0; if (imageBuffer.length > 24 && imageBuffer.toString('ascii', 1, 4) === 'PNG') { width = imageBuffer.readUInt32BE(16); height = imageBuffer.readUInt32BE(20); } return { data: base64Data, mimeType: 'image/png', width, height }; } finally { // Clean up temp file try { fs.unlinkSync(tmpFile); } catch { // Ignore cleanup errors } } } private async screenshotNative(options?: { window?: string }): Promise<{ data: string; mimeType: string; width: number; height: number }> { const params: Record<string, unknown> = {}; if (options?.window) params.window = options.window; const result = await this.sendCommand('screenshot', params) as { data: string; width: number; height: number }; // data is a Data URL like "data:image/jpeg;base64,..." // Extract the base64 part and mime type const match = result.data.match(/^data:([^;]+);base64,(.+)$/); if (match) { return { data: match[2], mimeType: match[1], width: result.width, height: result.height, }; } // Fallback: assume it's already raw base64 return { ...result, mimeType: 'image/png' }; }
- MCP tool handler for screenshot. Calls socketManager.screenshot() and formats the result as an MCP image content block with base64 data and mime type.screenshot: async (args: { window?: string }) => { const result = await socketManager.screenshot(args); return { content: [ { type: 'image' as const, data: result.data, mimeType: result.mimeType, }, ], }; },
- Tool schema definition for screenshot. Defines the tool name, description, and input schema using Zod with an optional 'window' parameter for specifying the target window label.screenshot: { name: 'screenshot', description: 'Take screenshot', inputSchema: z.object({ window: z.string().optional().describe('Window label (default: focused window)'), }), },
- packages/tauri-mcp/src/server.ts:14-23 (registration)Tool registration listing screenshot as one of the DEFAULT_ESSENTIAL_TOOLS. This array determines which tools are exposed by default when the ESSENTIAL_TOOLS environment variable is not set.const DEFAULT_ESSENTIAL_TOOLS = [ 'app_status', 'launch_app', 'stop_app', 'snapshot', 'click', 'fill', 'screenshot', 'navigate', ];