download-svg-assets
Download specific SVG assets from a Figma file by node ID. Specify file key, node details, and local path for saving. Handles PNG scaling and customizable SVG export options.
Instructions
根据图像或图标节点的ID,仅下载Figma文件中使用的SVG资源
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileKey | Yes | The key of the Figma file containing the node | |
| localPath | Yes | The absolute path to the directory where images are stored in the project. If the directory does not exist, it will be created. The format of this path should respect the directory format of the operating system you are running on. Don't use any special character escaping in the path name either. | |
| nodes | Yes | The nodes to fetch as images | |
| pngScale | No | Export scale for PNG images. Optional, defaults to 2 if not specified. Affects PNG images only. | |
| svgOptions | No | Options for SVG export |
Implementation Reference
- src/services/utility/index.ts:38-133 (registration)Registration of the 'download-svg-assets' MCP tool, including input schema and execute handler that delegates to figmaToolsCore.downloadFigmaSVGAssetsprivate dowdloadSVGAssets(): void { this.server.addTool({ name: 'download-svg-assets', description: '根据图像或图标节点的ID,仅下载Figma文件中使用的SVG资源', parameters: z.object({ fileKey: z.string().describe("The key of the Figma file containing the node"), nodes: z .object({ nodeId: z .string() .describe("The ID of the Figma image node to fetch, formatted as 1234:5678"), imageRef: z .string() .optional() .describe( "If a node has an imageRef fill, you must include this variable. Leave blank when downloading Vector SVG images.", ), fileName: z.string().describe("The local name for saving the fetched file"), }) .array() .describe("The nodes to fetch as images"), pngScale: z .number() .positive() .optional() .default(2) .describe( "Export scale for PNG images. Optional, defaults to 2 if not specified. Affects PNG images only.", ), localPath: z .string() .describe( "The absolute path to the directory where images are stored in the project. If the directory does not exist, it will be created. The format of this path should respect the directory format of the operating system you are running on. Don't use any special character escaping in the path name either.", ), svgOptions: z .object({ outlineText: z .boolean() .optional() .default(true) .describe("Whether to outline text in SVG exports. Default is true."), includeId: z .boolean() .optional() .default(false) .describe("Whether to include IDs in SVG exports. Default is false."), simplifyStroke: z .boolean() .optional() .default(true) .describe("Whether to simplify strokes in SVG exports. Default is true."), }) .optional() .default({}) .describe("Options for SVG export"), }), execute: async ({ fileKey, nodes, localPath, pngScale, svgOptions }, { session }) => { try { const downloads = await this.figmaToolsCore.downloadFigmaSVGAssets({ fileKey, nodes, localPath, pngScale, svgOptions, }) // If any download fails, return false const saveSuccess = !downloads.find(success => !success) const assetsPrompt = saveSuccess ? `Success, ${downloads.length} images downloaded: ${downloads.join(', ')}` : 'Failed' const prompt: string = ` <xml> <SVG-URL>${assetsPrompt}</SVG-URL> </xml> ` return { content: [ { type: 'text', text: prompt }, ], } } catch (error) { Logger.error(`Error downloading images from file ${fileKey}:`, error) return { isError: true, content: [{ type: 'text', text: `Error downloading images: ${error}` }], } } }, }) }
- src/services/figma/index.ts:288-312 (handler)Core handler logic for downloading SVG assets from Figma, called by the tool's execute function. Filters nodes without imageRef and calls figmaService.getSVGpublic async downloadFigmaSVGAssets({ fileKey, nodes, localPath }: DownloadFigmaImagesParams) { const figmaService = this.figmaService const renderRequests = nodes .filter(({ imageRef }) => !imageRef) .map(({ nodeId, fileName }) => ({ nodeId, fileName, fileType: fileName.endsWith('.svg') ? ('svg' as const) : ('png' as const), })) console.log('renderRequests', nodes) const renderDownloads = figmaService.getSVG(fileKey, renderRequests, localPath) const downloads = await Promise.all([renderDownloads]).then(([r]) => [ ...r, ]) return downloads }
- src/services/utility/index.ts:42-92 (schema)Zod schema defining input parameters for the 'download-svg-assets' toolparameters: z.object({ fileKey: z.string().describe("The key of the Figma file containing the node"), nodes: z .object({ nodeId: z .string() .describe("The ID of the Figma image node to fetch, formatted as 1234:5678"), imageRef: z .string() .optional() .describe( "If a node has an imageRef fill, you must include this variable. Leave blank when downloading Vector SVG images.", ), fileName: z.string().describe("The local name for saving the fetched file"), }) .array() .describe("The nodes to fetch as images"), pngScale: z .number() .positive() .optional() .default(2) .describe( "Export scale for PNG images. Optional, defaults to 2 if not specified. Affects PNG images only.", ), localPath: z .string() .describe( "The absolute path to the directory where images are stored in the project. If the directory does not exist, it will be created. The format of this path should respect the directory format of the operating system you are running on. Don't use any special character escaping in the path name either.", ), svgOptions: z .object({ outlineText: z .boolean() .optional() .default(true) .describe("Whether to outline text in SVG exports. Default is true."), includeId: z .boolean() .optional() .default(false) .describe("Whether to include IDs in SVG exports. Default is false."), simplifyStroke: z .boolean() .optional() .default(true) .describe("Whether to simplify strokes in SVG exports. Default is true."), }) .optional() .default({}) .describe("Options for SVG export"),