Figma MCP 使用指南与配置教程

642 阅读5分钟

什么是 Figma MCP

Figma MCP (Model Context Protocol) 是一种让 AI 助手直接与 Figma 设计文件交互的协议。通过 MCP,AI 可以读取设计文件、获取图层信息、导出资源,甚至协助进行设计工作。

核心功能

  • 设计文件读取 - 获取 Figma 文件的结构、图层信息
  • 资源导出 - 自动导出图标、图片等设计资源
  • 设计分析 - 分析设计系统、颜色方案、字体使用等
  • 代码生成 - 基于设计稿生成前端代码
  • 设计协作 - 与设计师协作优化设计方案

系统要求

基础要求

  • Node.js 16.0 或更高版本
  • npm 或 yarn 包管理器
  • Figma 账户(个人版或团队版)
  • 稳定的网络连接

推荐环境

  • macOS 12+ / Windows 11 / Ubuntu 22.04+
  • 16GB RAM(处理大型设计文件)
  • Figma 桌面应用(获得最佳体验)

配置步骤

1. 获取 Figma Access Token

  1. 登录 Figma 账户
  2. 点击头像 → Settings
  3. 滚动到 Personal access tokens 部分
  4. 点击 "Create new token"
  5. 输入 token 名称,设置适当权限
  6. 保存生成的 token(注意:只会显示一次)

2. 安装 MCP 服务器

# 全局安装 Figma MCP 服务器
npm install -g @figma/mcp-server

# 验证安装
figma-mcp --version

3. 基础配置

创建配置文件 ~/.figma-mcp/config.json

{
  "figma": {
    "access_token": "your_access_token_here",
    "base_url": "https://api.figma.com/v1"
  },
  "server": {
    "host": "localhost",
    "port": 3000,
    "enable_cors": true
  },
  "features": {
    "file_read": true,
    "resource_export": true,
    "code_generation": true
  }
}

4. 启动 MCP 服务器

# 启动服务器
figma-mcp serve

# 后台运行(推荐)
figma-mcp serve --daemon

# 查看状态
figma-mcp status

AI 工具特定配置

1. Claude Code 配置

在 Claude Code 配置文件中添加:

{
  "mcp_servers": {
    "figma": {
      "name": "Figma MCP",
      "url": "http://localhost:3000",
      "timeout": 30000,
      "max_tokens": 4096
    }
  },
  "figma_settings": {
    "mcp_enabled": true,
    "default_file_format": "svg",
    "export_assets": true,
    "code_language": "vue"
  }
}

使用示例:

# 读取 Figma 文件结构
claude-code figma:read "file_key_here"

# 导出所有图标
claude-code figma:export "file_key_here" --type=icon --format=svg

# 生成 Vue 组件代码
claude-code figma:generate "file_key_here" --component="Button"

2. Gemini 配置

在 Google AI Studio 或 Gemini API 配置:

{
  "tools": [
    {
      "function": {
        "name": "figma_mcp",
        "description": "Figma MCP endpoint",
        "parameters": {
          "endpoint": "http://localhost:3000/api",
          "auth": {
            "type": "bearer",
            "token": "your_figma_token"
          }
        }
      }
    }
  ],
  "system_instruction": [
    "You have access to Figma MCP for design file operations",
    "Use appropriate tools for file reading, asset export, and code generation"
  ]
}

Python SDK 使用:

import google.generativeai as genai
import requests

# 配置 Gemini 模型
model = genai.GenerativeModel('gemini-pro')

# 获取 Figma 数据
figma_data = requests.get('http://localhost:3000/api/file/file_key').json()

# 分析设计并生成代码
response = model.generate_content([
    f"基于这个 Figma 设计数据:{figma_data}",
    "请生成对应的 Vue 组件代码"
])

3. Trae 配置

在 Trae AI 编辑器 settings.json 中配置:

{
  "ai.figmaIntegration": {
    "enabled": true,
    "mcpServer": {
      "host": "localhost",
      "port": 3000,
      "secure": false
    },
    "autoSync": true,
    "exportSettings": {
      "format": "svg",
      "scale": "1x,2x,3x",
      "outputPath": "./assets/figma"
    }
  },
  "ai.codeGeneration": {
    "preferredLanguage": "typescript",
    "framework": "vue3",
    "cssProcessor": "less"
  }
}

