export_node_as_image
Export Figma design nodes as images in formats like PNG, JPG, SVG, or PDF. Specify the node ID and scale for precise, programmatic image extraction.
Instructions
Export a node as an image from Figma
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Export format | |
| nodeId | Yes | The ID of the node to export | |
| scale | No | Export scale |
Implementation Reference
- src/cursor_mcp_plugin/code.js:674-735 (handler)Main handler function that exports a Figma node as an image in various formats, converts the bytes to a base64 data URL, and returns it.async function exportNodeAsImage(params) { const { nodeId, format = "PNG", scale = 1 } = params || {}; if (!nodeId) { throw new Error("Missing nodeId parameter"); } const node = await figma.getNodeByIdAsync(nodeId); if (!node) { throw new Error(`Node not found with ID: ${nodeId}`); } if (!("exportAsync" in node)) { throw new Error(`Node does not support exporting: ${nodeId}`); } try { const settings = { format: format, constraint: { type: "SCALE", value: scale }, }; const bytes = await node.exportAsync(settings); let mimeType; switch (format) { case "PNG": mimeType = "image/png"; break; case "JPG": mimeType = "image/jpeg"; break; case "SVG": mimeType = "image/svg+xml"; break; case "PDF": mimeType = "application/pdf"; break; default: mimeType = "application/octet-stream"; } // Convert to base64 const uint8Array = new Uint8Array(bytes); let binary = ""; for (let i = 0; i < uint8Array.length; i++) { binary += String.fromCharCode(uint8Array[i]); } const base64 = btoa(binary); const imageData = `data:${mimeType};base64,${base64}`; return { nodeId, format, scale, mimeType, imageData, }; } catch (error) { throw new Error(`Error exporting node as image: ${error.message}`); } }
- src/talk_to_figma_mcp/server.ts:565-602 (registration)MCP server tool registration including input schema validation with Zod and proxy handler that forwards to Figma plugin.server.tool( "export_node_as_image", "Export a node as an image from Figma", { nodeId: z.string().describe("The ID of the node to export"), format: z.enum(["PNG", "JPG", "SVG", "PDF"]).optional().describe("Export format"), scale: z.number().positive().optional().describe("Export scale") }, async ({ nodeId, format, scale }) => { try { const result = await sendCommandToFigma('export_node_as_image', { nodeId, format: format || 'PNG', scale: scale || 1 }); const typedResult = result as { imageData: string, mimeType: string }; return { content: [ { type: "image", data: typedResult.imageData, mimeType: typedResult.mimeType || "image/png" } ] }; } catch (error) { return { content: [ { type: "text", text: `Error exporting node as image: ${error instanceof Error ? error.message : String(error)}` } ] }; } } );
- Input schema definition using Zod for nodeId (required string), format (optional enum PNG/JPG/SVG/PDF), scale (optional positive number).nodeId: z.string().describe("The ID of the node to export"), format: z.enum(["PNG", "JPG", "SVG", "PDF"]).optional().describe("Export format"), scale: z.number().positive().optional().describe("Export scale") },
- src/cursor_mcp_plugin/code.js:97-98 (registration)Command dispatcher case in Figma plugin that routes 'export_node_as_image' to the handler function.case "export_node_as_image": return await exportNodeAsImage(params);
- src/talk_to_figma_mcp/server.ts:775-775 (registration)Type union including 'export_node_as_image' command in FigmaCommand type.| 'export_node_as_image'