處理 Figma .fig 設計檔案與執行中的 OpenPencil 編輯器 — 檢視結構、查詢節點、分析設計 Token、匯出 PNG/SVG/PDF/JSX,並以程式化方式修改設計。當需要開啟、檢視、匯出、分析或編輯 .fig 檔案,或控制執行中的 OpenPencil 應用程式時使用。
OpenPencil
OpenPencil 為 .fig 設計檔案以及執行中的 OpenPencil 編輯器提供 CLI 與 MCP 伺服器。
支援兩種模式:
- 應用程式模式(App mode) — 省略檔案引數以連線至執行中的 OpenPencil 編輯器。
- 無頭模式(Headless mode) — 傳入檔案路徑直接對
.fig檔案進行操作。
# 應用程式模式 — 對編輯器中開啟的文件進行操作
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 bridge:
ws://127.0.0.1:7601 - MCP Streamable HTTP:
http://127.0.0.1:7600/mcp
CLI 指令
openpencil --help
0.12.x 中的指令:
info— 文件總覽:頁面、節點數量、字型tree— 印出包含型態與尺寸的階層樹pages— 列出頁面node— 透過 ID 查看詳細節點屬性selection— 從執行中的應用程式取得目前選取的項目find— 依名稱/型態搜尋節點query— 使用 XPath 選擇器搜尋節點variables— 列出變數與集合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 # 應用程式模式
openpencil pages design.fig
openpencil node design.fig --id 1:23
openpencil node --id 1:23 # 應用程式模式
openpencil selection --json
openpencil variables design.fig
openpencil variables --collection "Colors" --type COLOR
搜尋與 XPath 查詢
openpencil find design.fig --name "Button"
openpencil find --type FRAME # 應用程式模式
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]" # 應用程式模式
常見的節點型態: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 # 應用程式模式
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 # 應用程式模式
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'
# 應用程式模式 — 直接修改編輯器中開啟的即時文件
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 / Streamable HTTP
export PORT=7600
export OPENPENCIL_MCP_AUTH_TOKEN=secret # /mcp 的選填驗證
export OPENPENCIL_MCP_CORS_ORIGIN="*" # 選填 CORS
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
檔案與提示詞 (4): save_file、open_file、new_document、get_codegen_prompt
工具可用性可能取決於伺服器模式。
open_file、save_file以及寫入磁碟的匯出路徑需要設定OPENPENCIL_MCP_ROOT以限制路徑存取範圍。
Agent 的核心關鍵工具
query_nodes— 使用 XPath 選擇器尋找特定節點,無須擷取完整樹狀結構。get_jsx— 以render接收的相同格式檢視任意節點的 JSX。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 |
內邊距(Padding) |
justify |
"start"、"center"、"end"、"between" |
items |
"start"、"center"、"end"、"stretch" |
grow |
Flex 伸展係數 |
bg |
填滿顏色(hex) |
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 }。 - 若客戶端不支援 zoom-to-fit,可使用
node_bounds計算中心點並呼叫viewport_set { x, y, zoom }。 - 使用
analyze colors --similar來尋找極為接近的重複顏色。 - 對於未被專用 CLI/MCP 工具涵蓋的 Figma Plugin API 操作,可使用
eval。 - 將 CLI 輸出傳送(pipe)至指令稿時,請使用
--json。 - 在應用程式模式下,
eval與 MCP 修改會即時反映在編輯器中。






