@biolab/talk-to-figma 0.3.3

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