@mp3wizard/figma-console-mcp 1.19.0 → 1.19.2
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/cloudflare/core/annotation-tools.js +230 -0
- package/dist/cloudflare/core/cloud-websocket-connector.js +93 -0
- package/dist/cloudflare/core/deep-component-tools.js +128 -0
- package/dist/cloudflare/core/design-code-tools.js +65 -7
- package/dist/cloudflare/core/enrichment/enrichment-service.js +108 -12
- package/dist/cloudflare/core/figjam-tools.js +485 -0
- package/dist/cloudflare/core/figma-api.js +7 -4
- package/dist/cloudflare/core/figma-desktop-connector.js +108 -0
- package/dist/cloudflare/core/figma-tools.js +445 -55
- package/dist/cloudflare/core/port-discovery.js +88 -0
- package/dist/cloudflare/core/resolve-package-root.js +11 -0
- package/dist/cloudflare/core/slides-tools.js +607 -0
- package/dist/cloudflare/core/websocket-connector.js +93 -0
- package/dist/cloudflare/core/websocket-server.js +18 -9
- package/dist/cloudflare/index.js +164 -41
- package/dist/core/figma-api.d.ts.map +1 -1
- package/dist/core/figma-api.js +5 -2
- package/dist/core/figma-api.js.map +1 -1
- package/dist/core/figma-tools.d.ts.map +1 -1
- package/dist/core/figma-tools.js +11 -6
- package/dist/core/figma-tools.js.map +1 -1
- package/dist/core/resolve-package-root.d.ts +2 -0
- package/dist/core/resolve-package-root.d.ts.map +1 -0
- package/dist/core/resolve-package-root.js +12 -0
- package/dist/core/resolve-package-root.js.map +1 -0
- package/dist/core/websocket-server.d.ts.map +1 -1
- package/dist/core/websocket-server.js +7 -9
- package/dist/core/websocket-server.js.map +1 -1
- package/dist/local.d.ts +6 -0
- package/dist/local.d.ts.map +1 -1
- package/dist/local.js +22 -0
- package/dist/local.js.map +1 -1
- package/figma-desktop-bridge/code.js +103 -4
- package/package.json +1 -1
|
@@ -152,20 +152,116 @@ export class EnrichmentService {
|
|
|
152
152
|
}
|
|
153
153
|
enriched.styles_used = stylesUsed;
|
|
154
154
|
}
|
|
155
|
-
// Extract variables used
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
155
|
+
// Extract variables used and detect hardcoded values by walking the node tree
|
|
156
|
+
const varsUsed = [];
|
|
157
|
+
const hardcodedValues = [];
|
|
158
|
+
const variables = this.extractVariablesMap(data);
|
|
159
|
+
// Walk node tree to find boundVariables and hardcoded values
|
|
160
|
+
const walkForTokens = (node, path = "") => {
|
|
161
|
+
if (!node)
|
|
162
|
+
return;
|
|
163
|
+
const nodePath = path ? `${path} > ${node.name || node.id}` : (node.name || node.id);
|
|
164
|
+
const bv = node.boundVariables || {};
|
|
165
|
+
// Check fills
|
|
166
|
+
if (node.fills && Array.isArray(node.fills)) {
|
|
167
|
+
for (let i = 0; i < node.fills.length; i++) {
|
|
168
|
+
const fill = node.fills[i];
|
|
169
|
+
if (fill.type === "SOLID" && fill.visible !== false) {
|
|
170
|
+
const fillBv = bv.fills;
|
|
171
|
+
if (fillBv && (Array.isArray(fillBv) ? fillBv[i] : fillBv)) {
|
|
172
|
+
const varRef = Array.isArray(fillBv) ? fillBv[i] : fillBv;
|
|
173
|
+
if (varRef?.id) {
|
|
174
|
+
const varInfo = variables.get(varRef.id);
|
|
175
|
+
varsUsed.push({
|
|
176
|
+
variableId: varRef.id,
|
|
177
|
+
variableName: varInfo?.name || varRef.id,
|
|
178
|
+
property: "fill",
|
|
179
|
+
nodePath,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
// Hardcoded fill
|
|
185
|
+
const c = fill.color;
|
|
186
|
+
if (c) {
|
|
187
|
+
hardcodedValues.push({
|
|
188
|
+
property: "fill",
|
|
189
|
+
value: `rgb(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)})`,
|
|
190
|
+
nodePath,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// Check strokes
|
|
198
|
+
if (node.strokes && Array.isArray(node.strokes)) {
|
|
199
|
+
for (let i = 0; i < node.strokes.length; i++) {
|
|
200
|
+
const stroke = node.strokes[i];
|
|
201
|
+
if (stroke.type === "SOLID" && stroke.visible !== false) {
|
|
202
|
+
const strokeBv = bv.strokes;
|
|
203
|
+
if (strokeBv && (Array.isArray(strokeBv) ? strokeBv[i] : strokeBv)) {
|
|
204
|
+
const varRef = Array.isArray(strokeBv) ? strokeBv[i] : strokeBv;
|
|
205
|
+
if (varRef?.id) {
|
|
206
|
+
const varInfo = variables.get(varRef.id);
|
|
207
|
+
varsUsed.push({
|
|
208
|
+
variableId: varRef.id,
|
|
209
|
+
variableName: varInfo?.name || varRef.id,
|
|
210
|
+
property: "stroke",
|
|
211
|
+
nodePath,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const c = stroke.color;
|
|
217
|
+
if (c) {
|
|
218
|
+
hardcodedValues.push({
|
|
219
|
+
property: "stroke",
|
|
220
|
+
value: `rgb(${Math.round(c.r * 255)}, ${Math.round(c.g * 255)}, ${Math.round(c.b * 255)})`,
|
|
221
|
+
nodePath,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
// Check spacing/sizing tokens
|
|
229
|
+
const spacingProps = ["itemSpacing", "paddingLeft", "paddingRight", "paddingTop", "paddingBottom", "cornerRadius"];
|
|
230
|
+
for (const prop of spacingProps) {
|
|
231
|
+
if (node[prop] !== undefined && node[prop] !== 0) {
|
|
232
|
+
if (bv[prop]?.id) {
|
|
233
|
+
const varInfo = variables.get(bv[prop].id);
|
|
234
|
+
varsUsed.push({
|
|
235
|
+
variableId: bv[prop].id,
|
|
236
|
+
variableName: varInfo?.name || bv[prop].id,
|
|
237
|
+
property: prop,
|
|
238
|
+
nodePath,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
hardcodedValues.push({
|
|
243
|
+
property: prop,
|
|
244
|
+
value: node[prop],
|
|
245
|
+
nodePath,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// Recurse into children
|
|
251
|
+
if (node.children && Array.isArray(node.children)) {
|
|
252
|
+
for (const child of node.children) {
|
|
253
|
+
walkForTokens(child, nodePath);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
walkForTokens(component);
|
|
258
|
+
enriched.variables_used = varsUsed;
|
|
259
|
+
enriched.hardcoded_values = hardcodedValues;
|
|
164
260
|
// Calculate token coverage
|
|
165
|
-
const totalProps =
|
|
166
|
-
const tokenProps =
|
|
261
|
+
const totalProps = varsUsed.length + (enriched.styles_used?.length || 0) + hardcodedValues.length;
|
|
262
|
+
const tokenProps = varsUsed.length + (enriched.styles_used?.length || 0);
|
|
167
263
|
enriched.token_coverage =
|
|
168
|
-
totalProps > 0 ? Math.round((tokenProps / totalProps) * 100) :
|
|
264
|
+
totalProps > 0 ? Math.round((tokenProps / totalProps) * 100) : 100;
|
|
169
265
|
this.logger.info({ componentId: component.id, coverage: enriched.token_coverage }, "Component enrichment complete");
|
|
170
266
|
return enriched;
|
|
171
267
|
}
|
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { createChildLogger } from "./logger.js";
|
|
3
|
+
const logger = createChildLogger({ component: "figjam-tools" });
|
|
4
|
+
/** Valid sticky note colors */
|
|
5
|
+
const STICKY_COLORS = [
|
|
6
|
+
"YELLOW",
|
|
7
|
+
"BLUE",
|
|
8
|
+
"GREEN",
|
|
9
|
+
"PINK",
|
|
10
|
+
"ORANGE",
|
|
11
|
+
"PURPLE",
|
|
12
|
+
"RED",
|
|
13
|
+
"LIGHT_GRAY",
|
|
14
|
+
"GRAY",
|
|
15
|
+
];
|
|
16
|
+
/** Valid FigJam shape types */
|
|
17
|
+
const SHAPE_TYPES = [
|
|
18
|
+
"ROUNDED_RECTANGLE",
|
|
19
|
+
"DIAMOND",
|
|
20
|
+
"ELLIPSE",
|
|
21
|
+
"TRIANGLE_UP",
|
|
22
|
+
"TRIANGLE_DOWN",
|
|
23
|
+
"PARALLELOGRAM_RIGHT",
|
|
24
|
+
"PARALLELOGRAM_LEFT",
|
|
25
|
+
"ENG_DATABASE",
|
|
26
|
+
"ENG_QUEUE",
|
|
27
|
+
"ENG_FILE",
|
|
28
|
+
"ENG_FOLDER",
|
|
29
|
+
];
|
|
30
|
+
/** Valid FigJam node types for board content filtering */
|
|
31
|
+
const FIGJAM_NODE_TYPES = [
|
|
32
|
+
"STICKY",
|
|
33
|
+
"SHAPE_WITH_TEXT",
|
|
34
|
+
"CONNECTOR",
|
|
35
|
+
"TABLE",
|
|
36
|
+
"CODE_BLOCK",
|
|
37
|
+
"SECTION",
|
|
38
|
+
"FRAME",
|
|
39
|
+
"TEXT",
|
|
40
|
+
];
|
|
41
|
+
/** Maximum items for batch operations to prevent DoS / plugin timeouts */
|
|
42
|
+
const MAX_BATCH_SIZE = 200;
|
|
43
|
+
/** Maximum table dimensions */
|
|
44
|
+
const MAX_TABLE_ROWS = 100;
|
|
45
|
+
const MAX_TABLE_COLUMNS = 50;
|
|
46
|
+
/** Maximum text length per field */
|
|
47
|
+
const MAX_TEXT_LENGTH = 5000;
|
|
48
|
+
/** Maximum code block length */
|
|
49
|
+
const MAX_CODE_LENGTH = 50000;
|
|
50
|
+
/** Maximum node IDs for arrangement */
|
|
51
|
+
const MAX_ARRANGE_NODES = 500;
|
|
52
|
+
/** Maximum nodes to return from board content reads */
|
|
53
|
+
const MAX_READ_NODES = 1000;
|
|
54
|
+
/**
|
|
55
|
+
* Register FigJam-specific tools.
|
|
56
|
+
* These tools only work when the connected file is a FigJam board (editorType === 'figjam').
|
|
57
|
+
* Used by both local mode (src/local.ts) and cloud mode (src/index.ts).
|
|
58
|
+
*/
|
|
59
|
+
export function registerFigJamTools(server, getDesktopConnector) {
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// STICKY NOTE TOOLS
|
|
62
|
+
// ============================================================================
|
|
63
|
+
server.tool("figjam_create_sticky", `Create a sticky note on a FigJam board. Only works in FigJam files.
|
|
64
|
+
|
|
65
|
+
**Colors:** YELLOW, BLUE, GREEN, PINK, ORANGE, PURPLE, RED, LIGHT_GRAY, GRAY (default: YELLOW)`, {
|
|
66
|
+
text: z
|
|
67
|
+
.string()
|
|
68
|
+
.max(MAX_TEXT_LENGTH)
|
|
69
|
+
.describe("Text content for the sticky note"),
|
|
70
|
+
color: z
|
|
71
|
+
.enum(STICKY_COLORS)
|
|
72
|
+
.optional()
|
|
73
|
+
.describe("Sticky color"),
|
|
74
|
+
x: z.number().optional().describe("X position on canvas"),
|
|
75
|
+
y: z.number().optional().describe("Y position on canvas"),
|
|
76
|
+
}, async ({ text, color, x, y }) => {
|
|
77
|
+
try {
|
|
78
|
+
const connector = await getDesktopConnector();
|
|
79
|
+
const result = await connector.createSticky({ text, color, x, y });
|
|
80
|
+
return {
|
|
81
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
logger.error({ error }, "figjam_create_sticky failed");
|
|
86
|
+
return {
|
|
87
|
+
content: [
|
|
88
|
+
{
|
|
89
|
+
type: "text",
|
|
90
|
+
text: JSON.stringify({
|
|
91
|
+
error: error instanceof Error ? error.message : String(error),
|
|
92
|
+
hint: "This tool only works in FigJam files. Make sure the Desktop Bridge plugin is running in a FigJam board.",
|
|
93
|
+
}),
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
isError: true,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
server.tool("figjam_create_stickies", `Batch create multiple sticky notes on a FigJam board (max ${MAX_BATCH_SIZE}). Use this to populate boards from structured data (meeting notes, brainstorm ideas, etc.).
|
|
101
|
+
|
|
102
|
+
**Colors:** YELLOW, BLUE, GREEN, PINK, ORANGE, PURPLE, RED, LIGHT_GRAY, GRAY`, {
|
|
103
|
+
stickies: z
|
|
104
|
+
.array(z.object({
|
|
105
|
+
text: z.string().max(MAX_TEXT_LENGTH).describe("Text content"),
|
|
106
|
+
color: z.enum(STICKY_COLORS).optional().describe("Sticky color"),
|
|
107
|
+
x: z.number().optional().describe("X position"),
|
|
108
|
+
y: z.number().optional().describe("Y position"),
|
|
109
|
+
}))
|
|
110
|
+
.max(MAX_BATCH_SIZE)
|
|
111
|
+
.describe(`Array of sticky note specifications (max ${MAX_BATCH_SIZE})`),
|
|
112
|
+
}, async ({ stickies }) => {
|
|
113
|
+
try {
|
|
114
|
+
const connector = await getDesktopConnector();
|
|
115
|
+
const result = await connector.createStickies({ stickies });
|
|
116
|
+
return {
|
|
117
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
logger.error({ error }, "figjam_create_stickies failed");
|
|
122
|
+
return {
|
|
123
|
+
content: [
|
|
124
|
+
{
|
|
125
|
+
type: "text",
|
|
126
|
+
text: JSON.stringify({
|
|
127
|
+
error: error instanceof Error ? error.message : String(error),
|
|
128
|
+
hint: "This tool only works in FigJam files.",
|
|
129
|
+
}),
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
isError: true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
// ============================================================================
|
|
137
|
+
// CONNECTOR TOOL
|
|
138
|
+
// ============================================================================
|
|
139
|
+
server.tool("figjam_create_connector", `Connect two nodes with a connector line in FigJam. Use to create flowcharts, diagrams, and relationship maps.
|
|
140
|
+
|
|
141
|
+
Nodes must exist on the board (stickies, shapes, etc.). Use their node IDs from creation results.`, {
|
|
142
|
+
startNodeId: z.string().describe("Node ID of the start element"),
|
|
143
|
+
endNodeId: z.string().describe("Node ID of the end element"),
|
|
144
|
+
label: z
|
|
145
|
+
.string()
|
|
146
|
+
.max(MAX_TEXT_LENGTH)
|
|
147
|
+
.optional()
|
|
148
|
+
.describe("Optional text label on the connector"),
|
|
149
|
+
}, async ({ startNodeId, endNodeId, label }) => {
|
|
150
|
+
try {
|
|
151
|
+
const connector = await getDesktopConnector();
|
|
152
|
+
const result = await connector.createConnector({
|
|
153
|
+
startNodeId,
|
|
154
|
+
endNodeId,
|
|
155
|
+
label,
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
logger.error({ error }, "figjam_create_connector failed");
|
|
163
|
+
return {
|
|
164
|
+
content: [
|
|
165
|
+
{
|
|
166
|
+
type: "text",
|
|
167
|
+
text: JSON.stringify({
|
|
168
|
+
error: error instanceof Error ? error.message : String(error),
|
|
169
|
+
hint: "This tool only works in FigJam files. Both start and end nodes must exist.",
|
|
170
|
+
}),
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
isError: true,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// ============================================================================
|
|
178
|
+
// SHAPE WITH TEXT TOOL
|
|
179
|
+
// ============================================================================
|
|
180
|
+
server.tool("figjam_create_shape_with_text", `Create a labeled shape on a FigJam board. Use for flowchart nodes, process diagrams, and visual organization.
|
|
181
|
+
|
|
182
|
+
**Shape types:** ROUNDED_RECTANGLE (default), DIAMOND, ELLIPSE, TRIANGLE_UP, TRIANGLE_DOWN, PARALLELOGRAM_RIGHT, PARALLELOGRAM_LEFT, ENG_DATABASE, ENG_QUEUE, ENG_FILE, ENG_FOLDER`, {
|
|
183
|
+
text: z
|
|
184
|
+
.string()
|
|
185
|
+
.max(MAX_TEXT_LENGTH)
|
|
186
|
+
.optional()
|
|
187
|
+
.describe("Text label for the shape"),
|
|
188
|
+
shapeType: z
|
|
189
|
+
.enum(SHAPE_TYPES)
|
|
190
|
+
.optional()
|
|
191
|
+
.describe("Shape type"),
|
|
192
|
+
x: z.number().optional().describe("X position on canvas"),
|
|
193
|
+
y: z.number().optional().describe("Y position on canvas"),
|
|
194
|
+
}, async ({ text, shapeType, x, y }) => {
|
|
195
|
+
try {
|
|
196
|
+
const connector = await getDesktopConnector();
|
|
197
|
+
const result = await connector.createShapeWithText({
|
|
198
|
+
text,
|
|
199
|
+
shapeType,
|
|
200
|
+
x,
|
|
201
|
+
y,
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
logger.error({ error }, "figjam_create_shape_with_text failed");
|
|
209
|
+
return {
|
|
210
|
+
content: [
|
|
211
|
+
{
|
|
212
|
+
type: "text",
|
|
213
|
+
text: JSON.stringify({
|
|
214
|
+
error: error instanceof Error ? error.message : String(error),
|
|
215
|
+
hint: "This tool only works in FigJam files.",
|
|
216
|
+
}),
|
|
217
|
+
},
|
|
218
|
+
],
|
|
219
|
+
isError: true,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
// ============================================================================
|
|
224
|
+
// TABLE TOOL
|
|
225
|
+
// ============================================================================
|
|
226
|
+
server.tool("figjam_create_table", `Create a table on a FigJam board with optional cell data. Use for structured data display, comparison matrices, and organized information.
|
|
227
|
+
|
|
228
|
+
**Data format:** 2D array of strings, e.g. [["Header1", "Header2"], ["Row1Col1", "Row1Col2"]]`, {
|
|
229
|
+
rows: z
|
|
230
|
+
.number()
|
|
231
|
+
.min(1)
|
|
232
|
+
.max(MAX_TABLE_ROWS)
|
|
233
|
+
.describe(`Number of rows (1-${MAX_TABLE_ROWS})`),
|
|
234
|
+
columns: z
|
|
235
|
+
.number()
|
|
236
|
+
.min(1)
|
|
237
|
+
.max(MAX_TABLE_COLUMNS)
|
|
238
|
+
.describe(`Number of columns (1-${MAX_TABLE_COLUMNS})`),
|
|
239
|
+
data: z
|
|
240
|
+
.array(z.array(z.string().max(MAX_TEXT_LENGTH)))
|
|
241
|
+
.optional()
|
|
242
|
+
.describe("2D array of cell text content (row-major order)"),
|
|
243
|
+
x: z.number().optional().describe("X position on canvas"),
|
|
244
|
+
y: z.number().optional().describe("Y position on canvas"),
|
|
245
|
+
}, async ({ rows, columns, data, x, y }) => {
|
|
246
|
+
try {
|
|
247
|
+
const connector = await getDesktopConnector();
|
|
248
|
+
const result = await connector.createTable({
|
|
249
|
+
rows,
|
|
250
|
+
columns,
|
|
251
|
+
data,
|
|
252
|
+
x,
|
|
253
|
+
y,
|
|
254
|
+
});
|
|
255
|
+
return {
|
|
256
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
logger.error({ error }, "figjam_create_table failed");
|
|
261
|
+
return {
|
|
262
|
+
content: [
|
|
263
|
+
{
|
|
264
|
+
type: "text",
|
|
265
|
+
text: JSON.stringify({
|
|
266
|
+
error: error instanceof Error ? error.message : String(error),
|
|
267
|
+
hint: "This tool only works in FigJam files.",
|
|
268
|
+
}),
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
isError: true,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
// ============================================================================
|
|
276
|
+
// CODE BLOCK TOOL
|
|
277
|
+
// ============================================================================
|
|
278
|
+
server.tool("figjam_create_code_block", `Create a code block on a FigJam board. Use for sharing code snippets, config examples, or technical documentation in collaborative boards.`, {
|
|
279
|
+
code: z.string().max(MAX_CODE_LENGTH).describe("The code content"),
|
|
280
|
+
language: z
|
|
281
|
+
.string()
|
|
282
|
+
.optional()
|
|
283
|
+
.describe("Programming language (e.g., 'JAVASCRIPT', 'PYTHON', 'TYPESCRIPT', 'JSON', 'HTML', 'CSS')"),
|
|
284
|
+
x: z.number().optional().describe("X position on canvas"),
|
|
285
|
+
y: z.number().optional().describe("Y position on canvas"),
|
|
286
|
+
}, async ({ code, language, x, y }) => {
|
|
287
|
+
try {
|
|
288
|
+
const connector = await getDesktopConnector();
|
|
289
|
+
const result = await connector.createCodeBlock({
|
|
290
|
+
code,
|
|
291
|
+
language,
|
|
292
|
+
x,
|
|
293
|
+
y,
|
|
294
|
+
});
|
|
295
|
+
return {
|
|
296
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
logger.error({ error }, "figjam_create_code_block failed");
|
|
301
|
+
return {
|
|
302
|
+
content: [
|
|
303
|
+
{
|
|
304
|
+
type: "text",
|
|
305
|
+
text: JSON.stringify({
|
|
306
|
+
error: error instanceof Error ? error.message : String(error),
|
|
307
|
+
hint: "This tool only works in FigJam files.",
|
|
308
|
+
}),
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
isError: true,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
// ============================================================================
|
|
316
|
+
// LAYOUT HELPER TOOL
|
|
317
|
+
// ============================================================================
|
|
318
|
+
server.tool("figjam_auto_arrange", `Arrange nodes on a FigJam board in a grid, horizontal row, or vertical column layout. Use after batch-creating elements to organize them neatly.`, {
|
|
319
|
+
nodeIds: z
|
|
320
|
+
.array(z.string())
|
|
321
|
+
.max(MAX_ARRANGE_NODES)
|
|
322
|
+
.describe(`Array of node IDs to arrange (max ${MAX_ARRANGE_NODES})`),
|
|
323
|
+
layout: z
|
|
324
|
+
.enum(["grid", "horizontal", "vertical"])
|
|
325
|
+
.optional()
|
|
326
|
+
.default("grid")
|
|
327
|
+
.describe("Layout type: grid, horizontal, or vertical"),
|
|
328
|
+
spacing: z
|
|
329
|
+
.number()
|
|
330
|
+
.optional()
|
|
331
|
+
.default(40)
|
|
332
|
+
.describe("Spacing between nodes in pixels"),
|
|
333
|
+
columns: z
|
|
334
|
+
.number()
|
|
335
|
+
.optional()
|
|
336
|
+
.describe("Number of columns for grid layout (defaults to sqrt of node count)"),
|
|
337
|
+
}, async ({ nodeIds, layout, spacing, columns }) => {
|
|
338
|
+
try {
|
|
339
|
+
const connector = await getDesktopConnector();
|
|
340
|
+
// Compute grid columns safely on the server side — no string interpolation
|
|
341
|
+
const gridCols = columns || Math.ceil(Math.sqrt(nodeIds.length));
|
|
342
|
+
// Pass all parameters as a JSON object to avoid code injection.
|
|
343
|
+
// The plugin code reads from the params object, not interpolated strings.
|
|
344
|
+
const paramsJson = JSON.stringify({
|
|
345
|
+
nodeIds,
|
|
346
|
+
layout,
|
|
347
|
+
spacing,
|
|
348
|
+
gridCols,
|
|
349
|
+
});
|
|
350
|
+
// Use JSON.stringify to produce a properly-escaped double-quoted JS string literal.
|
|
351
|
+
// This handles all control characters including \u2028/\u2029 that manual
|
|
352
|
+
// single-quote escaping would miss.
|
|
353
|
+
const code = `
|
|
354
|
+
const params = JSON.parse(${JSON.stringify(paramsJson)});
|
|
355
|
+
const nodes = [];
|
|
356
|
+
for (const id of params.nodeIds) {
|
|
357
|
+
const node = await figma.getNodeByIdAsync(id);
|
|
358
|
+
if (node) nodes.push(node);
|
|
359
|
+
}
|
|
360
|
+
if (nodes.length === 0) throw new Error('No valid nodes found');
|
|
361
|
+
|
|
362
|
+
let x = nodes[0].x;
|
|
363
|
+
let y = nodes[0].y;
|
|
364
|
+
const startX = x;
|
|
365
|
+
let maxRowHeight = 0;
|
|
366
|
+
|
|
367
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
368
|
+
const node = nodes[i];
|
|
369
|
+
if (params.layout === 'horizontal') {
|
|
370
|
+
node.x = x;
|
|
371
|
+
node.y = y;
|
|
372
|
+
x += node.width + params.spacing;
|
|
373
|
+
} else if (params.layout === 'vertical') {
|
|
374
|
+
node.x = x;
|
|
375
|
+
node.y = y;
|
|
376
|
+
y += node.height + params.spacing;
|
|
377
|
+
} else {
|
|
378
|
+
const col = i % params.gridCols;
|
|
379
|
+
if (col === 0 && i > 0) {
|
|
380
|
+
y += maxRowHeight + params.spacing;
|
|
381
|
+
maxRowHeight = 0;
|
|
382
|
+
x = startX;
|
|
383
|
+
}
|
|
384
|
+
node.x = x;
|
|
385
|
+
node.y = y;
|
|
386
|
+
maxRowHeight = Math.max(maxRowHeight, node.height);
|
|
387
|
+
x += node.width + params.spacing;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return { arranged: nodes.length, layout: params.layout };
|
|
391
|
+
`;
|
|
392
|
+
const result = await connector.executeCodeViaUI(code, 10000);
|
|
393
|
+
return {
|
|
394
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
catch (error) {
|
|
398
|
+
logger.error({ error }, "figjam_auto_arrange failed");
|
|
399
|
+
return {
|
|
400
|
+
content: [
|
|
401
|
+
{
|
|
402
|
+
type: "text",
|
|
403
|
+
text: JSON.stringify({
|
|
404
|
+
error: error instanceof Error ? error.message : String(error),
|
|
405
|
+
hint: "Make sure all node IDs are valid and the Desktop Bridge plugin is running.",
|
|
406
|
+
}),
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
isError: true,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
// ============================================================================
|
|
414
|
+
// READ TOOLS — Query existing FigJam board content
|
|
415
|
+
// ============================================================================
|
|
416
|
+
server.tool("figjam_get_board_contents", `Read all content from a FigJam board. Returns stickies, shapes, connectors, tables, code blocks, and sections with their text content and positions.
|
|
417
|
+
|
|
418
|
+
Use this to understand what's on a board before modifying it, or to extract structured data from collaborative sessions.
|
|
419
|
+
|
|
420
|
+
**Filters:** Pass nodeTypes to limit results (e.g., ["STICKY"] for only stickies). Omit for everything.`, {
|
|
421
|
+
nodeTypes: z
|
|
422
|
+
.array(z.enum(FIGJAM_NODE_TYPES))
|
|
423
|
+
.optional()
|
|
424
|
+
.describe("Filter by node types. Omit for all."),
|
|
425
|
+
maxNodes: z
|
|
426
|
+
.number()
|
|
427
|
+
.min(1)
|
|
428
|
+
.max(MAX_READ_NODES)
|
|
429
|
+
.optional()
|
|
430
|
+
.default(500)
|
|
431
|
+
.describe(`Maximum nodes to return (1-${MAX_READ_NODES}, default: 500)`),
|
|
432
|
+
}, async ({ nodeTypes, maxNodes }) => {
|
|
433
|
+
try {
|
|
434
|
+
const connector = await getDesktopConnector();
|
|
435
|
+
const result = await connector.getBoardContents({
|
|
436
|
+
nodeTypes,
|
|
437
|
+
maxNodes,
|
|
438
|
+
});
|
|
439
|
+
return {
|
|
440
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
logger.error({ error }, "figjam_get_board_contents failed");
|
|
445
|
+
return {
|
|
446
|
+
content: [
|
|
447
|
+
{
|
|
448
|
+
type: "text",
|
|
449
|
+
text: JSON.stringify({
|
|
450
|
+
error: error instanceof Error ? error.message : String(error),
|
|
451
|
+
hint: "This tool only works in FigJam files. Make sure the Desktop Bridge plugin is running in a FigJam board.",
|
|
452
|
+
}),
|
|
453
|
+
},
|
|
454
|
+
],
|
|
455
|
+
isError: true,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
server.tool("figjam_get_connections", `Read the connection graph from a FigJam board. Returns all connectors with their start/end node references and labels.
|
|
460
|
+
|
|
461
|
+
Use this to understand relationships, flowcharts, and diagrams. Returns edges as {startNodeId, endNodeId, label} plus a summary of connected nodes.`, {}, async () => {
|
|
462
|
+
try {
|
|
463
|
+
const connector = await getDesktopConnector();
|
|
464
|
+
const result = await connector.getConnections();
|
|
465
|
+
return {
|
|
466
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
catch (error) {
|
|
470
|
+
logger.error({ error }, "figjam_get_connections failed");
|
|
471
|
+
return {
|
|
472
|
+
content: [
|
|
473
|
+
{
|
|
474
|
+
type: "text",
|
|
475
|
+
text: JSON.stringify({
|
|
476
|
+
error: error instanceof Error ? error.message : String(error),
|
|
477
|
+
hint: "This tool only works in FigJam files. Make sure the Desktop Bridge plugin is running in a FigJam board.",
|
|
478
|
+
}),
|
|
479
|
+
},
|
|
480
|
+
],
|
|
481
|
+
isError: true,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
@@ -72,8 +72,11 @@ export function withTimeout(promise, ms, label) {
|
|
|
72
72
|
const timeoutId = setTimeout(() => {
|
|
73
73
|
reject(new Error(`${label} timed out after ${ms}ms`));
|
|
74
74
|
}, ms);
|
|
75
|
-
// Ensure timeout is cleared if promise resolves first
|
|
76
|
-
|
|
75
|
+
// Ensure timeout is cleared if promise resolves/rejects first.
|
|
76
|
+
// The .catch() prevents an unhandled rejection when the original
|
|
77
|
+
// promise rejects — .finally() returns a new promise that inherits
|
|
78
|
+
// the rejection, and without .catch() it becomes unhandled.
|
|
79
|
+
promise.finally(() => clearTimeout(timeoutId)).catch(() => { });
|
|
77
80
|
});
|
|
78
81
|
return Promise.race([promise, timeoutPromise]);
|
|
79
82
|
}
|
|
@@ -341,8 +344,8 @@ export class FigmaAPI {
|
|
|
341
344
|
/**
|
|
342
345
|
* Helper: Get component metadata with properties
|
|
343
346
|
*/
|
|
344
|
-
async getComponentData(fileKey, nodeId) {
|
|
345
|
-
const response = await this.getNodes(fileKey, [nodeId], { depth
|
|
347
|
+
async getComponentData(fileKey, nodeId, depth = 4) {
|
|
348
|
+
const response = await this.getNodes(fileKey, [nodeId], { depth });
|
|
346
349
|
return response.nodes?.[nodeId];
|
|
347
350
|
}
|
|
348
351
|
/**
|