/**
* MCP tool: get_figma_image
* Export a rendered image of a Figma node.
*/
import {z} from 'zod';
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
import type {FigmaClient} from '../figma-client.js';
import {parseFigmaUrl} from '../url-parser.js';
const inputSchema = {
figmaUrl: z.string().url().describe('Figma design URL with a node-id parameter'),
format: z
.enum(['png', 'jpg', 'svg', 'pdf'])
.default('png')
.describe('Image export format (default: png)'),
scale: z
.number()
.min(0.01)
.max(4)
.default(2)
.describe('Image scale factor (default: 2, max: 4)'),
};
export function registerGetFigmaImage(server: McpServer, client: FigmaClient): void {
server.tool(
'get_figma_image',
'Export a rendered image of a Figma node as a URL',
inputSchema,
async ({figmaUrl, format, scale}) => {
const {fileKey, nodeId} = parseFigmaUrl(figmaUrl);
if (!nodeId) {
return {
isError: true,
content: [
{
type: 'text' as const,
text: 'A node-id is required in the Figma URL to export an image. Add ?node-id=<id> to the URL.',
},
],
};
}
const response = await client.getImage(fileKey, [nodeId], {format, scale});
if (response.err) {
return {
isError: true,
content: [{type: 'text' as const, text: `Figma image export error: ${response.err}`}],
};
}
const imageUrl = response.images[nodeId];
if (!imageUrl) {
return {
isError: true,
content: [{type: 'text' as const, text: `No image returned for node ${nodeId}`}],
};
}
return {
content: [
{
type: 'text' as const,
text: `Image exported successfully.\n\nURL: ${imageUrl}\n\nFormat: ${format}, Scale: ${scale}x`,
},
],
};
},
);
}