get_node_svg
Extract SVG code from specific design elements in Figma files to use vector graphics in web development and design applications.
Instructions
获取节点的SVG数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | Figma文件URL,必须包含node-id参数 |
Implementation Reference
- src/index.ts:154-167 (schema)Tool schema definition for 'get_node_svg', specifying input as Figma URL with node-id required.{ name: 'get_node_svg', description: '获取节点的SVG数据', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'Figma文件URL,必须包含node-id参数', }, }, required: ['url'], }, },
- src/index.ts:212-213 (registration)Tool registration in the switch statement dispatching 'get_node_svg' calls to handleGetNodeSVG.case 'get_node_svg': return await this.handleGetNodeSVG(args);
- src/index.ts:430-484 (handler)Primary handler for 'get_node_svg' tool: parses URL, delegates to element extractor for SVG data, formats response with success/error handling.private async handleGetNodeSVG(args: any) { const { url } = args; try { console.error(`开始获取节点SVG数据: ${url}`); const urlInfo = this.figmaService.parseUrl(url); if (!urlInfo.nodeId) { throw new Error('URL中缺少node-id参数'); } const svgData = await this.elementExtractor.getNodeAsSVG(urlInfo.fileId, urlInfo.nodeId); console.error(`成功获取SVG数据,长度: ${svgData.length} 字符`); return { content: [ { type: 'text', text: JSON.stringify({ success: true, data: { svg: svgData, fileId: urlInfo.fileId, nodeId: urlInfo.nodeId, dataLength: svgData.length, }, }, null, 2), }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : '未知错误'; console.error(`获取SVG数据失败: ${errorMessage}`); return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: errorMessage, troubleshooting: { commonIssues: [ '检查节点是否为矢量图形或可导出为SVG', '确认Figma token权限是否充足', '验证节点ID格式是否正确', ], } }, null, 2), }, ], }; } }
- src/figma-service.ts:201-216 (helper)Core helper function implementing SVG export: calls Figma images API with SVG format, downloads and returns SVG string.async getNodeAsSVG(fileId: string, nodeId: string): Promise<string> { try { const images = await this.exportImage(fileId, [nodeId], { format: 'svg' }); const svgUrl = images[nodeId]; if (!svgUrl) { throw new Error(`无法获取节点 ${nodeId} 的SVG数据`); } // 下载SVG内容 const response = await axios.get(svgUrl); return response.data; } catch (error) { throw new Error(`获取SVG数据失败: ${error instanceof Error ? error.message : '未知错误'}`); } }