处理 Figma .fig 设计文件和正在运行的 OpenPencil 编辑器——检查结构、查询节点、分析设计 Token、导出 PNG/SVG/PDF/JSX,以及通过编程方式修改设计。当需要打开、检查、导出、分析或编辑 .fig 文件,或控制正在运行的 OpenPencil 应用时使用。
OpenPencil
OpenPencil 为 .fig 设计文件和正在运行的 OpenPencil 编辑器提供了 CLI 和 MCP 服务端。
支持两种模式:
- App 模式 — 省略文件参数,直接连接到正在运行的 OpenPencil 编辑器。
- 无头模式(Headless) — 传入文件路径,直接处理
.fig文件。
# App 模式 — 操作编辑器中打开的文档
openpencil tree
# 无头模式 — 操作 .fig 文件
openpencil tree design.fig
当前参考版本:OpenPencil 0.12.x。MCP 服务端在 0.12.0 中提供了 106 个工具。
环境要求
# CLI
bun add -g @open-pencil/cli
# 桌面应用和外部 MCP 客户端使用的 MCP 服务端
bun add -g @open-pencil/mcp
全局安装 @open-pencil/mcp 后,桌面应用会在生产环境 Tauri 构建中自动启动 openpencil-mcp-http,并在以下端口暴露自动化服务:
- HTTP/RPC:
http://127.0.0.1:7600 - WebSocket 桥接:
ws://127.0.0.1:7601 - MCP 可流式 HTTP:
http://127.0.0.1:7600/mcp
CLI 命令
openpencil --help
0.12.x 中的命令:
info— 文档概览:页面、节点数量、字体tree— 打印包含类型和尺寸的层级树pages— 列出页面node— 按 ID 获取详细的节点属性selection— 获取正在运行的应用中的当前选中项find— 按名称/类型查找节点query— 使用 XPath 选择器搜索节点variables— 列出变量和变量集合(collections)export— 导出 PNG/JPG/WEBP/SVG/PDF/JSX/.figconvert— 在支持的文档格式之间进行转换analyze— 分析颜色、排版、间距、重复组/簇(clusters)lint— 一致性、结构和可访问性(无障碍)检查formats— 支持的文档/导出格式eval— 使用 Figma Plugin API 执行 JavaScript
检查(Inspect)
openpencil info design.fig
openpencil tree design.fig
openpencil tree --page "Components" --depth 3 # App 模式
openpencil pages design.fig
openpencil node design.fig --id 1:23
openpencil node --id 1:23 # App 模式
openpencil selection --json
openpencil variables design.fig
openpencil variables --collection "Colors" --type COLOR
搜索与 XPath 查询
openpencil find design.fig --name "Button"
openpencil find --type FRAME # App 模式
openpencil find design.fig --type TEXT --page "Home"
openpencil find design.fig --name "Card" --type COMPONENT --limit 50
openpencil query design.fig "//FRAME"
openpencil query design.fig "//FRAME[@width < 300]"
openpencil query design.fig "//TEXT[contains(@name, 'Button')]"
openpencil query design.fig "//COMPONENT[@stackMode]"
openpencil query design.fig "//COMPONENT//FRAME//TEXT"
openpencil query "//FRAME[@width > 1000]" # App 模式
常见节点类型:FRAME、TEXT、RECTANGLE、ELLIPSE、VECTOR、GROUP、COMPONENT、COMPONENT_SET、INSTANCE、SECTION、LINE、STAR、POLYGON、SLICE、BOOLEAN_OPERATION。
导出与转换
openpencil export design.fig -o hero.png
openpencil export -o hero.png # App 模式
openpencil export design.fig --node 1:23 -s 2 -o button@2x.png
openpencil export design.fig -f jpg -q 85 -o preview.jpg
openpencil export design.fig -f svg --node 1:23 -o icon.svg
openpencil export design.fig -f pdf -o page.pdf
openpencil export design.fig -f fig -o roundtrip.fig
openpencil export design.fig -f jsx -o component.jsx
openpencil export design.fig -f jsx --style tailwind -o component.tsx
openpencil export design.fig --thumbnail --width 1920 --height 1080
openpencil export --page "Components" -o components.png
openpencil convert design.fig -o design.pen
openpencil formats
分析与 Lint 校验
openpencil analyze colors design.fig
openpencil analyze colors --similar --threshold 10 # App 模式
openpencil analyze typography design.fig --group-by size
openpencil analyze spacing design.fig --grid 8
openpencil analyze clusters design.fig --min-count 3
openpencil lint design.fig
openpencil lint design.fig --json
Eval(Figma Plugin API)
使用兼容 Figma Plugin API 的运行时对文档执行 JavaScript:
openpencil eval design.fig -c 'figma.currentPage.findAll(n => n.type === "TEXT").length'
# App 模式 — 修改编辑器中的实时文档
openpencil eval -c '
const buttons = figma.currentPage.findAll(n => n.name === "Button");
buttons.forEach(b => { b.cornerRadius = 8 });
buttons.length + " buttons updated"
'
# 修改并保存到同一文件
openpencil eval design.fig -w -c '
const texts = figma.currentPage.findAll(n => n.type === "TEXT");
texts.forEach(t => { t.fontSize = 16 });
'
# 保存到不同的文件
openpencil eval design.fig -o modified.fig -c '...'
# 从 stdin 读取代码
echo 'figma.currentPage.children.map(n => n.name)' | openpencil eval design.fig --stdin
任何返回结构化数据的命令均在适当时支持 --json 参数。
MCP 服务端
Stdio MCP 客户端
默认使用 Bun:
{
"mcpServers": {
"open-pencil": {
"command": "bunx",
"args": ["openpencil-mcp"]
}
}
}
如果全局安装了 @open-pencil/mcp,直接使用二进制文件同样有效:
{
"mcpServers": {
"open-pencil": {
"command": "openpencil-mcp"
}
}
}
HTTP / 可流式 HTTP
export PORT=7600
export OPENPENCIL_MCP_AUTH_TOKEN=secret # /mcp 的可选身份验证
export OPENPENCIL_MCP_CORS_ORIGIN="*" # 可选跨域设置
export OPENPENCIL_MCP_ROOT=/path/to/files # 启用/限定 open_file/save_file 的路径范围
openpencil-mcp-http
# 或:bunx openpencil-mcp-http
MCP 工作流
- 打开/创建文档 — 当配置了
OPENPENCIL_MCP_ROOT时使用open_file { path },或使用new_document {}。 - 查询 —
get_page_tree、find_nodes、query_nodes、get_node、list_pages、get_current_page。 - 检查 —
get_jsx、diff_jsx、describe、export_image、export_svg、export_pdf。 - 修改 —
render、batch_update、update_node、set_fill、set_layout、create_shape、import_svg等。 - 导航 — 创建或编辑可见的画布内容后,调用
select_nodes和viewport_zoom_to_fit { id }(或node_bounds+viewport_set),以便用户在运行中的编辑器中看到结果。 - 保存/导出 —
save_file、export_image、export_svg、export_pdf,或使用 CLI 的export。
0.12.0 中的 MCP 工具(共 106 个)
读取与选择 (17): get_selection、get_node、find_nodes、get_page_tree、get_current_page、list_pages、select_nodes、query_nodes、get_components、switch_page、page_bounds、list_fonts、list_available_fonts、get_jsx、diff_jsx、describe、node_tree
创建与导入 (12): render、create_shape、create_component、create_instance、create_page、create_vector、create_slice、import_svg、search_icons、insert_icon、fetch_icons、stock_photo
修改 (24): update_node、batch_update、set_layout、set_layout_child、set_radius、set_fill、set_stroke、set_text、set_text_properties、set_effects、set_opacity、set_font、set_visible、set_constraints、set_rotation、set_minmax、set_font_range、set_text_resize、set_blend、set_locked、set_stroke_align、set_image_fill、set_variable、bind_variable
结构 (16): delete_node、reparent_node、node_resize、clone_node、node_move、rename_node、group_nodes、ungroup_node、flatten_nodes、node_to_component、node_bounds、node_ancestors、node_children、node_bindings、node_replace_with、arrange
变量 (9): list_variables、list_collections、get_variable、find_variables、create_variable、delete_variable、get_collection、create_collection、delete_collection
矢量与视口 (15): boolean_union、boolean_subtract、boolean_intersect、boolean_exclude、path_get、path_set、path_scale、path_flip、path_move、viewport_get、viewport_set、viewport_zoom_to_fit、export_svg、export_pdf、export_image
分析与生成 (9): analyze_colors、analyze_typography、analyze_spacing、analyze_clusters、diff_create、diff_show、design_to_tokens、design_to_component_map、calc
文件与 Prompt (4): save_file、open_file、new_document、get_codegen_prompt
工具可用性可能取决于服务端模式。
open_file、save_file以及写入磁盘的导出路径需要配置OPENPENCIL_MCP_ROOT来限定路径范围。
Agent 的核心工具
query_nodes— 使用 XPath 选择器查找特定节点,无需获取完整树结构。get_jsx— 将任意节点作为 JSX 进行检查,其格式与render所接受的格式相同。diff_jsx— 在编辑前对比两个节点的结构差异。describe— 对角色、视觉样式、布局和设计问题进行语义分析。batch_update— 高效地批量应用多个节点更新。export_image/export_svg/export_pdf— 视觉验证与交付产物导出。viewport_zoom_to_fit/viewport_set/viewport_get— 让实时编辑器对焦到新创建或编辑的设计上。get_codegen_prompt— 获取 OpenPencil 当前的 JSX/代码生成指引。
JSX 渲染
使用 render 工具或 eval 来创建组件树。如果不确定 JSX 语法,请先调用 get_codegen_prompt。
<Frame name="Card" w={320} h="hug" flex="col" gap={16} p={24} bg="#FFF" rounded={16}>
<Text size={18} weight="bold" color="#111">Title</Text>
<Text size={14} color="#666">Description text</Text>
<Frame flex="row" gap={8}>
<Frame w={80} h={36} bg="#3B82F6" rounded={8} justify="center" items="center">
<Text size={14} color="#FFF" weight="600">Action</Text>
</Frame>
</Frame>
</Frame>
元素类型:Frame、Text、Rectangle、Ellipse、Line、Star、Polygon、Group、Section、Component、Instance。
文本内容作为 <Text> 的子元素放置。请使用设计 JSX 属性,而不是 Figma API 字段名:
<Text size={48} weight="bold" font="Inter" color="#111">Design faster with AI</Text>
常用属性:
| 属性 | 含义 |
|---|---|
w, h |
宽度、高度(数字或 "hug" / "fill") |
flex |
"row" 或 "col" |
grid, columns, rows |
CSS Grid,例如 columns="1fr 200px 1fr" |
gap, rowGap, columnGap |
子项间距 |
p, px, py, pt, pr, pb, pl |
内边距 |
justify |
"start"、"center"、"end"、"between" |
items |
"start"、"center"、"end"、"stretch" |
grow |
Flex 增长系数 |
bg |
填充颜色(十六进制) |
rounded, roundedTL/TR/BL/BR |
圆角半径 |
stroke, strokeWidth |
描边颜色和粗细 |
opacity |
透明度 (0–1) |
rotate |
旋转角度 |
overflow |
"hidden" 用于裁剪子节点 |
shadow |
"offsetX offsetY blur #color" |
blur |
图层模糊 |
size, weight, font, color, textAlign |
文本属性 |
colStart, rowStart, colSpan, rowSpan |
网格(Grid)子项定位 |
提示技巧
- 省略文件路径可直接处理正在运行的 OpenPencil 编辑器中打开的文档。
- 先使用
info或get_page_tree了解文档概况。 - 对大型文件使用
tree --depth 2或query_nodes,避免输出内容过多。 - 使用
--node导出特定节点,以便快速进行视觉检查。 - 修改后使用
export_image来验证视觉效果。 - 创建可见设计后,将其选中并缩放编辑器视角至该设计:依次调用
select_nodes { ids: [id] }和viewport_zoom_to_fit { id }。 - 如果客户端不支持缩放到合适大小,可以使用
node_bounds计算中心位置,然后调用viewport_set { x, y, zoom }。 - 使用
analyze colors --similar查找相近的重复颜色。 - 对于未被专用 CLI/MCP 工具覆盖的 Figma Plugin API 操作,可使用
eval。 - 将 CLI 输出通过管道传输给脚本时使用
--json。 - 在 App 模式下,
eval和 MCP 修改会实时反映在编辑器中。






