@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.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +241 -0
  3. package/dist/browser/base.d.ts +50 -0
  4. package/dist/browser/base.d.ts.map +1 -0
  5. package/dist/browser/base.js +6 -0
  6. package/dist/browser/base.js.map +1 -0
  7. package/dist/browser/local.d.ts +81 -0
  8. package/dist/browser/local.d.ts.map +1 -0
  9. package/dist/browser/local.js +283 -0
  10. package/dist/browser/local.js.map +1 -0
  11. package/dist/cloudflare/browser/base.js +5 -0
  12. package/dist/cloudflare/browser/cloudflare.js +156 -0
  13. package/dist/cloudflare/browser-manager.js +157 -0
  14. package/dist/cloudflare/core/audit-log.js +62 -0
  15. package/dist/cloudflare/core/config.js +163 -0
  16. package/dist/cloudflare/core/console-monitor.js +427 -0
  17. package/dist/cloudflare/core/design-system-manifest.js +260 -0
  18. package/dist/cloudflare/core/enrichment/enrichment-service.js +272 -0
  19. package/dist/cloudflare/core/enrichment/index.js +7 -0
  20. package/dist/cloudflare/core/enrichment/relationship-mapper.js +351 -0
  21. package/dist/cloudflare/core/enrichment/style-resolver.js +326 -0
  22. package/dist/cloudflare/core/figma-api.js +273 -0
  23. package/dist/cloudflare/core/figma-desktop-connector.js +1029 -0
  24. package/dist/cloudflare/core/figma-reconstruction-spec.js +402 -0
  25. package/dist/cloudflare/core/figma-style-extractor.js +311 -0
  26. package/dist/cloudflare/core/figma-tools.js +2883 -0
  27. package/dist/cloudflare/core/logger.js +53 -0
  28. package/dist/cloudflare/core/plugin-bridge-connector.js +154 -0
  29. package/dist/cloudflare/core/plugin-bridge-server.js +174 -0
  30. package/dist/cloudflare/core/snippet-injector.js +96 -0
  31. package/dist/cloudflare/core/types/enriched.js +5 -0
  32. package/dist/cloudflare/core/types/index.js +4 -0
  33. package/dist/cloudflare/index.js +1061 -0
  34. package/dist/cloudflare/test-browser.js +88 -0
  35. package/dist/core/audit-log.d.ts +26 -0
  36. package/dist/core/audit-log.d.ts.map +1 -0
  37. package/dist/core/audit-log.js +63 -0
  38. package/dist/core/audit-log.js.map +1 -0
  39. package/dist/core/config.d.ts +17 -0
  40. package/dist/core/config.d.ts.map +1 -0
  41. package/dist/core/config.js +164 -0
  42. package/dist/core/config.js.map +1 -0
  43. package/dist/core/console-monitor.d.ts +82 -0
  44. package/dist/core/console-monitor.d.ts.map +1 -0
  45. package/dist/core/console-monitor.js +428 -0
  46. package/dist/core/console-monitor.js.map +1 -0
  47. package/dist/core/design-system-manifest.d.ts +272 -0
  48. package/dist/core/design-system-manifest.d.ts.map +1 -0
  49. package/dist/core/design-system-manifest.js +261 -0
  50. package/dist/core/design-system-manifest.js.map +1 -0
  51. package/dist/core/enrichment/enrichment-service.d.ts +52 -0
  52. package/dist/core/enrichment/enrichment-service.d.ts.map +1 -0
  53. package/dist/core/enrichment/enrichment-service.js +273 -0
  54. package/dist/core/enrichment/enrichment-service.js.map +1 -0
  55. package/dist/core/enrichment/index.d.ts +8 -0
  56. package/dist/core/enrichment/index.d.ts.map +1 -0
  57. package/dist/core/enrichment/index.js +8 -0
  58. package/dist/core/enrichment/index.js.map +1 -0
  59. package/dist/core/enrichment/relationship-mapper.d.ts +106 -0
  60. package/dist/core/enrichment/relationship-mapper.d.ts.map +1 -0
  61. package/dist/core/enrichment/relationship-mapper.js +352 -0
  62. package/dist/core/enrichment/relationship-mapper.js.map +1 -0
  63. package/dist/core/enrichment/style-resolver.d.ts +80 -0
  64. package/dist/core/enrichment/style-resolver.d.ts.map +1 -0
  65. package/dist/core/enrichment/style-resolver.js +327 -0
  66. package/dist/core/enrichment/style-resolver.js.map +1 -0
  67. package/dist/core/figma-api.d.ts +137 -0
  68. package/dist/core/figma-api.d.ts.map +1 -0
  69. package/dist/core/figma-api.js +274 -0
  70. package/dist/core/figma-api.js.map +1 -0
  71. package/dist/core/figma-desktop-connector.d.ts +238 -0
  72. package/dist/core/figma-desktop-connector.d.ts.map +1 -0
  73. package/dist/core/figma-desktop-connector.js +1030 -0
  74. package/dist/core/figma-desktop-connector.js.map +1 -0
  75. package/dist/core/figma-reconstruction-spec.d.ts +166 -0
  76. package/dist/core/figma-reconstruction-spec.d.ts.map +1 -0
  77. package/dist/core/figma-reconstruction-spec.js +403 -0
  78. package/dist/core/figma-reconstruction-spec.js.map +1 -0
  79. package/dist/core/figma-style-extractor.d.ts +76 -0
  80. package/dist/core/figma-style-extractor.d.ts.map +1 -0
  81. package/dist/core/figma-style-extractor.js +312 -0
  82. package/dist/core/figma-style-extractor.js.map +1 -0
  83. package/dist/core/figma-tools.d.ts +21 -0
  84. package/dist/core/figma-tools.d.ts.map +1 -0
  85. package/dist/core/figma-tools.js +2884 -0
  86. package/dist/core/figma-tools.js.map +1 -0
  87. package/dist/core/logger.d.ts +22 -0
  88. package/dist/core/logger.d.ts.map +1 -0
  89. package/dist/core/logger.js +54 -0
  90. package/dist/core/logger.js.map +1 -0
  91. package/dist/core/plugin-bridge-connector.d.ts +133 -0
  92. package/dist/core/plugin-bridge-connector.d.ts.map +1 -0
  93. package/dist/core/plugin-bridge-connector.js +155 -0
  94. package/dist/core/plugin-bridge-connector.js.map +1 -0
  95. package/dist/core/plugin-bridge-server.d.ts +42 -0
  96. package/dist/core/plugin-bridge-server.d.ts.map +1 -0
  97. package/dist/core/plugin-bridge-server.js +175 -0
  98. package/dist/core/plugin-bridge-server.js.map +1 -0
  99. package/dist/core/snippet-injector.d.ts +24 -0
  100. package/dist/core/snippet-injector.d.ts.map +1 -0
  101. package/dist/core/snippet-injector.js +97 -0
  102. package/dist/core/snippet-injector.js.map +1 -0
  103. package/dist/core/types/enriched.d.ts +213 -0
  104. package/dist/core/types/enriched.d.ts.map +1 -0
  105. package/dist/core/types/enriched.js +6 -0
  106. package/dist/core/types/enriched.js.map +1 -0
  107. package/dist/core/types/index.d.ts +116 -0
  108. package/dist/core/types/index.d.ts.map +1 -0
  109. package/dist/core/types/index.js +5 -0
  110. package/dist/core/types/index.js.map +1 -0
  111. package/dist/local-plugin-only.d.ts +13 -0
  112. package/dist/local-plugin-only.d.ts.map +1 -0
  113. package/dist/local-plugin-only.js +567 -0
  114. package/dist/local-plugin-only.js.map +1 -0
  115. package/dist/local.d.ts +73 -0
  116. package/dist/local.d.ts.map +1 -0
  117. package/dist/local.js +2466 -0
  118. package/dist/local.js.map +1 -0
  119. package/f-mcp-plugin/README.md +280 -0
  120. package/f-mcp-plugin/code.js +2222 -0
  121. package/f-mcp-plugin/manifest.json +14 -0
  122. package/f-mcp-plugin/ui.html +877 -0
  123. package/package.json +82 -0
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Local Browser Manager
3
+ * Connects to Figma Desktop via Chrome Remote Debugging Protocol
4
+ */
5
+ import puppeteer from 'puppeteer-core';
6
+ import { createChildLogger } from '../core/logger.js';
7
+ const logger = createChildLogger({ component: 'local-browser' });
8
+ /**
9
+ * Local Browser Manager
10
+ * Connects to existing Figma Desktop instance via remote debugging port
11
+ */
12
+ export class LocalBrowserManager {
13
+ constructor(config) {
14
+ this.browser = null;
15
+ this.page = null;
16
+ this.config = config;
17
+ }
18
+ /**
19
+ * Connect to Figma Desktop via remote debugging port
20
+ */
21
+ async launch() {
22
+ if (this.browser) {
23
+ logger.info('Browser already connected, reusing instance');
24
+ return;
25
+ }
26
+ const { debugHost, debugPort } = this.config;
27
+ const browserURL = `http://${debugHost}:${debugPort}`;
28
+ logger.info({ browserURL }, 'Connecting to Figma Desktop');
29
+ try {
30
+ // Connect to existing browser (Figma Desktop)
31
+ this.browser = await puppeteer.connect({
32
+ browserURL,
33
+ defaultViewport: null, // Use Figma's viewport
34
+ });
35
+ logger.info('Connected to Figma Desktop successfully');
36
+ // Handle disconnection
37
+ this.browser.on('disconnected', () => {
38
+ logger.warn('Disconnected from Figma Desktop');
39
+ this.browser = null;
40
+ this.page = null;
41
+ });
42
+ }
43
+ catch (error) {
44
+ logger.error({ error, browserURL }, 'Failed to connect to Figma Desktop');
45
+ throw new Error(`Failed to connect to Figma Desktop at ${browserURL}.\n\n` +
46
+ `Make sure:\n` +
47
+ `1. Figma Desktop is running\n` +
48
+ `2. Figma was launched with: --remote-debugging-port=${debugPort}\n` +
49
+ `3. "Use Developer VM" is enabled in: Plugins → Development → Use Developer VM\n\n` +
50
+ `macOS launch command:\n` +
51
+ ` open -a "Figma" --args --remote-debugging-port=${debugPort}\n\n` +
52
+ `Error: ${error instanceof Error ? error.message : String(error)}`);
53
+ }
54
+ }
55
+ /**
56
+ * Find the best page for plugin debugging
57
+ * Actively searches for pages with workers across ALL tabs
58
+ */
59
+ async findBestPage() {
60
+ if (!this.browser) {
61
+ return null;
62
+ }
63
+ const pages = await this.browser.pages();
64
+ // Find Figma pages with workers
65
+ const figmaPages = pages.filter(p => {
66
+ const url = p.url();
67
+ return url.includes('figma.com') && !url.includes('devtools');
68
+ });
69
+ if (figmaPages.length === 0) {
70
+ return null;
71
+ }
72
+ // Check each page for workers
73
+ const pagesWithWorkers = figmaPages
74
+ .map(p => ({
75
+ page: p,
76
+ workerCount: p.workers().length,
77
+ url: p.url()
78
+ }))
79
+ .filter(p => p.workerCount > 0)
80
+ .sort((a, b) => b.workerCount - a.workerCount); // Most workers first
81
+ if (pagesWithWorkers.length > 0) {
82
+ logger.info({
83
+ url: pagesWithWorkers[0].url,
84
+ workerCount: pagesWithWorkers[0].workerCount,
85
+ totalPagesWithWorkers: pagesWithWorkers.length
86
+ }, 'Found page with active plugin workers');
87
+ return pagesWithWorkers[0].page;
88
+ }
89
+ // No workers found - prefer design/file pages
90
+ const designPage = figmaPages.find(p => p.url().includes('/design/') || p.url().includes('/file/'));
91
+ return designPage || figmaPages[0];
92
+ }
93
+ /**
94
+ * Get active Figma page or create new one
95
+ * Prefers pages with active plugin workers for plugin debugging
96
+ */
97
+ async getPage() {
98
+ // Ensure connection is alive before proceeding
99
+ await this.ensureConnection();
100
+ if (!this.browser) {
101
+ await this.launch();
102
+ }
103
+ // ALWAYS re-check for best page (don't cache if we might have missed workers)
104
+ const bestPage = await this.findBestPage();
105
+ if (bestPage) {
106
+ const workerCount = bestPage.workers().length;
107
+ logger.info({
108
+ url: bestPage.url(),
109
+ workerCount,
110
+ cached: this.page === bestPage
111
+ }, 'Selected page for monitoring');
112
+ this.page = bestPage;
113
+ return this.page;
114
+ }
115
+ // Fallback: Get any existing page or create new one
116
+ const pages = await this.browser.pages();
117
+ if (pages.length > 0 && pages[0].url() !== 'about:blank') {
118
+ logger.warn({ url: pages[0].url() }, 'No Figma pages found, using first available page');
119
+ this.page = pages[0];
120
+ return this.page;
121
+ }
122
+ // Last resort: Create new page
123
+ logger.warn('No suitable pages found, creating new page in Figma Desktop');
124
+ this.page = await this.browser.newPage();
125
+ return this.page;
126
+ }
127
+ /**
128
+ * Navigate to Figma URL
129
+ */
130
+ async navigateToFigma(figmaUrl) {
131
+ // Ensure connection is alive before navigation
132
+ await this.ensureConnection();
133
+ const page = await this.getPage();
134
+ // Default to Figma homepage if no URL provided
135
+ const url = figmaUrl || 'https://www.figma.com';
136
+ logger.info({ url }, 'Navigating to Figma');
137
+ try {
138
+ await page.goto(url, {
139
+ waitUntil: 'networkidle2',
140
+ timeout: 30000,
141
+ });
142
+ logger.info({ url }, 'Navigation successful');
143
+ return page;
144
+ }
145
+ catch (error) {
146
+ logger.error({ error, url }, 'Navigation failed');
147
+ throw new Error(`Failed to navigate to ${url}: ${error}`);
148
+ }
149
+ }
150
+ /**
151
+ * Reload current page
152
+ */
153
+ async reload(hardReload = false) {
154
+ if (!this.page || this.page.isClosed()) {
155
+ throw new Error('No active page to reload');
156
+ }
157
+ logger.info({ hardReload }, 'Reloading page');
158
+ try {
159
+ await this.page.reload({
160
+ waitUntil: 'networkidle2',
161
+ timeout: 30000,
162
+ });
163
+ logger.info('Page reloaded successfully');
164
+ }
165
+ catch (error) {
166
+ logger.error({ error }, 'Page reload failed');
167
+ throw new Error(`Page reload failed: ${error}`);
168
+ }
169
+ }
170
+ /**
171
+ * Execute JavaScript in page context
172
+ */
173
+ async evaluate(fn) {
174
+ const page = await this.getPage();
175
+ return page.evaluate(fn);
176
+ }
177
+ // Screenshot functionality removed - use Figma REST API's getImages() instead
178
+ // See: figma_take_screenshot and figma_get_component_image tools
179
+ /**
180
+ * Check if browser is connected
181
+ */
182
+ isRunning() {
183
+ return this.browser !== null && this.browser.isConnected();
184
+ }
185
+ /**
186
+ * Disconnect from browser (doesn't close Figma Desktop)
187
+ */
188
+ async close() {
189
+ if (!this.browser) {
190
+ return;
191
+ }
192
+ logger.info('Disconnecting from Figma Desktop');
193
+ try {
194
+ // Just disconnect, don't close Figma Desktop
195
+ this.browser.disconnect();
196
+ this.browser = null;
197
+ this.page = null;
198
+ logger.info('Disconnected from Figma Desktop successfully');
199
+ }
200
+ catch (error) {
201
+ logger.error({ error }, 'Failed to disconnect from browser');
202
+ throw error;
203
+ }
204
+ }
205
+ /**
206
+ * Get current page URL
207
+ */
208
+ getCurrentUrl() {
209
+ if (!this.page || this.page.isClosed()) {
210
+ return null;
211
+ }
212
+ return this.page.url();
213
+ }
214
+ /**
215
+ * Check if the browser connection is still alive
216
+ * Returns false if connection is stale (e.g., after computer sleep)
217
+ */
218
+ async isConnectionAlive() {
219
+ try {
220
+ if (!this.browser || !this.page) {
221
+ return false;
222
+ }
223
+ // Try to get the page title - this will fail if connection is dead
224
+ await this.page.title();
225
+ return true;
226
+ }
227
+ catch (error) {
228
+ logger.warn({ error }, 'Browser connection appears to be dead');
229
+ return false;
230
+ }
231
+ }
232
+ /**
233
+ * Reconnect to Figma Desktop if connection was lost
234
+ * Call this before any operation that requires a live connection
235
+ */
236
+ async ensureConnection() {
237
+ const isAlive = await this.isConnectionAlive();
238
+ if (!isAlive) {
239
+ logger.info('Connection lost, attempting to reconnect to Figma Desktop');
240
+ // Clear stale references
241
+ this.browser = null;
242
+ this.page = null;
243
+ // Reconnect
244
+ await this.launch();
245
+ logger.info('Successfully reconnected to Figma Desktop');
246
+ }
247
+ }
248
+ /**
249
+ * Force a complete reconnection to Figma Desktop
250
+ * Use this when frames become detached or stale even though the browser appears connected
251
+ */
252
+ async forceReconnect() {
253
+ logger.info('Force reconnecting to Figma Desktop');
254
+ // Disconnect current connection if exists
255
+ if (this.browser) {
256
+ try {
257
+ this.browser.disconnect();
258
+ }
259
+ catch (e) {
260
+ // Ignore disconnect errors
261
+ }
262
+ }
263
+ // Clear all references
264
+ this.browser = null;
265
+ this.page = null;
266
+ // Reconnect
267
+ await this.launch();
268
+ logger.info('Force reconnect completed');
269
+ }
270
+ /**
271
+ * Wait for navigation
272
+ */
273
+ async waitForNavigation(timeout = 30000) {
274
+ if (!this.page || this.page.isClosed()) {
275
+ throw new Error('No active page');
276
+ }
277
+ await this.page.waitForNavigation({
278
+ waitUntil: 'networkidle2',
279
+ timeout,
280
+ });
281
+ }
282
+ }
283
+ //# sourceMappingURL=local.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local.js","sourceRoot":"","sources":["../../src/browser/local.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,SAAsC,MAAM,gBAAgB,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGtD,MAAM,MAAM,GAAG,iBAAiB,CAAC,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC,CAAC;AAUjE;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IAK/B,YAAY,MAA0B;QAJ9B,YAAO,GAAmB,IAAI,CAAC;QAC/B,SAAI,GAAgB,IAAI,CAAC;QAIhC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACX,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,MAAM,CAAC,IAAI,CAAC,6CAA6C,CAAC,CAAC;YAC3D,OAAO;QACR,CAAC;QAED,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QAC7C,MA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EnD,0CAA0C;QAC1C,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,IAAI,CAAC;gBACJ,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;YAC3B,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACZ,2BAA2B;YAC5B,CAAC;QACF,CAAC;QAED,uBAAuB;QACvB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,YAAY;QACZ,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACpB,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,iBAAiB,CAAC,OAAO,GAAG,KAAK;QACtC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;QACnC,CAAC;QAED,MAAM,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC;YACjC,SAAS,EAAE,cAAc;YACzB,OAAO;SACP,CAAC,CAAC;IACJ,CAAC;CACD"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Browser Manager Interface
3
+ * Abstract interface for browser automation across different runtimes
4
+ */
5
+ export {};
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Cloudflare Browser Manager
3
+ * Manages Puppeteer browser instance lifecycle for Cloudflare Browser Rendering API
4
+ */
5
+ import puppeteer from '@cloudflare/puppeteer';
6
+ import { createChildLogger } from '../core/logger.js';
7
+ const logger = createChildLogger({ component: 'cloudflare-browser' });
8
+ /**
9
+ * Cloudflare Browser Manager
10
+ * Implements IBrowserManager for Cloudflare Browser Rendering API
11
+ */
12
+ export class CloudflareBrowserManager {
13
+ constructor(env, config) {
14
+ this.browser = null;
15
+ this.page = null;
16
+ this.env = env;
17
+ this.config = config;
18
+ }
19
+ /**
20
+ * Launch browser instance via Cloudflare Browser Rendering API
21
+ */
22
+ async launch() {
23
+ if (this.browser) {
24
+ logger.info('Browser already running, reusing instance');
25
+ return;
26
+ }
27
+ logger.info('Launching browser with Cloudflare Browser Rendering API');
28
+ try {
29
+ this.browser = await puppeteer.launch(this.env.BROWSER, {
30
+ keep_alive: 600000, // Keep alive for 10 minutes
31
+ });
32
+ logger.info('Browser launched successfully');
33
+ }
34
+ catch (error) {
35
+ logger.error({ error }, 'Failed to launch browser');
36
+ throw new Error(`Browser launch failed: ${error}`);
37
+ }
38
+ }
39
+ /**
40
+ * Get or create a page instance
41
+ */
42
+ async getPage() {
43
+ if (!this.browser) {
44
+ await this.launch();
45
+ }
46
+ if (this.page && !this.page.isClosed()) {
47
+ return this.page;
48
+ }
49
+ logger.info('Creating new browser page');
50
+ this.page = await this.browser.newPage();
51
+ // Set viewport size
52
+ await this.page.setViewport({
53
+ width: 1920,
54
+ height: 1080,
55
+ deviceScaleFactor: 1,
56
+ });
57
+ logger.info('Browser page created');
58
+ return this.page;
59
+ }
60
+ /**
61
+ * Navigate to Figma URL
62
+ */
63
+ async navigateToFigma(figmaUrl) {
64
+ const page = await this.getPage();
65
+ // Default to Figma homepage if no URL provided
66
+ const url = figmaUrl || 'https://www.figma.com';
67
+ logger.info({ url }, 'Navigating to Figma');
68
+ try {
69
+ await page.goto(url, {
70
+ waitUntil: 'networkidle2',
71
+ timeout: 30000,
72
+ });
73
+ logger.info({ url }, 'Navigation successful');
74
+ return page;
75
+ }
76
+ catch (error) {
77
+ logger.error({ error, url }, 'Navigation failed');
78
+ throw new Error(`Failed to navigate to ${url}: ${error}`);
79
+ }
80
+ }
81
+ /**
82
+ * Reload current page
83
+ */
84
+ async reload(hardReload = false) {
85
+ if (!this.page || this.page.isClosed()) {
86
+ throw new Error('No active page to reload');
87
+ }
88
+ logger.info({ hardReload }, 'Reloading page');
89
+ try {
90
+ await this.page.reload({
91
+ waitUntil: 'networkidle2',
92
+ timeout: 30000,
93
+ });
94
+ logger.info('Page reloaded successfully');
95
+ }
96
+ catch (error) {
97
+ logger.error({ error }, 'Page reload failed');
98
+ throw new Error(`Page reload failed: ${error}`);
99
+ }
100
+ }
101
+ /**
102
+ * Execute JavaScript in page context
103
+ */
104
+ async evaluate(fn) {
105
+ const page = await this.getPage();
106
+ return page.evaluate(fn);
107
+ }
108
+ // Screenshot functionality removed - use Figma REST API's getImages() instead
109
+ // See: figma_take_screenshot and figma_get_component_image tools
110
+ /**
111
+ * Check if browser is running
112
+ */
113
+ isRunning() {
114
+ return this.browser !== null && this.browser.isConnected();
115
+ }
116
+ /**
117
+ * Close browser instance
118
+ */
119
+ async close() {
120
+ if (!this.browser) {
121
+ return;
122
+ }
123
+ logger.info('Closing browser');
124
+ try {
125
+ await this.browser.close();
126
+ this.browser = null;
127
+ this.page = null;
128
+ logger.info('Browser closed successfully');
129
+ }
130
+ catch (error) {
131
+ logger.error({ error }, 'Failed to close browser');
132
+ throw error;
133
+ }
134
+ }
135
+ /**
136
+ * Get current page URL
137
+ */
138
+ getCurrentUrl() {
139
+ if (!this.page || this.page.isClosed()) {
140
+ return null;
141
+ }
142
+ return this.page.url();
143
+ }
144
+ /**
145
+ * Wait for navigation
146
+ */
147
+ async waitForNavigation(timeout = 30000) {
148
+ if (!this.page || this.page.isClosed()) {
149
+ throw new Error('No active page');
150
+ }
151
+ await this.page.waitForNavigation({
152
+ waitUntil: 'networkidle2',
153
+ timeout,
154
+ });
155
+ }
156
+ }
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Browser Manager
3
+ * Manages Puppeteer browser instance lifecycle for Cloudflare Browser Rendering API
4
+ */
5
+ import puppeteer from '@cloudflare/puppeteer';
6
+ import { createChildLogger } from './core/logger.js';
7
+ const logger = createChildLogger({ component: 'browser-manager' });
8
+ /**
9
+ * Browser Manager
10
+ * Handles browser instance creation, page management, and navigation
11
+ */
12
+ export class BrowserManager {
13
+ constructor(env, config) {
14
+ this.browser = null;
15
+ this.page = null;
16
+ this.env = env;
17
+ this.config = config;
18
+ }
19
+ /**
20
+ * Launch browser instance
21
+ */
22
+ async launch() {
23
+ if (this.browser) {
24
+ logger.info('Browser already running, reusing instance');
25
+ return this.browser;
26
+ }
27
+ logger.info('Launching browser with Cloudflare Browser Rendering API');
28
+ try {
29
+ this.browser = await puppeteer.launch(this.env.BROWSER, {
30
+ keep_alive: 600000, // Keep alive for 10 minutes
31
+ });
32
+ logger.info('Browser launched successfully');
33
+ return this.browser;
34
+ }
35
+ catch (error) {
36
+ logger.error({ error }, 'Failed to launch browser');
37
+ throw new Error(`Browser launch failed: ${error}`);
38
+ }
39
+ }
40
+ /**
41
+ * Get or create a page instance
42
+ */
43
+ async getPage() {
44
+ if (!this.browser) {
45
+ await this.launch();
46
+ }
47
+ if (this.page && !this.page.isClosed()) {
48
+ return this.page;
49
+ }
50
+ logger.info('Creating new browser page');
51
+ this.page = await this.browser.newPage();
52
+ // Set viewport size
53
+ await this.page.setViewport({
54
+ width: 1920,
55
+ height: 1080,
56
+ deviceScaleFactor: 1,
57
+ });
58
+ logger.info('Browser page created');
59
+ return this.page;
60
+ }
61
+ /**
62
+ * Navigate to Figma URL
63
+ */
64
+ async navigateToFigma(figmaUrl) {
65
+ const page = await this.getPage();
66
+ // Default to Figma homepage if no URL provided
67
+ const url = figmaUrl || 'https://www.figma.com';
68
+ logger.info({ url }, 'Navigating to Figma');
69
+ try {
70
+ await page.goto(url, {
71
+ waitUntil: 'networkidle2',
72
+ timeout: 30000,
73
+ });
74
+ logger.info({ url }, 'Navigation successful');
75
+ return page;
76
+ }
77
+ catch (error) {
78
+ logger.error({ error, url }, 'Navigation failed');
79
+ throw new Error(`Failed to navigate to ${url}: ${error}`);
80
+ }
81
+ }
82
+ /**
83
+ * Reload current page
84
+ */
85
+ async reload(hardReload = false) {
86
+ if (!this.page || this.page.isClosed()) {
87
+ throw new Error('No active page to reload');
88
+ }
89
+ logger.info({ hardReload }, 'Reloading page');
90
+ try {
91
+ await this.page.reload({
92
+ waitUntil: 'networkidle2',
93
+ timeout: 30000,
94
+ });
95
+ logger.info('Page reloaded successfully');
96
+ }
97
+ catch (error) {
98
+ logger.error({ error }, 'Page reload failed');
99
+ throw new Error(`Page reload failed: ${error}`);
100
+ }
101
+ }
102
+ /**
103
+ * Execute JavaScript in page context
104
+ */
105
+ async evaluate(fn) {
106
+ const page = await this.getPage();
107
+ return page.evaluate(fn);
108
+ }
109
+ // Screenshot functionality removed - use Figma REST API's getImages() instead
110
+ // See: figma_take_screenshot and figma_get_component_image tools
111
+ /**
112
+ * Check if browser is running
113
+ */
114
+ isRunning() {
115
+ return this.browser !== null && this.browser.isConnected();
116
+ }
117
+ /**
118
+ * Close browser instance
119
+ */
120
+ async close() {
121
+ if (!this.browser) {
122
+ return;
123
+ }
124
+ logger.info('Closing browser');
125
+ try {
126
+ await this.browser.close();
127
+ this.browser = null;
128
+ this.page = null;
129
+ logger.info('Browser closed successfully');
130
+ }
131
+ catch (error) {
132
+ logger.error({ error }, 'Failed to close browser');
133
+ throw error;
134
+ }
135
+ }
136
+ /**
137
+ * Get current page URL
138
+ */
139
+ getCurrentUrl() {
140
+ if (!this.page || this.page.isClosed()) {
141
+ return null;
142
+ }
143
+ return this.page.url();
144
+ }
145
+ /**
146
+ * Wait for navigation
147
+ */
148
+ async waitForNavigation(timeout = 30000) {
149
+ if (!this.page || this.page.isClosed()) {
150
+ throw new Error('No active page');
151
+ }
152
+ await this.page.waitForNavigation({
153
+ waitUntil: 'networkidle2',
154
+ timeout,
155
+ });
156
+ }
157
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Enterprise audit log — tool invocations and connection events for compliance.
3
+ * Optional: enable via FIGMA_MCP_AUDIT_LOG_PATH (file path) or config local.auditLogPath.
4
+ * Format: one JSON object per line (NDJSON) for easy parsing and retention.
5
+ */
6
+ import { createWriteStream } from "fs";
7
+ import { appendFileSync } from "fs";
8
+ const noop = () => { };
9
+ let stream = null;
10
+ function getStream(path) {
11
+ if (stream)
12
+ return stream;
13
+ try {
14
+ stream = createWriteStream(path, { flags: "a" });
15
+ stream.on("error", () => {
16
+ stream = null;
17
+ });
18
+ return stream;
19
+ }
20
+ catch {
21
+ return null;
22
+ }
23
+ }
24
+ function writeLine(path, line) {
25
+ const s = getStream(path);
26
+ if (s && s.writable) {
27
+ s.write(line + "\n");
28
+ return;
29
+ }
30
+ try {
31
+ appendFileSync(path, line + "\n");
32
+ }
33
+ catch {
34
+ // ignore
35
+ }
36
+ }
37
+ /**
38
+ * Log an audit entry. No-op if path not set or write fails.
39
+ */
40
+ export function auditLog(path, entry) {
41
+ if (!path || path === "")
42
+ return;
43
+ const full = { ...entry, ts: new Date().toISOString() };
44
+ try {
45
+ writeLine(path, JSON.stringify(full));
46
+ }
47
+ catch {
48
+ noop();
49
+ }
50
+ }
51
+ /**
52
+ * Log a tool invocation (call from bridge after request completes).
53
+ */
54
+ export function auditTool(path, method, success, error, durationMs) {
55
+ auditLog(path, { event: "tool", method, success, error, durationMs });
56
+ }
57
+ /**
58
+ * Log plugin connection / disconnection.
59
+ */
60
+ export function auditPlugin(path, event) {
61
+ auditLog(path, { event });
62
+ }