figma-mcp-console 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -44
- package/bin/darwin-amd64/figma-mcp +0 -0
- package/bin/darwin-arm64/figma-mcp +0 -0
- package/bin/linux-amd64/figma-mcp +0 -0
- package/bin/linux-arm64/figma-mcp +0 -0
- package/bin/plugin/code.js +729 -0
- package/bin/plugin/manifest.json +14 -0
- package/bin/plugin/ui.html +191 -0
- package/bin/windows-amd64/figma-mcp.exe +0 -0
- package/bin/windows-arm64/figma-mcp.exe +0 -0
- package/package.json +4 -7
- package/run.js +50 -0
- package/bin.js +0 -90
package/README.md
CHANGED
|
@@ -32,12 +32,20 @@ No install needed — `npx` downloads and runs everything automatically.
|
|
|
32
32
|
<details>
|
|
33
33
|
<summary><b>Claude CLI</b></summary>
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
For one project — run inside that project (registers it there only):
|
|
36
36
|
|
|
37
37
|
```sh
|
|
38
38
|
claude mcp add figma-console -- npx -y figma-mcp-console@latest
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
For all your projects at once — add it globally instead:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
claude mcp add --scope user figma-console -- npx -y figma-mcp-console@latest
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Both work the same at runtime: each Claude session starts its own server instance in that project's directory (so exported assets land in the right project), and all instances share one local bridge to Figma automatically.
|
|
48
|
+
|
|
41
49
|
Or add to the project's `.mcp.json`:
|
|
42
50
|
|
|
43
51
|
```json
|
|
@@ -157,55 +165,44 @@ Standard MCP stdio transport — any client works. Set the command to `npx` with
|
|
|
157
165
|
|
|
158
166
|
</details>
|
|
159
167
|
|
|
160
|
-
> 💡 **Tip:**
|
|
168
|
+
> 💡 **Tip:** exported assets are written relative to the directory the server runs in. CLI tools (Claude CLI, Codex, Cursor, …) start it in your project directory, so global registration is fine. In clients without a working directory (e.g. Claude Desktop), pass explicit export paths or prefer a project-level config where available.
|
|
161
169
|
|
|
162
170
|
### Step 2 — Install the Figma plugin
|
|
163
171
|
|
|
164
|
-
1.
|
|
165
|
-
2. In **Figma Desktop**: **Plugins → Development → Import plugin from manifest…** → select `manifest.json` *(one-time step)*
|
|
172
|
+
1. Run **`npx figma-mcp-console install-plugin`** — it writes the plugin files locally and prints the path to `manifest.json`
|
|
173
|
+
2. In **Figma Desktop**: **Plugins → Development → Import plugin from manifest…** → select that `manifest.json` *(one-time step)*
|
|
166
174
|
3. Run it: **Plugins → Development → Figma MCP Console** — and **keep the plugin window open** while you work. 🟢 Green dot = connected
|
|
175
|
+
4. Working with several Figma files at once? Run the plugin in **each** file — every window connects to the same local bridge, and the AI picks the right file per call
|
|
167
176
|
|
|
168
177
|
### Step 3 — Try it
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
In Figma, right-click the page's frame → **Copy link to selection**, then paste this prompt:
|
|
173
|
-
|
|
174
|
-
> **📋 Copy-paste prompt:**
|
|
179
|
+
**Figma → code** — right-click a frame → **Copy link to selection**, then:
|
|
175
180
|
|
|
176
181
|
```text
|
|
177
|
-
Implement this Figma page in our project: <paste link
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
- Export icons from the design as SVG into public/icons and images as PNG
|
|
181
|
-
into public/images, and use those files in the code — do not create your own
|
|
182
|
-
- The design is the desktop layout (1440px); make the page responsive
|
|
183
|
-
and adapt it sensibly for tablet and mobile
|
|
184
|
-
- When done, screenshot the Figma frame, compare it with the implemented
|
|
185
|
-
page, and fix any differences before finishing
|
|
182
|
+
Implement this Figma page in our project: <paste link>. Match layout, spacing,
|
|
183
|
+
type and colors; export its icons/images into the project; screenshot the frame
|
|
184
|
+
and compare when done.
|
|
186
185
|
```
|
|
187
186
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
#### 🎨 Draw a Figma design from a requirement
|
|
191
|
-
|
|
192
|
-
> **📋 Copy-paste prompt:**
|
|
187
|
+
**Requirement → Figma:**
|
|
193
188
|
|
|
194
189
|
```text
|
|
195
|
-
Design a landing page (desktop, 1440px
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
- light theme, primary color #2563EB, font Inter
|
|
200
|
-
- use auto-layout for every section, 80px vertical spacing between sections
|
|
201
|
-
- when done, take a screenshot, compare it against this brief, and fix
|
|
202
|
-
anything that's off
|
|
190
|
+
Design a SaaS landing page in Figma (desktop, 1440px): hero + CTA, three feature
|
|
191
|
+
cards, pricing with three plans, footer. Light theme, use auto-layout, then
|
|
192
|
+
screenshot and fix anything off.
|
|
203
193
|
```
|
|
204
194
|
|
|
205
|
-
> 🎯
|
|
195
|
+
> 🎯 Asking the AI to **screenshot and compare** at the end makes it check its own work instead of guessing.
|
|
206
196
|
|
|
207
197
|
---
|
|
208
198
|
|
|
199
|
+
## 🗂 Multiple files, multiple projects
|
|
200
|
+
|
|
201
|
+
All AI sessions and all Figma files share **one local bridge** on port 2000 — the first MCP server to start owns it, later ones join it automatically, and if the owner exits another session takes over within a second. Nothing to configure.
|
|
202
|
+
|
|
203
|
+
- **Several Figma files**: run the plugin in each file (its window shows *Connected · \<file name\>*). With more than one file connected, every tool accepts a `file` parameter (file name, case-insensitive), and `list_files` shows what's connected — so you can say things like *"copy the primary color from **Design System** into the CTA button in **Landing Page**"* in a single session. With one file connected, nothing changes.
|
|
204
|
+
- **Several code projects**: each AI session (one per project) reaches every connected file through the shared bridge. State which file you mean in the prompt and each session works with the right one.
|
|
205
|
+
|
|
209
206
|
## 🧠 Teach your AI what to export, and where
|
|
210
207
|
|
|
211
208
|
Exports follow the file extension the AI picks (`.svg` for vector, `.png`/`.jpg` for bitmap, with an optional 0.5–4x scale). It decides based on what you ask — so put your preferences in the project rules file once, and stop repeating yourself:
|
|
@@ -218,27 +215,24 @@ Exports follow the file extension the AI picks (`.svg` for vector, `.png`/`.jpg`
|
|
|
218
215
|
| VS Code Copilot | `.github/copilot-instructions.md` |
|
|
219
216
|
| Windsurf | `.windsurfrules` |
|
|
220
217
|
|
|
221
|
-
> **📋 Copy-paste rules** (adjust paths
|
|
218
|
+
> **📋 Copy-paste rules** (adjust paths — this fits Next.js):
|
|
222
219
|
|
|
223
220
|
```markdown
|
|
224
221
|
## Figma asset rules
|
|
225
|
-
- Icons:
|
|
226
|
-
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
- File names: kebab-case, English, descriptive (arrow-left.svg, hero-banner.png).
|
|
230
|
-
Ignore the Figma layer names if they are messy.
|
|
231
|
-
- Design tokens: read them with get_variable_defs and map to our theme config.
|
|
232
|
-
- After building a screen, take a screenshot and fix issues before finishing.
|
|
222
|
+
- Icons: SVG into public/icons/. Images: PNG into public/images/.
|
|
223
|
+
- File names: kebab-case English (arrow-left.svg, hero-banner.png).
|
|
224
|
+
- Read design tokens with get_variable_defs; map them to our theme config.
|
|
225
|
+
- After building a screen, screenshot and fix issues before finishing.
|
|
233
226
|
```
|
|
234
227
|
|
|
235
228
|
## 🔧 Troubleshooting
|
|
236
229
|
|
|
237
230
|
| Problem | Fix |
|
|
238
231
|
|---|---|
|
|
239
|
-
| ❌ "Figma plugin is
|
|
240
|
-
| 🕐 Plugin says "
|
|
241
|
-
|
|
|
232
|
+
| ❌ "no Figma plugin is connected" | Open the plugin in Figma Desktop (**Plugins → Development → Figma MCP Console**) and keep its window open — in every file you want the AI to touch |
|
|
233
|
+
| 🕐 Plugin says "Searching for MCP server…" | Normal before your AI client connects — the server starts automatically with it. If it never connects, click the status line to retry, or restart your AI client |
|
|
234
|
+
| 🗂 "N Figma files are connected; pass the file parameter" | More than one plugin window is open — tell the AI which file you mean (or close the extra plugin windows) |
|
|
235
|
+
| 🚫 The MCP server doesn't appear in your AI tool | Check Node.js 18+ is installed (`node -v`) |
|
|
242
236
|
| 📁 Exported files end up in the wrong folder | Register the server in the project-level config, not the global one (see the tip in Step 1) |
|
|
243
237
|
| 🔤 Font errors when creating text | The Figma file uses a font not installed on your machine — the error message names it |
|
|
244
238
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
// Figma MCP Console — plugin main thread.
|
|
2
|
+
// Receives {id, command, params} from the Go bridge (relayed through ui.html),
|
|
3
|
+
// executes it against the Figma Plugin API, and replies {id, result|error}.
|
|
4
|
+
|
|
5
|
+
figma.showUI(__html__, { width: 300, height: 132 });
|
|
6
|
+
|
|
7
|
+
// Tell the UI which file this plugin window runs in; the UI forwards it to
|
|
8
|
+
// the bridge on every (re)connect so calls can be routed per file when
|
|
9
|
+
// several files run the plugin at once. figma.fileKey is undefined for
|
|
10
|
+
// non-published dev plugins, so the file name is the primary identifier.
|
|
11
|
+
figma.ui.postMessage({
|
|
12
|
+
register: { key: figma.fileKey || undefined, name: figma.root.name },
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// ---------- helpers ----------
|
|
16
|
+
|
|
17
|
+
function hexToRGB(hex) {
|
|
18
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex);
|
|
19
|
+
if (!m) throw new Error("invalid hex color: " + hex + " (expected #RRGGBB)");
|
|
20
|
+
const v = parseInt(m[1], 16);
|
|
21
|
+
return { r: ((v >> 16) & 255) / 255, g: ((v >> 8) & 255) / 255, b: (v & 255) / 255 };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function node(id) {
|
|
25
|
+
const n = await figma.getNodeByIdAsync(id);
|
|
26
|
+
if (!n) throw new Error("node not found: " + id);
|
|
27
|
+
return n;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Mixed values (figma.mixed) are Symbols, which postMessage cannot clone.
|
|
31
|
+
function val(x) {
|
|
32
|
+
return x === figma.mixed ? "mixed" : x;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function summarize(n) {
|
|
36
|
+
const s = { id: n.id, name: n.name, type: n.type };
|
|
37
|
+
if (n.parent) s.parentId = n.parent.id;
|
|
38
|
+
if ("x" in n) { s.x = n.x; s.y = n.y; }
|
|
39
|
+
if ("width" in n) { s.width = n.width; s.height = n.height; }
|
|
40
|
+
if (n.type === "TEXT") s.text = n.characters;
|
|
41
|
+
return s;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function serialize(n, depth) {
|
|
45
|
+
const s = summarize(n);
|
|
46
|
+
if ("visible" in n) s.visible = n.visible;
|
|
47
|
+
if ("opacity" in n) s.opacity = n.opacity;
|
|
48
|
+
if ("fills" in n && Array.isArray(n.fills)) s.fills = n.fills;
|
|
49
|
+
if ("strokes" in n && n.strokes.length) {
|
|
50
|
+
s.strokes = n.strokes;
|
|
51
|
+
s.strokeWeight = val(n.strokeWeight);
|
|
52
|
+
}
|
|
53
|
+
if ("effects" in n && n.effects.length) s.effects = n.effects;
|
|
54
|
+
if ("cornerRadius" in n && n.cornerRadius !== 0) s.cornerRadius = val(n.cornerRadius);
|
|
55
|
+
if (n.type === "TEXT") {
|
|
56
|
+
s.fontSize = val(n.fontSize);
|
|
57
|
+
s.fontName = val(n.fontName);
|
|
58
|
+
}
|
|
59
|
+
if ("layoutMode" in n && n.layoutMode !== "NONE") {
|
|
60
|
+
s.layoutMode = n.layoutMode;
|
|
61
|
+
s.itemSpacing = n.itemSpacing;
|
|
62
|
+
s.padding = {
|
|
63
|
+
top: n.paddingTop, right: n.paddingRight,
|
|
64
|
+
bottom: n.paddingBottom, left: n.paddingLeft,
|
|
65
|
+
};
|
|
66
|
+
s.primaryAxisAlignItems = n.primaryAxisAlignItems;
|
|
67
|
+
s.counterAxisAlignItems = n.counterAxisAlignItems;
|
|
68
|
+
}
|
|
69
|
+
if ("children" in n) {
|
|
70
|
+
s.childCount = n.children.length;
|
|
71
|
+
if (depth > 0) s.children = n.children.map((c) => serialize(c, depth - 1));
|
|
72
|
+
}
|
|
73
|
+
return s;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function parentOf(params) {
|
|
77
|
+
if (params.parent_id) {
|
|
78
|
+
const p = await node(params.parent_id);
|
|
79
|
+
if (!("appendChild" in p)) throw new Error("node " + params.parent_id + " cannot have children");
|
|
80
|
+
return p;
|
|
81
|
+
}
|
|
82
|
+
return figma.currentPage;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Builds a GradientPaint; the transform rotates the gradient around the
|
|
86
|
+
// node's center by `angle` degrees (0 = left-to-right).
|
|
87
|
+
function buildGradient(g) {
|
|
88
|
+
const stops = (g.stops || []).map((s) => {
|
|
89
|
+
const c = hexToRGB(s.color);
|
|
90
|
+
return {
|
|
91
|
+
position: s.position,
|
|
92
|
+
color: { r: c.r, g: c.g, b: c.b, a: s.opacity === undefined || s.opacity === null ? 1 : s.opacity },
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
if (stops.length < 2) throw new Error("gradient needs at least 2 stops");
|
|
96
|
+
const rad = ((g.angle || 0) * Math.PI) / 180;
|
|
97
|
+
const cos = Math.cos(rad);
|
|
98
|
+
const sin = Math.sin(rad);
|
|
99
|
+
return {
|
|
100
|
+
type: (g.type || "LINEAR").toUpperCase() === "RADIAL" ? "GRADIENT_RADIAL" : "GRADIENT_LINEAR",
|
|
101
|
+
gradientTransform: [
|
|
102
|
+
[cos, sin, 0.5 - 0.5 * cos - 0.5 * sin],
|
|
103
|
+
[-sin, cos, 0.5 + 0.5 * sin - 0.5 * cos],
|
|
104
|
+
],
|
|
105
|
+
gradientStops: stops,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// COLOR variables take {r,g,b,a}; accept hex strings for convenience.
|
|
110
|
+
function variableValue(v, value) {
|
|
111
|
+
if (v.resolvedType === "COLOR" && typeof value === "string") {
|
|
112
|
+
const c = hexToRGB(value);
|
|
113
|
+
return { r: c.r, g: c.g, b: c.b, a: 1 };
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function buildEffects(list) {
|
|
119
|
+
return (list || []).map((e) => {
|
|
120
|
+
const type = e.type;
|
|
121
|
+
if (type === "LAYER_BLUR" || type === "BACKGROUND_BLUR") {
|
|
122
|
+
return { type, radius: e.radius || 0, visible: true };
|
|
123
|
+
}
|
|
124
|
+
if (type !== "DROP_SHADOW" && type !== "INNER_SHADOW") {
|
|
125
|
+
throw new Error("unsupported effect type: " + type);
|
|
126
|
+
}
|
|
127
|
+
const color = hexToRGB(e.color || "#000000");
|
|
128
|
+
color.a = e.color_opacity === undefined ? 0.25 : e.color_opacity;
|
|
129
|
+
return {
|
|
130
|
+
type,
|
|
131
|
+
color,
|
|
132
|
+
offset: { x: e.offset_x || 0, y: e.offset_y || 0 },
|
|
133
|
+
radius: e.radius || 0,
|
|
134
|
+
spread: e.spread || 0,
|
|
135
|
+
visible: true,
|
|
136
|
+
blendMode: "NORMAL",
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function applyShapeParams(n, params) {
|
|
142
|
+
n.x = params.x;
|
|
143
|
+
n.y = params.y;
|
|
144
|
+
if (params.width > 0 && params.height > 0) n.resize(params.width, params.height);
|
|
145
|
+
if (params.name) n.name = params.name;
|
|
146
|
+
if (params.fill_color) n.fills = [{ type: "SOLID", color: hexToRGB(params.fill_color) }];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------- command handlers (names match MCP tool names) ----------
|
|
150
|
+
|
|
151
|
+
const handlers = {
|
|
152
|
+
async get_metadata() {
|
|
153
|
+
return {
|
|
154
|
+
file: figma.root.name,
|
|
155
|
+
currentPage: { id: figma.currentPage.id, name: figma.currentPage.name },
|
|
156
|
+
children: figma.currentPage.children.map(summarize),
|
|
157
|
+
};
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
async get_selection() {
|
|
161
|
+
return figma.currentPage.selection.map(summarize);
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
async get_design_context(params) {
|
|
165
|
+
const depth = params.depth === undefined ? 2 : params.depth;
|
|
166
|
+
return serialize(await node(params.node_id), depth);
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
async create_frame(params) {
|
|
170
|
+
const f = figma.createFrame();
|
|
171
|
+
applyShapeParams(f, params);
|
|
172
|
+
(await parentOf(params)).appendChild(f);
|
|
173
|
+
return summarize(f);
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
async create_rectangle(params) {
|
|
177
|
+
const r = figma.createRectangle();
|
|
178
|
+
applyShapeParams(r, params);
|
|
179
|
+
(await parentOf(params)).appendChild(r);
|
|
180
|
+
return summarize(r);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
async create_ellipse(params) {
|
|
184
|
+
const e = figma.createEllipse();
|
|
185
|
+
applyShapeParams(e, params);
|
|
186
|
+
(await parentOf(params)).appendChild(e);
|
|
187
|
+
return summarize(e);
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
async create_line(params) {
|
|
191
|
+
const l = figma.createLine();
|
|
192
|
+
l.x = params.x;
|
|
193
|
+
l.y = params.y;
|
|
194
|
+
l.resize(params.length > 0 ? params.length : 100, 0);
|
|
195
|
+
l.strokes = [{ type: "SOLID", color: hexToRGB(params.stroke_color || "#000000") }];
|
|
196
|
+
if (params.stroke_weight) l.strokeWeight = params.stroke_weight;
|
|
197
|
+
if (params.rotation !== undefined && params.rotation !== null) l.rotation = params.rotation;
|
|
198
|
+
if (params.name) l.name = params.name;
|
|
199
|
+
(await parentOf(params)).appendChild(l);
|
|
200
|
+
return summarize(l);
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
async import_image(params) {
|
|
204
|
+
const image = figma.createImage(figma.base64Decode(params.data));
|
|
205
|
+
const paint = { type: "IMAGE", imageHash: image.hash, scaleMode: params.scale_mode || "FILL" };
|
|
206
|
+
let n;
|
|
207
|
+
if (params.node_id) {
|
|
208
|
+
n = await node(params.node_id);
|
|
209
|
+
if (!("fills" in n)) throw new Error("node " + params.node_id + " has no fills");
|
|
210
|
+
n.fills = [paint];
|
|
211
|
+
} else {
|
|
212
|
+
const size = await image.getSizeAsync();
|
|
213
|
+
n = figma.createRectangle();
|
|
214
|
+
n.x = params.x || 0;
|
|
215
|
+
n.y = params.y || 0;
|
|
216
|
+
n.resize(params.width > 0 ? params.width : size.width, params.height > 0 ? params.height : size.height);
|
|
217
|
+
if (params.name) n.name = params.name;
|
|
218
|
+
n.fills = [paint];
|
|
219
|
+
(await parentOf(params)).appendChild(n);
|
|
220
|
+
}
|
|
221
|
+
return summarize(n);
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
async create_text(params) {
|
|
225
|
+
const font = { family: params.font_family || "Inter", style: params.font_style || "Regular" };
|
|
226
|
+
await figma.loadFontAsync(font);
|
|
227
|
+
const t = figma.createText();
|
|
228
|
+
t.fontName = font;
|
|
229
|
+
t.characters = params.text;
|
|
230
|
+
t.x = params.x;
|
|
231
|
+
t.y = params.y;
|
|
232
|
+
if (params.font_size) t.fontSize = params.font_size;
|
|
233
|
+
if (params.fill_color) t.fills = [{ type: "SOLID", color: hexToRGB(params.fill_color) }];
|
|
234
|
+
if (params.line_height) t.lineHeight = { value: params.line_height, unit: "PIXELS" };
|
|
235
|
+
if (params.letter_spacing !== undefined && params.letter_spacing !== null) {
|
|
236
|
+
t.letterSpacing = { value: params.letter_spacing, unit: "PIXELS" };
|
|
237
|
+
}
|
|
238
|
+
if (params.text_align) t.textAlignHorizontal = params.text_align;
|
|
239
|
+
if (params.max_width > 0) {
|
|
240
|
+
t.textAutoResize = "HEIGHT";
|
|
241
|
+
t.resize(params.max_width, t.height);
|
|
242
|
+
}
|
|
243
|
+
if (params.name) t.name = params.name;
|
|
244
|
+
(await parentOf(params)).appendChild(t);
|
|
245
|
+
return summarize(t);
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
async set_characters(params) {
|
|
249
|
+
const t = await node(params.node_id);
|
|
250
|
+
if (t.type !== "TEXT") throw new Error("node " + params.node_id + " is " + t.type + ", not TEXT");
|
|
251
|
+
if (t.characters.length > 0) {
|
|
252
|
+
const fonts = t.getRangeAllFontNames(0, t.characters.length);
|
|
253
|
+
await Promise.all(fonts.map(figma.loadFontAsync));
|
|
254
|
+
} else {
|
|
255
|
+
await figma.loadFontAsync(t.fontName);
|
|
256
|
+
}
|
|
257
|
+
t.characters = params.text;
|
|
258
|
+
return summarize(t);
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
async set_fills(params) {
|
|
262
|
+
const n = await node(params.node_id);
|
|
263
|
+
if (!("fills" in n)) throw new Error("node " + params.node_id + " has no fills");
|
|
264
|
+
let paint;
|
|
265
|
+
if (params.gradient) {
|
|
266
|
+
paint = buildGradient(params.gradient);
|
|
267
|
+
} else if (params.color) {
|
|
268
|
+
paint = { type: "SOLID", color: hexToRGB(params.color) };
|
|
269
|
+
} else {
|
|
270
|
+
throw new Error("set_fills needs color or gradient");
|
|
271
|
+
}
|
|
272
|
+
if (params.opacity !== undefined && params.opacity !== null) paint.opacity = params.opacity;
|
|
273
|
+
n.fills = [paint];
|
|
274
|
+
return summarize(n);
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
async move_nodes(params) {
|
|
278
|
+
const out = [];
|
|
279
|
+
for (const item of params.items) {
|
|
280
|
+
const n = await node(item.node_id);
|
|
281
|
+
if (!("x" in n)) throw new Error("node " + item.node_id + " cannot be moved");
|
|
282
|
+
n.x = item.x;
|
|
283
|
+
n.y = item.y;
|
|
284
|
+
out.push(summarize(n));
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
async resize_nodes(params) {
|
|
290
|
+
const out = [];
|
|
291
|
+
for (const item of params.items) {
|
|
292
|
+
const n = await node(item.node_id);
|
|
293
|
+
if (!("resize" in n)) throw new Error("node " + item.node_id + " cannot be resized");
|
|
294
|
+
n.resize(item.width, item.height);
|
|
295
|
+
out.push(summarize(n));
|
|
296
|
+
}
|
|
297
|
+
return out;
|
|
298
|
+
},
|
|
299
|
+
|
|
300
|
+
async remove_nodes(params) {
|
|
301
|
+
const deleted = [];
|
|
302
|
+
for (const id of params.node_ids) {
|
|
303
|
+
const n = await node(id);
|
|
304
|
+
deleted.push(summarize(n));
|
|
305
|
+
n.remove();
|
|
306
|
+
}
|
|
307
|
+
return { deleted };
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
async set_auto_layout(params) {
|
|
311
|
+
const n = await node(params.node_id);
|
|
312
|
+
if (!("layoutMode" in n)) throw new Error("node " + params.node_id + " does not support auto-layout");
|
|
313
|
+
n.layoutMode = params.layout_mode;
|
|
314
|
+
if (params.layout_mode === "NONE") return summarize(n);
|
|
315
|
+
if (params.item_spacing !== undefined) n.itemSpacing = params.item_spacing;
|
|
316
|
+
if (params.padding !== undefined) {
|
|
317
|
+
n.paddingTop = n.paddingRight = n.paddingBottom = n.paddingLeft = params.padding;
|
|
318
|
+
}
|
|
319
|
+
if (params.padding_top !== undefined) n.paddingTop = params.padding_top;
|
|
320
|
+
if (params.padding_right !== undefined) n.paddingRight = params.padding_right;
|
|
321
|
+
if (params.padding_bottom !== undefined) n.paddingBottom = params.padding_bottom;
|
|
322
|
+
if (params.padding_left !== undefined) n.paddingLeft = params.padding_left;
|
|
323
|
+
if (params.primary_axis_align) n.primaryAxisAlignItems = params.primary_axis_align;
|
|
324
|
+
if (params.counter_axis_align) n.counterAxisAlignItems = params.counter_axis_align;
|
|
325
|
+
if (params.primary_axis_sizing) n.primaryAxisSizingMode = params.primary_axis_sizing;
|
|
326
|
+
if (params.counter_axis_sizing) n.counterAxisSizingMode = params.counter_axis_sizing;
|
|
327
|
+
return summarize(n);
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
async set_corner_radius(params) {
|
|
331
|
+
const n = await node(params.node_id);
|
|
332
|
+
if (params.radius !== undefined) {
|
|
333
|
+
if (!("cornerRadius" in n)) throw new Error("node " + params.node_id + " has no corner radius");
|
|
334
|
+
n.cornerRadius = params.radius;
|
|
335
|
+
}
|
|
336
|
+
if (!("topLeftRadius" in n)) {
|
|
337
|
+
if (params.radius === undefined) throw new Error("node " + params.node_id + " has no per-corner radius");
|
|
338
|
+
return summarize(n);
|
|
339
|
+
}
|
|
340
|
+
if (params.top_left !== undefined) n.topLeftRadius = params.top_left;
|
|
341
|
+
if (params.top_right !== undefined) n.topRightRadius = params.top_right;
|
|
342
|
+
if (params.bottom_right !== undefined) n.bottomRightRadius = params.bottom_right;
|
|
343
|
+
if (params.bottom_left !== undefined) n.bottomLeftRadius = params.bottom_left;
|
|
344
|
+
return summarize(n);
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
async set_strokes(params) {
|
|
348
|
+
const n = await node(params.node_id);
|
|
349
|
+
if (!("strokes" in n)) throw new Error("node " + params.node_id + " has no strokes");
|
|
350
|
+
if (params.color) {
|
|
351
|
+
const paint = { type: "SOLID", color: hexToRGB(params.color) };
|
|
352
|
+
if (params.opacity !== undefined && params.opacity !== null) paint.opacity = params.opacity;
|
|
353
|
+
n.strokes = [paint];
|
|
354
|
+
} else {
|
|
355
|
+
n.strokes = [];
|
|
356
|
+
}
|
|
357
|
+
if (params.weight !== undefined && "strokeWeight" in n) n.strokeWeight = params.weight;
|
|
358
|
+
if (params.align && "strokeAlign" in n) n.strokeAlign = params.align;
|
|
359
|
+
return summarize(n);
|
|
360
|
+
},
|
|
361
|
+
|
|
362
|
+
async set_effects(params) {
|
|
363
|
+
const n = await node(params.node_id);
|
|
364
|
+
if (!("effects" in n)) throw new Error("node " + params.node_id + " has no effects");
|
|
365
|
+
n.effects = buildEffects(params.effects);
|
|
366
|
+
return summarize(n);
|
|
367
|
+
},
|
|
368
|
+
|
|
369
|
+
async download_assets(params) {
|
|
370
|
+
const out = [];
|
|
371
|
+
// One bad item (stale node id, un-exportable type) must not sink the
|
|
372
|
+
// whole batch — report it in place and keep exporting the rest.
|
|
373
|
+
for (const item of params.items) {
|
|
374
|
+
try {
|
|
375
|
+
const target = await node(item.node_id);
|
|
376
|
+
if (!("exportAsync" in target)) throw new Error("node " + item.node_id + " cannot be exported");
|
|
377
|
+
const format = (item.format || "PNG").toUpperCase();
|
|
378
|
+
let settings;
|
|
379
|
+
if (format === "SVG") {
|
|
380
|
+
settings = { format: "SVG" };
|
|
381
|
+
} else if (format === "PNG" || format === "JPG") {
|
|
382
|
+
let scale = item.scale || 1;
|
|
383
|
+
scale = Math.max(0.5, Math.min(scale, 4));
|
|
384
|
+
settings = { format, constraint: { type: "SCALE", value: scale } };
|
|
385
|
+
} else {
|
|
386
|
+
throw new Error("unsupported format: " + format + " (use PNG, JPG or SVG)");
|
|
387
|
+
}
|
|
388
|
+
const bytes = await target.exportAsync(settings);
|
|
389
|
+
out.push({
|
|
390
|
+
data: figma.base64Encode(bytes),
|
|
391
|
+
format,
|
|
392
|
+
name: target.name,
|
|
393
|
+
width: "width" in target ? target.width : 0,
|
|
394
|
+
height: "height" in target ? target.height : 0,
|
|
395
|
+
});
|
|
396
|
+
} catch (e) {
|
|
397
|
+
out.push({ error: String((e && e.message) || e) });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
async set_selection(params) {
|
|
404
|
+
const nodes = await Promise.all(params.node_ids.map(node));
|
|
405
|
+
figma.currentPage.selection = nodes;
|
|
406
|
+
figma.viewport.scrollAndZoomIntoView(nodes);
|
|
407
|
+
return nodes.map(summarize);
|
|
408
|
+
},
|
|
409
|
+
|
|
410
|
+
async find_nodes(params) {
|
|
411
|
+
const scope = params.node_id ? await node(params.node_id) : figma.currentPage;
|
|
412
|
+
if (!("findAll" in scope)) throw new Error("node " + scope.id + " has no children to search");
|
|
413
|
+
const name = params.name ? params.name.toLowerCase() : null;
|
|
414
|
+
const text = params.text ? params.text.toLowerCase() : null;
|
|
415
|
+
const types = params.types && params.types.length ? params.types : null;
|
|
416
|
+
const matches = scope.findAll((n) => {
|
|
417
|
+
if (types && types.indexOf(n.type) === -1) return false;
|
|
418
|
+
if (name && n.name.toLowerCase().indexOf(name) === -1) return false;
|
|
419
|
+
if (text && (n.type !== "TEXT" || n.characters.toLowerCase().indexOf(text) === -1)) return false;
|
|
420
|
+
return true;
|
|
421
|
+
});
|
|
422
|
+
const max = params.max_results || 50;
|
|
423
|
+
return {
|
|
424
|
+
total: matches.length,
|
|
425
|
+
truncated: matches.length > max,
|
|
426
|
+
nodes: matches.slice(0, max).map(summarize),
|
|
427
|
+
};
|
|
428
|
+
},
|
|
429
|
+
|
|
430
|
+
async group_nodes(params) {
|
|
431
|
+
const nodes = await Promise.all(params.node_ids.map(node));
|
|
432
|
+
const group = figma.group(nodes, nodes[0].parent);
|
|
433
|
+
if (params.name) group.name = params.name;
|
|
434
|
+
return summarize(group);
|
|
435
|
+
},
|
|
436
|
+
|
|
437
|
+
async ungroup_nodes(params) {
|
|
438
|
+
const released = [];
|
|
439
|
+
for (const id of params.node_ids) {
|
|
440
|
+
const n = await node(id);
|
|
441
|
+
if (n.type !== "GROUP" && n.type !== "FRAME") {
|
|
442
|
+
throw new Error("node " + id + " is " + n.type + ", not a GROUP or FRAME");
|
|
443
|
+
}
|
|
444
|
+
released.push(...figma.ungroup(n).map(summarize));
|
|
445
|
+
}
|
|
446
|
+
return { released };
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
async append_children(params) {
|
|
450
|
+
const parent = await node(params.parent_id);
|
|
451
|
+
if (!("appendChild" in parent)) throw new Error("node " + params.parent_id + " cannot have children");
|
|
452
|
+
const out = [];
|
|
453
|
+
let index = params.index;
|
|
454
|
+
for (const id of params.node_ids) {
|
|
455
|
+
const n = await node(id);
|
|
456
|
+
if (index === undefined || index === null) {
|
|
457
|
+
parent.appendChild(n);
|
|
458
|
+
} else {
|
|
459
|
+
parent.insertChild(index++, n);
|
|
460
|
+
}
|
|
461
|
+
out.push(summarize(n));
|
|
462
|
+
}
|
|
463
|
+
return out;
|
|
464
|
+
},
|
|
465
|
+
|
|
466
|
+
async clone_node(params) {
|
|
467
|
+
const n = await node(params.node_id);
|
|
468
|
+
if (!("clone" in n)) throw new Error("node " + params.node_id + " cannot be cloned");
|
|
469
|
+
const copy = n.clone();
|
|
470
|
+
if (params.parent_id) {
|
|
471
|
+
const p = await node(params.parent_id);
|
|
472
|
+
if (!("appendChild" in p)) throw new Error("node " + params.parent_id + " cannot have children");
|
|
473
|
+
p.appendChild(copy);
|
|
474
|
+
}
|
|
475
|
+
if (params.x !== undefined) copy.x = params.x;
|
|
476
|
+
if (params.y !== undefined) copy.y = params.y;
|
|
477
|
+
if (params.name) copy.name = params.name;
|
|
478
|
+
return summarize(copy);
|
|
479
|
+
},
|
|
480
|
+
|
|
481
|
+
async list_available_fonts(params) {
|
|
482
|
+
const fonts = await figma.listAvailableFontsAsync();
|
|
483
|
+
const filter = params.family ? params.family.toLowerCase() : null;
|
|
484
|
+
const byFamily = {};
|
|
485
|
+
const order = [];
|
|
486
|
+
for (const f of fonts) {
|
|
487
|
+
const fam = f.fontName.family;
|
|
488
|
+
if (filter && fam.toLowerCase().indexOf(filter) === -1) continue;
|
|
489
|
+
if (!byFamily[fam]) {
|
|
490
|
+
byFamily[fam] = [];
|
|
491
|
+
order.push(fam);
|
|
492
|
+
}
|
|
493
|
+
byFamily[fam].push(f.fontName.style);
|
|
494
|
+
}
|
|
495
|
+
const max = params.max_families || 50;
|
|
496
|
+
return {
|
|
497
|
+
total: order.length,
|
|
498
|
+
truncated: order.length > max,
|
|
499
|
+
families: order.slice(0, max).map((fam) => ({ family: fam, styles: byFamily[fam] })),
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
|
|
503
|
+
// ---------- components ----------
|
|
504
|
+
|
|
505
|
+
async create_component(params) {
|
|
506
|
+
const n = await node(params.node_id);
|
|
507
|
+
if (n.type !== "FRAME") throw new Error("node " + params.node_id + " is " + n.type + ", not a FRAME");
|
|
508
|
+
return summarize(figma.createComponentFromNode(n));
|
|
509
|
+
},
|
|
510
|
+
|
|
511
|
+
async get_local_components() {
|
|
512
|
+
await figma.loadAllPagesAsync();
|
|
513
|
+
const comps = figma.root.findAllWithCriteria({ types: ["COMPONENT", "COMPONENT_SET"] });
|
|
514
|
+
return comps.map((c) => ({
|
|
515
|
+
id: c.id,
|
|
516
|
+
name: c.name,
|
|
517
|
+
type: c.type,
|
|
518
|
+
description: c.description || "",
|
|
519
|
+
page: c.parent && c.parent.type === "PAGE" ? c.parent.name : undefined,
|
|
520
|
+
}));
|
|
521
|
+
},
|
|
522
|
+
|
|
523
|
+
async create_instance(params) {
|
|
524
|
+
const c = await node(params.component_id);
|
|
525
|
+
if (c.type !== "COMPONENT") throw new Error("node " + params.component_id + " is " + c.type + ", not a COMPONENT");
|
|
526
|
+
const inst = c.createInstance();
|
|
527
|
+
if (params.x !== undefined) inst.x = params.x;
|
|
528
|
+
if (params.y !== undefined) inst.y = params.y;
|
|
529
|
+
(await parentOf(params)).appendChild(inst);
|
|
530
|
+
return summarize(inst);
|
|
531
|
+
},
|
|
532
|
+
|
|
533
|
+
async swap_component(params) {
|
|
534
|
+
const inst = await node(params.instance_id);
|
|
535
|
+
if (inst.type !== "INSTANCE") throw new Error("node " + params.instance_id + " is " + inst.type + ", not an INSTANCE");
|
|
536
|
+
const c = await node(params.component_id);
|
|
537
|
+
if (c.type !== "COMPONENT") throw new Error("node " + params.component_id + " is " + c.type + ", not a COMPONENT");
|
|
538
|
+
inst.swapComponent(c);
|
|
539
|
+
return summarize(inst);
|
|
540
|
+
},
|
|
541
|
+
|
|
542
|
+
async detach_instance(params) {
|
|
543
|
+
const inst = await node(params.instance_id);
|
|
544
|
+
if (inst.type !== "INSTANCE") throw new Error("node " + params.instance_id + " is " + inst.type + ", not an INSTANCE");
|
|
545
|
+
return summarize(inst.detachInstance());
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
// ---------- styles ----------
|
|
549
|
+
|
|
550
|
+
async get_local_styles() {
|
|
551
|
+
const [paint, text, effect, grid] = await Promise.all([
|
|
552
|
+
figma.getLocalPaintStylesAsync(),
|
|
553
|
+
figma.getLocalTextStylesAsync(),
|
|
554
|
+
figma.getLocalEffectStylesAsync(),
|
|
555
|
+
figma.getLocalGridStylesAsync(),
|
|
556
|
+
]);
|
|
557
|
+
const brief = (s) => ({ id: s.id, name: s.name });
|
|
558
|
+
return {
|
|
559
|
+
paint: paint.map(brief),
|
|
560
|
+
text: text.map(brief),
|
|
561
|
+
effect: effect.map(brief),
|
|
562
|
+
grid: grid.map(brief),
|
|
563
|
+
};
|
|
564
|
+
},
|
|
565
|
+
|
|
566
|
+
async create_paint_style(params) {
|
|
567
|
+
const s = figma.createPaintStyle();
|
|
568
|
+
s.name = params.name;
|
|
569
|
+
const paint = { type: "SOLID", color: hexToRGB(params.color) };
|
|
570
|
+
if (params.opacity !== undefined && params.opacity !== null) paint.opacity = params.opacity;
|
|
571
|
+
s.paints = [paint];
|
|
572
|
+
return { id: s.id, name: s.name };
|
|
573
|
+
},
|
|
574
|
+
|
|
575
|
+
async create_text_style(params) {
|
|
576
|
+
const font = { family: params.font_family || "Inter", style: params.font_style || "Regular" };
|
|
577
|
+
await figma.loadFontAsync(font);
|
|
578
|
+
const s = figma.createTextStyle();
|
|
579
|
+
s.name = params.name;
|
|
580
|
+
s.fontName = font;
|
|
581
|
+
if (params.font_size) s.fontSize = params.font_size;
|
|
582
|
+
return { id: s.id, name: s.name };
|
|
583
|
+
},
|
|
584
|
+
|
|
585
|
+
async create_effect_style(params) {
|
|
586
|
+
const s = figma.createEffectStyle();
|
|
587
|
+
s.name = params.name;
|
|
588
|
+
s.effects = buildEffects(params.effects);
|
|
589
|
+
return { id: s.id, name: s.name };
|
|
590
|
+
},
|
|
591
|
+
|
|
592
|
+
async apply_style(params) {
|
|
593
|
+
const n = await node(params.node_id);
|
|
594
|
+
const style = await figma.getStyleByIdAsync(params.style_id);
|
|
595
|
+
if (!style) throw new Error("style not found: " + params.style_id);
|
|
596
|
+
if (style.type === "PAINT") {
|
|
597
|
+
if (params.target === "stroke") {
|
|
598
|
+
await n.setStrokeStyleIdAsync(style.id);
|
|
599
|
+
} else {
|
|
600
|
+
await n.setFillStyleIdAsync(style.id);
|
|
601
|
+
}
|
|
602
|
+
} else if (style.type === "TEXT") {
|
|
603
|
+
if (n.type !== "TEXT") throw new Error("node " + params.node_id + " is " + n.type + ", not TEXT");
|
|
604
|
+
await figma.loadFontAsync(style.fontName);
|
|
605
|
+
await n.setTextStyleIdAsync(style.id);
|
|
606
|
+
} else if (style.type === "EFFECT") {
|
|
607
|
+
await n.setEffectStyleIdAsync(style.id);
|
|
608
|
+
} else if (style.type === "GRID") {
|
|
609
|
+
await n.setGridStyleIdAsync(style.id);
|
|
610
|
+
} else {
|
|
611
|
+
throw new Error("unsupported style type: " + style.type);
|
|
612
|
+
}
|
|
613
|
+
return summarize(n);
|
|
614
|
+
},
|
|
615
|
+
|
|
616
|
+
// ---------- variables (design tokens) ----------
|
|
617
|
+
|
|
618
|
+
async get_variable_defs() {
|
|
619
|
+
const [collections, variables] = await Promise.all([
|
|
620
|
+
figma.variables.getLocalVariableCollectionsAsync(),
|
|
621
|
+
figma.variables.getLocalVariablesAsync(),
|
|
622
|
+
]);
|
|
623
|
+
return {
|
|
624
|
+
collections: collections.map((c) => ({
|
|
625
|
+
id: c.id,
|
|
626
|
+
name: c.name,
|
|
627
|
+
defaultModeId: c.defaultModeId,
|
|
628
|
+
modes: c.modes.map((m) => ({ modeId: m.modeId, name: m.name })),
|
|
629
|
+
})),
|
|
630
|
+
variables: variables.map((v) => ({
|
|
631
|
+
id: v.id,
|
|
632
|
+
name: v.name,
|
|
633
|
+
resolvedType: v.resolvedType,
|
|
634
|
+
collectionId: v.variableCollectionId,
|
|
635
|
+
valuesByMode: v.valuesByMode,
|
|
636
|
+
})),
|
|
637
|
+
};
|
|
638
|
+
},
|
|
639
|
+
|
|
640
|
+
async create_variable_collection(params) {
|
|
641
|
+
const c = figma.variables.createVariableCollection(params.name);
|
|
642
|
+
if (params.mode_name) c.renameMode(c.defaultModeId, params.mode_name);
|
|
643
|
+
return {
|
|
644
|
+
id: c.id,
|
|
645
|
+
name: c.name,
|
|
646
|
+
defaultModeId: c.defaultModeId,
|
|
647
|
+
modes: c.modes.map((m) => ({ modeId: m.modeId, name: m.name })),
|
|
648
|
+
};
|
|
649
|
+
},
|
|
650
|
+
|
|
651
|
+
async add_variable_mode(params) {
|
|
652
|
+
const c = await figma.variables.getVariableCollectionByIdAsync(params.collection_id);
|
|
653
|
+
if (!c) throw new Error("variable collection not found: " + params.collection_id);
|
|
654
|
+
const modeId = c.addMode(params.name);
|
|
655
|
+
return { collectionId: c.id, modeId, name: params.name };
|
|
656
|
+
},
|
|
657
|
+
|
|
658
|
+
async create_variable(params) {
|
|
659
|
+
const c = await figma.variables.getVariableCollectionByIdAsync(params.collection_id);
|
|
660
|
+
if (!c) throw new Error("variable collection not found: " + params.collection_id);
|
|
661
|
+
const v = figma.variables.createVariable(params.name, c, params.type);
|
|
662
|
+
if (params.value !== undefined && params.value !== null) {
|
|
663
|
+
v.setValueForMode(c.defaultModeId, variableValue(v, params.value));
|
|
664
|
+
}
|
|
665
|
+
return { id: v.id, name: v.name, resolvedType: v.resolvedType, collectionId: c.id };
|
|
666
|
+
},
|
|
667
|
+
|
|
668
|
+
async set_variable_value(params) {
|
|
669
|
+
const v = await figma.variables.getVariableByIdAsync(params.variable_id);
|
|
670
|
+
if (!v) throw new Error("variable not found: " + params.variable_id);
|
|
671
|
+
const c = await figma.variables.getVariableCollectionByIdAsync(v.variableCollectionId);
|
|
672
|
+
const modeId = params.mode_id || c.defaultModeId;
|
|
673
|
+
v.setValueForMode(modeId, variableValue(v, params.value));
|
|
674
|
+
return { id: v.id, name: v.name, modeId, valuesByMode: v.valuesByMode };
|
|
675
|
+
},
|
|
676
|
+
|
|
677
|
+
async set_bound_variable(params) {
|
|
678
|
+
const n = await node(params.node_id);
|
|
679
|
+
const v = await figma.variables.getVariableByIdAsync(params.variable_id);
|
|
680
|
+
if (!v) throw new Error("variable not found: " + params.variable_id);
|
|
681
|
+
const field = params.field;
|
|
682
|
+
if (field === "fills" || field === "strokes") {
|
|
683
|
+
if (!(field in n)) throw new Error("node " + params.node_id + " has no " + field);
|
|
684
|
+
const paints = n[field].length ? n[field] : [{ type: "SOLID", color: { r: 0, g: 0, b: 0 } }];
|
|
685
|
+
n[field] = [figma.variables.setBoundVariableForPaint(paints[0], "color", v)].concat(paints.slice(1));
|
|
686
|
+
} else {
|
|
687
|
+
n.setBoundVariable(field, v);
|
|
688
|
+
}
|
|
689
|
+
return summarize(n);
|
|
690
|
+
},
|
|
691
|
+
|
|
692
|
+
async get_screenshot(params) {
|
|
693
|
+
let target;
|
|
694
|
+
if (params.node_id) {
|
|
695
|
+
target = await node(params.node_id);
|
|
696
|
+
} else if (figma.currentPage.selection.length === 1) {
|
|
697
|
+
target = figma.currentPage.selection[0];
|
|
698
|
+
} else {
|
|
699
|
+
target = figma.currentPage;
|
|
700
|
+
}
|
|
701
|
+
if (!("exportAsync" in target)) throw new Error("node " + target.id + " cannot be exported");
|
|
702
|
+
let scale = params.scale || 1;
|
|
703
|
+
scale = Math.max(0.5, Math.min(scale, 4));
|
|
704
|
+
// Cap the output size: huge pages at high scale produce multi-MB base64
|
|
705
|
+
// payloads that are slow to ship and waste the AI's context.
|
|
706
|
+
const maxDim = Math.max("width" in target ? target.width : 0, "height" in target ? target.height : 0);
|
|
707
|
+
if (maxDim * scale > 2000) scale = 2000 / maxDim;
|
|
708
|
+
const bytes = await target.exportAsync({ format: "PNG", constraint: { type: "SCALE", value: scale } });
|
|
709
|
+
return {
|
|
710
|
+
data: figma.base64Encode(bytes),
|
|
711
|
+
width: ("width" in target ? target.width : 0) * scale,
|
|
712
|
+
height: ("height" in target ? target.height : 0) * scale,
|
|
713
|
+
};
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
// ---------- dispatch ----------
|
|
718
|
+
|
|
719
|
+
figma.ui.onmessage = async (msg) => {
|
|
720
|
+
if (!msg || !msg.id || !msg.command) return;
|
|
721
|
+
const h = handlers[msg.command];
|
|
722
|
+
try {
|
|
723
|
+
if (!h) throw new Error("unknown command: " + msg.command);
|
|
724
|
+
const result = await h(msg.params || {});
|
|
725
|
+
figma.ui.postMessage({ id: msg.id, result: result === undefined ? null : result });
|
|
726
|
+
} catch (e) {
|
|
727
|
+
figma.ui.postMessage({ id: msg.id, error: String((e && e.message) || e) });
|
|
728
|
+
}
|
|
729
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Figma MCP Console",
|
|
3
|
+
"id": "figma-mcp-console-dev",
|
|
4
|
+
"api": "1.0.0",
|
|
5
|
+
"main": "code.js",
|
|
6
|
+
"ui": "ui.html",
|
|
7
|
+
"editorType": ["figma"],
|
|
8
|
+
"documentAccess": "dynamic-page",
|
|
9
|
+
"networkAccess": {
|
|
10
|
+
"allowedDomains": ["none"],
|
|
11
|
+
"devAllowedDomains": ["ws://localhost:2000"],
|
|
12
|
+
"reasoning": "Connects to a local MCP bridge server so AI assistants can read and edit this file."
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
<style>
|
|
2
|
+
* {
|
|
3
|
+
box-sizing: border-box;
|
|
4
|
+
}
|
|
5
|
+
html,
|
|
6
|
+
body {
|
|
7
|
+
height: 100%;
|
|
8
|
+
}
|
|
9
|
+
body {
|
|
10
|
+
font:
|
|
11
|
+
12px/1.5 -apple-system,
|
|
12
|
+
BlinkMacSystemFont,
|
|
13
|
+
"Segoe UI",
|
|
14
|
+
sans-serif;
|
|
15
|
+
margin: 0;
|
|
16
|
+
padding: 14px;
|
|
17
|
+
color: #2c2c34;
|
|
18
|
+
background: linear-gradient(160deg, #f6f7fb 0%, #eef0f7 100%);
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-direction: column;
|
|
21
|
+
}
|
|
22
|
+
.card {
|
|
23
|
+
background: #fff;
|
|
24
|
+
border: 1px solid #e6e8f0;
|
|
25
|
+
border-radius: 10px;
|
|
26
|
+
padding: 9px 11px;
|
|
27
|
+
box-shadow: 0 1px 2px rgba(30, 34, 60, 0.04);
|
|
28
|
+
}
|
|
29
|
+
#status-row {
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
gap: 8px;
|
|
33
|
+
cursor: pointer;
|
|
34
|
+
font-weight: 600;
|
|
35
|
+
font-size: 13px;
|
|
36
|
+
color: #1f2330;
|
|
37
|
+
}
|
|
38
|
+
#dot {
|
|
39
|
+
flex: none;
|
|
40
|
+
width: 9px;
|
|
41
|
+
height: 9px;
|
|
42
|
+
border-radius: 50%;
|
|
43
|
+
background: #e0555b;
|
|
44
|
+
box-shadow: 0 0 0 3px rgba(224, 85, 91, 0.15);
|
|
45
|
+
transition:
|
|
46
|
+
background 0.2s,
|
|
47
|
+
box-shadow 0.2s;
|
|
48
|
+
}
|
|
49
|
+
#dot.on {
|
|
50
|
+
background: #22a06b;
|
|
51
|
+
box-shadow: 0 0 0 3px rgba(34, 160, 107, 0.15);
|
|
52
|
+
}
|
|
53
|
+
#hint {
|
|
54
|
+
color: #6b6f7d;
|
|
55
|
+
margin-top: 8px;
|
|
56
|
+
font-size: 11px;
|
|
57
|
+
line-height: 1.45;
|
|
58
|
+
}
|
|
59
|
+
#credit {
|
|
60
|
+
margin-top: auto;
|
|
61
|
+
padding-top: 10px;
|
|
62
|
+
text-align: center;
|
|
63
|
+
color: #a0a4b0;
|
|
64
|
+
font-size: 10px;
|
|
65
|
+
}
|
|
66
|
+
#credit a {
|
|
67
|
+
color: #8288a0;
|
|
68
|
+
text-decoration: none;
|
|
69
|
+
}
|
|
70
|
+
#credit a:hover {
|
|
71
|
+
text-decoration: underline;
|
|
72
|
+
}
|
|
73
|
+
</style>
|
|
74
|
+
|
|
75
|
+
<div class="card">
|
|
76
|
+
<div id="status-row">
|
|
77
|
+
<span id="dot"></span><span id="status">Connecting…</span>
|
|
78
|
+
</div>
|
|
79
|
+
<div id="hint">
|
|
80
|
+
Keep this window open while AI works. Open the plugin in each file you want
|
|
81
|
+
it to edit.
|
|
82
|
+
</div>
|
|
83
|
+
</div>
|
|
84
|
+
<div id="credit">
|
|
85
|
+
<a href="https://github.com/hoangann2000/figma-mcp-console" target="_blank"
|
|
86
|
+
>@hoangann2000</a
|
|
87
|
+
>
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
<script>
|
|
91
|
+
// Bridge UI: connect to the shared MCP bridge on localhost and relay
|
|
92
|
+
// frames between it and the plugin main thread.
|
|
93
|
+
//
|
|
94
|
+
// One bridge serves every plugin window and every MCP session, so there is
|
|
95
|
+
// a single well-known port (also the only one the manifest's
|
|
96
|
+
// devAllowedDomains permits). While disconnected we retry on a short loop;
|
|
97
|
+
// background tabs throttle timers, so a visibility change and a click on
|
|
98
|
+
// the status line also trigger an immediate attempt.
|
|
99
|
+
const PORT = 2000,
|
|
100
|
+
RETRY = 1000;
|
|
101
|
+
let ws = null; // open connection, null while disconnected
|
|
102
|
+
let attempt = null; // socket currently trying to connect
|
|
103
|
+
let reg = null; // {key, name} of this file, sent by the main thread
|
|
104
|
+
|
|
105
|
+
function setStatus(connected, text) {
|
|
106
|
+
document.getElementById("dot").className = connected ? "on" : "";
|
|
107
|
+
document.getElementById("status").textContent = text;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function label() {
|
|
111
|
+
return reg ? "Connected · " + reg.name : "Connected (port " + PORT + ")";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function sendRegister() {
|
|
115
|
+
if (ws && ws.readyState === WebSocket.OPEN && reg) {
|
|
116
|
+
ws.send(JSON.stringify({ register: reg }));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function connect() {
|
|
121
|
+
if (ws || attempt) return;
|
|
122
|
+
let sock;
|
|
123
|
+
try {
|
|
124
|
+
sock = new WebSocket("ws://localhost:" + PORT + "/ws");
|
|
125
|
+
} catch (e) {
|
|
126
|
+
schedule();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
attempt = sock;
|
|
130
|
+
sock.onopen = () => {
|
|
131
|
+
attempt = null;
|
|
132
|
+
ws = sock;
|
|
133
|
+
sendRegister();
|
|
134
|
+
setStatus(true, label());
|
|
135
|
+
};
|
|
136
|
+
sock.onmessage = (e) => {
|
|
137
|
+
const msg = JSON.parse(e.data);
|
|
138
|
+
if (msg.hello) {
|
|
139
|
+
setStatus(true, label());
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// Relay server request to the plugin main thread.
|
|
143
|
+
parent.postMessage({ pluginMessage: msg }, "*");
|
|
144
|
+
};
|
|
145
|
+
sock.onclose = () => {
|
|
146
|
+
if (attempt === sock) attempt = null;
|
|
147
|
+
if (ws === sock) ws = null;
|
|
148
|
+
if (!ws) {
|
|
149
|
+
setStatus(false, "Searching for MCP server…");
|
|
150
|
+
schedule();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
sock.onerror = () => {
|
|
154
|
+
try {
|
|
155
|
+
sock.close();
|
|
156
|
+
} catch (e) {}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let retryTimer = null;
|
|
161
|
+
function schedule() {
|
|
162
|
+
if (retryTimer) return;
|
|
163
|
+
retryTimer = setTimeout(() => {
|
|
164
|
+
retryTimer = null;
|
|
165
|
+
connect();
|
|
166
|
+
}, RETRY);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Relay plugin main-thread frames: the register frame is cached here and
|
|
170
|
+
// re-sent on every reconnect; everything else goes to the server.
|
|
171
|
+
window.onmessage = (e) => {
|
|
172
|
+
const msg = e.data.pluginMessage;
|
|
173
|
+
if (!msg) return;
|
|
174
|
+
if (msg.register) {
|
|
175
|
+
reg = msg.register;
|
|
176
|
+
sendRegister();
|
|
177
|
+
if (ws) setStatus(true, label());
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
181
|
+
ws.send(JSON.stringify(msg));
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
document.getElementById("status-row").onclick = () => connect();
|
|
186
|
+
document.addEventListener("visibilitychange", () => {
|
|
187
|
+
if (!document.hidden && !ws) connect();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
connect();
|
|
191
|
+
</script>
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "figma-mcp-console",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepublishOnly": "cp ../README.md ./README.md"
|
|
6
6
|
},
|
|
7
7
|
"description": "Figma MCP server for AI coding assistants — real-time read/write via a Figma Desktop plugin. No API token, no rate limits.",
|
|
8
8
|
"bin": {
|
|
9
|
-
"figma-mcp-console": "
|
|
9
|
+
"figma-mcp-console": "run.js"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
|
-
"
|
|
12
|
+
"run.js",
|
|
13
|
+
"bin/"
|
|
13
14
|
],
|
|
14
15
|
"engines": {
|
|
15
16
|
"node": ">=18"
|
|
@@ -22,9 +23,5 @@
|
|
|
22
23
|
"cursor",
|
|
23
24
|
"design"
|
|
24
25
|
],
|
|
25
|
-
"repository": {
|
|
26
|
-
"type": "git",
|
|
27
|
-
"url": "git+https://github.com/hoangann2000/figma-mcp-console.git"
|
|
28
|
-
},
|
|
29
26
|
"license": "MIT"
|
|
30
27
|
}
|
package/run.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Chạy Go binary đi kèm trong gói npm — KHÔNG tải mạng/GitHub.
|
|
3
|
+
// stdout là transport MCP → mọi log của wrapper ra stderr.
|
|
4
|
+
"use strict";
|
|
5
|
+
const { spawnSync } = require("child_process");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const os = require("os");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
const plat = process.platform === "win32" ? "windows" : process.platform; // darwin|linux|windows
|
|
11
|
+
const arch = { x64: "amd64", arm64: "arm64" }[process.arch];
|
|
12
|
+
function fail(m) {
|
|
13
|
+
process.stderr.write(`figma-mcp-console: ${m}\n`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
if (!arch || !["darwin", "linux", "windows"].includes(plat))
|
|
17
|
+
fail(`unsupported platform: ${process.platform}/${process.arch}`);
|
|
18
|
+
|
|
19
|
+
const exe = plat === "windows" ? ".exe" : "";
|
|
20
|
+
const binPath = path.join(
|
|
21
|
+
__dirname,
|
|
22
|
+
"bin",
|
|
23
|
+
`${plat}-${arch}`,
|
|
24
|
+
`figma-mcp${exe}`,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// Lệnh con: install-plugin
|
|
28
|
+
if (process.argv[2] === "install-plugin") {
|
|
29
|
+
const srcDir = path.join(__dirname, "bin", "plugin");
|
|
30
|
+
const destDir = path.join(os.homedir(), ".figma-mcp-console", "plugin");
|
|
31
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
32
|
+
for (const f of ["code.js", "ui.html", "manifest.json"])
|
|
33
|
+
fs.copyFileSync(path.join(srcDir, f), path.join(destDir, f));
|
|
34
|
+
process.stdout.write(
|
|
35
|
+
"Plugin đã cài. Trong Figma Desktop:\n" +
|
|
36
|
+
" Plugins → Development → Import plugin from manifest…\n" +
|
|
37
|
+
" Chọn file:\n " +
|
|
38
|
+
path.join(destDir, "manifest.json") +
|
|
39
|
+
"\n",
|
|
40
|
+
);
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!fs.existsSync(binPath)) fail(`không tìm thấy binary cho ${plat}-${arch}`);
|
|
45
|
+
try {
|
|
46
|
+
fs.chmodSync(binPath, 0o755);
|
|
47
|
+
} catch (_) {}
|
|
48
|
+
const r = spawnSync(binPath, process.argv.slice(2), { stdio: "inherit" });
|
|
49
|
+
if (r.error) fail(`không khởi động được server: ${r.error.message}`);
|
|
50
|
+
process.exit(r.status === null ? 1 : r.status);
|
package/bin.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Thin npx wrapper: downloads the figma-mcp Go binary for this platform from
|
|
3
|
-
// GitHub Releases (once, cached in the home directory) and executes it.
|
|
4
|
-
//
|
|
5
|
-
// IMPORTANT: stdout is the MCP JSON-RPC transport — every message from this
|
|
6
|
-
// wrapper must go to stderr.
|
|
7
|
-
|
|
8
|
-
"use strict";
|
|
9
|
-
|
|
10
|
-
const { spawnSync } = require("child_process");
|
|
11
|
-
const fs = require("fs");
|
|
12
|
-
const https = require("https");
|
|
13
|
-
const os = require("os");
|
|
14
|
-
const path = require("path");
|
|
15
|
-
|
|
16
|
-
const pkg = require("./package.json");
|
|
17
|
-
const REPO = "hoangann2000/figma-mcp-console"; // must match the GitHub repo
|
|
18
|
-
|
|
19
|
-
// The GitHub release tag to download the server binary from. Decoupled from
|
|
20
|
-
// pkg.version so npm-only updates (README, wrapper fixes) don't require a new
|
|
21
|
-
// GitHub release. Bump this ONLY when a new vX.Y.Z release exists on GitHub.
|
|
22
|
-
const BINARY_VERSION = "0.2.1";
|
|
23
|
-
|
|
24
|
-
const GOOS = { darwin: "darwin", linux: "linux", win32: "windows" }[process.platform];
|
|
25
|
-
const GOARCH = { x64: "amd64", arm64: "arm64" }[process.arch];
|
|
26
|
-
|
|
27
|
-
function fail(msg) {
|
|
28
|
-
process.stderr.write(`figma-mcp-console: ${msg}\n`);
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!GOOS || !GOARCH) fail(`unsupported platform: ${process.platform}/${process.arch}`);
|
|
33
|
-
if (GOOS === "windows" && GOARCH === "arm64") fail("windows/arm64 builds are not published");
|
|
34
|
-
|
|
35
|
-
const exe = GOOS === "windows" ? ".exe" : "";
|
|
36
|
-
const asset = `figma-mcp_${GOOS}_${GOARCH}${exe}`;
|
|
37
|
-
const url = `https://github.com/${REPO}/releases/download/v${BINARY_VERSION}/${asset}`;
|
|
38
|
-
const cacheDir = path.join(os.homedir(), ".figma-mcp-console", BINARY_VERSION);
|
|
39
|
-
const binPath = path.join(cacheDir, `figma-mcp${exe}`);
|
|
40
|
-
|
|
41
|
-
function download(from, dest, redirects, done) {
|
|
42
|
-
if (redirects > 5) return done(new Error("too many redirects"));
|
|
43
|
-
https
|
|
44
|
-
.get(from, { headers: { "User-Agent": "figma-mcp-console-npx" } }, (res) => {
|
|
45
|
-
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
46
|
-
res.resume();
|
|
47
|
-
return download(res.headers.location, dest, redirects + 1, done);
|
|
48
|
-
}
|
|
49
|
-
if (res.statusCode !== 200) {
|
|
50
|
-
res.resume();
|
|
51
|
-
return done(new Error(`GET ${from} returned ${res.statusCode}`));
|
|
52
|
-
}
|
|
53
|
-
const file = fs.createWriteStream(dest, { mode: 0o755 });
|
|
54
|
-
res.pipe(file);
|
|
55
|
-
file.on("finish", () => file.close(done));
|
|
56
|
-
file.on("error", done);
|
|
57
|
-
})
|
|
58
|
-
.on("error", done);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function run() {
|
|
62
|
-
const result = spawnSync(binPath, process.argv.slice(2), { stdio: "inherit" });
|
|
63
|
-
if (result.error) fail(`failed to start server: ${result.error.message}`);
|
|
64
|
-
process.exit(result.status === null ? 1 : result.status);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (fs.existsSync(binPath)) {
|
|
68
|
-
run();
|
|
69
|
-
} else {
|
|
70
|
-
process.stderr.write(`figma-mcp-console: downloading ${asset} v${BINARY_VERSION}...\n`);
|
|
71
|
-
fs.mkdirSync(cacheDir, { recursive: true });
|
|
72
|
-
// Download to a temp name, then rename, so a concurrently started client
|
|
73
|
-
// never executes a half-written binary.
|
|
74
|
-
const tmp = `${binPath}.${process.pid}.tmp`;
|
|
75
|
-
download(url, tmp, 0, (err) => {
|
|
76
|
-
if (err) {
|
|
77
|
-
try { fs.unlinkSync(tmp); } catch (_) {}
|
|
78
|
-
fail(`download failed: ${err.message}\n from: ${url}\n Check your network, or build from source (see the repo README).`);
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
fs.renameSync(tmp, binPath);
|
|
82
|
-
} catch (e) {
|
|
83
|
-
// Another process may have won the race; that's fine if the file exists.
|
|
84
|
-
try { fs.unlinkSync(tmp); } catch (_) {}
|
|
85
|
-
if (!fs.existsSync(binPath)) fail(`could not install binary: ${e.message}`);
|
|
86
|
-
}
|
|
87
|
-
process.stderr.write("figma-mcp-console: ready\n");
|
|
88
|
-
run();
|
|
89
|
-
});
|
|
90
|
-
}
|