@fragments-sdk/cli 0.2.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.
Files changed (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +106 -0
  3. package/dist/bin.d.ts +1 -0
  4. package/dist/bin.js +4783 -0
  5. package/dist/bin.js.map +1 -0
  6. package/dist/chunk-4FDQSGKX.js +786 -0
  7. package/dist/chunk-4FDQSGKX.js.map +1 -0
  8. package/dist/chunk-7H2MMGYG.js +369 -0
  9. package/dist/chunk-7H2MMGYG.js.map +1 -0
  10. package/dist/chunk-BSCG3IP7.js +619 -0
  11. package/dist/chunk-BSCG3IP7.js.map +1 -0
  12. package/dist/chunk-LY2CFFPY.js +898 -0
  13. package/dist/chunk-LY2CFFPY.js.map +1 -0
  14. package/dist/chunk-MUZ6CM66.js +6636 -0
  15. package/dist/chunk-MUZ6CM66.js.map +1 -0
  16. package/dist/chunk-OAENNG3G.js +1489 -0
  17. package/dist/chunk-OAENNG3G.js.map +1 -0
  18. package/dist/chunk-XHNKNI6J.js +235 -0
  19. package/dist/chunk-XHNKNI6J.js.map +1 -0
  20. package/dist/core-DWKLGY4N.js +68 -0
  21. package/dist/core-DWKLGY4N.js.map +1 -0
  22. package/dist/generate-4LQNJ7SX.js +249 -0
  23. package/dist/generate-4LQNJ7SX.js.map +1 -0
  24. package/dist/index.d.ts +775 -0
  25. package/dist/index.js +41 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/init-EMVI47QG.js +416 -0
  28. package/dist/init-EMVI47QG.js.map +1 -0
  29. package/dist/mcp-bin.d.ts +1 -0
  30. package/dist/mcp-bin.js +1117 -0
  31. package/dist/mcp-bin.js.map +1 -0
  32. package/dist/scan-4YPRF7FV.js +12 -0
  33. package/dist/scan-4YPRF7FV.js.map +1 -0
  34. package/dist/service-QSZMZJBJ.js +208 -0
  35. package/dist/service-QSZMZJBJ.js.map +1 -0
  36. package/dist/static-viewer-MIPGZ4Z7.js +12 -0
  37. package/dist/static-viewer-MIPGZ4Z7.js.map +1 -0
  38. package/dist/test-SQ5ZHXWU.js +1067 -0
  39. package/dist/test-SQ5ZHXWU.js.map +1 -0
  40. package/dist/tokens-HSGMYK64.js +173 -0
  41. package/dist/tokens-HSGMYK64.js.map +1 -0
  42. package/dist/viewer-YRF4SQE4.js +11101 -0
  43. package/dist/viewer-YRF4SQE4.js.map +1 -0
  44. package/package.json +107 -0
  45. package/src/ai.ts +266 -0
  46. package/src/analyze.ts +265 -0
  47. package/src/bin.ts +916 -0
  48. package/src/build.ts +248 -0
  49. package/src/commands/a11y.ts +302 -0
  50. package/src/commands/add.ts +313 -0
  51. package/src/commands/audit.ts +195 -0
  52. package/src/commands/baseline.ts +221 -0
  53. package/src/commands/build.ts +144 -0
  54. package/src/commands/compare.ts +337 -0
  55. package/src/commands/context.ts +107 -0
  56. package/src/commands/dev.ts +107 -0
  57. package/src/commands/enhance.ts +858 -0
  58. package/src/commands/generate.ts +391 -0
  59. package/src/commands/init.ts +531 -0
  60. package/src/commands/link/figma.ts +645 -0
  61. package/src/commands/link/index.ts +10 -0
  62. package/src/commands/link/storybook.ts +267 -0
  63. package/src/commands/list.ts +49 -0
  64. package/src/commands/metrics.ts +114 -0
  65. package/src/commands/reset.ts +242 -0
  66. package/src/commands/scan.ts +537 -0
  67. package/src/commands/storygen.ts +207 -0
  68. package/src/commands/tokens.ts +251 -0
  69. package/src/commands/validate.ts +93 -0
  70. package/src/commands/verify.ts +215 -0
  71. package/src/core/composition.test.ts +262 -0
  72. package/src/core/composition.ts +255 -0
  73. package/src/core/config.ts +84 -0
  74. package/src/core/constants.ts +111 -0
  75. package/src/core/context.ts +380 -0
  76. package/src/core/defineSegment.ts +137 -0
  77. package/src/core/discovery.ts +337 -0
  78. package/src/core/figma.ts +263 -0
  79. package/src/core/fragment-types.ts +214 -0
  80. package/src/core/generators/context.ts +389 -0
  81. package/src/core/generators/index.ts +23 -0
  82. package/src/core/generators/registry.ts +364 -0
  83. package/src/core/generators/typescript-extractor.ts +374 -0
  84. package/src/core/importAnalyzer.ts +217 -0
  85. package/src/core/index.ts +149 -0
  86. package/src/core/loader.ts +155 -0
  87. package/src/core/node.ts +63 -0
  88. package/src/core/parser.ts +551 -0
  89. package/src/core/previewLoader.ts +172 -0
  90. package/src/core/schema/fragment.schema.json +189 -0
  91. package/src/core/schema/registry.schema.json +137 -0
  92. package/src/core/schema.ts +182 -0
  93. package/src/core/storyAdapter.test.ts +571 -0
  94. package/src/core/storyAdapter.ts +761 -0
  95. package/src/core/token-types.ts +287 -0
  96. package/src/core/types.ts +754 -0
  97. package/src/diff.ts +323 -0
  98. package/src/index.ts +43 -0
  99. package/src/mcp/__tests__/projectFields.test.ts +130 -0
  100. package/src/mcp/bin.ts +36 -0
  101. package/src/mcp/index.ts +8 -0
  102. package/src/mcp/server.ts +1310 -0
  103. package/src/mcp/utils.ts +54 -0
  104. package/src/mcp-bin.ts +36 -0
  105. package/src/migrate/__tests__/argTypes/argTypes.test.ts +189 -0
  106. package/src/migrate/__tests__/args/args.test.ts +452 -0
  107. package/src/migrate/__tests__/meta/meta.test.ts +198 -0
  108. package/src/migrate/__tests__/stories/stories.test.ts +278 -0
  109. package/src/migrate/__tests__/utils/utils.test.ts +371 -0
  110. package/src/migrate/__tests__/values/values.test.ts +303 -0
  111. package/src/migrate/bin.ts +108 -0
  112. package/src/migrate/converter.ts +658 -0
  113. package/src/migrate/detect.ts +196 -0
  114. package/src/migrate/index.ts +45 -0
  115. package/src/migrate/migrate.ts +163 -0
  116. package/src/migrate/parser.ts +1136 -0
  117. package/src/migrate/report.ts +624 -0
  118. package/src/migrate/types.ts +169 -0
  119. package/src/screenshot.ts +249 -0
  120. package/src/service/__tests__/ast-utils.test.ts +426 -0
  121. package/src/service/__tests__/enhance-scanner.test.ts +200 -0
  122. package/src/service/__tests__/figma/figma.test.ts +652 -0
  123. package/src/service/__tests__/metrics-store.test.ts +409 -0
  124. package/src/service/__tests__/patch-generator.test.ts +186 -0
  125. package/src/service/__tests__/props-extractor.test.ts +365 -0
  126. package/src/service/__tests__/token-registry.test.ts +267 -0
  127. package/src/service/analytics.ts +659 -0
  128. package/src/service/ast-utils.ts +444 -0
  129. package/src/service/browser-pool.ts +339 -0
  130. package/src/service/capture.ts +267 -0
  131. package/src/service/diff.ts +279 -0
  132. package/src/service/enhance/aggregator.ts +489 -0
  133. package/src/service/enhance/cache.ts +275 -0
  134. package/src/service/enhance/codebase-scanner.ts +357 -0
  135. package/src/service/enhance/context-generator.ts +529 -0
  136. package/src/service/enhance/doc-extractor.ts +523 -0
  137. package/src/service/enhance/index.ts +131 -0
  138. package/src/service/enhance/props-extractor.ts +665 -0
  139. package/src/service/enhance/scanner.ts +445 -0
  140. package/src/service/enhance/storybook-parser.ts +552 -0
  141. package/src/service/enhance/types.ts +346 -0
  142. package/src/service/enhance/variant-renderer.ts +479 -0
  143. package/src/service/figma.ts +1008 -0
  144. package/src/service/index.ts +249 -0
  145. package/src/service/metrics-store.ts +333 -0
  146. package/src/service/patch-generator.ts +349 -0
  147. package/src/service/report.ts +854 -0
  148. package/src/service/storage.ts +401 -0
  149. package/src/service/token-fixes.ts +281 -0
  150. package/src/service/token-parser.ts +504 -0
  151. package/src/service/token-registry.ts +721 -0
  152. package/src/service/utils.ts +172 -0
  153. package/src/setup.ts +241 -0
  154. package/src/shared/command-wrapper.ts +81 -0
  155. package/src/shared/dev-server-client.ts +199 -0
  156. package/src/shared/index.ts +8 -0
  157. package/src/shared/segment-loader.ts +59 -0
  158. package/src/shared/types.ts +147 -0
  159. package/src/static-viewer.ts +715 -0
  160. package/src/test/discovery.ts +172 -0
  161. package/src/test/index.ts +281 -0
  162. package/src/test/reporters/console.ts +194 -0
  163. package/src/test/reporters/json.ts +190 -0
  164. package/src/test/reporters/junit.ts +186 -0
  165. package/src/test/runner.ts +598 -0
  166. package/src/test/types.ts +245 -0
  167. package/src/test/watch.ts +200 -0
  168. package/src/validators.ts +152 -0
  169. package/src/viewer/__tests__/jsx-parser.test.ts +502 -0
  170. package/src/viewer/__tests__/render-utils.test.ts +232 -0
  171. package/src/viewer/__tests__/style-utils.test.ts +404 -0
  172. package/src/viewer/bin.ts +86 -0
  173. package/src/viewer/cli/health.ts +256 -0
  174. package/src/viewer/cli/index.ts +33 -0
  175. package/src/viewer/cli/scan.ts +124 -0
  176. package/src/viewer/cli/utils.ts +174 -0
  177. package/src/viewer/components/AccessibilityPanel.tsx +1404 -0
  178. package/src/viewer/components/ActionCapture.tsx +172 -0
  179. package/src/viewer/components/ActionsPanel.tsx +371 -0
  180. package/src/viewer/components/App.tsx +638 -0
  181. package/src/viewer/components/BottomPanel.tsx +224 -0
  182. package/src/viewer/components/CodePanel.tsx +589 -0
  183. package/src/viewer/components/CommandPalette.tsx +336 -0
  184. package/src/viewer/components/ComponentGraph.tsx +394 -0
  185. package/src/viewer/components/ComponentHeader.tsx +85 -0
  186. package/src/viewer/components/ContractPanel.tsx +234 -0
  187. package/src/viewer/components/ErrorBoundary.tsx +85 -0
  188. package/src/viewer/components/FigmaEmbed.tsx +231 -0
  189. package/src/viewer/components/FragmentEditor.tsx +485 -0
  190. package/src/viewer/components/HealthDashboard.tsx +452 -0
  191. package/src/viewer/components/HmrStatusIndicator.tsx +71 -0
  192. package/src/viewer/components/Icons.tsx +417 -0
  193. package/src/viewer/components/InteractionsPanel.tsx +720 -0
  194. package/src/viewer/components/IsolatedPreviewFrame.tsx +321 -0
  195. package/src/viewer/components/IsolatedRender.tsx +111 -0
  196. package/src/viewer/components/KeyboardShortcutsHelp.tsx +89 -0
  197. package/src/viewer/components/LandingPage.tsx +441 -0
  198. package/src/viewer/components/Layout.tsx +22 -0
  199. package/src/viewer/components/LeftSidebar.tsx +391 -0
  200. package/src/viewer/components/MultiViewportPreview.tsx +429 -0
  201. package/src/viewer/components/PreviewArea.tsx +404 -0
  202. package/src/viewer/components/PreviewFrameHost.tsx +310 -0
  203. package/src/viewer/components/PreviewPane.tsx +150 -0
  204. package/src/viewer/components/PreviewToolbar.tsx +176 -0
  205. package/src/viewer/components/PropsEditor.tsx +512 -0
  206. package/src/viewer/components/PropsTable.tsx +98 -0
  207. package/src/viewer/components/RelationsSection.tsx +57 -0
  208. package/src/viewer/components/ResizablePanel.tsx +328 -0
  209. package/src/viewer/components/RightSidebar.tsx +118 -0
  210. package/src/viewer/components/ScreenshotButton.tsx +90 -0
  211. package/src/viewer/components/Sidebar.tsx +169 -0
  212. package/src/viewer/components/SkeletonLoader.tsx +156 -0
  213. package/src/viewer/components/StoryRenderer.tsx +128 -0
  214. package/src/viewer/components/ThemeProvider.tsx +96 -0
  215. package/src/viewer/components/Toast.tsx +67 -0
  216. package/src/viewer/components/TokenStylePanel.tsx +708 -0
  217. package/src/viewer/components/UsageSection.tsx +95 -0
  218. package/src/viewer/components/VariantMatrix.tsx +350 -0
  219. package/src/viewer/components/VariantRenderer.tsx +131 -0
  220. package/src/viewer/components/VariantTabs.tsx +84 -0
  221. package/src/viewer/components/ViewportSelector.tsx +165 -0
  222. package/src/viewer/components/_future/CreatePage.tsx +836 -0
  223. package/src/viewer/composition-renderer.ts +381 -0
  224. package/src/viewer/constants/index.ts +1 -0
  225. package/src/viewer/constants/ui.ts +185 -0
  226. package/src/viewer/entry.tsx +299 -0
  227. package/src/viewer/hooks/index.ts +2 -0
  228. package/src/viewer/hooks/useA11yCache.ts +383 -0
  229. package/src/viewer/hooks/useA11yService.ts +498 -0
  230. package/src/viewer/hooks/useActions.ts +138 -0
  231. package/src/viewer/hooks/useAppState.ts +124 -0
  232. package/src/viewer/hooks/useFigmaIntegration.ts +132 -0
  233. package/src/viewer/hooks/useHmrStatus.ts +109 -0
  234. package/src/viewer/hooks/useKeyboardShortcuts.ts +222 -0
  235. package/src/viewer/hooks/usePreviewBridge.ts +347 -0
  236. package/src/viewer/hooks/useScrollSpy.ts +78 -0
  237. package/src/viewer/hooks/useUrlState.ts +330 -0
  238. package/src/viewer/hooks/useViewSettings.ts +125 -0
  239. package/src/viewer/index.html +28 -0
  240. package/src/viewer/index.ts +14 -0
  241. package/src/viewer/intelligence/healthReport.ts +505 -0
  242. package/src/viewer/intelligence/styleDrift.ts +340 -0
  243. package/src/viewer/intelligence/usageScanner.ts +309 -0
  244. package/src/viewer/jsx-parser.ts +485 -0
  245. package/src/viewer/postcss.config.js +6 -0
  246. package/src/viewer/preview-frame-entry.tsx +25 -0
  247. package/src/viewer/preview-frame.html +109 -0
  248. package/src/viewer/render-template.html +68 -0
  249. package/src/viewer/render-utils.ts +170 -0
  250. package/src/viewer/server.ts +276 -0
  251. package/src/viewer/style-utils.ts +414 -0
  252. package/src/viewer/styles/globals.css +355 -0
  253. package/src/viewer/tailwind.config.js +37 -0
  254. package/src/viewer/types/a11y.ts +197 -0
  255. package/src/viewer/utils/a11y-fixes.ts +471 -0
  256. package/src/viewer/utils/actionExport.ts +372 -0
  257. package/src/viewer/utils/colorSchemes.ts +201 -0
  258. package/src/viewer/utils/detectRelationships.ts +256 -0
  259. package/src/viewer/vite-plugin.ts +2143 -0
@@ -0,0 +1,1310 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import {
4
+ CallToolRequestSchema,
5
+ ListToolsRequestSchema,
6
+ type Tool,
7
+ } from '@modelcontextprotocol/sdk/types.js';
8
+ import {
9
+ BRAND,
10
+ DEFAULTS,
11
+ generateContext,
12
+ type CompiledSegmentsFile,
13
+ type VerifyResult,
14
+ type Theme,
15
+ } from '../core/index.js';
16
+ // ../service is lazy-imported to avoid requiring playwright at startup.
17
+ // Visual tools (render, fix) load it on first use.
18
+ type ServiceModule = typeof import('../service/index.js');
19
+ let _service: ServiceModule | null = null;
20
+ async function getService(): Promise<ServiceModule> {
21
+ if (!_service) {
22
+ try {
23
+ _service = await import('../service/index.js');
24
+ } catch {
25
+ throw new Error(
26
+ 'Visual tools require playwright. Install it with: npm install playwright'
27
+ );
28
+ }
29
+ }
30
+ return _service;
31
+ }
32
+ import { readFile } from 'node:fs/promises';
33
+ import { existsSync, readFileSync } from 'node:fs';
34
+ import { join, dirname, resolve } from 'node:path';
35
+ import { projectFields } from './utils.js';
36
+
37
+ /**
38
+ * MCP Tool names - derived from BRAND constants
39
+ */
40
+ const TOOL_NAMES = {
41
+ discover: `${BRAND.nameLower}_discover`,
42
+ inspect: `${BRAND.nameLower}_inspect`,
43
+ recipe: `${BRAND.nameLower}_recipe`,
44
+ render: `${BRAND.nameLower}_render`,
45
+ fix: `${BRAND.nameLower}_fix`,
46
+ } as const;
47
+
48
+ /**
49
+ * Placeholder patterns to filter out from usage text.
50
+ * These are auto-generated and provide no value to AI agents.
51
+ */
52
+ const PLACEHOLDER_PATTERNS = [
53
+ /^\w+ component is needed$/i,
54
+ /^Alternative component is more appropriate$/i,
55
+ /^Use \w+ when you need/i,
56
+ ];
57
+
58
+ /**
59
+ * Filter out placeholder text from usage arrays
60
+ */
61
+ function filterPlaceholders(items: string[] | undefined): string[] {
62
+ if (!items) return [];
63
+ return items.filter(item =>
64
+ !PLACEHOLDER_PATTERNS.some(pattern => pattern.test(item.trim()))
65
+ );
66
+ }
67
+
68
+ /**
69
+ * MCP Server configuration
70
+ */
71
+ export interface McpServerConfig {
72
+ /** Project root directory */
73
+ projectRoot: string;
74
+
75
+ /** Viewer base URL */
76
+ viewerUrl?: string;
77
+
78
+ /** Default theme for verification */
79
+ theme?: Theme;
80
+
81
+ /** Diff threshold percentage */
82
+ threshold?: number;
83
+ }
84
+
85
+ /**
86
+ * Tool definitions for the MCP server — 5 consolidated tools
87
+ */
88
+ const TOOLS: Tool[] = [
89
+ {
90
+ name: TOOL_NAMES.discover,
91
+ description: `Discover components in the design system. Use with no params to list all components. Use 'useCase' for AI-powered suggestions. Use 'component' to find alternatives. Use 'compact' for a token-efficient overview.`,
92
+ inputSchema: {
93
+ type: 'object' as const,
94
+ properties: {
95
+ useCase: {
96
+ type: 'string',
97
+ description: 'Description of what you want to build — returns ranked suggestions (e.g., "form for user email input", "button to submit data")',
98
+ },
99
+ component: {
100
+ type: 'string',
101
+ description: 'Component name to find alternatives for (e.g., "Button")',
102
+ },
103
+ category: {
104
+ type: 'string',
105
+ description: 'Filter by category (e.g., "actions", "forms", "layout")',
106
+ },
107
+ search: {
108
+ type: 'string',
109
+ description: 'Search term to filter by name, description, or tags',
110
+ },
111
+ status: {
112
+ type: 'string',
113
+ enum: ['stable', 'beta', 'deprecated', 'experimental'],
114
+ description: 'Filter by component status',
115
+ },
116
+ format: {
117
+ type: 'string',
118
+ enum: ['markdown', 'json'],
119
+ description: 'Output format for context mode (default: markdown)',
120
+ },
121
+ compact: {
122
+ type: 'boolean',
123
+ description: 'If true, returns minimal output (just component names and categories)',
124
+ },
125
+ includeCode: {
126
+ type: 'boolean',
127
+ description: 'If true, includes code examples for each variant',
128
+ },
129
+ includeRelations: {
130
+ type: 'boolean',
131
+ description: 'If true, includes component relationships',
132
+ },
133
+ },
134
+ },
135
+ },
136
+ {
137
+ name: TOOL_NAMES.inspect,
138
+ description: `Get detailed information about a specific component: props, usage guidelines, code examples, accessibility — all in one call. Use 'fields' to request only specific data for token efficiency.`,
139
+ inputSchema: {
140
+ type: 'object' as const,
141
+ properties: {
142
+ component: {
143
+ type: 'string',
144
+ description: 'Component name (e.g., "Button", "Input")',
145
+ },
146
+ fields: {
147
+ type: 'array',
148
+ items: { type: 'string' },
149
+ description: 'Specific fields to return (e.g., ["meta", "usage.when", "contract.propsSummary", "props", "examples", "guidelines"]). If omitted, returns everything. Supports dot notation.',
150
+ },
151
+ variant: {
152
+ type: 'string',
153
+ description: 'Filter examples to a specific variant name (e.g., "Default", "Primary")',
154
+ },
155
+ maxExamples: {
156
+ type: 'number',
157
+ description: 'Maximum number of code examples to return (default: all)',
158
+ },
159
+ maxLines: {
160
+ type: 'number',
161
+ description: 'Maximum lines per code example (truncates longer examples)',
162
+ },
163
+ },
164
+ required: ['component'],
165
+ },
166
+ },
167
+ {
168
+ name: TOOL_NAMES.recipe,
169
+ description: `Search and retrieve composition recipes — named patterns showing how design system components wire together for common use cases (e.g., "Login Form", "Settings Page"). Returns the recipe with its code pattern.`,
170
+ inputSchema: {
171
+ type: 'object' as const,
172
+ properties: {
173
+ name: {
174
+ type: 'string',
175
+ description: 'Exact recipe name to retrieve (e.g., "Login Form")',
176
+ },
177
+ search: {
178
+ type: 'string',
179
+ description: 'Free-text search across recipe names, descriptions, tags, and components',
180
+ },
181
+ component: {
182
+ type: 'string',
183
+ description: 'Filter recipes that use a specific component (e.g., "Button")',
184
+ },
185
+ },
186
+ },
187
+ },
188
+ {
189
+ name: TOOL_NAMES.render,
190
+ description: `Render a component and return a screenshot. Optionally compare against a stored baseline ('baseline: true') or against a Figma design ('figmaUrl'). Use this to verify your implementation looks correct.`,
191
+ inputSchema: {
192
+ type: 'object' as const,
193
+ properties: {
194
+ component: {
195
+ type: 'string',
196
+ description: 'Component name (e.g., "Button", "Card", "Input")',
197
+ },
198
+ variant: {
199
+ type: 'string',
200
+ description: 'Variant name for baseline/compare modes',
201
+ },
202
+ props: {
203
+ type: 'object',
204
+ description: 'Props to pass to the component (e.g., { "variant": "primary", "children": "Click me" })',
205
+ },
206
+ viewport: {
207
+ type: 'object',
208
+ properties: {
209
+ width: { type: 'number', description: 'Viewport width (default: 800)' },
210
+ height: { type: 'number', description: 'Viewport height (default: 600)' },
211
+ },
212
+ description: 'Optional viewport size for the render',
213
+ },
214
+ baseline: {
215
+ type: 'boolean',
216
+ description: 'If true, compares the render against the stored baseline screenshot (requires variant)',
217
+ },
218
+ figmaUrl: {
219
+ type: 'string',
220
+ description: 'Figma frame URL — if provided, compares the render against the Figma design',
221
+ },
222
+ theme: {
223
+ type: 'string',
224
+ enum: ['light', 'dark'],
225
+ description: 'Theme for baseline verification (default: light)',
226
+ },
227
+ threshold: {
228
+ type: 'number',
229
+ description: 'Diff threshold percentage (default: 5 for baseline, 1 for Figma)',
230
+ },
231
+ },
232
+ required: ['component'],
233
+ },
234
+ },
235
+ {
236
+ name: TOOL_NAMES.fix,
237
+ description: `Generate patches to fix token compliance issues in a component. Returns unified diff patches that replace hardcoded CSS values with design token references. Use this after fragments_render identifies issues to automatically fix them.`,
238
+ inputSchema: {
239
+ type: 'object' as const,
240
+ properties: {
241
+ component: {
242
+ type: 'string',
243
+ description: 'Component name to generate fixes for (e.g., "Button", "Card")',
244
+ },
245
+ variant: {
246
+ type: 'string',
247
+ description: 'Specific variant to fix (optional, fixes all variants if omitted)',
248
+ },
249
+ fixType: {
250
+ type: 'string',
251
+ enum: ['token', 'all'],
252
+ description: 'Type of fixes to generate: "token" for hardcoded→token replacements, "all" for all available fixes (default: "all")',
253
+ },
254
+ },
255
+ required: ['component'],
256
+ },
257
+ },
258
+ ];
259
+
260
+ /**
261
+ * Create and configure the MCP server
262
+ */
263
+ export function createMcpServer(config: McpServerConfig): Server {
264
+ const server = new Server(
265
+ {
266
+ name: `${BRAND.nameLower}-mcp`,
267
+ version: '0.0.1',
268
+ },
269
+ {
270
+ capabilities: {
271
+ tools: {},
272
+ },
273
+ }
274
+ );
275
+
276
+ // Lazy-loaded resources
277
+ let segmentsData: CompiledSegmentsFile | null = null;
278
+ let packageName: string | null = null;
279
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- lazy-loaded from service
280
+ let browserPool: any = null;
281
+ let storageManager: any = null;
282
+ let diffEngine: any = null;
283
+ let isPoolWarming = false;
284
+
285
+ /**
286
+ * Find fragments.json files:
287
+ * 1. Walk up from projectRoot (for library authors with a local build)
288
+ * 2. Read package.json deps and resolve packages with a "fragments" field
289
+ */
290
+ function findFragmentsJson(startDir: string): string[] {
291
+ const found: string[] = [];
292
+ const resolvedStart = resolve(startDir);
293
+
294
+ // 1. Walk upward from startDir (library author flow)
295
+ let dir = resolvedStart;
296
+ while (true) {
297
+ const candidate = join(dir, BRAND.outFile);
298
+ if (existsSync(candidate)) {
299
+ found.push(candidate);
300
+ break;
301
+ }
302
+ const parent = dirname(dir);
303
+ if (parent === dir) break;
304
+ dir = parent;
305
+ }
306
+
307
+ // 2. Read package.json and resolve deps with "fragments" field
308
+ const pkgJsonPath = join(resolvedStart, 'package.json');
309
+ if (existsSync(pkgJsonPath)) {
310
+ try {
311
+ const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
312
+ const allDeps = {
313
+ ...pkgJson.dependencies,
314
+ ...pkgJson.devDependencies,
315
+ };
316
+ for (const depName of Object.keys(allDeps)) {
317
+ const depPkgPath = join(resolvedStart, 'node_modules', depName, 'package.json');
318
+ if (!existsSync(depPkgPath)) continue;
319
+ try {
320
+ const depPkg = JSON.parse(readFileSync(depPkgPath, 'utf-8'));
321
+ if (depPkg.fragments) {
322
+ const fragmentsPath = join(resolvedStart, 'node_modules', depName, depPkg.fragments);
323
+ if (existsSync(fragmentsPath) && !found.includes(fragmentsPath)) {
324
+ found.push(fragmentsPath);
325
+ }
326
+ }
327
+ } catch {
328
+ // Skip unreadable package
329
+ }
330
+ }
331
+ } catch {
332
+ // No package.json or unreadable
333
+ }
334
+ }
335
+
336
+ return found;
337
+ }
338
+
339
+ async function loadSegments(): Promise<CompiledSegmentsFile> {
340
+ if (segmentsData) {
341
+ return segmentsData;
342
+ }
343
+
344
+ const paths = findFragmentsJson(config.projectRoot);
345
+
346
+ if (paths.length === 0) {
347
+ throw new Error(
348
+ `No ${BRAND.outFile} found. Searched ${config.projectRoot} and package.json dependencies. ` +
349
+ `Either run \`${BRAND.cliCommand} build\` or install a package with a "fragments" field in its package.json.`
350
+ );
351
+ }
352
+
353
+ // Load and merge all found fragments files
354
+ const content = await readFile(paths[0], 'utf-8');
355
+ segmentsData = JSON.parse(content) as CompiledSegmentsFile;
356
+
357
+ for (let i = 1; i < paths.length; i++) {
358
+ const extra = JSON.parse(await readFile(paths[i], 'utf-8')) as CompiledSegmentsFile;
359
+ Object.assign(segmentsData.segments, extra.segments);
360
+ if (extra.recipes) {
361
+ segmentsData.recipes = { ...segmentsData.recipes, ...extra.recipes };
362
+ }
363
+ }
364
+
365
+ return segmentsData;
366
+ }
367
+
368
+ /**
369
+ * Get the package name from package.json for import statements
370
+ */
371
+ async function getPackageName(): Promise<string> {
372
+ if (packageName) {
373
+ return packageName;
374
+ }
375
+
376
+ const packageJsonPath = join(config.projectRoot, 'package.json');
377
+ if (existsSync(packageJsonPath)) {
378
+ try {
379
+ const content = await readFile(packageJsonPath, 'utf-8');
380
+ const pkg = JSON.parse(content) as { name?: string };
381
+ if (pkg.name) {
382
+ packageName = pkg.name;
383
+ return packageName;
384
+ }
385
+ } catch {
386
+ // Fall through to default
387
+ }
388
+ }
389
+
390
+ // Default fallback
391
+ packageName = 'your-component-library';
392
+ return packageName;
393
+ }
394
+
395
+ /**
396
+ * Get or create browser pool with extended idle timeout for MCP
397
+ */
398
+ async function getBrowserPool() {
399
+ if (!browserPool) {
400
+ const { BrowserPool } = await getService();
401
+ browserPool = new BrowserPool({
402
+ viewport: DEFAULTS.viewport,
403
+ // 30 minute idle timeout for MCP - server runs continuously
404
+ idleTimeoutMs: 30 * 60 * 1000,
405
+ poolSize: 2, // Keep 2 contexts warm for faster captures
406
+ });
407
+ }
408
+ return browserPool;
409
+ }
410
+
411
+ /**
412
+ * Pre-warm browser pool in background (non-blocking)
413
+ */
414
+ function warmBrowserPool(): void {
415
+ if (isPoolWarming || browserPool?.isReady) {
416
+ return;
417
+ }
418
+ isPoolWarming = true;
419
+
420
+ // Warm in background - don't await
421
+ getBrowserPool().then((pool) => {
422
+ pool.warmup().then(() => {
423
+ isPoolWarming = false;
424
+ }).catch(() => {
425
+ isPoolWarming = false;
426
+ });
427
+ }).catch(() => {
428
+ isPoolWarming = false;
429
+ });
430
+ }
431
+
432
+ /**
433
+ * Get or create storage manager
434
+ */
435
+ async function getStorageManager() {
436
+ if (!storageManager) {
437
+ const { StorageManager } = await getService();
438
+ storageManager = new StorageManager({
439
+ projectRoot: config.projectRoot,
440
+ });
441
+ await storageManager.initialize();
442
+ }
443
+ return storageManager;
444
+ }
445
+
446
+ /**
447
+ * Get or create diff engine
448
+ */
449
+ async function getDiffEngine() {
450
+ if (!diffEngine) {
451
+ const { DiffEngine } = await getService();
452
+ diffEngine = new DiffEngine(config.threshold ?? DEFAULTS.diffThreshold);
453
+ }
454
+ return diffEngine;
455
+ }
456
+
457
+ // Register tool listing
458
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
459
+ return { tools: TOOLS };
460
+ });
461
+
462
+ // Register tool execution
463
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
464
+ const { name, arguments: args } = request.params;
465
+
466
+ try {
467
+ switch (name) {
468
+ // ================================================================
469
+ // DISCOVER — list, suggest, context, alternatives
470
+ // ================================================================
471
+ case TOOL_NAMES.discover: {
472
+ const data = await loadSegments();
473
+ const useCase = (args?.useCase as string) ?? undefined;
474
+ const componentForAlts = (args?.component as string) ?? undefined;
475
+ const category = (args?.category as string) ?? undefined;
476
+ const search = (args?.search as string)?.toLowerCase() ?? undefined;
477
+ const status = (args?.status as string) ?? undefined;
478
+ const format = (args?.format as 'markdown' | 'json') ?? 'markdown';
479
+ const compact = (args?.compact as boolean) ?? false;
480
+ const includeCode = (args?.includeCode as boolean) ?? false;
481
+ const includeRelations = (args?.includeRelations as boolean) ?? false;
482
+
483
+ // --- Context mode: compact or format specified with no specific query ---
484
+ if (compact || (args?.format && !useCase && !componentForAlts && !category && !search && !status)) {
485
+ const segments = Object.values(data.segments);
486
+ const recipes = Object.values(data.recipes ?? {});
487
+
488
+ const { content: ctxContent, tokenEstimate } = generateContext(segments, {
489
+ format,
490
+ compact,
491
+ include: {
492
+ code: includeCode,
493
+ relations: includeRelations,
494
+ },
495
+ }, recipes);
496
+
497
+ return {
498
+ content: [{
499
+ type: 'text' as const,
500
+ text: ctxContent,
501
+ }],
502
+ _meta: { tokenEstimate },
503
+ };
504
+ }
505
+
506
+ // --- Suggest mode: useCase provided ---
507
+ if (useCase) {
508
+ const useCaseLower = useCase.toLowerCase();
509
+ const context = ((args as Record<string, unknown>)?.context as string)?.toLowerCase() ?? '';
510
+ const searchTerms = `${useCaseLower} ${context}`.split(/\s+/).filter(Boolean);
511
+
512
+ const synonymMap: Record<string, string[]> = {
513
+ 'form': ['input', 'field', 'submit', 'validation'],
514
+ 'input': ['form', 'field', 'text', 'entry'],
515
+ 'button': ['action', 'click', 'submit', 'trigger'],
516
+ 'action': ['button', 'click', 'trigger'],
517
+ 'alert': ['notification', 'message', 'warning', 'error', 'feedback'],
518
+ 'notification': ['alert', 'message', 'toast'],
519
+ 'card': ['container', 'panel', 'box', 'content'],
520
+ 'toggle': ['switch', 'checkbox', 'boolean', 'on/off'],
521
+ 'switch': ['toggle', 'checkbox', 'boolean'],
522
+ 'badge': ['tag', 'label', 'status', 'indicator'],
523
+ 'status': ['badge', 'indicator', 'state'],
524
+ 'login': ['auth', 'signin', 'authentication', 'form'],
525
+ 'auth': ['login', 'signin', 'authentication'],
526
+ };
527
+
528
+ const expandedTerms = new Set(searchTerms);
529
+ searchTerms.forEach(term => {
530
+ const synonyms = synonymMap[term];
531
+ if (synonyms) {
532
+ synonyms.forEach(syn => expandedTerms.add(syn));
533
+ }
534
+ });
535
+
536
+ const scored = Object.values(data.segments).map((s) => {
537
+ let score = 0;
538
+ const reasons: string[] = [];
539
+
540
+ const nameLower = s.meta.name.toLowerCase();
541
+ if (searchTerms.some((term) => nameLower.includes(term))) {
542
+ score += 15;
543
+ reasons.push(`Name matches search`);
544
+ } else if (Array.from(expandedTerms).some((term) => nameLower.includes(term))) {
545
+ score += 8;
546
+ reasons.push(`Name matches related term`);
547
+ }
548
+
549
+ const desc = s.meta.description?.toLowerCase() ?? '';
550
+ const descMatches = searchTerms.filter((term) => desc.includes(term));
551
+ if (descMatches.length > 0) {
552
+ score += descMatches.length * 6;
553
+ reasons.push(`Description matches: ${descMatches.join(', ')}`);
554
+ }
555
+
556
+ const tags = s.meta.tags?.map((t) => t.toLowerCase()) ?? [];
557
+ const tagMatches = searchTerms.filter((term) =>
558
+ tags.some((tag) => tag.includes(term))
559
+ );
560
+ if (tagMatches.length > 0) {
561
+ score += tagMatches.length * 4;
562
+ reasons.push(`Tags match: ${tagMatches.join(', ')}`);
563
+ }
564
+
565
+ const whenUsed = s.usage?.when?.join(' ').toLowerCase() ?? '';
566
+ const whenMatches = searchTerms.filter((term) => whenUsed.includes(term));
567
+ if (whenMatches.length > 0) {
568
+ score += whenMatches.length * 10;
569
+ reasons.push(`Use cases match: "${whenMatches.join(', ')}"`);
570
+ }
571
+
572
+ const expandedWhenMatches = Array.from(expandedTerms).filter(
573
+ (term) => !searchTerms.includes(term) && whenUsed.includes(term)
574
+ );
575
+ if (expandedWhenMatches.length > 0) {
576
+ score += expandedWhenMatches.length * 5;
577
+ reasons.push(`Related use cases: "${expandedWhenMatches.join(', ')}"`);
578
+ }
579
+
580
+ const cat = s.meta.category?.toLowerCase() ?? '';
581
+ if (searchTerms.some((term) => cat.includes(term))) {
582
+ score += 8;
583
+ reasons.push(`Category: ${s.meta.category}`);
584
+ }
585
+
586
+ const variantText = s.variants
587
+ .map(v => `${v.name} ${v.description || ''}`.toLowerCase())
588
+ .join(' ');
589
+ const variantMatches = searchTerms.filter(term => variantText.includes(term));
590
+ if (variantMatches.length > 0) {
591
+ score += variantMatches.length * 3;
592
+ reasons.push(`Variants match: ${variantMatches.join(', ')}`);
593
+ }
594
+
595
+ if (s.meta.status === 'stable') {
596
+ score += 5;
597
+ reasons.push('Stable component');
598
+ } else if (s.meta.status === 'beta') {
599
+ score += 2;
600
+ }
601
+
602
+ if (s.meta.status === 'deprecated') {
603
+ score -= 25;
604
+ reasons.push('Deprecated - consider alternatives');
605
+ }
606
+
607
+ const filteredWhen = filterPlaceholders(s.usage?.when).slice(0, 3);
608
+ const filteredWhenNot = filterPlaceholders(s.usage?.whenNot).slice(0, 2);
609
+
610
+ let confidence: 'high' | 'medium' | 'low';
611
+ if (score >= 25) confidence = 'high';
612
+ else if (score >= 15) confidence = 'medium';
613
+ else confidence = 'low';
614
+
615
+ return {
616
+ component: s.meta.name,
617
+ category: s.meta.category,
618
+ description: s.meta.description,
619
+ score,
620
+ confidence,
621
+ reasons,
622
+ usage: { when: filteredWhen, whenNot: filteredWhenNot },
623
+ variantCount: s.variants.length,
624
+ status: s.meta.status,
625
+ };
626
+ });
627
+
628
+ const MIN_SCORE = 8;
629
+ const filtered = scored
630
+ .filter((s) => s.score >= MIN_SCORE)
631
+ .sort((a, b) => b.score - a.score);
632
+
633
+ const suggestions: typeof filtered = [];
634
+ const categoryCount: Record<string, number> = {};
635
+ for (const item of filtered) {
636
+ const cat = item.category || 'uncategorized';
637
+ const count = categoryCount[cat] || 0;
638
+ if (count < 2 || suggestions.length < 3) {
639
+ suggestions.push(item);
640
+ categoryCount[cat] = count + 1;
641
+ if (suggestions.length >= 5) break;
642
+ }
643
+ }
644
+
645
+ const compositionHint = suggestions.length >= 2
646
+ ? `These components work well together. For example, ${suggestions[0].component} can be combined with ${suggestions.slice(1, 3).map(s => s.component).join(' and ')}.`
647
+ : undefined;
648
+
649
+ return {
650
+ content: [{
651
+ type: 'text' as const,
652
+ text: JSON.stringify({
653
+ useCase,
654
+ context: context || undefined,
655
+ suggestions: suggestions.map(({ score, ...rest }) => rest),
656
+ recommendation: suggestions.length > 0
657
+ ? `Best match: ${suggestions[0].component} (${suggestions[0].confidence} confidence) - ${suggestions[0].description}`
658
+ : 'No matching components found. Try different keywords or browse with fragments_discover.',
659
+ compositionHint,
660
+ nextStep: suggestions.length > 0
661
+ ? `Use fragments_inspect("${suggestions[0].component}") for full details.`
662
+ : undefined,
663
+ }, null, 2),
664
+ }],
665
+ };
666
+ }
667
+
668
+ // --- Alternatives mode: component provided (no useCase) ---
669
+ if (componentForAlts) {
670
+ const segment = Object.values(data.segments).find(
671
+ (s) => s.meta.name.toLowerCase() === componentForAlts.toLowerCase()
672
+ );
673
+
674
+ if (!segment) {
675
+ throw new Error(`Component "${componentForAlts}" not found. Use fragments_discover to see available components.`);
676
+ }
677
+
678
+ const relations = segment.relations ?? [];
679
+
680
+ const referencedBy = Object.values(data.segments)
681
+ .filter((s) =>
682
+ s.relations?.some((r) => r.component.toLowerCase() === componentForAlts.toLowerCase())
683
+ )
684
+ .map((s) => ({
685
+ component: s.meta.name,
686
+ relationship: s.relations?.find(
687
+ (r) => r.component.toLowerCase() === componentForAlts.toLowerCase()
688
+ )?.relationship,
689
+ note: s.relations?.find(
690
+ (r) => r.component.toLowerCase() === componentForAlts.toLowerCase()
691
+ )?.note,
692
+ }));
693
+
694
+ const sameCategory = Object.values(data.segments)
695
+ .filter(
696
+ (s) =>
697
+ s.meta.category === segment.meta.category &&
698
+ s.meta.name.toLowerCase() !== componentForAlts.toLowerCase()
699
+ )
700
+ .map((s) => ({
701
+ component: s.meta.name,
702
+ description: s.meta.description,
703
+ }));
704
+
705
+ return {
706
+ content: [{
707
+ type: 'text' as const,
708
+ text: JSON.stringify({
709
+ component: segment.meta.name,
710
+ category: segment.meta.category,
711
+ directRelations: relations,
712
+ referencedBy,
713
+ sameCategory,
714
+ suggestion: relations.find((r) => r.relationship === 'alternative')
715
+ ? `Consider ${relations.find((r) => r.relationship === 'alternative')?.component}: ${relations.find((r) => r.relationship === 'alternative')?.note}`
716
+ : undefined,
717
+ }, null, 2),
718
+ }],
719
+ };
720
+ }
721
+
722
+ // --- Default: list mode ---
723
+ const segments = Object.values(data.segments)
724
+ .filter((s) => {
725
+ if (category && s.meta.category !== category) return false;
726
+ if (status && (s.meta.status ?? 'stable') !== status) return false;
727
+ if (search) {
728
+ const nameMatch = s.meta.name.toLowerCase().includes(search);
729
+ const descMatch = s.meta.description?.toLowerCase().includes(search);
730
+ const tagMatch = s.meta.tags?.some((t) => t.toLowerCase().includes(search));
731
+ if (!nameMatch && !descMatch && !tagMatch) return false;
732
+ }
733
+ return true;
734
+ })
735
+ .map((s) => ({
736
+ name: s.meta.name,
737
+ category: s.meta.category,
738
+ description: s.meta.description,
739
+ status: s.meta.status ?? 'stable',
740
+ variantCount: s.variants.length,
741
+ tags: s.meta.tags ?? [],
742
+ }));
743
+
744
+ return {
745
+ content: [{
746
+ type: 'text' as const,
747
+ text: JSON.stringify({
748
+ total: segments.length,
749
+ segments,
750
+ categories: [...new Set(segments.map((s) => s.category))],
751
+ hint: segments.length === 0
752
+ ? 'No components found. Try broader search terms or check available categories.'
753
+ : segments.length > 5
754
+ ? 'Use fragments_discover with useCase for recommendations, or fragments_inspect for details on a specific component.'
755
+ : undefined,
756
+ }, null, 2),
757
+ }],
758
+ };
759
+ }
760
+
761
+ // ================================================================
762
+ // INSPECT — get + guidelines + example in one call
763
+ // ================================================================
764
+ case TOOL_NAMES.inspect: {
765
+ const data = await loadSegments();
766
+ const componentName = args?.component as string;
767
+ const fields = args?.fields as string[] | undefined;
768
+ const variantName = (args?.variant as string) ?? undefined;
769
+ const maxExamples = args?.maxExamples as number | undefined;
770
+ const maxLines = args?.maxLines as number | undefined;
771
+
772
+ if (!componentName) {
773
+ throw new Error('component is required');
774
+ }
775
+
776
+ const segment = Object.values(data.segments).find(
777
+ (s) => s.meta.name.toLowerCase() === componentName.toLowerCase()
778
+ );
779
+
780
+ if (!segment) {
781
+ throw new Error(`Component "${componentName}" not found. Use fragments_discover to see available components.`);
782
+ }
783
+
784
+ // Build the full inspect result combining get + guidelines + example
785
+ const pkgName = await getPackageName();
786
+
787
+ // Filter variants for examples
788
+ let variants = segment.variants;
789
+ if (variantName) {
790
+ const filtered = variants.filter(
791
+ (v) => v.name.toLowerCase() === variantName.toLowerCase()
792
+ );
793
+ if (filtered.length > 0) {
794
+ variants = filtered;
795
+ }
796
+ }
797
+ if (maxExamples && maxExamples > 0) {
798
+ variants = variants.slice(0, maxExamples);
799
+ }
800
+
801
+ const truncateCode = (code: string): string => {
802
+ if (!maxLines || maxLines <= 0) return code;
803
+ const lines = code.split('\n');
804
+ if (lines.length <= maxLines) return code;
805
+ return lines.slice(0, maxLines).join('\n') + '\n// ... truncated';
806
+ };
807
+
808
+ const examples = variants.map((variant) => {
809
+ if (variant.code) {
810
+ return {
811
+ variant: variant.name,
812
+ description: variant.description,
813
+ code: truncateCode(variant.code),
814
+ };
815
+ }
816
+ return {
817
+ variant: variant.name,
818
+ description: variant.description,
819
+ code: `<${segment.meta.name} />`,
820
+ note: 'No code example provided in fragment. Refer to props for customization.',
821
+ };
822
+ });
823
+
824
+ const propsReference = Object.entries(segment.props ?? {}).map(([propName, prop]) => ({
825
+ name: propName,
826
+ type: prop.type,
827
+ required: prop.required,
828
+ default: prop.default,
829
+ description: prop.description,
830
+ }));
831
+
832
+ const propConstraints = Object.entries(segment.props ?? {})
833
+ .filter(([, prop]) => prop.constraints && prop.constraints.length > 0)
834
+ .map(([pName, prop]) => ({
835
+ prop: pName,
836
+ constraints: prop.constraints,
837
+ }));
838
+
839
+ const fullResult = {
840
+ // Component data (from old "get")
841
+ meta: segment.meta,
842
+ props: segment.props,
843
+ variants: segment.variants,
844
+ relations: segment.relations,
845
+ contract: segment.contract,
846
+ generated: segment._generated,
847
+ // Guidelines (from old "guidelines")
848
+ guidelines: {
849
+ when: filterPlaceholders(segment.usage?.when),
850
+ whenNot: filterPlaceholders(segment.usage?.whenNot),
851
+ guidelines: segment.usage?.guidelines ?? [],
852
+ accessibility: segment.usage?.accessibility ?? [],
853
+ propConstraints,
854
+ alternatives: segment.relations
855
+ ?.filter((r) => r.relationship === 'alternative')
856
+ .map((r) => ({
857
+ component: r.component,
858
+ note: r.note,
859
+ })) ?? [],
860
+ },
861
+ // Examples (from old "example")
862
+ examples: {
863
+ import: `import { ${segment.meta.name} } from '${pkgName}';`,
864
+ code: examples,
865
+ propsReference,
866
+ },
867
+ };
868
+
869
+ // Apply field projection if specified
870
+ const result = fields && fields.length > 0
871
+ ? projectFields(fullResult as unknown as Record<string, unknown>, fields)
872
+ : fullResult;
873
+
874
+ return {
875
+ content: [{
876
+ type: 'text' as const,
877
+ text: JSON.stringify(result, null, 2),
878
+ }],
879
+ };
880
+ }
881
+
882
+ // ================================================================
883
+ // RECIPE — unchanged
884
+ // ================================================================
885
+ case TOOL_NAMES.recipe: {
886
+ const data = await loadSegments();
887
+ const recipeName = args?.name as string | undefined;
888
+ const search = (args?.search as string)?.toLowerCase() ?? undefined;
889
+ const component = (args?.component as string)?.toLowerCase() ?? undefined;
890
+
891
+ const allRecipes = Object.values(data.recipes ?? {});
892
+
893
+ if (allRecipes.length === 0) {
894
+ return {
895
+ content: [{
896
+ type: 'text' as const,
897
+ text: JSON.stringify({
898
+ total: 0,
899
+ recipes: [],
900
+ hint: `No recipes found. Run \`${BRAND.cliCommand} build\` after adding .recipe.ts files.`,
901
+ }, null, 2),
902
+ }],
903
+ };
904
+ }
905
+
906
+ let filtered = allRecipes;
907
+
908
+ if (recipeName) {
909
+ filtered = filtered.filter(
910
+ r => r.name.toLowerCase() === recipeName.toLowerCase()
911
+ );
912
+ }
913
+
914
+ if (search) {
915
+ filtered = filtered.filter(r => {
916
+ const haystack = [
917
+ r.name,
918
+ r.description,
919
+ ...(r.tags ?? []),
920
+ ...r.components,
921
+ r.category,
922
+ ].join(' ').toLowerCase();
923
+ return haystack.includes(search);
924
+ });
925
+ }
926
+
927
+ if (component) {
928
+ filtered = filtered.filter(r =>
929
+ r.components.some(c => c.toLowerCase() === component)
930
+ );
931
+ }
932
+
933
+ return {
934
+ content: [{
935
+ type: 'text' as const,
936
+ text: JSON.stringify({
937
+ total: filtered.length,
938
+ recipes: filtered,
939
+ }, null, 2),
940
+ }],
941
+ };
942
+ }
943
+
944
+ // ================================================================
945
+ // RENDER — render + verify + compare
946
+ // ================================================================
947
+ case TOOL_NAMES.render: {
948
+ const componentName = args?.component as string;
949
+ const variantName = args?.variant as string | undefined;
950
+ const props = (args?.props as Record<string, unknown>) ?? {};
951
+ const viewport = args?.viewport as { width?: number; height?: number } | undefined;
952
+ const useBaseline = (args?.baseline as boolean) ?? false;
953
+ const figmaUrl = args?.figmaUrl as string | undefined;
954
+ const theme = (args?.theme as Theme) ?? config.theme ?? DEFAULTS.theme;
955
+ const threshold = (args?.threshold as number) ?? (figmaUrl ? 1.0 : config.threshold ?? DEFAULTS.diffThreshold);
956
+
957
+ if (!componentName) {
958
+ return {
959
+ content: [{
960
+ type: 'text' as const,
961
+ text: 'Error: component name is required',
962
+ }],
963
+ isError: true,
964
+ };
965
+ }
966
+
967
+ // --- Baseline verify mode ---
968
+ if (useBaseline) {
969
+ if (!variantName) {
970
+ throw new Error('variant is required when baseline is true');
971
+ }
972
+
973
+ const { Timer, CaptureEngine: CE, bufferToBase64Url: toBase64 } = await getService();
974
+ const timer = new Timer();
975
+
976
+ const storage = await getStorageManager();
977
+ const pool = await getBrowserPool();
978
+ const diff = await getDiffEngine();
979
+
980
+ const baseline = await storage.loadBaseline(componentName, variantName, theme);
981
+
982
+ if (!baseline) {
983
+ return {
984
+ content: [{
985
+ type: 'text' as const,
986
+ text: JSON.stringify({
987
+ verdict: 'error',
988
+ matches: false,
989
+ diffPercentage: 0,
990
+ screenshot: '',
991
+ baseline: '',
992
+ notes: [],
993
+ error: `No baseline found for ${componentName}/${variantName}. Run \`${BRAND.cliCommand} screenshot\` first.`,
994
+ timing: { renderMs: 0, captureMs: 0, diffMs: 0, totalMs: timer.elapsed() },
995
+ } satisfies VerifyResult, null, 2),
996
+ }],
997
+ };
998
+ }
999
+
1000
+ const viewerUrl = config.viewerUrl ?? `http://localhost:${DEFAULTS.port}`;
1001
+ const captureEngine = new CE(pool, viewerUrl);
1002
+
1003
+ const current = await captureEngine.captureVariant(componentName, variantName, {
1004
+ theme,
1005
+ delay: DEFAULTS.captureDelayMs,
1006
+ });
1007
+
1008
+ let diffResult;
1009
+ let matches = false;
1010
+
1011
+ if (diff.areIdentical(current, baseline)) {
1012
+ matches = true;
1013
+ diffResult = {
1014
+ matches: true,
1015
+ diffPercentage: 0,
1016
+ diffPixelCount: 0,
1017
+ totalPixels: current.viewport.width * current.viewport.height,
1018
+ changedRegions: [],
1019
+ diffTimeMs: 0,
1020
+ };
1021
+ } else {
1022
+ diffResult = diff.compare(current, baseline, { threshold });
1023
+ matches = diffResult.matches;
1024
+ }
1025
+
1026
+ const result: VerifyResult = {
1027
+ verdict: matches ? 'pass' : 'fail',
1028
+ matches,
1029
+ diffPercentage: diffResult.diffPercentage,
1030
+ screenshot: toBase64(current.data),
1031
+ baseline: toBase64(baseline.data),
1032
+ diffImage: diffResult.diffImage
1033
+ ? toBase64(diffResult.diffImage)
1034
+ : undefined,
1035
+ notes: matches
1036
+ ? ['Screenshot matches baseline within threshold']
1037
+ : [
1038
+ `Diff percentage (${diffResult.diffPercentage}%) exceeds threshold (${threshold}%)`,
1039
+ `${diffResult.changedRegions.length} changed region(s) detected`,
1040
+ ],
1041
+ timing: {
1042
+ renderMs: current.metadata.renderTimeMs,
1043
+ captureMs: current.metadata.captureTimeMs,
1044
+ diffMs: diffResult.diffTimeMs,
1045
+ totalMs: timer.elapsed(),
1046
+ },
1047
+ };
1048
+
1049
+ return {
1050
+ content: [{
1051
+ type: 'text' as const,
1052
+ text: JSON.stringify(result, null, 2),
1053
+ }],
1054
+ };
1055
+ }
1056
+
1057
+ // --- Figma compare mode ---
1058
+ if (figmaUrl) {
1059
+ const baseUrl = config.viewerUrl ?? 'http://localhost:6006';
1060
+ const compareUrl = `${baseUrl}/fragments/compare`;
1061
+
1062
+ try {
1063
+ const response = await fetch(compareUrl, {
1064
+ method: 'POST',
1065
+ headers: { 'Content-Type': 'application/json' },
1066
+ body: JSON.stringify({
1067
+ component: componentName,
1068
+ variant: variantName,
1069
+ props,
1070
+ figmaUrl,
1071
+ threshold,
1072
+ }),
1073
+ });
1074
+
1075
+ interface CompareResult {
1076
+ match?: boolean;
1077
+ diffPercentage?: number;
1078
+ threshold?: number;
1079
+ rendered?: string;
1080
+ figma?: string;
1081
+ diff?: string;
1082
+ figmaUrl?: string;
1083
+ changedRegions?: Array<{ x: number; y: number; width: number; height: number }>;
1084
+ error?: string;
1085
+ suggestion?: string;
1086
+ }
1087
+
1088
+ const result = await response.json() as CompareResult;
1089
+
1090
+ if (!response.ok || result.error) {
1091
+ return {
1092
+ content: [{
1093
+ type: 'text' as const,
1094
+ text: `Compare error: ${result.error ?? 'Unknown error'}${result.suggestion ? `\nSuggestion: ${result.suggestion}` : ''}`,
1095
+ }],
1096
+ isError: true,
1097
+ };
1098
+ }
1099
+
1100
+ const content: Array<{ type: string; text?: string; data?: string; mimeType?: string }> = [];
1101
+
1102
+ const summaryText = result.match
1103
+ ? `MATCH: ${componentName} matches Figma design (${result.diffPercentage}% diff, threshold: ${result.threshold}%)`
1104
+ : `MISMATCH: ${componentName} differs from Figma design by ${result.diffPercentage}% (threshold: ${result.threshold}%)`;
1105
+
1106
+ content.push({ type: 'text' as const, text: summaryText });
1107
+
1108
+ if (result.diff && !result.match) {
1109
+ content.push({
1110
+ type: 'image' as const,
1111
+ data: result.diff.replace('data:image/png;base64,', ''),
1112
+ mimeType: 'image/png',
1113
+ });
1114
+ content.push({
1115
+ type: 'text' as const,
1116
+ text: `Diff image above shows visual differences (red highlights). Changed regions: ${result.changedRegions?.length ?? 0}`,
1117
+ });
1118
+ }
1119
+
1120
+ content.push({
1121
+ type: 'text' as const,
1122
+ text: JSON.stringify({
1123
+ match: result.match,
1124
+ diffPercentage: result.diffPercentage,
1125
+ threshold: result.threshold,
1126
+ figmaUrl: result.figmaUrl,
1127
+ changedRegions: result.changedRegions,
1128
+ }, null, 2),
1129
+ });
1130
+
1131
+ return { content };
1132
+ } catch (error) {
1133
+ return {
1134
+ content: [{
1135
+ type: 'text' as const,
1136
+ text: `Failed to compare component: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure the Fragments dev server is running and FIGMA_ACCESS_TOKEN is set.`,
1137
+ }],
1138
+ isError: true,
1139
+ };
1140
+ }
1141
+ }
1142
+
1143
+ // --- Default: pure render mode ---
1144
+ const baseUrl = config.viewerUrl ?? 'http://localhost:6006';
1145
+ const renderUrl = `${baseUrl}/fragments/render`;
1146
+
1147
+ try {
1148
+ const response = await fetch(renderUrl, {
1149
+ method: 'POST',
1150
+ headers: { 'Content-Type': 'application/json' },
1151
+ body: JSON.stringify({
1152
+ component: componentName,
1153
+ props,
1154
+ viewport: viewport ?? { width: 800, height: 600 },
1155
+ }),
1156
+ });
1157
+
1158
+ const result = await response.json() as { screenshot?: string; error?: string };
1159
+
1160
+ if (!response.ok || result.error) {
1161
+ return {
1162
+ content: [{
1163
+ type: 'text' as const,
1164
+ text: `Render error: ${result.error ?? 'Unknown error'}`,
1165
+ }],
1166
+ isError: true,
1167
+ };
1168
+ }
1169
+
1170
+ return {
1171
+ content: [
1172
+ {
1173
+ type: 'image' as const,
1174
+ data: result.screenshot!.replace('data:image/png;base64,', ''),
1175
+ mimeType: 'image/png',
1176
+ },
1177
+ {
1178
+ type: 'text' as const,
1179
+ text: `Successfully rendered ${componentName} with props: ${JSON.stringify(props)}`,
1180
+ },
1181
+ ],
1182
+ };
1183
+ } catch (error) {
1184
+ return {
1185
+ content: [{
1186
+ type: 'text' as const,
1187
+ text: `Failed to render component: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure the Fragments dev server is running.`,
1188
+ }],
1189
+ isError: true,
1190
+ };
1191
+ }
1192
+ }
1193
+
1194
+ // ================================================================
1195
+ // FIX — unchanged
1196
+ // ================================================================
1197
+ case TOOL_NAMES.fix: {
1198
+ const data = await loadSegments();
1199
+ const componentName = args?.component as string;
1200
+ const variantName = (args?.variant as string) ?? undefined;
1201
+ const fixType = (args?.fixType as 'token' | 'all') ?? 'all';
1202
+
1203
+ if (!componentName) {
1204
+ throw new Error('component is required');
1205
+ }
1206
+
1207
+ const segment = Object.values(data.segments).find(
1208
+ (s) => s.meta.name.toLowerCase() === componentName.toLowerCase()
1209
+ );
1210
+
1211
+ if (!segment) {
1212
+ throw new Error(`Component "${componentName}" not found. Use fragments_discover to see available components.`);
1213
+ }
1214
+
1215
+ const baseUrl = config.viewerUrl ?? 'http://localhost:6006';
1216
+ const fixUrl = `${baseUrl}/fragments/fix`;
1217
+
1218
+ try {
1219
+ const response = await fetch(fixUrl, {
1220
+ method: 'POST',
1221
+ headers: { 'Content-Type': 'application/json' },
1222
+ body: JSON.stringify({
1223
+ component: componentName,
1224
+ variant: variantName,
1225
+ fixType,
1226
+ }),
1227
+ });
1228
+
1229
+ interface FixResult {
1230
+ patches: Array<{ file: string; diff: string }>;
1231
+ summary: string;
1232
+ error?: string;
1233
+ }
1234
+
1235
+ const result = await response.json() as FixResult;
1236
+
1237
+ if (!response.ok || result.error) {
1238
+ return {
1239
+ content: [{
1240
+ type: 'text' as const,
1241
+ text: `Fix generation error: ${result.error ?? 'Unknown error'}`,
1242
+ }],
1243
+ isError: true,
1244
+ };
1245
+ }
1246
+
1247
+ return {
1248
+ content: [{
1249
+ type: 'text' as const,
1250
+ text: JSON.stringify({
1251
+ component: componentName,
1252
+ variant: variantName ?? 'all',
1253
+ fixType,
1254
+ patches: result.patches,
1255
+ summary: result.summary,
1256
+ patchCount: result.patches.length,
1257
+ nextStep: result.patches.length > 0
1258
+ ? 'Apply patches using your editor or `patch` command, then run fragments_render with baseline:true to confirm fixes.'
1259
+ : undefined,
1260
+ }, null, 2),
1261
+ }],
1262
+ };
1263
+ } catch (error) {
1264
+ return {
1265
+ content: [{
1266
+ type: 'text' as const,
1267
+ text: `Failed to generate fixes: ${error instanceof Error ? error.message : 'Unknown error'}. Make sure the Fragments dev server is running.`,
1268
+ }],
1269
+ isError: true,
1270
+ };
1271
+ }
1272
+ }
1273
+
1274
+ default:
1275
+ throw new Error(`Unknown tool: ${name}`);
1276
+ }
1277
+ } catch (error) {
1278
+ return {
1279
+ content: [
1280
+ {
1281
+ type: 'text' as const,
1282
+ text: JSON.stringify({
1283
+ error: error instanceof Error ? error.message : String(error),
1284
+ }),
1285
+ },
1286
+ ],
1287
+ isError: true,
1288
+ };
1289
+ }
1290
+ });
1291
+
1292
+ // Cleanup on close
1293
+ server.onclose = async () => {
1294
+ if (browserPool) {
1295
+ await browserPool.shutdown();
1296
+ }
1297
+ };
1298
+
1299
+ return server;
1300
+ }
1301
+
1302
+ /**
1303
+ * Start the MCP server with stdio transport
1304
+ */
1305
+ export async function startMcpServer(config: McpServerConfig): Promise<void> {
1306
+ const server = createMcpServer(config);
1307
+ const transport = new StdioServerTransport();
1308
+
1309
+ await server.connect(transport);
1310
+ }