@atezer/figma-mcp-bridge 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +241 -0
- package/dist/browser/base.d.ts +50 -0
- package/dist/browser/base.d.ts.map +1 -0
- package/dist/browser/base.js +6 -0
- package/dist/browser/base.js.map +1 -0
- package/dist/browser/local.d.ts +81 -0
- package/dist/browser/local.d.ts.map +1 -0
- package/dist/browser/local.js +283 -0
- package/dist/browser/local.js.map +1 -0
- package/dist/cloudflare/browser/base.js +5 -0
- package/dist/cloudflare/browser/cloudflare.js +156 -0
- package/dist/cloudflare/browser-manager.js +157 -0
- package/dist/cloudflare/core/audit-log.js +62 -0
- package/dist/cloudflare/core/config.js +163 -0
- package/dist/cloudflare/core/console-monitor.js +427 -0
- package/dist/cloudflare/core/design-system-manifest.js +260 -0
- package/dist/cloudflare/core/enrichment/enrichment-service.js +272 -0
- package/dist/cloudflare/core/enrichment/index.js +7 -0
- package/dist/cloudflare/core/enrichment/relationship-mapper.js +351 -0
- package/dist/cloudflare/core/enrichment/style-resolver.js +326 -0
- package/dist/cloudflare/core/figma-api.js +273 -0
- package/dist/cloudflare/core/figma-desktop-connector.js +1029 -0
- package/dist/cloudflare/core/figma-reconstruction-spec.js +402 -0
- package/dist/cloudflare/core/figma-style-extractor.js +311 -0
- package/dist/cloudflare/core/figma-tools.js +2883 -0
- package/dist/cloudflare/core/logger.js +53 -0
- package/dist/cloudflare/core/plugin-bridge-connector.js +154 -0
- package/dist/cloudflare/core/plugin-bridge-server.js +174 -0
- package/dist/cloudflare/core/snippet-injector.js +96 -0
- package/dist/cloudflare/core/types/enriched.js +5 -0
- package/dist/cloudflare/core/types/index.js +4 -0
- package/dist/cloudflare/index.js +1061 -0
- package/dist/cloudflare/test-browser.js +88 -0
- package/dist/core/audit-log.d.ts +26 -0
- package/dist/core/audit-log.d.ts.map +1 -0
- package/dist/core/audit-log.js +63 -0
- package/dist/core/audit-log.js.map +1 -0
- package/dist/core/config.d.ts +17 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +164 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/console-monitor.d.ts +82 -0
- package/dist/core/console-monitor.d.ts.map +1 -0
- package/dist/core/console-monitor.js +428 -0
- package/dist/core/console-monitor.js.map +1 -0
- package/dist/core/design-system-manifest.d.ts +272 -0
- package/dist/core/design-system-manifest.d.ts.map +1 -0
- package/dist/core/design-system-manifest.js +261 -0
- package/dist/core/design-system-manifest.js.map +1 -0
- package/dist/core/enrichment/enrichment-service.d.ts +52 -0
- package/dist/core/enrichment/enrichment-service.d.ts.map +1 -0
- package/dist/core/enrichment/enrichment-service.js +273 -0
- package/dist/core/enrichment/enrichment-service.js.map +1 -0
- package/dist/core/enrichment/index.d.ts +8 -0
- package/dist/core/enrichment/index.d.ts.map +1 -0
- package/dist/core/enrichment/index.js +8 -0
- package/dist/core/enrichment/index.js.map +1 -0
- package/dist/core/enrichment/relationship-mapper.d.ts +106 -0
- package/dist/core/enrichment/relationship-mapper.d.ts.map +1 -0
- package/dist/core/enrichment/relationship-mapper.js +352 -0
- package/dist/core/enrichment/relationship-mapper.js.map +1 -0
- package/dist/core/enrichment/style-resolver.d.ts +80 -0
- package/dist/core/enrichment/style-resolver.d.ts.map +1 -0
- package/dist/core/enrichment/style-resolver.js +327 -0
- package/dist/core/enrichment/style-resolver.js.map +1 -0
- package/dist/core/figma-api.d.ts +137 -0
- package/dist/core/figma-api.d.ts.map +1 -0
- package/dist/core/figma-api.js +274 -0
- package/dist/core/figma-api.js.map +1 -0
- package/dist/core/figma-desktop-connector.d.ts +238 -0
- package/dist/core/figma-desktop-connector.d.ts.map +1 -0
- package/dist/core/figma-desktop-connector.js +1030 -0
- package/dist/core/figma-desktop-connector.js.map +1 -0
- package/dist/core/figma-reconstruction-spec.d.ts +166 -0
- package/dist/core/figma-reconstruction-spec.d.ts.map +1 -0
- package/dist/core/figma-reconstruction-spec.js +403 -0
- package/dist/core/figma-reconstruction-spec.js.map +1 -0
- package/dist/core/figma-style-extractor.d.ts +76 -0
- package/dist/core/figma-style-extractor.d.ts.map +1 -0
- package/dist/core/figma-style-extractor.js +312 -0
- package/dist/core/figma-style-extractor.js.map +1 -0
- package/dist/core/figma-tools.d.ts +21 -0
- package/dist/core/figma-tools.d.ts.map +1 -0
- package/dist/core/figma-tools.js +2884 -0
- package/dist/core/figma-tools.js.map +1 -0
- package/dist/core/logger.d.ts +22 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +54 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/plugin-bridge-connector.d.ts +133 -0
- package/dist/core/plugin-bridge-connector.d.ts.map +1 -0
- package/dist/core/plugin-bridge-connector.js +155 -0
- package/dist/core/plugin-bridge-connector.js.map +1 -0
- package/dist/core/plugin-bridge-server.d.ts +42 -0
- package/dist/core/plugin-bridge-server.d.ts.map +1 -0
- package/dist/core/plugin-bridge-server.js +175 -0
- package/dist/core/plugin-bridge-server.js.map +1 -0
- package/dist/core/snippet-injector.d.ts +24 -0
- package/dist/core/snippet-injector.d.ts.map +1 -0
- package/dist/core/snippet-injector.js +97 -0
- package/dist/core/snippet-injector.js.map +1 -0
- package/dist/core/types/enriched.d.ts +213 -0
- package/dist/core/types/enriched.d.ts.map +1 -0
- package/dist/core/types/enriched.js +6 -0
- package/dist/core/types/enriched.js.map +1 -0
- package/dist/core/types/index.d.ts +116 -0
- package/dist/core/types/index.d.ts.map +1 -0
- package/dist/core/types/index.js +5 -0
- package/dist/core/types/index.js.map +1 -0
- package/dist/local-plugin-only.d.ts +13 -0
- package/dist/local-plugin-only.d.ts.map +1 -0
- package/dist/local-plugin-only.js +567 -0
- package/dist/local-plugin-only.js.map +1 -0
- package/dist/local.d.ts +73 -0
- package/dist/local.d.ts.map +1 -0
- package/dist/local.js +2466 -0
- package/dist/local.js.map +1 -0
- package/f-mcp-plugin/README.md +280 -0
- package/f-mcp-plugin/code.js +2222 -0
- package/f-mcp-plugin/manifest.json +14 -0
- package/f-mcp-plugin/ui.html +877 -0
- package/package.json +82 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Figma REST API Client
|
|
3
|
+
* Handles HTTP calls to Figma's REST API for file data, variables, components, and styles
|
|
4
|
+
*/
|
|
5
|
+
import { createChildLogger } from './logger.js';
|
|
6
|
+
const logger = createChildLogger({ component: 'figma-api' });
|
|
7
|
+
const FIGMA_API_BASE = 'https://api.figma.com/v1';
|
|
8
|
+
/**
|
|
9
|
+
* Extract file key from Figma URL
|
|
10
|
+
* @example https://www.figma.com/design/abc123/My-File -> abc123
|
|
11
|
+
*/
|
|
12
|
+
export function extractFileKey(url) {
|
|
13
|
+
try {
|
|
14
|
+
const urlObj = new URL(url);
|
|
15
|
+
// Match patterns like /design/FILE_KEY or /file/FILE_KEY
|
|
16
|
+
const match = urlObj.pathname.match(/\/(design|file)\/([a-zA-Z0-9]+)/);
|
|
17
|
+
return match ? match[2] : null;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
logger.error({ error, url }, 'Failed to extract file key from URL');
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Figma API Client
|
|
26
|
+
* Makes authenticated requests to Figma REST API
|
|
27
|
+
*/
|
|
28
|
+
export class FigmaAPI {
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.accessToken = config.accessToken;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Make authenticated request to Figma API
|
|
34
|
+
*/
|
|
35
|
+
async request(endpoint, options = {}) {
|
|
36
|
+
const url = `${FIGMA_API_BASE}${endpoint}`;
|
|
37
|
+
// Detect token type and use appropriate authentication header
|
|
38
|
+
// OAuth tokens start with 'figu_' and require Authorization: Bearer header
|
|
39
|
+
// Personal Access Tokens use X-Figma-Token header
|
|
40
|
+
const isOAuthToken = this.accessToken.startsWith('figu_');
|
|
41
|
+
// Debug logging to verify token is being used
|
|
42
|
+
const tokenPreview = this.accessToken ? `${this.accessToken.substring(0, 10)}...` : 'NO TOKEN';
|
|
43
|
+
logger.info({
|
|
44
|
+
url,
|
|
45
|
+
tokenPreview,
|
|
46
|
+
hasToken: !!this.accessToken,
|
|
47
|
+
tokenLength: this.accessToken?.length,
|
|
48
|
+
isOAuthToken,
|
|
49
|
+
authMethod: isOAuthToken ? 'Bearer' : 'X-Figma-Token'
|
|
50
|
+
}, 'Making Figma API request with token');
|
|
51
|
+
const headers = {
|
|
52
|
+
'Content-Type': 'application/json',
|
|
53
|
+
...(options.headers || {}),
|
|
54
|
+
};
|
|
55
|
+
// Add authentication header based on token type
|
|
56
|
+
if (isOAuthToken) {
|
|
57
|
+
headers['Authorization'] = `Bearer ${this.accessToken}`;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
headers['X-Figma-Token'] = this.accessToken;
|
|
61
|
+
}
|
|
62
|
+
const response = await fetch(url, {
|
|
63
|
+
...options,
|
|
64
|
+
headers,
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
const errorText = await response.text();
|
|
68
|
+
logger.error({ status: response.status, statusText: response.statusText, body: errorText }, 'Figma API request failed');
|
|
69
|
+
throw new Error(`Figma API error (${response.status}): ${errorText}`);
|
|
70
|
+
}
|
|
71
|
+
const data = await response.json();
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* GET /v1/files/:file_key
|
|
76
|
+
* Get full file data including document tree, components, and styles
|
|
77
|
+
*/
|
|
78
|
+
async getFile(fileKey, options) {
|
|
79
|
+
let endpoint = `/files/${fileKey}`;
|
|
80
|
+
const params = new URLSearchParams();
|
|
81
|
+
if (options?.version)
|
|
82
|
+
params.append('version', options.version);
|
|
83
|
+
if (options?.ids)
|
|
84
|
+
params.append('ids', options.ids.join(','));
|
|
85
|
+
if (options?.depth !== undefined)
|
|
86
|
+
params.append('depth', options.depth.toString());
|
|
87
|
+
if (options?.geometry)
|
|
88
|
+
params.append('geometry', options.geometry);
|
|
89
|
+
if (options?.plugin_data)
|
|
90
|
+
params.append('plugin_data', options.plugin_data);
|
|
91
|
+
if (options?.branch_data)
|
|
92
|
+
params.append('branch_data', 'true');
|
|
93
|
+
if (params.toString()) {
|
|
94
|
+
endpoint += `?${params.toString()}`;
|
|
95
|
+
}
|
|
96
|
+
return this.request(endpoint);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* GET /v1/files/:file_key/variables/local
|
|
100
|
+
* Get local variables (design tokens) from a file
|
|
101
|
+
*/
|
|
102
|
+
async getLocalVariables(fileKey) {
|
|
103
|
+
const response = await this.request(`/files/${fileKey}/variables/local`);
|
|
104
|
+
// Figma API returns {status, error, meta: {variableCollections, variables}}
|
|
105
|
+
// Extract meta to match expected format
|
|
106
|
+
return response.meta || response;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* GET /v1/files/:file_key/variables/published
|
|
110
|
+
* Get published variables from a file
|
|
111
|
+
*/
|
|
112
|
+
async getPublishedVariables(fileKey) {
|
|
113
|
+
const response = await this.request(`/files/${fileKey}/variables/published`);
|
|
114
|
+
// Figma API returns {status, error, meta: {variableCollections, variables}}
|
|
115
|
+
// Extract meta to match expected format
|
|
116
|
+
return response.meta || response;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* GET /v1/files/:file_key/nodes
|
|
120
|
+
* Get specific nodes by ID
|
|
121
|
+
*/
|
|
122
|
+
async getNodes(fileKey, nodeIds, options) {
|
|
123
|
+
let endpoint = `/files/${fileKey}/nodes`;
|
|
124
|
+
const params = new URLSearchParams();
|
|
125
|
+
params.append('ids', nodeIds.join(','));
|
|
126
|
+
if (options?.version)
|
|
127
|
+
params.append('version', options.version);
|
|
128
|
+
if (options?.depth !== undefined)
|
|
129
|
+
params.append('depth', options.depth.toString());
|
|
130
|
+
if (options?.geometry)
|
|
131
|
+
params.append('geometry', options.geometry);
|
|
132
|
+
if (options?.plugin_data)
|
|
133
|
+
params.append('plugin_data', options.plugin_data);
|
|
134
|
+
endpoint += `?${params.toString()}`;
|
|
135
|
+
return this.request(endpoint);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* GET /v1/files/:file_key/styles
|
|
139
|
+
* Get styles from a file
|
|
140
|
+
*/
|
|
141
|
+
async getStyles(fileKey) {
|
|
142
|
+
return this.request(`/files/${fileKey}/styles`);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* GET /v1/files/:file_key/components
|
|
146
|
+
* Get components from a file
|
|
147
|
+
*/
|
|
148
|
+
async getComponents(fileKey) {
|
|
149
|
+
return this.request(`/files/${fileKey}/components`);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* GET /v1/files/:file_key/component_sets
|
|
153
|
+
* Get component sets (variants) from a file
|
|
154
|
+
*/
|
|
155
|
+
async getComponentSets(fileKey) {
|
|
156
|
+
return this.request(`/files/${fileKey}/component_sets`);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* GET /v1/images/:file_key
|
|
160
|
+
* Renders images for specified nodes
|
|
161
|
+
* @param fileKey - The file key
|
|
162
|
+
* @param nodeIds - Node IDs to render (single string or array)
|
|
163
|
+
* @param options - Rendering options
|
|
164
|
+
* @returns Map of node IDs to image URLs (URLs expire after 30 days)
|
|
165
|
+
*/
|
|
166
|
+
async getImages(fileKey, nodeIds, options) {
|
|
167
|
+
const params = new URLSearchParams();
|
|
168
|
+
// Handle single or multiple node IDs
|
|
169
|
+
const ids = Array.isArray(nodeIds) ? nodeIds.join(',') : nodeIds;
|
|
170
|
+
params.append('ids', ids);
|
|
171
|
+
// Add optional parameters
|
|
172
|
+
if (options?.scale !== undefined)
|
|
173
|
+
params.append('scale', options.scale.toString());
|
|
174
|
+
if (options?.format)
|
|
175
|
+
params.append('format', options.format);
|
|
176
|
+
if (options?.svg_outline_text !== undefined)
|
|
177
|
+
params.append('svg_outline_text', options.svg_outline_text.toString());
|
|
178
|
+
if (options?.svg_include_id !== undefined)
|
|
179
|
+
params.append('svg_include_id', options.svg_include_id.toString());
|
|
180
|
+
if (options?.svg_include_node_id !== undefined)
|
|
181
|
+
params.append('svg_include_node_id', options.svg_include_node_id.toString());
|
|
182
|
+
if (options?.svg_simplify_stroke !== undefined)
|
|
183
|
+
params.append('svg_simplify_stroke', options.svg_simplify_stroke.toString());
|
|
184
|
+
if (options?.contents_only !== undefined)
|
|
185
|
+
params.append('contents_only', options.contents_only.toString());
|
|
186
|
+
const endpoint = `/images/${fileKey}?${params.toString()}`;
|
|
187
|
+
logger.info({ fileKey, ids, options }, 'Rendering images');
|
|
188
|
+
return this.request(endpoint);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Helper: Get all design tokens (variables) with formatted output
|
|
192
|
+
*/
|
|
193
|
+
async getAllVariables(fileKey) {
|
|
194
|
+
// Don't catch errors - let them bubble up so proper error messages are shown
|
|
195
|
+
const [local, published] = await Promise.all([
|
|
196
|
+
this.getLocalVariables(fileKey),
|
|
197
|
+
this.getPublishedVariables(fileKey).catch(() => ({ variables: {} })), // Published can fail gracefully
|
|
198
|
+
]);
|
|
199
|
+
return { local, published };
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Helper: Get component metadata with properties
|
|
203
|
+
*/
|
|
204
|
+
async getComponentData(fileKey, nodeId) {
|
|
205
|
+
const response = await this.getNodes(fileKey, [nodeId], { depth: 2 });
|
|
206
|
+
return response.nodes?.[nodeId];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Helper: Search for components by name
|
|
210
|
+
*/
|
|
211
|
+
async searchComponents(fileKey, searchTerm) {
|
|
212
|
+
const { meta } = await this.getComponents(fileKey);
|
|
213
|
+
const components = meta?.components || [];
|
|
214
|
+
return components.filter((comp) => comp.name?.toLowerCase().includes(searchTerm.toLowerCase()));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Helper function to format variables for display
|
|
219
|
+
*/
|
|
220
|
+
export function formatVariables(variablesData) {
|
|
221
|
+
const collections = Object.entries(variablesData.variableCollections || {}).map(([id, collection]) => ({
|
|
222
|
+
id,
|
|
223
|
+
name: collection.name,
|
|
224
|
+
key: collection.key,
|
|
225
|
+
modes: collection.modes,
|
|
226
|
+
variableIds: collection.variableIds,
|
|
227
|
+
}));
|
|
228
|
+
const variables = Object.entries(variablesData.variables || {}).map(([id, variable]) => ({
|
|
229
|
+
id,
|
|
230
|
+
name: variable.name,
|
|
231
|
+
key: variable.key,
|
|
232
|
+
resolvedType: variable.resolvedType,
|
|
233
|
+
valuesByMode: variable.valuesByMode,
|
|
234
|
+
variableCollectionId: variable.variableCollectionId,
|
|
235
|
+
scopes: variable.scopes,
|
|
236
|
+
description: variable.description,
|
|
237
|
+
}));
|
|
238
|
+
const variablesByType = variables.reduce((acc, v) => {
|
|
239
|
+
acc[v.resolvedType] = (acc[v.resolvedType] || 0) + 1;
|
|
240
|
+
return acc;
|
|
241
|
+
}, {});
|
|
242
|
+
return {
|
|
243
|
+
collections,
|
|
244
|
+
variables,
|
|
245
|
+
summary: {
|
|
246
|
+
totalCollections: collections.length,
|
|
247
|
+
totalVariables: variables.length,
|
|
248
|
+
variablesByType,
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Helper function to format component data for display
|
|
254
|
+
*/
|
|
255
|
+
export function formatComponentData(componentNode) {
|
|
256
|
+
return {
|
|
257
|
+
id: componentNode.id,
|
|
258
|
+
name: componentNode.name,
|
|
259
|
+
type: componentNode.type,
|
|
260
|
+
description: componentNode.description,
|
|
261
|
+
descriptionMarkdown: componentNode.descriptionMarkdown,
|
|
262
|
+
properties: componentNode.componentPropertyDefinitions,
|
|
263
|
+
children: componentNode.children?.map((child) => ({
|
|
264
|
+
id: child.id,
|
|
265
|
+
name: child.name,
|
|
266
|
+
type: child.type,
|
|
267
|
+
})),
|
|
268
|
+
bounds: componentNode.absoluteBoundingBox,
|
|
269
|
+
fills: componentNode.fills,
|
|
270
|
+
strokes: componentNode.strokes,
|
|
271
|
+
effects: componentNode.effects,
|
|
272
|
+
};
|
|
273
|
+
}
|