@biolab/talk-to-figma 0.3.3 → 0.4.1

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/dist/cli.js ADDED
@@ -0,0 +1,2760 @@
1
+ #!/usr/bin/env node
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
+ };
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+
12
+ // src/relay.ts
13
+ var relay_exports = {};
14
+ __export(relay_exports, {
15
+ startRelay: () => startRelay
16
+ });
17
+ import { WebSocketServer, WebSocket } from "ws";
18
+ import { createServer } from "http";
19
+ function startRelay() {
20
+ const port = parseInt(process.env.PORT || "3055", 10);
21
+ const httpServer = createServer((req, res) => {
22
+ res.setHeader("Access-Control-Allow-Origin", "*");
23
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
24
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
25
+ if (req.method === "OPTIONS") {
26
+ res.writeHead(204);
27
+ res.end();
28
+ return;
29
+ }
30
+ res.writeHead(200);
31
+ res.end("WebSocket server running");
32
+ });
33
+ const wss = new WebSocketServer({ server: httpServer });
34
+ wss.on("connection", (ws2) => {
35
+ console.log("New client connected");
36
+ ws2.send(
37
+ JSON.stringify({
38
+ type: "system",
39
+ message: "Please join a channel to start chatting"
40
+ })
41
+ );
42
+ ws2.on("message", (raw) => {
43
+ try {
44
+ const data = JSON.parse(raw.toString());
45
+ console.log(`
46
+ === Received message from client ===`);
47
+ console.log(`Type: ${data.type}, Channel: ${data.channel || "N/A"}`);
48
+ if (data.message?.command) {
49
+ console.log(`Command: ${data.message.command}, ID: ${data.id}`);
50
+ } else if (data.message?.result) {
51
+ console.log(`Response: ID: ${data.id}, Has Result: ${!!data.message.result}`);
52
+ }
53
+ console.log(`Full message:`, JSON.stringify(data, null, 2));
54
+ if (data.type === "join") {
55
+ const channelName = data.channel;
56
+ if (!channelName || typeof channelName !== "string") {
57
+ ws2.send(JSON.stringify({ type: "error", message: "Channel name is required" }));
58
+ return;
59
+ }
60
+ if (!channels.has(channelName)) {
61
+ channels.set(channelName, /* @__PURE__ */ new Set());
62
+ }
63
+ const channelClients = channels.get(channelName);
64
+ channelClients.add(ws2);
65
+ console.log(`
66
+ \u2713 Client joined channel "${channelName}" (${channelClients.size} total clients)`);
67
+ ws2.send(
68
+ JSON.stringify({
69
+ type: "system",
70
+ message: `Joined channel: ${channelName}`,
71
+ channel: channelName
72
+ })
73
+ );
74
+ ws2.send(
75
+ JSON.stringify({
76
+ type: "system",
77
+ message: {
78
+ id: data.id,
79
+ result: "Connected to channel: " + channelName
80
+ },
81
+ channel: channelName
82
+ })
83
+ );
84
+ channelClients.forEach((client) => {
85
+ if (client !== ws2 && client.readyState === WebSocket.OPEN) {
86
+ client.send(
87
+ JSON.stringify({
88
+ type: "system",
89
+ message: "A new user has joined the channel",
90
+ channel: channelName
91
+ })
92
+ );
93
+ }
94
+ });
95
+ return;
96
+ }
97
+ if (data.type === "message") {
98
+ const channelName = data.channel;
99
+ if (!channelName || typeof channelName !== "string") {
100
+ ws2.send(JSON.stringify({ type: "error", message: "Channel name is required" }));
101
+ return;
102
+ }
103
+ const channelClients = channels.get(channelName);
104
+ if (!channelClients || !channelClients.has(ws2)) {
105
+ ws2.send(JSON.stringify({ type: "error", message: "You must join the channel first" }));
106
+ return;
107
+ }
108
+ let broadcastCount = 0;
109
+ channelClients.forEach((client) => {
110
+ if (client !== ws2 && client.readyState === WebSocket.OPEN) {
111
+ broadcastCount++;
112
+ const broadcastMessage = {
113
+ type: "broadcast",
114
+ message: data.message,
115
+ sender: "peer",
116
+ channel: channelName
117
+ };
118
+ console.log(`
119
+ === Broadcasting to peer #${broadcastCount} ===`);
120
+ console.log(JSON.stringify(broadcastMessage, null, 2));
121
+ client.send(JSON.stringify(broadcastMessage));
122
+ }
123
+ });
124
+ if (broadcastCount === 0) {
125
+ console.log(`\u26A0\uFE0F No other clients in channel "${channelName}" to receive message!`);
126
+ } else {
127
+ console.log(`\u2713 Broadcast to ${broadcastCount} peer(s) in channel "${channelName}"`);
128
+ }
129
+ }
130
+ } catch (err) {
131
+ console.error("Error handling message:", err);
132
+ }
133
+ });
134
+ ws2.on("close", () => {
135
+ console.log("Client disconnected");
136
+ channels.forEach((clients) => {
137
+ clients.delete(ws2);
138
+ });
139
+ });
140
+ });
141
+ httpServer.listen(port, () => {
142
+ console.log(`WebSocket relay running on port ${port}`);
143
+ });
144
+ }
145
+ var channels;
146
+ var init_relay = __esm({
147
+ "src/relay.ts"() {
148
+ channels = /* @__PURE__ */ new Map();
149
+ }
150
+ });
151
+
152
+ // src/talk_to_figma_mcp/server.ts
153
+ var server_exports = {};
154
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
155
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
156
+ import { z } from "zod";
157
+ import WebSocket2 from "ws";
158
+ import { v4 as uuidv4 } from "uuid";
159
+ function rgbaToHex(color) {
160
+ if (color.startsWith("#")) {
161
+ return color;
162
+ }
163
+ const r = Math.round(color.r * 255);
164
+ const g = Math.round(color.g * 255);
165
+ const b = Math.round(color.b * 255);
166
+ const a = Math.round(color.a * 255);
167
+ return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}${a === 255 ? "" : a.toString(16).padStart(2, "0")}`;
168
+ }
169
+ function filterFigmaNode(node) {
170
+ if (node.type === "VECTOR") {
171
+ return null;
172
+ }
173
+ const filtered = {
174
+ id: node.id,
175
+ name: node.name,
176
+ type: node.type
177
+ };
178
+ if (node.fills && node.fills.length > 0) {
179
+ filtered.fills = node.fills.map((fill) => {
180
+ const processedFill = { ...fill };
181
+ delete processedFill.imageRef;
182
+ if (processedFill.gradientStops) {
183
+ processedFill.gradientStops = processedFill.gradientStops.map((stop) => {
184
+ const processedStop = { ...stop };
185
+ if (processedStop.color) {
186
+ processedStop.color = rgbaToHex(processedStop.color);
187
+ }
188
+ return processedStop;
189
+ });
190
+ }
191
+ if (processedFill.color) {
192
+ processedFill.color = rgbaToHex(processedFill.color);
193
+ }
194
+ return processedFill;
195
+ });
196
+ }
197
+ if (node.strokes && node.strokes.length > 0) {
198
+ filtered.strokes = node.strokes.map((stroke) => {
199
+ const processedStroke = { ...stroke };
200
+ if (processedStroke.color) {
201
+ processedStroke.color = rgbaToHex(processedStroke.color);
202
+ }
203
+ return processedStroke;
204
+ });
205
+ }
206
+ if (node.cornerRadius !== void 0) {
207
+ filtered.cornerRadius = node.cornerRadius;
208
+ }
209
+ if (node.absoluteBoundingBox) {
210
+ filtered.absoluteBoundingBox = node.absoluteBoundingBox;
211
+ }
212
+ if (node.characters) {
213
+ filtered.characters = node.characters;
214
+ }
215
+ if (node.style) {
216
+ filtered.style = {
217
+ fontFamily: node.style.fontFamily,
218
+ fontStyle: node.style.fontStyle,
219
+ fontWeight: node.style.fontWeight,
220
+ fontSize: node.style.fontSize,
221
+ textAlignHorizontal: node.style.textAlignHorizontal,
222
+ letterSpacing: node.style.letterSpacing,
223
+ lineHeightPx: node.style.lineHeightPx
224
+ };
225
+ }
226
+ if (node.children) {
227
+ filtered.children = node.children.map((child) => filterFigmaNode(child)).filter((child) => child !== null);
228
+ }
229
+ return filtered;
230
+ }
231
+ function connectToFigma(port = 3055) {
232
+ if (ws && ws.readyState === WebSocket2.OPEN) {
233
+ logger.info("Already connected to Figma");
234
+ return;
235
+ }
236
+ const wsUrl = serverUrl === "localhost" ? `${WS_URL}:${port}` : WS_URL;
237
+ logger.info(`Connecting to Figma socket server at ${wsUrl}...`);
238
+ ws = new WebSocket2(wsUrl);
239
+ ws.on("open", () => {
240
+ logger.info("Connected to Figma socket server");
241
+ currentChannel = null;
242
+ });
243
+ ws.on("message", (data) => {
244
+ try {
245
+ const json = JSON.parse(data);
246
+ if (json.type === "progress_update") {
247
+ const progressData = json.message.data;
248
+ const requestId = json.id || "";
249
+ if (requestId && pendingRequests.has(requestId)) {
250
+ const request = pendingRequests.get(requestId);
251
+ request.lastActivity = Date.now();
252
+ clearTimeout(request.timeout);
253
+ request.timeout = setTimeout(() => {
254
+ if (pendingRequests.has(requestId)) {
255
+ logger.error(`Request ${requestId} timed out after extended period of inactivity`);
256
+ pendingRequests.delete(requestId);
257
+ request.reject(new Error("Request to Figma timed out"));
258
+ }
259
+ }, 6e4);
260
+ logger.info(`Progress update for ${progressData.commandType}: ${progressData.progress}% - ${progressData.message}`);
261
+ if (progressData.status === "completed" && progressData.progress === 100) {
262
+ logger.info(`Operation ${progressData.commandType} completed, waiting for final result`);
263
+ }
264
+ }
265
+ return;
266
+ }
267
+ const myResponse = json.message;
268
+ logger.debug(`Received message: ${JSON.stringify(myResponse)}`);
269
+ logger.log("myResponse" + JSON.stringify(myResponse));
270
+ if (myResponse.id && pendingRequests.has(myResponse.id) && myResponse.result) {
271
+ const request = pendingRequests.get(myResponse.id);
272
+ clearTimeout(request.timeout);
273
+ if (myResponse.error) {
274
+ logger.error(`Error from Figma: ${myResponse.error}`);
275
+ request.reject(new Error(myResponse.error));
276
+ } else {
277
+ if (myResponse.result) {
278
+ request.resolve(myResponse.result);
279
+ }
280
+ }
281
+ pendingRequests.delete(myResponse.id);
282
+ } else {
283
+ logger.info(`Received broadcast message: ${JSON.stringify(myResponse)}`);
284
+ }
285
+ } catch (error) {
286
+ logger.error(`Error parsing message: ${error instanceof Error ? error.message : String(error)}`);
287
+ }
288
+ });
289
+ ws.on("error", (error) => {
290
+ logger.error(`Socket error: ${error}`);
291
+ });
292
+ ws.on("close", () => {
293
+ logger.info("Disconnected from Figma socket server");
294
+ ws = null;
295
+ for (const [id, request] of pendingRequests.entries()) {
296
+ clearTimeout(request.timeout);
297
+ request.reject(new Error("Connection closed"));
298
+ pendingRequests.delete(id);
299
+ }
300
+ logger.info("Attempting to reconnect in 2 seconds...");
301
+ setTimeout(() => connectToFigma(port), 2e3);
302
+ });
303
+ }
304
+ async function joinChannel(channelName) {
305
+ if (!ws || ws.readyState !== WebSocket2.OPEN) {
306
+ throw new Error("Not connected to Figma");
307
+ }
308
+ try {
309
+ await sendCommandToFigma("join", { channel: channelName });
310
+ currentChannel = channelName;
311
+ logger.info(`Joined channel: ${channelName}`);
312
+ } catch (error) {
313
+ logger.error(`Failed to join channel: ${error instanceof Error ? error.message : String(error)}`);
314
+ throw error;
315
+ }
316
+ }
317
+ function sendCommandToFigma(command, params = {}, timeoutMs = 3e4) {
318
+ return new Promise((resolve, reject) => {
319
+ if (!ws || ws.readyState !== WebSocket2.OPEN) {
320
+ connectToFigma();
321
+ reject(new Error("Not connected to Figma. Attempting to connect..."));
322
+ return;
323
+ }
324
+ const requiresChannel = command !== "join";
325
+ if (requiresChannel && !currentChannel) {
326
+ reject(new Error("Must join a channel before sending commands"));
327
+ return;
328
+ }
329
+ const id = uuidv4();
330
+ const request = {
331
+ id,
332
+ type: command === "join" ? "join" : "message",
333
+ ...command === "join" ? { channel: params.channel } : { channel: currentChannel },
334
+ message: {
335
+ id,
336
+ command,
337
+ params: {
338
+ ...params,
339
+ commandId: id
340
+ // Include the command ID in params
341
+ }
342
+ }
343
+ };
344
+ const timeout = setTimeout(() => {
345
+ if (pendingRequests.has(id)) {
346
+ pendingRequests.delete(id);
347
+ logger.error(`Request ${id} to Figma timed out after ${timeoutMs / 1e3} seconds`);
348
+ reject(new Error("Request to Figma timed out"));
349
+ }
350
+ }, timeoutMs);
351
+ pendingRequests.set(id, {
352
+ resolve,
353
+ reject,
354
+ timeout,
355
+ lastActivity: Date.now()
356
+ });
357
+ logger.info(`Sending command to Figma: ${command}`);
358
+ logger.debug(`Request details: ${JSON.stringify(request)}`);
359
+ ws.send(JSON.stringify(request));
360
+ });
361
+ }
362
+ async function main() {
363
+ try {
364
+ connectToFigma();
365
+ } catch (error) {
366
+ logger.warn(`Could not connect to Figma initially: ${error instanceof Error ? error.message : String(error)}`);
367
+ logger.warn("Will try to connect when the first command is sent");
368
+ }
369
+ const transport = new StdioServerTransport();
370
+ await server.connect(transport);
371
+ logger.info("FigmaMCP server running on stdio");
372
+ }
373
+ var logger, ws, pendingRequests, currentChannel, server, args, serverArg, serverUrl, WS_URL;
374
+ var init_server = __esm({
375
+ "src/talk_to_figma_mcp/server.ts"() {
376
+ logger = {
377
+ info: (message) => process.stderr.write(`[INFO] ${message}
378
+ `),
379
+ debug: (message) => process.stderr.write(`[DEBUG] ${message}
380
+ `),
381
+ warn: (message) => process.stderr.write(`[WARN] ${message}
382
+ `),
383
+ error: (message) => process.stderr.write(`[ERROR] ${message}
384
+ `),
385
+ log: (message) => process.stderr.write(`[LOG] ${message}
386
+ `)
387
+ };
388
+ ws = null;
389
+ pendingRequests = /* @__PURE__ */ new Map();
390
+ currentChannel = null;
391
+ server = new McpServer({
392
+ name: "TalkToFigmaMCP",
393
+ version: "1.0.0"
394
+ });
395
+ args = process.argv.slice(2);
396
+ serverArg = args.find((arg) => arg.startsWith("--server="));
397
+ serverUrl = serverArg ? serverArg.split("=")[1] : "localhost";
398
+ WS_URL = serverUrl === "localhost" ? `ws://${serverUrl}` : `wss://${serverUrl}`;
399
+ server.tool(
400
+ "get_document_info",
401
+ "Get detailed information about the current Figma document",
402
+ {},
403
+ async () => {
404
+ try {
405
+ const result = await sendCommandToFigma("get_document_info");
406
+ return {
407
+ content: [
408
+ {
409
+ type: "text",
410
+ text: JSON.stringify(result)
411
+ }
412
+ ]
413
+ };
414
+ } catch (error) {
415
+ return {
416
+ content: [
417
+ {
418
+ type: "text",
419
+ text: `Error getting document info: ${error instanceof Error ? error.message : String(error)}`
420
+ }
421
+ ]
422
+ };
423
+ }
424
+ }
425
+ );
426
+ server.tool(
427
+ "get_selection",
428
+ "Get information about the current selection in Figma",
429
+ {},
430
+ async () => {
431
+ try {
432
+ const result = await sendCommandToFigma("get_selection");
433
+ return {
434
+ content: [
435
+ {
436
+ type: "text",
437
+ text: JSON.stringify(result)
438
+ }
439
+ ]
440
+ };
441
+ } catch (error) {
442
+ return {
443
+ content: [
444
+ {
445
+ type: "text",
446
+ text: `Error getting selection: ${error instanceof Error ? error.message : String(error)}`
447
+ }
448
+ ]
449
+ };
450
+ }
451
+ }
452
+ );
453
+ server.tool(
454
+ "read_my_design",
455
+ "Get detailed information about the current selection in Figma, including all node details",
456
+ {},
457
+ async () => {
458
+ try {
459
+ const result = await sendCommandToFigma("read_my_design", {});
460
+ return {
461
+ content: [
462
+ {
463
+ type: "text",
464
+ text: JSON.stringify(result)
465
+ }
466
+ ]
467
+ };
468
+ } catch (error) {
469
+ return {
470
+ content: [
471
+ {
472
+ type: "text",
473
+ text: `Error getting node info: ${error instanceof Error ? error.message : String(error)}`
474
+ }
475
+ ]
476
+ };
477
+ }
478
+ }
479
+ );
480
+ server.tool(
481
+ "get_node_info",
482
+ "Get detailed information about a specific node in Figma",
483
+ {
484
+ nodeId: z.string().describe("The ID of the node to get information about")
485
+ },
486
+ async ({ nodeId }) => {
487
+ try {
488
+ const result = await sendCommandToFigma("get_node_info", { nodeId });
489
+ return {
490
+ content: [
491
+ {
492
+ type: "text",
493
+ text: JSON.stringify(filterFigmaNode(result))
494
+ }
495
+ ]
496
+ };
497
+ } catch (error) {
498
+ return {
499
+ content: [
500
+ {
501
+ type: "text",
502
+ text: `Error getting node info: ${error instanceof Error ? error.message : String(error)}`
503
+ }
504
+ ]
505
+ };
506
+ }
507
+ }
508
+ );
509
+ server.tool(
510
+ "get_nodes_info",
511
+ "Get detailed information about multiple nodes in Figma",
512
+ {
513
+ nodeIds: z.array(z.string()).describe("Array of node IDs to get information about")
514
+ },
515
+ async ({ nodeIds }) => {
516
+ try {
517
+ const results = await Promise.all(
518
+ nodeIds.map(async (nodeId) => {
519
+ const result = await sendCommandToFigma("get_node_info", { nodeId });
520
+ return { nodeId, info: result };
521
+ })
522
+ );
523
+ return {
524
+ content: [
525
+ {
526
+ type: "text",
527
+ text: JSON.stringify(results.map((result) => filterFigmaNode(result.info)))
528
+ }
529
+ ]
530
+ };
531
+ } catch (error) {
532
+ return {
533
+ content: [
534
+ {
535
+ type: "text",
536
+ text: `Error getting nodes info: ${error instanceof Error ? error.message : String(error)}`
537
+ }
538
+ ]
539
+ };
540
+ }
541
+ }
542
+ );
543
+ server.tool(
544
+ "create_rectangle",
545
+ "Create a new rectangle in Figma",
546
+ {
547
+ x: z.number().describe("X position"),
548
+ y: z.number().describe("Y position"),
549
+ width: z.number().describe("Width of the rectangle"),
550
+ height: z.number().describe("Height of the rectangle"),
551
+ name: z.string().optional().describe("Optional name for the rectangle"),
552
+ parentId: z.string().optional().describe("Optional parent node ID to append the rectangle to")
553
+ },
554
+ async ({ x, y, width, height, name, parentId }) => {
555
+ try {
556
+ const result = await sendCommandToFigma("create_rectangle", {
557
+ x,
558
+ y,
559
+ width,
560
+ height,
561
+ name: name || "Rectangle",
562
+ parentId
563
+ });
564
+ return {
565
+ content: [
566
+ {
567
+ type: "text",
568
+ text: `Created rectangle "${JSON.stringify(result)}"`
569
+ }
570
+ ]
571
+ };
572
+ } catch (error) {
573
+ return {
574
+ content: [
575
+ {
576
+ type: "text",
577
+ text: `Error creating rectangle: ${error instanceof Error ? error.message : String(error)}`
578
+ }
579
+ ]
580
+ };
581
+ }
582
+ }
583
+ );
584
+ server.tool(
585
+ "create_frame",
586
+ "Create a new frame in Figma",
587
+ {
588
+ x: z.number().describe("X position"),
589
+ y: z.number().describe("Y position"),
590
+ width: z.number().describe("Width of the frame"),
591
+ height: z.number().describe("Height of the frame"),
592
+ name: z.string().optional().describe("Optional name for the frame"),
593
+ parentId: z.string().optional().describe("Optional parent node ID to append the frame to"),
594
+ fillColor: z.object({
595
+ r: z.number().min(0).max(1).describe("Red component (0-1)"),
596
+ g: z.number().min(0).max(1).describe("Green component (0-1)"),
597
+ b: z.number().min(0).max(1).describe("Blue component (0-1)"),
598
+ a: z.number().min(0).max(1).optional().describe("Alpha component (0-1)")
599
+ }).optional().describe("Fill color in RGBA format"),
600
+ strokeColor: z.object({
601
+ r: z.number().min(0).max(1).describe("Red component (0-1)"),
602
+ g: z.number().min(0).max(1).describe("Green component (0-1)"),
603
+ b: z.number().min(0).max(1).describe("Blue component (0-1)"),
604
+ a: z.number().min(0).max(1).optional().describe("Alpha component (0-1)")
605
+ }).optional().describe("Stroke color in RGBA format"),
606
+ strokeWeight: z.number().positive().optional().describe("Stroke weight"),
607
+ layoutMode: z.enum(["NONE", "HORIZONTAL", "VERTICAL"]).optional().describe("Auto-layout mode for the frame"),
608
+ layoutWrap: z.enum(["NO_WRAP", "WRAP"]).optional().describe("Whether the auto-layout frame wraps its children"),
609
+ paddingTop: z.number().optional().describe("Top padding for auto-layout frame"),
610
+ paddingRight: z.number().optional().describe("Right padding for auto-layout frame"),
611
+ paddingBottom: z.number().optional().describe("Bottom padding for auto-layout frame"),
612
+ paddingLeft: z.number().optional().describe("Left padding for auto-layout frame"),
613
+ primaryAxisAlignItems: z.enum(["MIN", "MAX", "CENTER", "SPACE_BETWEEN"]).optional().describe("Primary axis alignment for auto-layout frame. Note: When set to SPACE_BETWEEN, itemSpacing will be ignored as children will be evenly spaced."),
614
+ counterAxisAlignItems: z.enum(["MIN", "MAX", "CENTER", "BASELINE"]).optional().describe("Counter axis alignment for auto-layout frame"),
615
+ layoutSizingHorizontal: z.enum(["FIXED", "HUG", "FILL"]).optional().describe("Horizontal sizing mode for auto-layout frame"),
616
+ layoutSizingVertical: z.enum(["FIXED", "HUG", "FILL"]).optional().describe("Vertical sizing mode for auto-layout frame"),
617
+ itemSpacing: z.number().optional().describe("Distance between children in auto-layout frame. Note: This value will be ignored if primaryAxisAlignItems is set to SPACE_BETWEEN.")
618
+ },
619
+ async ({
620
+ x,
621
+ y,
622
+ width,
623
+ height,
624
+ name,
625
+ parentId,
626
+ fillColor,
627
+ strokeColor,
628
+ strokeWeight,
629
+ layoutMode,
630
+ layoutWrap,
631
+ paddingTop,
632
+ paddingRight,
633
+ paddingBottom,
634
+ paddingLeft,
635
+ primaryAxisAlignItems,
636
+ counterAxisAlignItems,
637
+ layoutSizingHorizontal,
638
+ layoutSizingVertical,
639
+ itemSpacing
640
+ }) => {
641
+ try {
642
+ const result = await sendCommandToFigma("create_frame", {
643
+ x,
644
+ y,
645
+ width,
646
+ height,
647
+ name: name || "Frame",
648
+ parentId,
649
+ fillColor: fillColor || { r: 1, g: 1, b: 1, a: 1 },
650
+ strokeColor,
651
+ strokeWeight,
652
+ layoutMode,
653
+ layoutWrap,
654
+ paddingTop,
655
+ paddingRight,
656
+ paddingBottom,
657
+ paddingLeft,
658
+ primaryAxisAlignItems,
659
+ counterAxisAlignItems,
660
+ layoutSizingHorizontal,
661
+ layoutSizingVertical,
662
+ itemSpacing
663
+ });
664
+ const typedResult = result;
665
+ return {
666
+ content: [
667
+ {
668
+ type: "text",
669
+ text: `Created frame "${typedResult.name}" with ID: ${typedResult.id}. Use the ID as the parentId to appendChild inside this frame.`
670
+ }
671
+ ]
672
+ };
673
+ } catch (error) {
674
+ return {
675
+ content: [
676
+ {
677
+ type: "text",
678
+ text: `Error creating frame: ${error instanceof Error ? error.message : String(error)}`
679
+ }
680
+ ]
681
+ };
682
+ }
683
+ }
684
+ );
685
+ server.tool(
686
+ "create_text",
687
+ "Create a new text element in Figma",
688
+ {
689
+ x: z.number().describe("X position"),
690
+ y: z.number().describe("Y position"),
691
+ text: z.string().describe("Text content"),
692
+ fontSize: z.number().optional().describe("Font size (default: 14)"),
693
+ fontWeight: z.number().optional().describe("Font weight (e.g., 400 for Regular, 700 for Bold)"),
694
+ fontColor: z.object({
695
+ r: z.number().min(0).max(1).describe("Red component (0-1)"),
696
+ g: z.number().min(0).max(1).describe("Green component (0-1)"),
697
+ b: z.number().min(0).max(1).describe("Blue component (0-1)"),
698
+ a: z.number().min(0).max(1).optional().describe("Alpha component (0-1)")
699
+ }).optional().describe("Font color in RGBA format"),
700
+ name: z.string().optional().describe("Semantic layer name for the text node"),
701
+ parentId: z.string().optional().describe("Optional parent node ID to append the text to")
702
+ },
703
+ async ({ x, y, text, fontSize, fontWeight, fontColor, name, parentId }) => {
704
+ try {
705
+ const result = await sendCommandToFigma("create_text", {
706
+ x,
707
+ y,
708
+ text,
709
+ fontSize: fontSize || 14,
710
+ fontWeight: fontWeight || 400,
711
+ fontColor: fontColor || { r: 0, g: 0, b: 0, a: 1 },
712
+ name: name || "Text",
713
+ parentId
714
+ });
715
+ const typedResult = result;
716
+ return {
717
+ content: [
718
+ {
719
+ type: "text",
720
+ text: `Created text "${typedResult.name}" with ID: ${typedResult.id}`
721
+ }
722
+ ]
723
+ };
724
+ } catch (error) {
725
+ return {
726
+ content: [
727
+ {
728
+ type: "text",
729
+ text: `Error creating text: ${error instanceof Error ? error.message : String(error)}`
730
+ }
731
+ ]
732
+ };
733
+ }
734
+ }
735
+ );
736
+ server.tool(
737
+ "set_fill_color",
738
+ "Set the fill color of a node in Figma can be TextNode or FrameNode",
739
+ {
740
+ nodeId: z.string().describe("The ID of the node to modify"),
741
+ r: z.number().min(0).max(1).describe("Red component (0-1)"),
742
+ g: z.number().min(0).max(1).describe("Green component (0-1)"),
743
+ b: z.number().min(0).max(1).describe("Blue component (0-1)"),
744
+ a: z.number().min(0).max(1).optional().describe("Alpha component (0-1)")
745
+ },
746
+ async ({ nodeId, r, g, b, a }) => {
747
+ try {
748
+ const result = await sendCommandToFigma("set_fill_color", {
749
+ nodeId,
750
+ color: { r, g, b, a: a || 1 }
751
+ });
752
+ const typedResult = result;
753
+ return {
754
+ content: [
755
+ {
756
+ type: "text",
757
+ text: `Set fill color of node "${typedResult.name}" to RGBA(${r}, ${g}, ${b}, ${a || 1})`
758
+ }
759
+ ]
760
+ };
761
+ } catch (error) {
762
+ return {
763
+ content: [
764
+ {
765
+ type: "text",
766
+ text: `Error setting fill color: ${error instanceof Error ? error.message : String(error)}`
767
+ }
768
+ ]
769
+ };
770
+ }
771
+ }
772
+ );
773
+ server.tool(
774
+ "set_stroke_color",
775
+ "Set the stroke color of a node in Figma",
776
+ {
777
+ nodeId: z.string().describe("The ID of the node to modify"),
778
+ r: z.number().min(0).max(1).describe("Red component (0-1)"),
779
+ g: z.number().min(0).max(1).describe("Green component (0-1)"),
780
+ b: z.number().min(0).max(1).describe("Blue component (0-1)"),
781
+ a: z.number().min(0).max(1).optional().describe("Alpha component (0-1)"),
782
+ weight: z.number().positive().optional().describe("Stroke weight")
783
+ },
784
+ async ({ nodeId, r, g, b, a, weight }) => {
785
+ try {
786
+ const result = await sendCommandToFigma("set_stroke_color", {
787
+ nodeId,
788
+ color: { r, g, b, a: a || 1 },
789
+ weight: weight || 1
790
+ });
791
+ const typedResult = result;
792
+ return {
793
+ content: [
794
+ {
795
+ type: "text",
796
+ text: `Set stroke color of node "${typedResult.name}" to RGBA(${r}, ${g}, ${b}, ${a || 1}) with weight ${weight || 1}`
797
+ }
798
+ ]
799
+ };
800
+ } catch (error) {
801
+ return {
802
+ content: [
803
+ {
804
+ type: "text",
805
+ text: `Error setting stroke color: ${error instanceof Error ? error.message : String(error)}`
806
+ }
807
+ ]
808
+ };
809
+ }
810
+ }
811
+ );
812
+ server.tool(
813
+ "move_node",
814
+ "Move a node to a new position in Figma",
815
+ {
816
+ nodeId: z.string().describe("The ID of the node to move"),
817
+ x: z.number().describe("New X position"),
818
+ y: z.number().describe("New Y position")
819
+ },
820
+ async ({ nodeId, x, y }) => {
821
+ try {
822
+ const result = await sendCommandToFigma("move_node", { nodeId, x, y });
823
+ const typedResult = result;
824
+ return {
825
+ content: [
826
+ {
827
+ type: "text",
828
+ text: `Moved node "${typedResult.name}" to position (${x}, ${y})`
829
+ }
830
+ ]
831
+ };
832
+ } catch (error) {
833
+ return {
834
+ content: [
835
+ {
836
+ type: "text",
837
+ text: `Error moving node: ${error instanceof Error ? error.message : String(error)}`
838
+ }
839
+ ]
840
+ };
841
+ }
842
+ }
843
+ );
844
+ server.tool(
845
+ "clone_node",
846
+ "Clone an existing node in Figma",
847
+ {
848
+ nodeId: z.string().describe("The ID of the node to clone"),
849
+ x: z.number().optional().describe("New X position for the clone"),
850
+ y: z.number().optional().describe("New Y position for the clone")
851
+ },
852
+ async ({ nodeId, x, y }) => {
853
+ try {
854
+ const result = await sendCommandToFigma("clone_node", { nodeId, x, y });
855
+ const typedResult = result;
856
+ return {
857
+ content: [
858
+ {
859
+ type: "text",
860
+ text: `Cloned node "${typedResult.name}" with new ID: ${typedResult.id}${x !== void 0 && y !== void 0 ? ` at position (${x}, ${y})` : ""}`
861
+ }
862
+ ]
863
+ };
864
+ } catch (error) {
865
+ return {
866
+ content: [
867
+ {
868
+ type: "text",
869
+ text: `Error cloning node: ${error instanceof Error ? error.message : String(error)}`
870
+ }
871
+ ]
872
+ };
873
+ }
874
+ }
875
+ );
876
+ server.tool(
877
+ "resize_node",
878
+ "Resize a node in Figma",
879
+ {
880
+ nodeId: z.string().describe("The ID of the node to resize"),
881
+ width: z.number().positive().describe("New width"),
882
+ height: z.number().positive().describe("New height")
883
+ },
884
+ async ({ nodeId, width, height }) => {
885
+ try {
886
+ const result = await sendCommandToFigma("resize_node", {
887
+ nodeId,
888
+ width,
889
+ height
890
+ });
891
+ const typedResult = result;
892
+ return {
893
+ content: [
894
+ {
895
+ type: "text",
896
+ text: `Resized node "${typedResult.name}" to width ${width} and height ${height}`
897
+ }
898
+ ]
899
+ };
900
+ } catch (error) {
901
+ return {
902
+ content: [
903
+ {
904
+ type: "text",
905
+ text: `Error resizing node: ${error instanceof Error ? error.message : String(error)}`
906
+ }
907
+ ]
908
+ };
909
+ }
910
+ }
911
+ );
912
+ server.tool(
913
+ "delete_node",
914
+ "Delete a node from Figma",
915
+ {
916
+ nodeId: z.string().describe("The ID of the node to delete")
917
+ },
918
+ async ({ nodeId }) => {
919
+ try {
920
+ await sendCommandToFigma("delete_node", { nodeId });
921
+ return {
922
+ content: [
923
+ {
924
+ type: "text",
925
+ text: `Deleted node with ID: ${nodeId}`
926
+ }
927
+ ]
928
+ };
929
+ } catch (error) {
930
+ return {
931
+ content: [
932
+ {
933
+ type: "text",
934
+ text: `Error deleting node: ${error instanceof Error ? error.message : String(error)}`
935
+ }
936
+ ]
937
+ };
938
+ }
939
+ }
940
+ );
941
+ server.tool(
942
+ "delete_multiple_nodes",
943
+ "Delete multiple nodes from Figma at once",
944
+ {
945
+ nodeIds: z.array(z.string()).describe("Array of node IDs to delete")
946
+ },
947
+ async ({ nodeIds }) => {
948
+ try {
949
+ const result = await sendCommandToFigma("delete_multiple_nodes", { nodeIds });
950
+ return {
951
+ content: [
952
+ {
953
+ type: "text",
954
+ text: JSON.stringify(result)
955
+ }
956
+ ]
957
+ };
958
+ } catch (error) {
959
+ return {
960
+ content: [
961
+ {
962
+ type: "text",
963
+ text: `Error deleting multiple nodes: ${error instanceof Error ? error.message : String(error)}`
964
+ }
965
+ ]
966
+ };
967
+ }
968
+ }
969
+ );
970
+ server.tool(
971
+ "export_node_as_image",
972
+ "Export a node as an image from Figma",
973
+ {
974
+ nodeId: z.string().describe("The ID of the node to export"),
975
+ format: z.enum(["PNG", "JPG", "SVG", "PDF"]).optional().describe("Export format"),
976
+ scale: z.number().positive().optional().describe("Export scale")
977
+ },
978
+ async ({ nodeId, format, scale }) => {
979
+ try {
980
+ const result = await sendCommandToFigma("export_node_as_image", {
981
+ nodeId,
982
+ format: format || "PNG",
983
+ scale: scale || 1
984
+ });
985
+ const typedResult = result;
986
+ return {
987
+ content: [
988
+ {
989
+ type: "image",
990
+ data: typedResult.imageData,
991
+ mimeType: typedResult.mimeType || "image/png"
992
+ }
993
+ ]
994
+ };
995
+ } catch (error) {
996
+ return {
997
+ content: [
998
+ {
999
+ type: "text",
1000
+ text: `Error exporting node as image: ${error instanceof Error ? error.message : String(error)}`
1001
+ }
1002
+ ]
1003
+ };
1004
+ }
1005
+ }
1006
+ );
1007
+ server.tool(
1008
+ "set_text_content",
1009
+ "Set the text content of an existing text node in Figma",
1010
+ {
1011
+ nodeId: z.string().describe("The ID of the text node to modify"),
1012
+ text: z.string().describe("New text content")
1013
+ },
1014
+ async ({ nodeId, text }) => {
1015
+ try {
1016
+ const result = await sendCommandToFigma("set_text_content", {
1017
+ nodeId,
1018
+ text
1019
+ });
1020
+ const typedResult = result;
1021
+ return {
1022
+ content: [
1023
+ {
1024
+ type: "text",
1025
+ text: `Updated text content of node "${typedResult.name}" to "${text}"`
1026
+ }
1027
+ ]
1028
+ };
1029
+ } catch (error) {
1030
+ return {
1031
+ content: [
1032
+ {
1033
+ type: "text",
1034
+ text: `Error setting text content: ${error instanceof Error ? error.message : String(error)}`
1035
+ }
1036
+ ]
1037
+ };
1038
+ }
1039
+ }
1040
+ );
1041
+ server.tool(
1042
+ "get_styles",
1043
+ "Get all styles from the current Figma document",
1044
+ {},
1045
+ async () => {
1046
+ try {
1047
+ const result = await sendCommandToFigma("get_styles");
1048
+ return {
1049
+ content: [
1050
+ {
1051
+ type: "text",
1052
+ text: JSON.stringify(result)
1053
+ }
1054
+ ]
1055
+ };
1056
+ } catch (error) {
1057
+ return {
1058
+ content: [
1059
+ {
1060
+ type: "text",
1061
+ text: `Error getting styles: ${error instanceof Error ? error.message : String(error)}`
1062
+ }
1063
+ ]
1064
+ };
1065
+ }
1066
+ }
1067
+ );
1068
+ server.tool(
1069
+ "get_local_components",
1070
+ "Get all local components from the Figma document",
1071
+ {},
1072
+ async () => {
1073
+ try {
1074
+ const result = await sendCommandToFigma("get_local_components");
1075
+ return {
1076
+ content: [
1077
+ {
1078
+ type: "text",
1079
+ text: JSON.stringify(result)
1080
+ }
1081
+ ]
1082
+ };
1083
+ } catch (error) {
1084
+ return {
1085
+ content: [
1086
+ {
1087
+ type: "text",
1088
+ text: `Error getting local components: ${error instanceof Error ? error.message : String(error)}`
1089
+ }
1090
+ ]
1091
+ };
1092
+ }
1093
+ }
1094
+ );
1095
+ server.tool(
1096
+ "get_local_variables",
1097
+ "Get all local variables and variable collections from the Figma document, including values per mode",
1098
+ {},
1099
+ async () => {
1100
+ try {
1101
+ const result = await sendCommandToFigma("get_local_variables");
1102
+ return {
1103
+ content: [{ type: "text", text: JSON.stringify(result) }]
1104
+ };
1105
+ } catch (error) {
1106
+ return {
1107
+ content: [{ type: "text", text: `Error getting local variables: ${error instanceof Error ? error.message : String(error)}` }]
1108
+ };
1109
+ }
1110
+ }
1111
+ );
1112
+ server.tool(
1113
+ "get_local_variable_collections",
1114
+ "Get all local variable collections with their modes",
1115
+ {},
1116
+ async () => {
1117
+ try {
1118
+ const result = await sendCommandToFigma("get_local_variable_collections");
1119
+ return {
1120
+ content: [{ type: "text", text: JSON.stringify(result) }]
1121
+ };
1122
+ } catch (error) {
1123
+ return {
1124
+ content: [{ type: "text", text: `Error getting local variable collections: ${error instanceof Error ? error.message : String(error)}` }]
1125
+ };
1126
+ }
1127
+ }
1128
+ );
1129
+ server.tool(
1130
+ "get_variable_by_id",
1131
+ "Get detailed information about a specific variable by its ID, including values per mode, scopes, and code syntax",
1132
+ {
1133
+ variableId: z.string().describe("The ID of the variable to retrieve")
1134
+ },
1135
+ async ({ variableId }) => {
1136
+ try {
1137
+ const result = await sendCommandToFigma("get_variable_by_id", { variableId });
1138
+ return {
1139
+ content: [{ type: "text", text: JSON.stringify(result) }]
1140
+ };
1141
+ } catch (error) {
1142
+ return {
1143
+ content: [{ type: "text", text: `Error getting variable: ${error instanceof Error ? error.message : String(error)}` }]
1144
+ };
1145
+ }
1146
+ }
1147
+ );
1148
+ server.tool(
1149
+ "create_variable_collection",
1150
+ "Create a new variable collection in the Figma document",
1151
+ {
1152
+ name: z.string().describe("Name for the new variable collection")
1153
+ },
1154
+ async ({ name }) => {
1155
+ try {
1156
+ const result = await sendCommandToFigma("create_variable_collection", { name });
1157
+ return {
1158
+ content: [{ type: "text", text: JSON.stringify(result) }]
1159
+ };
1160
+ } catch (error) {
1161
+ return {
1162
+ content: [{ type: "text", text: `Error creating variable collection: ${error instanceof Error ? error.message : String(error)}` }]
1163
+ };
1164
+ }
1165
+ }
1166
+ );
1167
+ server.tool(
1168
+ "create_variable",
1169
+ "Create a new variable in a variable collection",
1170
+ {
1171
+ name: z.string().describe("Name for the new variable"),
1172
+ collectionId: z.string().describe("ID of the variable collection to add the variable to"),
1173
+ resolvedType: z.enum(["COLOR", "FLOAT", "STRING", "BOOLEAN"]).describe("The resolved type of the variable")
1174
+ },
1175
+ async ({ name, collectionId, resolvedType }) => {
1176
+ try {
1177
+ const result = await sendCommandToFigma("create_variable", { name, collectionId, resolvedType });
1178
+ return {
1179
+ content: [{ type: "text", text: JSON.stringify(result) }]
1180
+ };
1181
+ } catch (error) {
1182
+ return {
1183
+ content: [{ type: "text", text: `Error creating variable: ${error instanceof Error ? error.message : String(error)}` }]
1184
+ };
1185
+ }
1186
+ }
1187
+ );
1188
+ server.tool(
1189
+ "set_variable_value",
1190
+ "Set the value of a variable for a specific mode. For COLOR type use {r, g, b, a} with 0-1 range. For FLOAT use a number. For STRING use a string. For BOOLEAN use true/false.",
1191
+ {
1192
+ variableId: z.string().describe("The ID of the variable"),
1193
+ modeId: z.string().describe("The mode ID to set the value for"),
1194
+ value: z.any().describe("The value to set - type depends on variable resolvedType (COLOR: {r,g,b,a}, FLOAT: number, STRING: string, BOOLEAN: boolean)")
1195
+ },
1196
+ async ({ variableId, modeId, value }) => {
1197
+ try {
1198
+ const result = await sendCommandToFigma("set_variable_value", { variableId, modeId, value });
1199
+ return {
1200
+ content: [{ type: "text", text: JSON.stringify(result) }]
1201
+ };
1202
+ } catch (error) {
1203
+ return {
1204
+ content: [{ type: "text", text: `Error setting variable value: ${error instanceof Error ? error.message : String(error)}` }]
1205
+ };
1206
+ }
1207
+ }
1208
+ );
1209
+ server.tool(
1210
+ "set_variable_mode_name",
1211
+ "Rename a mode in a variable collection",
1212
+ {
1213
+ collectionId: z.string().describe("The ID of the variable collection"),
1214
+ modeId: z.string().describe("The ID of the mode to rename"),
1215
+ newName: z.string().describe("The new name for the mode")
1216
+ },
1217
+ async ({ collectionId, modeId, newName }) => {
1218
+ try {
1219
+ const result = await sendCommandToFigma("set_variable_mode_name", { collectionId, modeId, newName });
1220
+ return {
1221
+ content: [{ type: "text", text: JSON.stringify(result) }]
1222
+ };
1223
+ } catch (error) {
1224
+ return {
1225
+ content: [{ type: "text", text: `Error renaming variable mode: ${error instanceof Error ? error.message : String(error)}` }]
1226
+ };
1227
+ }
1228
+ }
1229
+ );
1230
+ server.tool(
1231
+ "set_variable_binding",
1232
+ "Bind a variable to a node property. Fields include 'fills/0/color', 'strokes/0/color', 'width', 'height', 'itemSpacing', 'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom', etc.",
1233
+ {
1234
+ nodeId: z.string().describe("The ID of the node to bind the variable to"),
1235
+ field: z.string().describe("The property field to bind (e.g. 'fills/0/color', 'width', 'itemSpacing')"),
1236
+ variableId: z.string().describe("The ID of the variable to bind")
1237
+ },
1238
+ async ({ nodeId, field, variableId }) => {
1239
+ try {
1240
+ const result = await sendCommandToFigma("set_variable_binding", { nodeId, field, variableId });
1241
+ return {
1242
+ content: [{ type: "text", text: JSON.stringify(result) }]
1243
+ };
1244
+ } catch (error) {
1245
+ return {
1246
+ content: [{ type: "text", text: `Error setting variable binding: ${error instanceof Error ? error.message : String(error)}` }]
1247
+ };
1248
+ }
1249
+ }
1250
+ );
1251
+ server.tool(
1252
+ "get_team_library_components",
1253
+ "Get all available components from team libraries",
1254
+ {},
1255
+ async () => {
1256
+ try {
1257
+ const result = await sendCommandToFigma("get_team_library_components");
1258
+ return {
1259
+ content: [{ type: "text", text: JSON.stringify(result) }]
1260
+ };
1261
+ } catch (error) {
1262
+ return {
1263
+ content: [{ type: "text", text: `Error getting team library components: ${error instanceof Error ? error.message : String(error)}` }]
1264
+ };
1265
+ }
1266
+ }
1267
+ );
1268
+ server.tool(
1269
+ "get_team_library_variables",
1270
+ "Get all available variables from team libraries",
1271
+ {},
1272
+ async () => {
1273
+ try {
1274
+ const result = await sendCommandToFigma("get_team_library_variables");
1275
+ return {
1276
+ content: [{ type: "text", text: JSON.stringify(result) }]
1277
+ };
1278
+ } catch (error) {
1279
+ return {
1280
+ content: [{ type: "text", text: `Error getting team library variables: ${error instanceof Error ? error.message : String(error)}` }]
1281
+ };
1282
+ }
1283
+ }
1284
+ );
1285
+ server.tool(
1286
+ "import_variable_by_key",
1287
+ "Import a variable from a team library by its key",
1288
+ {
1289
+ key: z.string().describe("The key of the variable to import from the team library")
1290
+ },
1291
+ async ({ key }) => {
1292
+ try {
1293
+ const result = await sendCommandToFigma("import_variable_by_key", { key });
1294
+ return {
1295
+ content: [{ type: "text", text: JSON.stringify(result) }]
1296
+ };
1297
+ } catch (error) {
1298
+ return {
1299
+ content: [{ type: "text", text: `Error importing variable: ${error instanceof Error ? error.message : String(error)}` }]
1300
+ };
1301
+ }
1302
+ }
1303
+ );
1304
+ server.tool(
1305
+ "get_annotations",
1306
+ "Get all annotations in the current document or specific node",
1307
+ {
1308
+ nodeId: z.string().describe("node ID to get annotations for specific node"),
1309
+ includeCategories: z.boolean().optional().default(true).describe("Whether to include category information")
1310
+ },
1311
+ async ({ nodeId, includeCategories }) => {
1312
+ try {
1313
+ const result = await sendCommandToFigma("get_annotations", {
1314
+ nodeId,
1315
+ includeCategories
1316
+ });
1317
+ return {
1318
+ content: [
1319
+ {
1320
+ type: "text",
1321
+ text: JSON.stringify(result)
1322
+ }
1323
+ ]
1324
+ };
1325
+ } catch (error) {
1326
+ return {
1327
+ content: [
1328
+ {
1329
+ type: "text",
1330
+ text: `Error getting annotations: ${error instanceof Error ? error.message : String(error)}`
1331
+ }
1332
+ ]
1333
+ };
1334
+ }
1335
+ }
1336
+ );
1337
+ server.tool(
1338
+ "set_annotation",
1339
+ "Create or update an annotation",
1340
+ {
1341
+ nodeId: z.string().describe("The ID of the node to annotate"),
1342
+ annotationId: z.string().optional().describe("The ID of the annotation to update (if updating existing annotation)"),
1343
+ labelMarkdown: z.string().describe("The annotation text in markdown format"),
1344
+ categoryId: z.string().optional().describe("The ID of the annotation category"),
1345
+ properties: z.array(z.object({
1346
+ type: z.string()
1347
+ })).optional().describe("Additional properties for the annotation")
1348
+ },
1349
+ async ({ nodeId, annotationId, labelMarkdown, categoryId, properties }) => {
1350
+ try {
1351
+ const result = await sendCommandToFigma("set_annotation", {
1352
+ nodeId,
1353
+ annotationId,
1354
+ labelMarkdown,
1355
+ categoryId,
1356
+ properties
1357
+ });
1358
+ return {
1359
+ content: [
1360
+ {
1361
+ type: "text",
1362
+ text: JSON.stringify(result)
1363
+ }
1364
+ ]
1365
+ };
1366
+ } catch (error) {
1367
+ return {
1368
+ content: [
1369
+ {
1370
+ type: "text",
1371
+ text: `Error setting annotation: ${error instanceof Error ? error.message : String(error)}`
1372
+ }
1373
+ ]
1374
+ };
1375
+ }
1376
+ }
1377
+ );
1378
+ server.tool(
1379
+ "set_multiple_annotations",
1380
+ "Set multiple annotations parallelly in a node",
1381
+ {
1382
+ nodeId: z.string().describe("The ID of the node containing the elements to annotate"),
1383
+ annotations: z.array(
1384
+ z.object({
1385
+ nodeId: z.string().describe("The ID of the node to annotate"),
1386
+ labelMarkdown: z.string().describe("The annotation text in markdown format"),
1387
+ categoryId: z.string().optional().describe("The ID of the annotation category"),
1388
+ annotationId: z.string().optional().describe("The ID of the annotation to update (if updating existing annotation)"),
1389
+ properties: z.array(z.object({
1390
+ type: z.string()
1391
+ })).optional().describe("Additional properties for the annotation")
1392
+ })
1393
+ ).describe("Array of annotations to apply")
1394
+ },
1395
+ async ({ nodeId, annotations }) => {
1396
+ try {
1397
+ if (!annotations || annotations.length === 0) {
1398
+ return {
1399
+ content: [
1400
+ {
1401
+ type: "text",
1402
+ text: "No annotations provided"
1403
+ }
1404
+ ]
1405
+ };
1406
+ }
1407
+ const initialStatus = {
1408
+ type: "text",
1409
+ text: `Starting annotation process for ${annotations.length} nodes. This will be processed in batches of 5...`
1410
+ };
1411
+ let totalProcessed = 0;
1412
+ const totalToProcess = annotations.length;
1413
+ const result = await sendCommandToFigma("set_multiple_annotations", {
1414
+ nodeId,
1415
+ annotations
1416
+ });
1417
+ const typedResult = result;
1418
+ const success = typedResult.annotationsApplied && typedResult.annotationsApplied > 0;
1419
+ const progressText = `
1420
+ Annotation process completed:
1421
+ - ${typedResult.annotationsApplied || 0} of ${totalToProcess} successfully applied
1422
+ - ${typedResult.annotationsFailed || 0} failed
1423
+ - Processed in ${typedResult.completedInChunks || 1} batches
1424
+ `;
1425
+ const detailedResults = typedResult.results || [];
1426
+ const failedResults = detailedResults.filter((item) => !item.success);
1427
+ let detailedResponse = "";
1428
+ if (failedResults.length > 0) {
1429
+ detailedResponse = `
1430
+
1431
+ Nodes that failed:
1432
+ ${failedResults.map(
1433
+ (item) => `- ${item.nodeId}: ${item.error || "Unknown error"}`
1434
+ ).join("\n")}`;
1435
+ }
1436
+ return {
1437
+ content: [
1438
+ initialStatus,
1439
+ {
1440
+ type: "text",
1441
+ text: progressText + detailedResponse
1442
+ }
1443
+ ]
1444
+ };
1445
+ } catch (error) {
1446
+ return {
1447
+ content: [
1448
+ {
1449
+ type: "text",
1450
+ text: `Error setting multiple annotations: ${error instanceof Error ? error.message : String(error)}`
1451
+ }
1452
+ ]
1453
+ };
1454
+ }
1455
+ }
1456
+ );
1457
+ server.tool(
1458
+ "create_component_instance",
1459
+ "Create an instance of a component in Figma",
1460
+ {
1461
+ componentKey: z.string().describe("Key of the component to instantiate"),
1462
+ x: z.number().describe("X position"),
1463
+ y: z.number().describe("Y position")
1464
+ },
1465
+ async ({ componentKey, x, y }) => {
1466
+ try {
1467
+ const result = await sendCommandToFigma("create_component_instance", {
1468
+ componentKey,
1469
+ x,
1470
+ y
1471
+ });
1472
+ const typedResult = result;
1473
+ return {
1474
+ content: [
1475
+ {
1476
+ type: "text",
1477
+ text: JSON.stringify(typedResult)
1478
+ }
1479
+ ]
1480
+ };
1481
+ } catch (error) {
1482
+ return {
1483
+ content: [
1484
+ {
1485
+ type: "text",
1486
+ text: `Error creating component instance: ${error instanceof Error ? error.message : String(error)}`
1487
+ }
1488
+ ]
1489
+ };
1490
+ }
1491
+ }
1492
+ );
1493
+ server.tool(
1494
+ "get_instance_overrides",
1495
+ "Get all override properties from a selected component instance. These overrides can be applied to other instances, which will swap them to match the source component.",
1496
+ {
1497
+ nodeId: z.string().optional().describe("Optional ID of the component instance to get overrides from. If not provided, currently selected instance will be used.")
1498
+ },
1499
+ async ({ nodeId }) => {
1500
+ try {
1501
+ const result = await sendCommandToFigma("get_instance_overrides", {
1502
+ instanceNodeId: nodeId || null
1503
+ });
1504
+ const typedResult = result;
1505
+ return {
1506
+ content: [
1507
+ {
1508
+ type: "text",
1509
+ text: typedResult.success ? `Successfully got instance overrides: ${typedResult.message}` : `Failed to get instance overrides: ${typedResult.message}`
1510
+ }
1511
+ ]
1512
+ };
1513
+ } catch (error) {
1514
+ return {
1515
+ content: [
1516
+ {
1517
+ type: "text",
1518
+ text: `Error copying instance overrides: ${error instanceof Error ? error.message : String(error)}`
1519
+ }
1520
+ ]
1521
+ };
1522
+ }
1523
+ }
1524
+ );
1525
+ server.tool(
1526
+ "set_instance_overrides",
1527
+ "Apply previously copied overrides to selected component instances. Target instances will be swapped to the source component and all copied override properties will be applied.",
1528
+ {
1529
+ sourceInstanceId: z.string().describe("ID of the source component instance"),
1530
+ targetNodeIds: z.array(z.string()).describe("Array of target instance IDs. Currently selected instances will be used.")
1531
+ },
1532
+ async ({ sourceInstanceId, targetNodeIds }) => {
1533
+ try {
1534
+ const result = await sendCommandToFigma("set_instance_overrides", {
1535
+ sourceInstanceId,
1536
+ targetNodeIds: targetNodeIds || []
1537
+ });
1538
+ const typedResult = result;
1539
+ if (typedResult.success) {
1540
+ const successCount = typedResult.results?.filter((r) => r.success).length || 0;
1541
+ return {
1542
+ content: [
1543
+ {
1544
+ type: "text",
1545
+ text: `Successfully applied ${typedResult.totalCount || 0} overrides to ${successCount} instances.`
1546
+ }
1547
+ ]
1548
+ };
1549
+ } else {
1550
+ return {
1551
+ content: [
1552
+ {
1553
+ type: "text",
1554
+ text: `Failed to set instance overrides: ${typedResult.message}`
1555
+ }
1556
+ ]
1557
+ };
1558
+ }
1559
+ } catch (error) {
1560
+ return {
1561
+ content: [
1562
+ {
1563
+ type: "text",
1564
+ text: `Error setting instance overrides: ${error instanceof Error ? error.message : String(error)}`
1565
+ }
1566
+ ]
1567
+ };
1568
+ }
1569
+ }
1570
+ );
1571
+ server.tool(
1572
+ "set_corner_radius",
1573
+ "Set the corner radius of a node in Figma",
1574
+ {
1575
+ nodeId: z.string().describe("The ID of the node to modify"),
1576
+ radius: z.number().min(0).describe("Corner radius value"),
1577
+ corners: z.array(z.boolean()).length(4).optional().describe(
1578
+ "Optional array of 4 booleans to specify which corners to round [topLeft, topRight, bottomRight, bottomLeft]"
1579
+ )
1580
+ },
1581
+ async ({ nodeId, radius, corners }) => {
1582
+ try {
1583
+ const result = await sendCommandToFigma("set_corner_radius", {
1584
+ nodeId,
1585
+ radius,
1586
+ corners: corners || [true, true, true, true]
1587
+ });
1588
+ const typedResult = result;
1589
+ return {
1590
+ content: [
1591
+ {
1592
+ type: "text",
1593
+ text: `Set corner radius of node "${typedResult.name}" to ${radius}px`
1594
+ }
1595
+ ]
1596
+ };
1597
+ } catch (error) {
1598
+ return {
1599
+ content: [
1600
+ {
1601
+ type: "text",
1602
+ text: `Error setting corner radius: ${error instanceof Error ? error.message : String(error)}`
1603
+ }
1604
+ ]
1605
+ };
1606
+ }
1607
+ }
1608
+ );
1609
+ server.prompt(
1610
+ "design_strategy",
1611
+ "Best practices for working with Figma designs",
1612
+ (extra) => {
1613
+ return {
1614
+ messages: [
1615
+ {
1616
+ role: "assistant",
1617
+ content: {
1618
+ type: "text",
1619
+ text: `When working with Figma designs, follow these best practices:
1620
+
1621
+ 1. Start with Document Structure:
1622
+ - First use get_document_info() to understand the current document
1623
+ - Plan your layout hierarchy before creating elements
1624
+ - Create a main container frame for each screen/section
1625
+
1626
+ 2. Naming Conventions:
1627
+ - Use descriptive, semantic names for all elements
1628
+ - Follow a consistent naming pattern (e.g., "Login Screen", "Logo Container", "Email Input")
1629
+ - Group related elements with meaningful names
1630
+
1631
+ 3. Layout Hierarchy:
1632
+ - Create parent frames first, then add child elements
1633
+ - For forms/login screens:
1634
+ * Start with the main screen container frame
1635
+ * Create a logo container at the top
1636
+ * Group input fields in their own containers
1637
+ * Place action buttons (login, submit) after inputs
1638
+ * Add secondary elements (forgot password, signup links) last
1639
+
1640
+ 4. Input Fields Structure:
1641
+ - Create a container frame for each input field
1642
+ - Include a label text above or inside the input
1643
+ - Group related inputs (e.g., username/password) together
1644
+
1645
+ 5. Element Creation:
1646
+ - Use create_frame() for containers and input fields
1647
+ - Use create_text() for labels, buttons text, and links
1648
+ - Set appropriate colors and styles:
1649
+ * Use fillColor for backgrounds
1650
+ * Use strokeColor for borders
1651
+ * Set proper fontWeight for different text elements
1652
+
1653
+ 6. Mofifying existing elements:
1654
+ - use set_text_content() to modify text content.
1655
+
1656
+ 7. Visual Hierarchy:
1657
+ - Position elements in logical reading order (top to bottom)
1658
+ - Maintain consistent spacing between elements
1659
+ - Use appropriate font sizes for different text types:
1660
+ * Larger for headings/welcome text
1661
+ * Medium for input labels
1662
+ * Standard for button text
1663
+ * Smaller for helper text/links
1664
+
1665
+ 8. Best Practices:
1666
+ - Verify each creation with get_node_info()
1667
+ - Use parentId to maintain proper hierarchy
1668
+ - Group related elements together in frames
1669
+ - Keep consistent spacing and alignment
1670
+
1671
+ Example Login Screen Structure:
1672
+ - Login Screen (main frame)
1673
+ - Logo Container (frame)
1674
+ - Logo (image/text)
1675
+ - Welcome Text (text)
1676
+ - Input Container (frame)
1677
+ - Email Input (frame)
1678
+ - Email Label (text)
1679
+ - Email Field (frame)
1680
+ - Password Input (frame)
1681
+ - Password Label (text)
1682
+ - Password Field (frame)
1683
+ - Login Button (frame)
1684
+ - Button Text (text)
1685
+ - Helper Links (frame)
1686
+ - Forgot Password (text)
1687
+ - Don't have account (text)`
1688
+ }
1689
+ }
1690
+ ],
1691
+ description: "Best practices for working with Figma designs"
1692
+ };
1693
+ }
1694
+ );
1695
+ server.prompt(
1696
+ "read_design_strategy",
1697
+ "Best practices for reading Figma designs",
1698
+ (extra) => {
1699
+ return {
1700
+ messages: [
1701
+ {
1702
+ role: "assistant",
1703
+ content: {
1704
+ type: "text",
1705
+ text: `When reading Figma designs, follow these best practices:
1706
+
1707
+ 1. Start with selection:
1708
+ - First use read_my_design() to understand the current selection
1709
+ - If no selection ask user to select single or multiple nodes
1710
+ `
1711
+ }
1712
+ }
1713
+ ],
1714
+ description: "Best practices for reading Figma designs"
1715
+ };
1716
+ }
1717
+ );
1718
+ server.tool(
1719
+ "scan_text_nodes",
1720
+ "Scan all text nodes in the selected Figma node",
1721
+ {
1722
+ nodeId: z.string().describe("ID of the node to scan")
1723
+ },
1724
+ async ({ nodeId }) => {
1725
+ try {
1726
+ const initialStatus = {
1727
+ type: "text",
1728
+ text: "Starting text node scanning. This may take a moment for large designs..."
1729
+ };
1730
+ const result = await sendCommandToFigma("scan_text_nodes", {
1731
+ nodeId,
1732
+ useChunking: true,
1733
+ // Enable chunking on the plugin side
1734
+ chunkSize: 10
1735
+ // Process 10 nodes at a time
1736
+ });
1737
+ if (result && typeof result === "object" && "chunks" in result) {
1738
+ const typedResult = result;
1739
+ const summaryText = `
1740
+ Scan completed:
1741
+ - Found ${typedResult.totalNodes} text nodes
1742
+ - Processed in ${typedResult.chunks} chunks
1743
+ `;
1744
+ return {
1745
+ content: [
1746
+ initialStatus,
1747
+ {
1748
+ type: "text",
1749
+ text: summaryText
1750
+ },
1751
+ {
1752
+ type: "text",
1753
+ text: JSON.stringify(typedResult.textNodes, null, 2)
1754
+ }
1755
+ ]
1756
+ };
1757
+ }
1758
+ return {
1759
+ content: [
1760
+ initialStatus,
1761
+ {
1762
+ type: "text",
1763
+ text: JSON.stringify(result, null, 2)
1764
+ }
1765
+ ]
1766
+ };
1767
+ } catch (error) {
1768
+ return {
1769
+ content: [
1770
+ {
1771
+ type: "text",
1772
+ text: `Error scanning text nodes: ${error instanceof Error ? error.message : String(error)}`
1773
+ }
1774
+ ]
1775
+ };
1776
+ }
1777
+ }
1778
+ );
1779
+ server.tool(
1780
+ "scan_nodes_by_types",
1781
+ "Scan for child nodes with specific types in the selected Figma node",
1782
+ {
1783
+ nodeId: z.string().describe("ID of the node to scan"),
1784
+ types: z.array(z.string()).describe("Array of node types to find in the child nodes (e.g. ['COMPONENT', 'FRAME'])")
1785
+ },
1786
+ async ({ nodeId, types }) => {
1787
+ try {
1788
+ const initialStatus = {
1789
+ type: "text",
1790
+ text: `Starting node type scanning for types: ${types.join(", ")}...`
1791
+ };
1792
+ const result = await sendCommandToFigma("scan_nodes_by_types", {
1793
+ nodeId,
1794
+ types
1795
+ });
1796
+ if (result && typeof result === "object" && "matchingNodes" in result) {
1797
+ const typedResult = result;
1798
+ const summaryText = `Scan completed: Found ${typedResult.count} nodes matching types: ${typedResult.searchedTypes.join(", ")}`;
1799
+ return {
1800
+ content: [
1801
+ initialStatus,
1802
+ {
1803
+ type: "text",
1804
+ text: summaryText
1805
+ },
1806
+ {
1807
+ type: "text",
1808
+ text: JSON.stringify(typedResult.matchingNodes, null, 2)
1809
+ }
1810
+ ]
1811
+ };
1812
+ }
1813
+ return {
1814
+ content: [
1815
+ initialStatus,
1816
+ {
1817
+ type: "text",
1818
+ text: JSON.stringify(result, null, 2)
1819
+ }
1820
+ ]
1821
+ };
1822
+ } catch (error) {
1823
+ return {
1824
+ content: [
1825
+ {
1826
+ type: "text",
1827
+ text: `Error scanning nodes by types: ${error instanceof Error ? error.message : String(error)}`
1828
+ }
1829
+ ]
1830
+ };
1831
+ }
1832
+ }
1833
+ );
1834
+ server.prompt(
1835
+ "text_replacement_strategy",
1836
+ "Systematic approach for replacing text in Figma designs",
1837
+ (extra) => {
1838
+ return {
1839
+ messages: [
1840
+ {
1841
+ role: "assistant",
1842
+ content: {
1843
+ type: "text",
1844
+ text: `# Intelligent Text Replacement Strategy
1845
+
1846
+ ## 1. Analyze Design & Identify Structure
1847
+ - Scan text nodes to understand the overall structure of the design
1848
+ - Use AI pattern recognition to identify logical groupings:
1849
+ * Tables (rows, columns, headers, cells)
1850
+ * Lists (items, headers, nested lists)
1851
+ * Card groups (similar cards with recurring text fields)
1852
+ * Forms (labels, input fields, validation text)
1853
+ * Navigation (menu items, breadcrumbs)
1854
+ \`\`\`
1855
+ scan_text_nodes(nodeId: "node-id")
1856
+ get_node_info(nodeId: "node-id") // optional
1857
+ \`\`\`
1858
+
1859
+ ## 2. Strategic Chunking for Complex Designs
1860
+ - Divide replacement tasks into logical content chunks based on design structure
1861
+ - Use one of these chunking strategies that best fits the design:
1862
+ * **Structural Chunking**: Table rows/columns, list sections, card groups
1863
+ * **Spatial Chunking**: Top-to-bottom, left-to-right in screen areas
1864
+ * **Semantic Chunking**: Content related to the same topic or functionality
1865
+ * **Component-Based Chunking**: Process similar component instances together
1866
+
1867
+ ## 3. Progressive Replacement with Verification
1868
+ - Create a safe copy of the node for text replacement
1869
+ - Replace text chunk by chunk with continuous progress updates
1870
+ - After each chunk is processed:
1871
+ * Export that section as a small, manageable image
1872
+ * Verify text fits properly and maintain design integrity
1873
+ * Fix issues before proceeding to the next chunk
1874
+
1875
+ \`\`\`
1876
+ // Clone the node to create a safe copy
1877
+ clone_node(nodeId: "selected-node-id", x: [new-x], y: [new-y])
1878
+
1879
+ // Replace text chunk by chunk
1880
+ set_multiple_text_contents(
1881
+ nodeId: "parent-node-id",
1882
+ text: [
1883
+ { nodeId: "node-id-1", text: "New text 1" },
1884
+ // More nodes in this chunk...
1885
+ ]
1886
+ )
1887
+
1888
+ // Verify chunk with small, targeted image exports
1889
+ export_node_as_image(nodeId: "chunk-node-id", format: "PNG", scale: 0.5)
1890
+ \`\`\`
1891
+
1892
+ ## 4. Intelligent Handling for Table Data
1893
+ - For tabular content:
1894
+ * Process one row or column at a time
1895
+ * Maintain alignment and spacing between cells
1896
+ * Consider conditional formatting based on cell content
1897
+ * Preserve header/data relationships
1898
+
1899
+ ## 5. Smart Text Adaptation
1900
+ - Adaptively handle text based on container constraints:
1901
+ * Auto-detect space constraints and adjust text length
1902
+ * Apply line breaks at appropriate linguistic points
1903
+ * Maintain text hierarchy and emphasis
1904
+ * Consider font scaling for critical content that must fit
1905
+
1906
+ ## 6. Progressive Feedback Loop
1907
+ - Establish a continuous feedback loop during replacement:
1908
+ * Real-time progress updates (0-100%)
1909
+ * Small image exports after each chunk for verification
1910
+ * Issues identified early and resolved incrementally
1911
+ * Quick adjustments applied to subsequent chunks
1912
+
1913
+ ## 7. Final Verification & Context-Aware QA
1914
+ - After all chunks are processed:
1915
+ * Export the entire design at reduced scale for final verification
1916
+ * Check for cross-chunk consistency issues
1917
+ * Verify proper text flow between different sections
1918
+ * Ensure design harmony across the full composition
1919
+
1920
+ ## 8. Chunk-Specific Export Scale Guidelines
1921
+ - Scale exports appropriately based on chunk size:
1922
+ * Small chunks (1-5 elements): scale 1.0
1923
+ * Medium chunks (6-20 elements): scale 0.7
1924
+ * Large chunks (21-50 elements): scale 0.5
1925
+ * Very large chunks (50+ elements): scale 0.3
1926
+ * Full design verification: scale 0.2
1927
+
1928
+ ## Sample Chunking Strategy for Common Design Types
1929
+
1930
+ ### Tables
1931
+ - Process by logical rows (5-10 rows per chunk)
1932
+ - Alternative: Process by column for columnar analysis
1933
+ - Tip: Always include header row in first chunk for reference
1934
+
1935
+ ### Card Lists
1936
+ - Group 3-5 similar cards per chunk
1937
+ - Process entire cards to maintain internal consistency
1938
+ - Verify text-to-image ratio within cards after each chunk
1939
+
1940
+ ### Forms
1941
+ - Group related fields (e.g., "Personal Information", "Payment Details")
1942
+ - Process labels and input fields together
1943
+ - Ensure validation messages and hints are updated with their fields
1944
+
1945
+ ### Navigation & Menus
1946
+ - Process hierarchical levels together (main menu, submenu)
1947
+ - Respect information architecture relationships
1948
+ - Verify menu fit and alignment after replacement
1949
+
1950
+ ## Best Practices
1951
+ - **Preserve Design Intent**: Always prioritize design integrity
1952
+ - **Structural Consistency**: Maintain alignment, spacing, and hierarchy
1953
+ - **Visual Feedback**: Verify each chunk visually before proceeding
1954
+ - **Incremental Improvement**: Learn from each chunk to improve subsequent ones
1955
+ - **Balance Automation & Control**: Let AI handle repetitive replacements but maintain oversight
1956
+ - **Respect Content Relationships**: Keep related content consistent across chunks
1957
+
1958
+ Remember that text is never just text\u2014it's a core design element that must work harmoniously with the overall composition. This chunk-based strategy allows you to methodically transform text while maintaining design integrity.`
1959
+ }
1960
+ }
1961
+ ],
1962
+ description: "Systematic approach for replacing text in Figma designs"
1963
+ };
1964
+ }
1965
+ );
1966
+ server.tool(
1967
+ "set_multiple_text_contents",
1968
+ "Set multiple text contents parallelly in a node",
1969
+ {
1970
+ nodeId: z.string().describe("The ID of the node containing the text nodes to replace"),
1971
+ text: z.array(
1972
+ z.object({
1973
+ nodeId: z.string().describe("The ID of the text node"),
1974
+ text: z.string().describe("The replacement text")
1975
+ })
1976
+ ).describe("Array of text node IDs and their replacement texts")
1977
+ },
1978
+ async ({ nodeId, text }) => {
1979
+ try {
1980
+ if (!text || text.length === 0) {
1981
+ return {
1982
+ content: [
1983
+ {
1984
+ type: "text",
1985
+ text: "No text provided"
1986
+ }
1987
+ ]
1988
+ };
1989
+ }
1990
+ const initialStatus = {
1991
+ type: "text",
1992
+ text: `Starting text replacement for ${text.length} nodes. This will be processed in batches of 5...`
1993
+ };
1994
+ let totalProcessed = 0;
1995
+ const totalToProcess = text.length;
1996
+ const result = await sendCommandToFigma("set_multiple_text_contents", {
1997
+ nodeId,
1998
+ text
1999
+ });
2000
+ const typedResult = result;
2001
+ const success = typedResult.replacementsApplied && typedResult.replacementsApplied > 0;
2002
+ const progressText = `
2003
+ Text replacement completed:
2004
+ - ${typedResult.replacementsApplied || 0} of ${totalToProcess} successfully updated
2005
+ - ${typedResult.replacementsFailed || 0} failed
2006
+ - Processed in ${typedResult.completedInChunks || 1} batches
2007
+ `;
2008
+ const detailedResults = typedResult.results || [];
2009
+ const failedResults = detailedResults.filter((item) => !item.success);
2010
+ let detailedResponse = "";
2011
+ if (failedResults.length > 0) {
2012
+ detailedResponse = `
2013
+
2014
+ Nodes that failed:
2015
+ ${failedResults.map(
2016
+ (item) => `- ${item.nodeId}: ${item.error || "Unknown error"}`
2017
+ ).join("\n")}`;
2018
+ }
2019
+ return {
2020
+ content: [
2021
+ initialStatus,
2022
+ {
2023
+ type: "text",
2024
+ text: progressText + detailedResponse
2025
+ }
2026
+ ]
2027
+ };
2028
+ } catch (error) {
2029
+ return {
2030
+ content: [
2031
+ {
2032
+ type: "text",
2033
+ text: `Error setting multiple text contents: ${error instanceof Error ? error.message : String(error)}`
2034
+ }
2035
+ ]
2036
+ };
2037
+ }
2038
+ }
2039
+ );
2040
+ server.prompt(
2041
+ "annotation_conversion_strategy",
2042
+ "Strategy for converting manual annotations to Figma's native annotations",
2043
+ (extra) => {
2044
+ return {
2045
+ messages: [
2046
+ {
2047
+ role: "assistant",
2048
+ content: {
2049
+ type: "text",
2050
+ text: `# Automatic Annotation Conversion
2051
+
2052
+ ## Process Overview
2053
+
2054
+ The process of converting manual annotations (numbered/alphabetical indicators with connected descriptions) to Figma's native annotations:
2055
+
2056
+ 1. Get selected frame/component information
2057
+ 2. Scan and collect all annotation text nodes
2058
+ 3. Scan target UI elements (components, instances, frames)
2059
+ 4. Match annotations to appropriate UI elements
2060
+ 5. Apply native Figma annotations
2061
+
2062
+ ## Step 1: Get Selection and Initial Setup
2063
+
2064
+ First, get the selected frame or component that contains annotations:
2065
+
2066
+ \`\`\`typescript
2067
+ // Get the selected frame/component
2068
+ const selection = await get_selection();
2069
+ const selectedNodeId = selection[0].id
2070
+
2071
+ // Get available annotation categories for later use
2072
+ const annotationData = await get_annotations({
2073
+ nodeId: selectedNodeId,
2074
+ includeCategories: true
2075
+ });
2076
+ const categories = annotationData.categories;
2077
+ \`\`\`
2078
+
2079
+ ## Step 2: Scan Annotation Text Nodes
2080
+
2081
+ Scan all text nodes to identify annotations and their descriptions:
2082
+
2083
+ \`\`\`typescript
2084
+ // Get all text nodes in the selection
2085
+ const textNodes = await scan_text_nodes({
2086
+ nodeId: selectedNodeId
2087
+ });
2088
+
2089
+ // Filter and group annotation markers and descriptions
2090
+
2091
+ // Markers typically have these characteristics:
2092
+ // - Short text content (usually single digit/letter)
2093
+ // - Specific font styles (often bold)
2094
+ // - Located in a container with "Marker" or "Dot" in the name
2095
+ // - Have a clear naming pattern (e.g., "1", "2", "3" or "A", "B", "C")
2096
+
2097
+
2098
+ // Identify description nodes
2099
+ // Usually longer text nodes near markers or with matching numbers in path
2100
+
2101
+ \`\`\`
2102
+
2103
+ ## Step 3: Scan Target UI Elements
2104
+
2105
+ Get all potential target elements that annotations might refer to:
2106
+
2107
+ \`\`\`typescript
2108
+ // Scan for all UI elements that could be annotation targets
2109
+ const targetNodes = await scan_nodes_by_types({
2110
+ nodeId: selectedNodeId,
2111
+ types: [
2112
+ "COMPONENT",
2113
+ "INSTANCE",
2114
+ "FRAME"
2115
+ ]
2116
+ });
2117
+ \`\`\`
2118
+
2119
+ ## Step 4: Match Annotations to Targets
2120
+
2121
+ Match each annotation to its target UI element using these strategies in order of priority:
2122
+
2123
+ 1. **Path-Based Matching**:
2124
+ - Look at the marker's parent container name in the Figma layer hierarchy
2125
+ - Remove any "Marker:" or "Annotation:" prefixes from the parent name
2126
+ - Find UI elements that share the same parent name or have it in their path
2127
+ - This works well when markers are grouped with their target elements
2128
+
2129
+ 2. **Name-Based Matching**:
2130
+ - Extract key terms from the annotation description
2131
+ - Look for UI elements whose names contain these key terms
2132
+ - Consider both exact matches and semantic similarities
2133
+ - Particularly effective for form fields, buttons, and labeled components
2134
+
2135
+ 3. **Proximity-Based Matching** (fallback):
2136
+ - Calculate the center point of the marker
2137
+ - Find the closest UI element by measuring distances to element centers
2138
+ - Consider the marker's position relative to nearby elements
2139
+ - Use this method when other matching strategies fail
2140
+
2141
+ Additional Matching Considerations:
2142
+ - Give higher priority to matches found through path-based matching
2143
+ - Consider the type of UI element when evaluating matches
2144
+ - Take into account the annotation's context and content
2145
+ - Use a combination of strategies for more accurate matching
2146
+
2147
+ ## Step 5: Apply Native Annotations
2148
+
2149
+ Convert matched annotations to Figma's native annotations using batch processing:
2150
+
2151
+ \`\`\`typescript
2152
+ // Prepare annotations array for batch processing
2153
+ const annotationsToApply = Object.values(annotations).map(({ marker, description }) => {
2154
+ // Find target using multiple strategies
2155
+ const target =
2156
+ findTargetByPath(marker, targetNodes) ||
2157
+ findTargetByName(description, targetNodes) ||
2158
+ findTargetByProximity(marker, targetNodes);
2159
+
2160
+ if (target) {
2161
+ // Determine appropriate category based on content
2162
+ const category = determineCategory(description.characters, categories);
2163
+
2164
+ // Determine appropriate additional annotationProperty based on content
2165
+ const annotationProperty = determineProperties(description.characters, target.type);
2166
+
2167
+ return {
2168
+ nodeId: target.id,
2169
+ labelMarkdown: description.characters,
2170
+ categoryId: category.id,
2171
+ properties: annotationProperty
2172
+ };
2173
+ }
2174
+ return null;
2175
+ }).filter(Boolean); // Remove null entries
2176
+
2177
+ // Apply annotations in batches using set_multiple_annotations
2178
+ if (annotationsToApply.length > 0) {
2179
+ await set_multiple_annotations({
2180
+ nodeId: selectedNodeId,
2181
+ annotations: annotationsToApply
2182
+ });
2183
+ }
2184
+ \`\`\`
2185
+
2186
+
2187
+ This strategy focuses on practical implementation based on real-world usage patterns, emphasizing the importance of handling various UI elements as annotation targets, not just text nodes.`
2188
+ }
2189
+ }
2190
+ ],
2191
+ description: "Strategy for converting manual annotations to Figma's native annotations"
2192
+ };
2193
+ }
2194
+ );
2195
+ server.prompt(
2196
+ "swap_overrides_instances",
2197
+ "Guide to swap instance overrides between instances",
2198
+ (extra) => {
2199
+ return {
2200
+ messages: [
2201
+ {
2202
+ role: "assistant",
2203
+ content: {
2204
+ type: "text",
2205
+ text: `# Swap Component Instance and Override Strategy
2206
+
2207
+ ## Overview
2208
+ This strategy enables transferring content and property overrides from a source instance to one or more target instances in Figma, maintaining design consistency while reducing manual work.
2209
+
2210
+ ## Step-by-Step Process
2211
+
2212
+ ### 1. Selection Analysis
2213
+ - Use \`get_selection()\` to identify the parent component or selected instances
2214
+ - For parent components, scan for instances with \`scan_nodes_by_types({ nodeId: "parent-id", types: ["INSTANCE"] })\`
2215
+ - Identify custom slots by name patterns (e.g. "Custom Slot*" or "Instance Slot") or by examining text content
2216
+ - Determine which is the source instance (with content to copy) and which are targets (where to apply content)
2217
+
2218
+ ### 2. Extract Source Overrides
2219
+ - Use \`get_instance_overrides()\` to extract customizations from the source instance
2220
+ - This captures text content, property values, and style overrides
2221
+ - Command syntax: \`get_instance_overrides({ nodeId: "source-instance-id" })\`
2222
+ - Look for successful response like "Got component information from [instance name]"
2223
+
2224
+ ### 3. Apply Overrides to Targets
2225
+ - Apply captured overrides using \`set_instance_overrides()\`
2226
+ - Command syntax:
2227
+ \`\`\`
2228
+ set_instance_overrides({
2229
+ sourceInstanceId: "source-instance-id",
2230
+ targetNodeIds: ["target-id-1", "target-id-2", ...]
2231
+ })
2232
+ \`\`\`
2233
+
2234
+ ### 4. Verification
2235
+ - Verify results with \`get_node_info()\` or \`read_my_design()\`
2236
+ - Confirm text content and style overrides have transferred successfully
2237
+
2238
+ ## Key Tips
2239
+ - Always join the appropriate channel first with \`join_channel()\`
2240
+ - When working with multiple targets, check the full selection with \`get_selection()\`
2241
+ - Preserve component relationships by using instance overrides rather than direct text manipulation`
2242
+ }
2243
+ }
2244
+ ],
2245
+ description: "Strategy for transferring overrides between component instances in Figma"
2246
+ };
2247
+ }
2248
+ );
2249
+ server.tool(
2250
+ "set_layout_mode",
2251
+ "Set the layout mode and wrap behavior of a frame in Figma",
2252
+ {
2253
+ nodeId: z.string().describe("The ID of the frame to modify"),
2254
+ layoutMode: z.enum(["NONE", "HORIZONTAL", "VERTICAL"]).describe("Layout mode for the frame"),
2255
+ layoutWrap: z.enum(["NO_WRAP", "WRAP"]).optional().describe("Whether the auto-layout frame wraps its children")
2256
+ },
2257
+ async ({ nodeId, layoutMode, layoutWrap }) => {
2258
+ try {
2259
+ const result = await sendCommandToFigma("set_layout_mode", {
2260
+ nodeId,
2261
+ layoutMode,
2262
+ layoutWrap: layoutWrap || "NO_WRAP"
2263
+ });
2264
+ const typedResult = result;
2265
+ return {
2266
+ content: [
2267
+ {
2268
+ type: "text",
2269
+ text: `Set layout mode of frame "${typedResult.name}" to ${layoutMode}${layoutWrap ? ` with ${layoutWrap}` : ""}`
2270
+ }
2271
+ ]
2272
+ };
2273
+ } catch (error) {
2274
+ return {
2275
+ content: [
2276
+ {
2277
+ type: "text",
2278
+ text: `Error setting layout mode: ${error instanceof Error ? error.message : String(error)}`
2279
+ }
2280
+ ]
2281
+ };
2282
+ }
2283
+ }
2284
+ );
2285
+ server.tool(
2286
+ "set_padding",
2287
+ "Set padding values for an auto-layout frame in Figma",
2288
+ {
2289
+ nodeId: z.string().describe("The ID of the frame to modify"),
2290
+ paddingTop: z.number().optional().describe("Top padding value"),
2291
+ paddingRight: z.number().optional().describe("Right padding value"),
2292
+ paddingBottom: z.number().optional().describe("Bottom padding value"),
2293
+ paddingLeft: z.number().optional().describe("Left padding value")
2294
+ },
2295
+ async ({ nodeId, paddingTop, paddingRight, paddingBottom, paddingLeft }) => {
2296
+ try {
2297
+ const result = await sendCommandToFigma("set_padding", {
2298
+ nodeId,
2299
+ paddingTop,
2300
+ paddingRight,
2301
+ paddingBottom,
2302
+ paddingLeft
2303
+ });
2304
+ const typedResult = result;
2305
+ const paddingMessages = [];
2306
+ if (paddingTop !== void 0) paddingMessages.push(`top: ${paddingTop}`);
2307
+ if (paddingRight !== void 0) paddingMessages.push(`right: ${paddingRight}`);
2308
+ if (paddingBottom !== void 0) paddingMessages.push(`bottom: ${paddingBottom}`);
2309
+ if (paddingLeft !== void 0) paddingMessages.push(`left: ${paddingLeft}`);
2310
+ const paddingText = paddingMessages.length > 0 ? `padding (${paddingMessages.join(", ")})` : "padding";
2311
+ return {
2312
+ content: [
2313
+ {
2314
+ type: "text",
2315
+ text: `Set ${paddingText} for frame "${typedResult.name}"`
2316
+ }
2317
+ ]
2318
+ };
2319
+ } catch (error) {
2320
+ return {
2321
+ content: [
2322
+ {
2323
+ type: "text",
2324
+ text: `Error setting padding: ${error instanceof Error ? error.message : String(error)}`
2325
+ }
2326
+ ]
2327
+ };
2328
+ }
2329
+ }
2330
+ );
2331
+ server.tool(
2332
+ "set_axis_align",
2333
+ "Set primary and counter axis alignment for an auto-layout frame in Figma",
2334
+ {
2335
+ nodeId: z.string().describe("The ID of the frame to modify"),
2336
+ primaryAxisAlignItems: z.enum(["MIN", "MAX", "CENTER", "SPACE_BETWEEN"]).optional().describe("Primary axis alignment (MIN/MAX = left/right in horizontal, top/bottom in vertical). Note: When set to SPACE_BETWEEN, itemSpacing will be ignored as children will be evenly spaced."),
2337
+ counterAxisAlignItems: z.enum(["MIN", "MAX", "CENTER", "BASELINE"]).optional().describe("Counter axis alignment (MIN/MAX = top/bottom in horizontal, left/right in vertical)")
2338
+ },
2339
+ async ({ nodeId, primaryAxisAlignItems, counterAxisAlignItems }) => {
2340
+ try {
2341
+ const result = await sendCommandToFigma("set_axis_align", {
2342
+ nodeId,
2343
+ primaryAxisAlignItems,
2344
+ counterAxisAlignItems
2345
+ });
2346
+ const typedResult = result;
2347
+ const alignMessages = [];
2348
+ if (primaryAxisAlignItems !== void 0) alignMessages.push(`primary: ${primaryAxisAlignItems}`);
2349
+ if (counterAxisAlignItems !== void 0) alignMessages.push(`counter: ${counterAxisAlignItems}`);
2350
+ const alignText = alignMessages.length > 0 ? `axis alignment (${alignMessages.join(", ")})` : "axis alignment";
2351
+ return {
2352
+ content: [
2353
+ {
2354
+ type: "text",
2355
+ text: `Set ${alignText} for frame "${typedResult.name}"`
2356
+ }
2357
+ ]
2358
+ };
2359
+ } catch (error) {
2360
+ return {
2361
+ content: [
2362
+ {
2363
+ type: "text",
2364
+ text: `Error setting axis alignment: ${error instanceof Error ? error.message : String(error)}`
2365
+ }
2366
+ ]
2367
+ };
2368
+ }
2369
+ }
2370
+ );
2371
+ server.tool(
2372
+ "set_layout_sizing",
2373
+ "Set horizontal and vertical sizing modes for an auto-layout frame in Figma",
2374
+ {
2375
+ nodeId: z.string().describe("The ID of the frame to modify"),
2376
+ layoutSizingHorizontal: z.enum(["FIXED", "HUG", "FILL"]).optional().describe("Horizontal sizing mode (HUG for frames/text only, FILL for auto-layout children only)"),
2377
+ layoutSizingVertical: z.enum(["FIXED", "HUG", "FILL"]).optional().describe("Vertical sizing mode (HUG for frames/text only, FILL for auto-layout children only)")
2378
+ },
2379
+ async ({ nodeId, layoutSizingHorizontal, layoutSizingVertical }) => {
2380
+ try {
2381
+ const result = await sendCommandToFigma("set_layout_sizing", {
2382
+ nodeId,
2383
+ layoutSizingHorizontal,
2384
+ layoutSizingVertical
2385
+ });
2386
+ const typedResult = result;
2387
+ const sizingMessages = [];
2388
+ if (layoutSizingHorizontal !== void 0) sizingMessages.push(`horizontal: ${layoutSizingHorizontal}`);
2389
+ if (layoutSizingVertical !== void 0) sizingMessages.push(`vertical: ${layoutSizingVertical}`);
2390
+ const sizingText = sizingMessages.length > 0 ? `layout sizing (${sizingMessages.join(", ")})` : "layout sizing";
2391
+ return {
2392
+ content: [
2393
+ {
2394
+ type: "text",
2395
+ text: `Set ${sizingText} for frame "${typedResult.name}"`
2396
+ }
2397
+ ]
2398
+ };
2399
+ } catch (error) {
2400
+ return {
2401
+ content: [
2402
+ {
2403
+ type: "text",
2404
+ text: `Error setting layout sizing: ${error instanceof Error ? error.message : String(error)}`
2405
+ }
2406
+ ]
2407
+ };
2408
+ }
2409
+ }
2410
+ );
2411
+ server.tool(
2412
+ "set_item_spacing",
2413
+ "Set distance between children in an auto-layout frame",
2414
+ {
2415
+ nodeId: z.string().describe("The ID of the frame to modify"),
2416
+ itemSpacing: z.number().optional().describe("Distance between children. Note: This value will be ignored if primaryAxisAlignItems is set to SPACE_BETWEEN."),
2417
+ counterAxisSpacing: z.number().optional().describe("Distance between wrapped rows/columns. Only works when layoutWrap is set to WRAP.")
2418
+ },
2419
+ async ({ nodeId, itemSpacing, counterAxisSpacing }) => {
2420
+ try {
2421
+ const params = { nodeId };
2422
+ if (itemSpacing !== void 0) params.itemSpacing = itemSpacing;
2423
+ if (counterAxisSpacing !== void 0) params.counterAxisSpacing = counterAxisSpacing;
2424
+ const result = await sendCommandToFigma("set_item_spacing", params);
2425
+ const typedResult = result;
2426
+ let message = `Updated spacing for frame "${typedResult.name}":`;
2427
+ if (itemSpacing !== void 0) message += ` itemSpacing=${itemSpacing}`;
2428
+ if (counterAxisSpacing !== void 0) message += ` counterAxisSpacing=${counterAxisSpacing}`;
2429
+ return {
2430
+ content: [
2431
+ {
2432
+ type: "text",
2433
+ text: message
2434
+ }
2435
+ ]
2436
+ };
2437
+ } catch (error) {
2438
+ return {
2439
+ content: [
2440
+ {
2441
+ type: "text",
2442
+ text: `Error setting spacing: ${error instanceof Error ? error.message : String(error)}`
2443
+ }
2444
+ ]
2445
+ };
2446
+ }
2447
+ }
2448
+ );
2449
+ server.tool(
2450
+ "get_reactions",
2451
+ "Get Figma Prototyping Reactions from multiple nodes. CRITICAL: The output MUST be processed using the 'reaction_to_connector_strategy' prompt IMMEDIATELY to generate parameters for connector lines via the 'create_connections' tool.",
2452
+ {
2453
+ nodeIds: z.array(z.string()).describe("Array of node IDs to get reactions from")
2454
+ },
2455
+ async ({ nodeIds }) => {
2456
+ try {
2457
+ const result = await sendCommandToFigma("get_reactions", { nodeIds });
2458
+ return {
2459
+ content: [
2460
+ {
2461
+ type: "text",
2462
+ text: JSON.stringify(result)
2463
+ },
2464
+ {
2465
+ type: "text",
2466
+ text: "IMPORTANT: You MUST now use the reaction data above and follow the `reaction_to_connector_strategy` prompt to prepare the parameters for the `create_connections` tool call. This is a required next step."
2467
+ }
2468
+ ],
2469
+ followUp: {
2470
+ type: "prompt",
2471
+ prompt: "reaction_to_connector_strategy"
2472
+ }
2473
+ };
2474
+ } catch (error) {
2475
+ return {
2476
+ content: [
2477
+ {
2478
+ type: "text",
2479
+ text: `Error getting reactions: ${error instanceof Error ? error.message : String(error)}`
2480
+ }
2481
+ ]
2482
+ };
2483
+ }
2484
+ }
2485
+ );
2486
+ server.tool(
2487
+ "set_default_connector",
2488
+ "Set a copied connector node as the default connector",
2489
+ {
2490
+ connectorId: z.string().optional().describe("The ID of the connector node to set as default")
2491
+ },
2492
+ async ({ connectorId }) => {
2493
+ try {
2494
+ const result = await sendCommandToFigma("set_default_connector", {
2495
+ connectorId
2496
+ });
2497
+ return {
2498
+ content: [
2499
+ {
2500
+ type: "text",
2501
+ text: `Default connector set: ${JSON.stringify(result)}`
2502
+ }
2503
+ ]
2504
+ };
2505
+ } catch (error) {
2506
+ return {
2507
+ content: [
2508
+ {
2509
+ type: "text",
2510
+ text: `Error setting default connector: ${error instanceof Error ? error.message : String(error)}`
2511
+ }
2512
+ ]
2513
+ };
2514
+ }
2515
+ }
2516
+ );
2517
+ server.tool(
2518
+ "create_connections",
2519
+ "Create connections between nodes using the default connector style",
2520
+ {
2521
+ connections: z.array(z.object({
2522
+ startNodeId: z.string().describe("ID of the starting node"),
2523
+ endNodeId: z.string().describe("ID of the ending node"),
2524
+ text: z.string().optional().describe("Optional text to display on the connector")
2525
+ })).describe("Array of node connections to create")
2526
+ },
2527
+ async ({ connections }) => {
2528
+ try {
2529
+ if (!connections || connections.length === 0) {
2530
+ return {
2531
+ content: [
2532
+ {
2533
+ type: "text",
2534
+ text: "No connections provided"
2535
+ }
2536
+ ]
2537
+ };
2538
+ }
2539
+ const result = await sendCommandToFigma("create_connections", {
2540
+ connections
2541
+ });
2542
+ return {
2543
+ content: [
2544
+ {
2545
+ type: "text",
2546
+ text: `Created ${connections.length} connections: ${JSON.stringify(result)}`
2547
+ }
2548
+ ]
2549
+ };
2550
+ } catch (error) {
2551
+ return {
2552
+ content: [
2553
+ {
2554
+ type: "text",
2555
+ text: `Error creating connections: ${error instanceof Error ? error.message : String(error)}`
2556
+ }
2557
+ ]
2558
+ };
2559
+ }
2560
+ }
2561
+ );
2562
+ server.tool(
2563
+ "set_focus",
2564
+ "Set focus on a specific node in Figma by selecting it and scrolling viewport to it",
2565
+ {
2566
+ nodeId: z.string().describe("The ID of the node to focus on")
2567
+ },
2568
+ async ({ nodeId }) => {
2569
+ try {
2570
+ const result = await sendCommandToFigma("set_focus", { nodeId });
2571
+ const typedResult = result;
2572
+ return {
2573
+ content: [
2574
+ {
2575
+ type: "text",
2576
+ text: `Focused on node "${typedResult.name}" (ID: ${typedResult.id})`
2577
+ }
2578
+ ]
2579
+ };
2580
+ } catch (error) {
2581
+ return {
2582
+ content: [
2583
+ {
2584
+ type: "text",
2585
+ text: `Error setting focus: ${error instanceof Error ? error.message : String(error)}`
2586
+ }
2587
+ ]
2588
+ };
2589
+ }
2590
+ }
2591
+ );
2592
+ server.tool(
2593
+ "set_selections",
2594
+ "Set selection to multiple nodes in Figma and scroll viewport to show them",
2595
+ {
2596
+ nodeIds: z.array(z.string()).describe("Array of node IDs to select")
2597
+ },
2598
+ async ({ nodeIds }) => {
2599
+ try {
2600
+ const result = await sendCommandToFigma("set_selections", { nodeIds });
2601
+ const typedResult = result;
2602
+ return {
2603
+ content: [
2604
+ {
2605
+ type: "text",
2606
+ text: `Selected ${typedResult.count} nodes: ${typedResult.selectedNodes.map((node) => `"${node.name}" (${node.id})`).join(", ")}`
2607
+ }
2608
+ ]
2609
+ };
2610
+ } catch (error) {
2611
+ return {
2612
+ content: [
2613
+ {
2614
+ type: "text",
2615
+ text: `Error setting selections: ${error instanceof Error ? error.message : String(error)}`
2616
+ }
2617
+ ]
2618
+ };
2619
+ }
2620
+ }
2621
+ );
2622
+ server.prompt(
2623
+ "reaction_to_connector_strategy",
2624
+ "Strategy for converting Figma prototype reactions to connector lines using the output of 'get_reactions'",
2625
+ (extra) => {
2626
+ return {
2627
+ messages: [
2628
+ {
2629
+ role: "assistant",
2630
+ content: {
2631
+ type: "text",
2632
+ text: `# Strategy: Convert Figma Prototype Reactions to Connector Lines
2633
+
2634
+ ## Goal
2635
+ Process the JSON output from the \`get_reactions\` tool to generate an array of connection objects suitable for the \`create_connections\` tool. This visually represents prototype flows as connector lines on the Figma canvas.
2636
+
2637
+ ## Input Data
2638
+ You will receive JSON data from the \`get_reactions\` tool. This data contains an array of nodes, each with potential reactions. A typical reaction object looks like this:
2639
+ \`\`\`json
2640
+ {
2641
+ "trigger": { "type": "ON_CLICK" },
2642
+ "action": {
2643
+ "type": "NAVIGATE",
2644
+ "destinationId": "destination-node-id",
2645
+ "navigationTransition": { ... },
2646
+ "preserveScrollPosition": false
2647
+ }
2648
+ }
2649
+ \`\`\`
2650
+
2651
+ ## Step-by-Step Process
2652
+
2653
+ ### 1. Preparation & Context Gathering
2654
+ - **Action:** Call \`read_my_design\` on the relevant node(s) to get context about the nodes involved (names, types, etc.). This helps in generating meaningful connector labels later.
2655
+ - **Action:** Call \`set_default_connector\` **without** the \`connectorId\` parameter.
2656
+ - **Check Result:** Analyze the response from \`set_default_connector\`.
2657
+ - If it confirms a default connector is already set (e.g., "Default connector is already set"), proceed to Step 2.
2658
+ - If it indicates no default connector is set (e.g., "No default connector set..."), you **cannot** proceed with \`create_connections\` yet. Inform the user they need to manually copy a connector from FigJam, paste it onto the current page, select it, and then you can run \`set_default_connector({ connectorId: "SELECTED_NODE_ID" })\` before attempting \`create_connections\`. **Do not proceed to Step 2 until a default connector is confirmed.**
2659
+
2660
+ ### 2. Filter and Transform Reactions from \`get_reactions\` Output
2661
+ - **Iterate:** Go through the JSON array provided by \`get_reactions\`. For each node in the array:
2662
+ - Iterate through its \`reactions\` array.
2663
+ - **Filter:** Keep only reactions where the \`action\` meets these criteria:
2664
+ - Has a \`type\` that implies a connection (e.g., \`NAVIGATE\`, \`OPEN_OVERLAY\`, \`SWAP_OVERLAY\`). **Ignore** types like \`CHANGE_TO\`, \`CLOSE_OVERLAY\`, etc.
2665
+ - Has a valid \`destinationId\` property.
2666
+ - **Extract:** For each valid reaction, extract the following information:
2667
+ - \`sourceNodeId\`: The ID of the node the reaction belongs to (from the outer loop).
2668
+ - \`destinationNodeId\`: The value of \`action.destinationId\`.
2669
+ - \`actionType\`: The value of \`action.type\`.
2670
+ - \`triggerType\`: The value of \`trigger.type\`.
2671
+
2672
+ ### 3. Generate Connector Text Labels
2673
+ - **For each extracted connection:** Create a concise, descriptive text label string.
2674
+ - **Combine Information:** Use the \`actionType\`, \`triggerType\`, and potentially the names of the source/destination nodes (obtained from Step 1's \`read_my_design\` or by calling \`get_node_info\` if necessary) to generate the label.
2675
+ - **Example Labels:**
2676
+ - If \`triggerType\` is "ON_CLICK" and \`actionType\` is "NAVIGATE": "On click, navigate to [Destination Node Name]"
2677
+ - If \`triggerType\` is "ON_DRAG" and \`actionType\` is "OPEN_OVERLAY": "On drag, open [Destination Node Name] overlay"
2678
+ - **Keep it brief and informative.** Let this generated string be \`generatedText\`.
2679
+
2680
+ ### 4. Prepare the \`connections\` Array for \`create_connections\`
2681
+ - **Structure:** Create a JSON array where each element is an object representing a connection.
2682
+ - **Format:** Each object in the array must have the following structure:
2683
+ \`\`\`json
2684
+ {
2685
+ "startNodeId": "sourceNodeId_from_step_2",
2686
+ "endNodeId": "destinationNodeId_from_step_2",
2687
+ "text": "generatedText_from_step_3"
2688
+ }
2689
+ \`\`\`
2690
+ - **Result:** This final array is the value you will pass to the \`connections\` parameter when calling the \`create_connections\` tool.
2691
+
2692
+ ### 5. Execute Connection Creation
2693
+ - **Action:** Call the \`create_connections\` tool, passing the array generated in Step 4 as the \`connections\` argument.
2694
+ - **Verify:** Check the response from \`create_connections\` to confirm success or failure.
2695
+
2696
+ This detailed process ensures you correctly interpret the reaction data, prepare the necessary information, and use the appropriate tools to create the connector lines.`
2697
+ }
2698
+ }
2699
+ ],
2700
+ description: "Strategy for converting Figma prototype reactions to connector lines using the output of 'get_reactions'"
2701
+ };
2702
+ }
2703
+ );
2704
+ server.tool(
2705
+ "join_channel",
2706
+ "Join a specific channel to communicate with Figma",
2707
+ {
2708
+ channel: z.string().describe("The name of the channel to join").default("")
2709
+ },
2710
+ async ({ channel }) => {
2711
+ try {
2712
+ if (!channel) {
2713
+ return {
2714
+ content: [
2715
+ {
2716
+ type: "text",
2717
+ text: "Please provide a channel name to join:"
2718
+ }
2719
+ ],
2720
+ followUp: {
2721
+ tool: "join_channel",
2722
+ description: "Join the specified channel"
2723
+ }
2724
+ };
2725
+ }
2726
+ await joinChannel(channel);
2727
+ return {
2728
+ content: [
2729
+ {
2730
+ type: "text",
2731
+ text: `Successfully joined channel: ${channel}`
2732
+ }
2733
+ ]
2734
+ };
2735
+ } catch (error) {
2736
+ return {
2737
+ content: [
2738
+ {
2739
+ type: "text",
2740
+ text: `Error joining channel: ${error instanceof Error ? error.message : String(error)}`
2741
+ }
2742
+ ]
2743
+ };
2744
+ }
2745
+ }
2746
+ );
2747
+ main().catch((error) => {
2748
+ logger.error(`Error starting FigmaMCP server: ${error instanceof Error ? error.message : String(error)}`);
2749
+ process.exit(1);
2750
+ });
2751
+ }
2752
+ });
2753
+
2754
+ // src/cli.ts
2755
+ if (process.argv.includes("--relay")) {
2756
+ Promise.resolve().then(() => (init_relay(), relay_exports)).then((m) => m.startRelay());
2757
+ } else {
2758
+ Promise.resolve().then(() => init_server());
2759
+ }
2760
+ //# sourceMappingURL=cli.js.map