快捷命令:

# 在设计文件中右键菜单
Figma MCP: Export Assets
Figma MCP: Generate Component Code
Figma MCP: Sync Design Tokens
Figma MCP: Analyze Design System

使用技巧

1. 高效读取策略

批量文件处理:

# 读取多个相关文件
figma-mcp batch-read file1 key1 file2 key2 --format=json

# 只读取特定页面
figma-mcp read "file_key" --page="Mobile App" --depth=3

2. 智能资源导出

按类型批量导出:

# 导出所有图标
figma-mcp export "file_key" --filter="type=frame&name=*icon*" --suffix="@2x"

# 自动命名和组织
figma-mcp export "file_key" --naming-pattern="{page}/{component}/{state}" --create-dirs

3. 代码生成优化

React 组件生成:

{
  "code_generation": {
    "framework": "react",
    "styling": "styled-components",
    "typescript": true,
    "responsive": true,
    "accessibility": true
  }
}

Vue 3 组合式 API:

{
  "code_generation": {
    "framework": "vue3",
    "composition": true,
    "css_processor": "less",
    "auto_imports": true
  }
}

4. 设计系统分析

# 提取颜色方案
figma-mcp analyze "file_key" --type=colors --export-css="variables.css"

# 分析字体使用
figma-mcp analyze "file_key" --type=typography --report="typography-analysis.md"

# 组件使用情况统计
figma-mcp analyze "file_key" --type=components --format=csv

最佳实践

1. 文件组织

  • 使用清晰的命名约定(文件和组件)
  • 建立统一的图层结构
  • 定期整理和归档旧版本

2. 权限管理

  • 为 AI 访问创建专用的受限访问 Token
  • 定期轮换访问密钥
  • 监控 API 使用情况

3. 性能优化

  • 在读取大型文件时使用分页
  • 缓存经常访问的设计文件
  • 避免在高峰期进行大量导出操作

4. 协作流程

  • 建立设计文件到代码的映射标准
  • 设置自动化的设计检查流程
  • 保持设计与代码版本的同步

故障排除

常见问题

连接失败:

# 检查服务状态
figma-mcp status

# 重启服务
figma-mcp restart

Token 失效:

  • 检查 Token 是否过期
  • 验证权限设置
  • 在网络设置中检查代理配置

导出失败:

  • 确保资源有正确的导出设置
  • 检查文件访问权限
  • 验证输出路径权限

调试模式

# 启用详细日志
figma-mcp serve --debug --log-level=verbose

# 保存调试日志
figma-mcp serve --debug --log-file="debug.log"

高级用法

自定义插件开发

// 自定义 MCP 插件示例
class CustomFigmaPlugin {
  constructor(server) {
    this.server = server;
  }

  async processDesignFile(fileKey) {
    // 自定义处理逻辑
    const file = await this.server.getFile(fileKey);
    return this.customAnalysis(file);
  }
}

// 注册插件
figma-mcp.registerPlugin('custom-analysis', CustomFigmaPlugin);

工作流程自动化

#!/bin/bash
# 自动工作流程脚本

# 1. 拉取最新设计文件
figma-mcp sync "main-design-file" --watch

# 2. 自动导出更新的资源
figma-mcp export "main-design-file" --incremental --auto-commit

# 3. 生成代码并推送到仓库
figma-mcp generate "main-design-file" --framework=vue3 --output=./src/components

# 4. 触发构建和部署
git add .
git commit -m "Update from Figma design $(date)"
git push origin main

总结

Figma MCP 为 AI 辅助设计开发提供了强大的技术基础。通过合理配置和有效使用,可以显著提升从设计到代码的转换效率。关键在于建立标准化的工作流程,保持设计与开发的紧密协作,并持续优化自动化流程。

建议从小规模项目开始尝试,逐步扩展到完整的开发流程中。随着工具的成熟和团队的适应,Figma MCP 将成为设计系统和组件驱动开发的重要组成部分。