@storybook/mcp 0.2.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +56 -12
- package/dist/index.js +335 -350
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import { McpServer } from "tmcp";
|
|
2
2
|
import * as v from "valibot";
|
|
3
3
|
import { Documentation } from "react-docgen";
|
|
4
|
+
import { ComponentDoc } from "react-docgen-typescript";
|
|
4
5
|
|
|
5
6
|
//#region src/types.d.ts
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
+
* Represents a single Storybook source (local or remote).
|
|
8
9
|
*/
|
|
9
|
-
type
|
|
10
|
+
type Source = {
|
|
11
|
+
/** Unique identifier for this source (e.g., 'local', 'tetra') */
|
|
12
|
+
id: string;
|
|
13
|
+
/** Human-readable title (e.g., 'Local', 'Tetra Design System') */
|
|
14
|
+
title: string;
|
|
15
|
+
/** Remote URL, undefined for local source */
|
|
16
|
+
url?: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* All manifests for a single source.
|
|
20
|
+
*/
|
|
21
|
+
type SourceManifests = {
|
|
22
|
+
source: Source;
|
|
23
|
+
componentManifest: ComponentManifestMap;
|
|
24
|
+
docsManifest?: DocsManifestMap;
|
|
25
|
+
/** Error message if fetching this source failed */
|
|
26
|
+
error?: string;
|
|
27
|
+
};
|
|
10
28
|
/**
|
|
11
29
|
* Custom context passed to MCP server and tools.
|
|
12
30
|
* Contains the request object and optional manifest provider.
|
|
@@ -16,21 +34,21 @@ type StorybookContext = {
|
|
|
16
34
|
* The incoming HTTP request being processed.
|
|
17
35
|
*/
|
|
18
36
|
request?: Request;
|
|
19
|
-
/**
|
|
20
|
-
* Output format for component manifests.
|
|
21
|
-
* @default 'markdown'
|
|
22
|
-
*/
|
|
23
|
-
format?: OutputFormat;
|
|
24
37
|
/**
|
|
25
38
|
* Optional function to provide custom manifest retrieval logic.
|
|
26
39
|
* If provided, this function will be called instead of the default fetch-based provider.
|
|
27
|
-
* The function receives the request object
|
|
28
|
-
*
|
|
40
|
+
* The function receives the request object, a path to the manifest file, and optionally
|
|
41
|
+
* a source (in multi-source mode).
|
|
29
42
|
* The default provider requires a request object and constructs the manifest URL from the request origin,
|
|
30
43
|
* replacing /mcp with /manifests/components.json.
|
|
31
44
|
* Custom providers can use the request parameter to determine the manifest source, or ignore it entirely.
|
|
32
45
|
*/
|
|
33
|
-
manifestProvider?: (request: Request | undefined, path: string) => Promise<string>;
|
|
46
|
+
manifestProvider?: (request: Request | undefined, path: string, source?: Source) => Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Sources configuration for multi-source mode.
|
|
49
|
+
* When provided, tools will fetch and display manifests grouped by source.
|
|
50
|
+
*/
|
|
51
|
+
sources?: Source[];
|
|
34
52
|
/**
|
|
35
53
|
* Optional handler called when list-all-documentation tool is invoked.
|
|
36
54
|
* Receives the context and the component manifest.
|
|
@@ -39,6 +57,8 @@ type StorybookContext = {
|
|
|
39
57
|
context: StorybookContext;
|
|
40
58
|
manifests: AllManifests;
|
|
41
59
|
resultText: string;
|
|
60
|
+
/** Present in multi-source mode — all source manifests including errors */
|
|
61
|
+
sources?: SourceManifests[];
|
|
42
62
|
}) => void | Promise<void>;
|
|
43
63
|
/**
|
|
44
64
|
* Optional handler called when get-documentation tool is invoked.
|
|
@@ -48,6 +68,7 @@ type StorybookContext = {
|
|
|
48
68
|
context: StorybookContext;
|
|
49
69
|
input: {
|
|
50
70
|
id: string;
|
|
71
|
+
storybookId?: string;
|
|
51
72
|
};
|
|
52
73
|
} & ({
|
|
53
74
|
foundDocumentation: ComponentManifest | Doc;
|
|
@@ -74,6 +95,13 @@ declare const Doc: v.ObjectSchema<{
|
|
|
74
95
|
}, undefined>, undefined>;
|
|
75
96
|
}, undefined>;
|
|
76
97
|
type Doc = v.InferOutput<typeof Doc>;
|
|
98
|
+
/**
|
|
99
|
+
* Component documentation from react-docgen-typescript, extended with export name.
|
|
100
|
+
* Matches the shape produced by Storybook's manifest generator.
|
|
101
|
+
*/
|
|
102
|
+
type ComponentDocWithExportName = ComponentDoc & {
|
|
103
|
+
exportName: string;
|
|
104
|
+
};
|
|
77
105
|
declare const ComponentManifest: v.ObjectSchema<{
|
|
78
106
|
readonly id: v.StringSchema<undefined>;
|
|
79
107
|
readonly path: v.StringSchema<undefined>;
|
|
@@ -92,6 +120,7 @@ declare const ComponentManifest: v.ObjectSchema<{
|
|
|
92
120
|
}, undefined>, undefined>;
|
|
93
121
|
}, undefined>, undefined>, undefined>;
|
|
94
122
|
readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
|
|
123
|
+
readonly reactDocgenTypescript: v.OptionalSchema<v.CustomSchema<ComponentDocWithExportName, undefined>, undefined>;
|
|
95
124
|
readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
|
|
96
125
|
readonly id: v.StringSchema<undefined>;
|
|
97
126
|
readonly name: v.StringSchema<undefined>;
|
|
@@ -133,6 +162,7 @@ declare const ComponentManifestMap: v.ObjectSchema<{
|
|
|
133
162
|
}, undefined>, undefined>;
|
|
134
163
|
}, undefined>, undefined>, undefined>;
|
|
135
164
|
readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
|
|
165
|
+
readonly reactDocgenTypescript: v.OptionalSchema<v.CustomSchema<ComponentDocWithExportName, undefined>, undefined>;
|
|
136
166
|
readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
|
|
137
167
|
readonly id: v.StringSchema<undefined>;
|
|
138
168
|
readonly name: v.StringSchema<undefined>;
|
|
@@ -186,7 +216,9 @@ declare function addListAllDocumentationTool(server: McpServer<any, StorybookCon
|
|
|
186
216
|
//#endregion
|
|
187
217
|
//#region src/tools/get-documentation.d.ts
|
|
188
218
|
declare const GET_TOOL_NAME = "get-documentation";
|
|
189
|
-
declare function addGetDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']
|
|
219
|
+
declare function addGetDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled'], options?: {
|
|
220
|
+
multiSource?: boolean;
|
|
221
|
+
}): Promise<void>;
|
|
190
222
|
//#endregion
|
|
191
223
|
//#region src/tools/get-documentation-for-story.d.ts
|
|
192
224
|
declare const GET_STORY_TOOL_NAME = "get-documentation-for-story";
|
|
@@ -198,6 +230,18 @@ declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookCo
|
|
|
198
230
|
*/
|
|
199
231
|
declare const COMPONENT_MANIFEST_PATH = "./manifests/components.json";
|
|
200
232
|
declare const DOCS_MANIFEST_PATH = "./manifests/docs.json";
|
|
233
|
+
/**
|
|
234
|
+
* Gets manifests from multiple sources.
|
|
235
|
+
* Returns an array of source manifests, each containing the source info and its manifests.
|
|
236
|
+
* Failures for individual sources are captured as errors rather than failing the entire request.
|
|
237
|
+
*
|
|
238
|
+
* @param sources - Array of source configurations
|
|
239
|
+
* @param request - The HTTP request (used for local source)
|
|
240
|
+
* @param manifestProvider - Function to fetch manifests, receives source as third parameter
|
|
241
|
+
* @returns Promise resolving to array of source manifests
|
|
242
|
+
* @throws {ManifestGetError} If no sources could be fetched successfully
|
|
243
|
+
*/
|
|
244
|
+
declare function getMultiSourceManifests(sources: Source[], request?: Request, manifestProvider?: (request: Request | undefined, path: string, source?: Source) => Promise<string>): Promise<SourceManifests[]>;
|
|
201
245
|
//#endregion
|
|
202
246
|
//#region src/index.d.ts
|
|
203
247
|
type InitializeRequestParams = {
|
|
@@ -237,4 +281,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
|
|
|
237
281
|
type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
|
|
238
282
|
declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
|
|
239
283
|
//#endregion
|
|
240
|
-
export { COMPONENT_MANIFEST_PATH, type ComponentManifest,
|
|
284
|
+
export { COMPONENT_MANIFEST_PATH, type ComponentManifest, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, type Source, type SourceManifests, type StorybookContext, StorybookMcpHandlerOptions, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import * as v from "valibot";
|
|
|
5
5
|
|
|
6
6
|
//#region package.json
|
|
7
7
|
var name = "@storybook/mcp";
|
|
8
|
-
var version = "0.
|
|
8
|
+
var version = "0.4.0";
|
|
9
9
|
var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
|
|
10
10
|
|
|
11
11
|
//#endregion
|
|
@@ -48,6 +48,7 @@ const ComponentManifest = v.object({
|
|
|
48
48
|
import: v.optional(v.string()),
|
|
49
49
|
stories: v.optional(v.array(Story)),
|
|
50
50
|
reactDocgen: v.optional(v.custom(() => true)),
|
|
51
|
+
reactDocgenTypescript: v.optional(v.custom(() => true)),
|
|
51
52
|
docs: v.optional(v.record(v.string(), Doc))
|
|
52
53
|
});
|
|
53
54
|
const ComponentManifestMap = v.object({
|
|
@@ -119,14 +120,19 @@ ${error instanceof v.ValiError ? error.issues.map((i) => i.message).join("\n") :
|
|
|
119
120
|
*
|
|
120
121
|
* @param request - The HTTP request to get the manifest for (optional when using custom manifestProvider)
|
|
121
122
|
* @param manifestProvider - Optional custom function to get the manifest
|
|
123
|
+
* @param source - Optional source for multi-source mode
|
|
122
124
|
* @returns A promise that resolves to the parsed ComponentManifestMap
|
|
123
125
|
* @throws {ManifestGetError} If getting the manifest fails or the response is invalid
|
|
124
126
|
*/
|
|
125
|
-
async function getManifests(request, manifestProvider) {
|
|
127
|
+
async function getManifests(request, manifestProvider, source) {
|
|
126
128
|
const provider = manifestProvider ?? defaultManifestProvider;
|
|
127
|
-
const [componentResult, docsResult] = await Promise.allSettled([provider(request, COMPONENT_MANIFEST_PATH), provider(request, DOCS_MANIFEST_PATH)]);
|
|
129
|
+
const [componentResult, docsResult] = await Promise.allSettled([provider(request, COMPONENT_MANIFEST_PATH, source), provider(request, DOCS_MANIFEST_PATH, source)]);
|
|
128
130
|
const getUrl = (path) => request ? getManifestUrlFromRequest(request, path) : "Unknown manifest source";
|
|
129
|
-
if (componentResult.status === "rejected")
|
|
131
|
+
if (componentResult.status === "rejected") {
|
|
132
|
+
const reason = componentResult.reason;
|
|
133
|
+
const hint = reason instanceof ManifestGetError && reason.message.includes("404") ? `\nHint: The Storybook at this URL may not have the component manifest enabled. Add \`features: { experimentalComponentsManifest: true }\` to its main.ts config.` : "";
|
|
134
|
+
throw new ManifestGetError(`Failed to get component manifest: ${reason instanceof Error ? reason.message : String(reason)}${hint}`, getUrl(COMPONENT_MANIFEST_PATH), reason instanceof Error ? reason : void 0);
|
|
135
|
+
}
|
|
130
136
|
const componentManifest = parseManifest({
|
|
131
137
|
jsonString: componentResult.value,
|
|
132
138
|
schema: ComponentManifestMap,
|
|
@@ -167,90 +173,39 @@ async function defaultManifestProvider(request, path) {
|
|
|
167
173
|
if (!contentType?.includes("application/json")) throw new ManifestGetError(`Invalid content type: expected application/json, got ${contentType}`, manifestUrl);
|
|
168
174
|
return response.text();
|
|
169
175
|
}
|
|
170
|
-
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region src/utils/manifest-formatter/types.ts
|
|
173
|
-
/**
|
|
174
|
-
* Maximum length for a summary before truncation.
|
|
175
|
-
*/
|
|
176
|
-
const MAX_SUMMARY_LENGTH = 90;
|
|
177
|
-
/**
|
|
178
|
-
* Maximum number of stories to show in full detail in component manifests.
|
|
179
|
-
* Remaining stories will be shown as names only.
|
|
180
|
-
*/
|
|
181
|
-
const MAX_STORIES_TO_SHOW = 3;
|
|
182
|
-
|
|
183
|
-
//#endregion
|
|
184
|
-
//#region src/utils/manifest-formatter/extract-docs-summary.ts
|
|
185
|
-
/**
|
|
186
|
-
* Extracts a summary from MDX content.
|
|
187
|
-
* The summary is created by:
|
|
188
|
-
* 1. Removing import statements
|
|
189
|
-
* 2. Removing JSX/MDX expressions
|
|
190
|
-
* 3. Extracting only text content from JSX/HTML elements
|
|
191
|
-
* 4. Truncating to MAX_SUMMARY_LENGTH characters if needed
|
|
192
|
-
*
|
|
193
|
-
* @param content - The MDX content string
|
|
194
|
-
* @returns A summary string, or undefined if no meaningful text content is found
|
|
195
|
-
*/
|
|
196
|
-
function extractDocsSummary(content) {
|
|
197
|
-
let result = content;
|
|
198
|
-
result = result.replace(/^\s*import\s+(?:[\s\S]*?from\s+)?['"][^'"]+['"];?\s*$/gm, "");
|
|
199
|
-
let prevResult = "";
|
|
200
|
-
while (prevResult !== result) {
|
|
201
|
-
prevResult = result;
|
|
202
|
-
result = result.replace(/\{[^{}]*\}/g, "");
|
|
203
|
-
}
|
|
204
|
-
result = result.replace(/<[^>]+\/>/g, "");
|
|
205
|
-
prevResult = "";
|
|
206
|
-
while (prevResult !== result) {
|
|
207
|
-
prevResult = result;
|
|
208
|
-
result = result.replace(/<(\w+)[^>]*>([\s\S]*?)<\/\1>/g, "$2");
|
|
209
|
-
}
|
|
210
|
-
result = result.replace(/<[^>]+>/g, "");
|
|
211
|
-
result = result.replace(/\s+/g, " ").trim();
|
|
212
|
-
if (!result) return;
|
|
213
|
-
if (result.length > MAX_SUMMARY_LENGTH) return `${result.slice(0, MAX_SUMMARY_LENGTH)}...`;
|
|
214
|
-
return result;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
//#endregion
|
|
218
|
-
//#region src/utils/dedent.ts
|
|
219
176
|
/**
|
|
220
|
-
*
|
|
177
|
+
* Gets manifests from multiple sources.
|
|
178
|
+
* Returns an array of source manifests, each containing the source info and its manifests.
|
|
179
|
+
* Failures for individual sources are captured as errors rather than failing the entire request.
|
|
221
180
|
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* - https://github.com/tamino-martinius/node-ts-dedent/pull/41
|
|
228
|
-
* 2. Keeps bundle size smaller for such a simple utility (avoid "is-even" syndrome)
|
|
181
|
+
* @param sources - Array of source configurations
|
|
182
|
+
* @param request - The HTTP request (used for local source)
|
|
183
|
+
* @param manifestProvider - Function to fetch manifests, receives source as third parameter
|
|
184
|
+
* @returns Promise resolving to array of source manifests
|
|
185
|
+
* @throws {ManifestGetError} If no sources could be fetched successfully
|
|
229
186
|
*/
|
|
230
|
-
function
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
});
|
|
253
|
-
return string;
|
|
187
|
+
async function getMultiSourceManifests(sources, request, manifestProvider) {
|
|
188
|
+
const results = await Promise.all(sources.map(async (source) => {
|
|
189
|
+
try {
|
|
190
|
+
const manifests = await getManifests(request, manifestProvider, source);
|
|
191
|
+
return {
|
|
192
|
+
source,
|
|
193
|
+
componentManifest: manifests.componentManifest,
|
|
194
|
+
docsManifest: manifests.docsManifest
|
|
195
|
+
};
|
|
196
|
+
} catch (error) {
|
|
197
|
+
return {
|
|
198
|
+
source,
|
|
199
|
+
componentManifest: {
|
|
200
|
+
v: 1,
|
|
201
|
+
components: {}
|
|
202
|
+
},
|
|
203
|
+
error: error instanceof Error ? error.message : String(error)
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}));
|
|
207
|
+
if (results.filter((r) => !r.error).length === 0) throw new ManifestGetError(`Failed to fetch manifests from any source. Errors:\n${results.map((r) => `- ${r.source.title}: ${r.error}`).join("\n")}`);
|
|
208
|
+
return results;
|
|
254
209
|
}
|
|
255
210
|
|
|
256
211
|
//#endregion
|
|
@@ -300,131 +255,115 @@ const parseReactDocgen = (reactDocgen) => {
|
|
|
300
255
|
required: prop.required
|
|
301
256
|
}])) };
|
|
302
257
|
};
|
|
258
|
+
/**
|
|
259
|
+
* Parse react-docgen-typescript output into the same simplified ParsedDocgen format.
|
|
260
|
+
* RDT uses flat type strings (prop.type.name / prop.type.raw) instead of react-docgen's
|
|
261
|
+
* nested tsType structure, so no serialization is needed.
|
|
262
|
+
*/
|
|
263
|
+
const parseReactDocgenTypescript = (reactDocgenTypescript) => {
|
|
264
|
+
const props = reactDocgenTypescript.props ?? {};
|
|
265
|
+
return { props: Object.fromEntries(Object.entries(props).map(([propName, prop]) => [propName, {
|
|
266
|
+
description: prop.description || void 0,
|
|
267
|
+
type: prop.type?.raw ?? prop.type?.name,
|
|
268
|
+
defaultValue: prop.defaultValue?.value,
|
|
269
|
+
required: prop.required
|
|
270
|
+
}])) };
|
|
271
|
+
};
|
|
303
272
|
|
|
304
273
|
//#endregion
|
|
305
|
-
//#region src/utils/
|
|
274
|
+
//#region src/utils/dedent.ts
|
|
306
275
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
276
|
+
* Copied from https://github.com/tamino-martinius/node-ts-dedent/blob/18c4736c79806d7fe78bdaaaf5ae307b79f9574a/src/index.ts
|
|
277
|
+
*
|
|
278
|
+
* Rationale for vendoring instead of using the ts-dedent package:
|
|
279
|
+
* 1. The package has a broken ESM distribution - the IDE auto-imports as
|
|
280
|
+
* `import dedent from 'ts-dedent'` but that breaks at runtime, requiring
|
|
281
|
+
* manual changes to named imports. See:
|
|
282
|
+
* - https://github.com/tamino-martinius/node-ts-dedent/issues/42
|
|
283
|
+
* - https://github.com/tamino-martinius/node-ts-dedent/pull/41
|
|
284
|
+
* 2. Keeps bundle size smaller for such a simple utility (avoid "is-even" syndrome)
|
|
309
285
|
*/
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const storiesToShow = storiesWithSnippets.slice(0, MAX_STORIES_TO_SHOW);
|
|
322
|
-
const remainingStories = storiesWithSnippets.slice(MAX_STORIES_TO_SHOW);
|
|
323
|
-
for (const story of storiesToShow) {
|
|
324
|
-
const storyParts = [];
|
|
325
|
-
storyParts.push(dedent`<story>
|
|
326
|
-
<story_name>${story.name.replace(/([A-Z])/g, " $1").trim()}</story_name>`);
|
|
327
|
-
if (story.description) storyParts.push(dedent`<story_description>
|
|
328
|
-
${story.description}
|
|
329
|
-
</story_description>`);
|
|
330
|
-
storyParts.push("<story_code>");
|
|
331
|
-
if (componentManifest.import) storyParts.push(`${componentManifest.import}\n`);
|
|
332
|
-
storyParts.push(dedent`${story.snippet}
|
|
333
|
-
</story_code>
|
|
334
|
-
</story>`);
|
|
335
|
-
parts.push(storyParts.join("\n"));
|
|
336
|
-
}
|
|
337
|
-
if (remainingStories.length > 0) {
|
|
338
|
-
parts.push("<other_stories>");
|
|
339
|
-
for (const story of remainingStories) {
|
|
340
|
-
const storyName = story.name.replace(/([A-Z])/g, " $1").trim();
|
|
341
|
-
parts.push(dedent`<story_name>${storyName}</story_name>`);
|
|
342
|
-
}
|
|
343
|
-
parts.push("</other_stories>");
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
if (componentManifest.reactDocgen) {
|
|
347
|
-
const parsedDocgen = parseReactDocgen(componentManifest.reactDocgen);
|
|
348
|
-
const propEntries = Object.entries(parsedDocgen.props);
|
|
349
|
-
if (propEntries.length > 0) {
|
|
350
|
-
parts.push("<props>");
|
|
351
|
-
for (const [propName, propInfo] of propEntries) {
|
|
352
|
-
parts.push(dedent`<prop>
|
|
353
|
-
<prop_name>${propName}</prop_name>`);
|
|
354
|
-
if (propInfo.description !== void 0) parts.push(dedent`<prop_description>
|
|
355
|
-
${propInfo.description}
|
|
356
|
-
</prop_description>`);
|
|
357
|
-
if (propInfo.type !== void 0) parts.push(dedent`<prop_type>${propInfo.type}</prop_type>`);
|
|
358
|
-
if (propInfo.required !== void 0) parts.push(dedent`<prop_required>${propInfo.required}</prop_required>`);
|
|
359
|
-
if (propInfo.defaultValue !== void 0) parts.push(dedent`<prop_default>${propInfo.defaultValue}</prop_default>`);
|
|
360
|
-
parts.push("</prop>");
|
|
361
|
-
}
|
|
362
|
-
parts.push("</props>");
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
parts.push("</component>");
|
|
366
|
-
return parts.join("\n");
|
|
367
|
-
},
|
|
368
|
-
formatDocsManifest(doc) {
|
|
369
|
-
return dedent`<doc>
|
|
370
|
-
<title>${doc.title}</title>
|
|
371
|
-
<content>
|
|
372
|
-
${doc.content}
|
|
373
|
-
</content>
|
|
374
|
-
</doc>`;
|
|
375
|
-
},
|
|
376
|
-
formatManifestsToLists(manifests) {
|
|
377
|
-
const parts = [];
|
|
378
|
-
parts.push("<components>");
|
|
379
|
-
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
380
|
-
parts.push(dedent`<component>
|
|
381
|
-
<id>${component.id}</id>
|
|
382
|
-
<name>${component.name}</name>`);
|
|
383
|
-
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH ? `${component.description.slice(0, MAX_SUMMARY_LENGTH)}...` : component.description : void 0);
|
|
384
|
-
if (summary) parts.push(dedent`<summary>
|
|
385
|
-
${summary}
|
|
386
|
-
</summary>`);
|
|
387
|
-
parts.push("</component>");
|
|
388
|
-
}
|
|
389
|
-
parts.push("</components>");
|
|
390
|
-
if (!manifests.docsManifest) return parts.join("\n");
|
|
391
|
-
parts.push("<docs>");
|
|
392
|
-
for (const doc of Object.values(manifests.docsManifest.docs)) {
|
|
393
|
-
const summary = doc.summary ?? extractDocsSummary(doc.content);
|
|
394
|
-
parts.push(dedent`<doc>
|
|
395
|
-
<id>${doc.id}</id>
|
|
396
|
-
<title>${doc.title}</title>`);
|
|
397
|
-
if (summary) parts.push(dedent`<summary>
|
|
398
|
-
${summary}
|
|
399
|
-
</summary>`);
|
|
400
|
-
parts.push("</doc>");
|
|
401
|
-
}
|
|
402
|
-
parts.push("</docs>");
|
|
403
|
-
return parts.join("\n");
|
|
404
|
-
},
|
|
405
|
-
formatStoryDocumentation(componentManifest, storyName) {
|
|
406
|
-
const story = componentManifest.stories?.find((s) => s.name === storyName);
|
|
407
|
-
if (!story || !story.snippet) return "";
|
|
408
|
-
const parts = [];
|
|
409
|
-
const displayStoryName = story.name.replace(/([A-Z])/g, " $1").trim();
|
|
410
|
-
parts.push(dedent`<story_documentation>
|
|
411
|
-
<component_name>${componentManifest.name}</component_name>
|
|
412
|
-
<story_name>${displayStoryName}</story_name>`);
|
|
413
|
-
if (story.description) parts.push(dedent`<story_description>
|
|
414
|
-
${story.description}
|
|
415
|
-
</story_description>`);
|
|
416
|
-
parts.push("<story_code>");
|
|
417
|
-
if (componentManifest.import) parts.push(`${componentManifest.import}\n`);
|
|
418
|
-
parts.push(dedent`${story.snippet}
|
|
419
|
-
</story_code>
|
|
420
|
-
</story_documentation>`);
|
|
421
|
-
return parts.join("\n");
|
|
286
|
+
function dedent(templ, ...values) {
|
|
287
|
+
let strings = Array.from(typeof templ === "string" ? [templ] : templ);
|
|
288
|
+
strings[strings.length - 1] = strings.at(-1).replace(/\r?\n([\t ]*)$/, "");
|
|
289
|
+
const indentLengths = strings.reduce((arr, str) => {
|
|
290
|
+
const matches = str.match(/\n([\t ]+|(?!\s).)/g);
|
|
291
|
+
if (matches) return arr.concat(matches.map((match) => match.match(/[\t ]/g)?.length ?? 0));
|
|
292
|
+
return arr;
|
|
293
|
+
}, []);
|
|
294
|
+
if (indentLengths.length) {
|
|
295
|
+
const pattern = new RegExp(`\n[\t ]{${Math.min(...indentLengths)}}`, "g");
|
|
296
|
+
strings = strings.map((str) => str.replace(pattern, "\n"));
|
|
422
297
|
}
|
|
423
|
-
|
|
298
|
+
strings[0] = strings[0].replace(/^\r?\n/, "");
|
|
299
|
+
let string = strings[0];
|
|
300
|
+
values.forEach((value, i) => {
|
|
301
|
+
const endentations = string.match(/(?:^|\n)( *)$/);
|
|
302
|
+
const endentation = endentations ? endentations[1] : "";
|
|
303
|
+
let indentedValue = value;
|
|
304
|
+
if (typeof value === "string" && value.includes("\n")) indentedValue = String(value).split("\n").map((str, i$1) => {
|
|
305
|
+
return i$1 === 0 ? str : `${endentation}${str}`;
|
|
306
|
+
}).join("\n");
|
|
307
|
+
string += `${String(indentedValue)}${strings[i + 1]}`;
|
|
308
|
+
});
|
|
309
|
+
return string;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/utils/manifest-formatter/extract-docs-summary.ts
|
|
314
|
+
/**
|
|
315
|
+
* Maximum length for a summary before truncation.
|
|
316
|
+
*/
|
|
317
|
+
const MAX_SUMMARY_LENGTH = 90;
|
|
318
|
+
/**
|
|
319
|
+
* Extracts a summary from MDX content.
|
|
320
|
+
* The summary is created by:
|
|
321
|
+
* 1. Removing import statements
|
|
322
|
+
* 2. Removing JSX/MDX expressions
|
|
323
|
+
* 3. Extracting only text content from JSX/HTML elements
|
|
324
|
+
* 4. Truncating to MAX_SUMMARY_LENGTH characters if needed
|
|
325
|
+
*
|
|
326
|
+
* @param content - The MDX content string
|
|
327
|
+
* @returns A summary string, or undefined if no meaningful text content is found
|
|
328
|
+
*/
|
|
329
|
+
function extractDocsSummary(content) {
|
|
330
|
+
let result = content;
|
|
331
|
+
result = result.replace(/^\s*import\s+(?:[\s\S]*?from\s+)?['"][^'"]+['"];?\s*$/gm, "");
|
|
332
|
+
let prevResult = "";
|
|
333
|
+
while (prevResult !== result) {
|
|
334
|
+
prevResult = result;
|
|
335
|
+
result = result.replace(/\{[^{}]*\}/g, "");
|
|
336
|
+
}
|
|
337
|
+
result = result.replace(/<[^>]+\/>/g, "");
|
|
338
|
+
prevResult = "";
|
|
339
|
+
while (prevResult !== result) {
|
|
340
|
+
prevResult = result;
|
|
341
|
+
result = result.replace(/<(\w+)[^>]*>([\s\S]*?)<\/\1>/g, "$2");
|
|
342
|
+
}
|
|
343
|
+
result = result.replace(/<[^>]+>/g, "");
|
|
344
|
+
result = result.replace(/\s+/g, " ").trim();
|
|
345
|
+
if (!result) return;
|
|
346
|
+
if (result.length > MAX_SUMMARY_LENGTH) return `${result.slice(0, MAX_SUMMARY_LENGTH)}...`;
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
424
349
|
|
|
425
350
|
//#endregion
|
|
426
351
|
//#region src/utils/manifest-formatter/markdown.ts
|
|
427
352
|
/**
|
|
353
|
+
* Maximum number of stories to show in full detail in component manifests.
|
|
354
|
+
* Remaining stories will be shown as names only.
|
|
355
|
+
*/
|
|
356
|
+
const MAX_STORIES_TO_SHOW = 3;
|
|
357
|
+
function formatComponentLine(component) {
|
|
358
|
+
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH ? `${component.description.slice(0, MAX_SUMMARY_LENGTH)}...` : component.description : void 0);
|
|
359
|
+
if (summary) return `- ${component.name} (${component.id}): ${summary}`;
|
|
360
|
+
return `- ${component.name} (${component.id})`;
|
|
361
|
+
}
|
|
362
|
+
function formatDocLine(doc) {
|
|
363
|
+
const summary = doc.summary ?? extractDocsSummary(doc.content);
|
|
364
|
+
return `- ${doc.title} (${doc.id})${summary ? `: ${summary}` : ""}`;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
428
367
|
* Extracts a summary from an object with optional summary and description fields.
|
|
429
368
|
* Prefers summary if available, otherwise truncates description to maxLength.
|
|
430
369
|
*/
|
|
@@ -433,6 +372,13 @@ function extractSummary(item, maxLength = MAX_SUMMARY_LENGTH) {
|
|
|
433
372
|
if (item.description) return item.description.length > maxLength ? `${item.description.slice(0, maxLength)}...` : item.description;
|
|
434
373
|
}
|
|
435
374
|
/**
|
|
375
|
+
* Extract parsed docgen from a component manifest, preferring reactDocgen over reactDocgenTypescript.
|
|
376
|
+
*/
|
|
377
|
+
function getParsedDocgen(componentManifest) {
|
|
378
|
+
if (componentManifest.reactDocgen) return parseReactDocgen(componentManifest.reactDocgen);
|
|
379
|
+
if (componentManifest.reactDocgenTypescript) return parseReactDocgenTypescript(componentManifest.reactDocgenTypescript);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
436
382
|
* Formats a story's content (description + code snippet) into markdown.
|
|
437
383
|
* Reusable helper for both formatComponentManifest and formatStoryDocumentation.
|
|
438
384
|
*/
|
|
@@ -452,160 +398,155 @@ function formatStoryContent(story, importStatement) {
|
|
|
452
398
|
return parts;
|
|
453
399
|
}
|
|
454
400
|
/**
|
|
455
|
-
*
|
|
456
|
-
* Formats component data into token-efficient markdown with headers, lists, and tables.
|
|
457
|
-
* Uses adaptive formatting based on prop complexity to optimize token usage.
|
|
401
|
+
* Format a single component manifest into markdown.
|
|
458
402
|
*/
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
403
|
+
function formatComponentManifest(componentManifest) {
|
|
404
|
+
const parts = [];
|
|
405
|
+
parts.push(`# ${componentManifest.name}`);
|
|
406
|
+
parts.push("");
|
|
407
|
+
parts.push(`ID: ${componentManifest.id}`);
|
|
408
|
+
parts.push("");
|
|
409
|
+
if (componentManifest.description) {
|
|
410
|
+
parts.push(componentManifest.description);
|
|
463
411
|
parts.push("");
|
|
464
|
-
|
|
412
|
+
}
|
|
413
|
+
const parsedDocgen = getParsedDocgen(componentManifest);
|
|
414
|
+
if (componentManifest.stories && componentManifest.stories.length > 0) {
|
|
415
|
+
parts.push("## Stories");
|
|
465
416
|
parts.push("");
|
|
466
|
-
|
|
467
|
-
|
|
417
|
+
const storiesWithSnippets = componentManifest.stories.filter((s) => s.snippet);
|
|
418
|
+
const hasProps = parsedDocgen && Object.keys(parsedDocgen.props).length > 0;
|
|
419
|
+
const storiesToShow = hasProps ? storiesWithSnippets.slice(0, MAX_STORIES_TO_SHOW) : storiesWithSnippets;
|
|
420
|
+
const remainingStories = hasProps ? storiesWithSnippets.slice(MAX_STORIES_TO_SHOW) : [];
|
|
421
|
+
for (const story of storiesToShow) {
|
|
422
|
+
parts.push(`### ${story.name}`);
|
|
423
|
+
parts.push("");
|
|
424
|
+
parts.push(...formatStoryContent(story, componentManifest.import));
|
|
468
425
|
parts.push("");
|
|
469
426
|
}
|
|
470
|
-
if (
|
|
471
|
-
parts.push("
|
|
427
|
+
if (remainingStories.length > 0) {
|
|
428
|
+
if (storiesToShow.length > 0) parts.push("### Other Stories");
|
|
472
429
|
parts.push("");
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
for (const story of storiesToShow) {
|
|
478
|
-
parts.push(`### ${story.name}`);
|
|
479
|
-
parts.push("");
|
|
480
|
-
parts.push(...formatStoryContent(story, componentManifest.import));
|
|
481
|
-
parts.push("");
|
|
482
|
-
}
|
|
483
|
-
if (remainingStories.length > 0) {
|
|
484
|
-
if (storiesToShow.length > 0) parts.push("### Other Stories");
|
|
485
|
-
parts.push("");
|
|
486
|
-
for (const story of remainingStories) {
|
|
487
|
-
const summary = extractSummary(story);
|
|
488
|
-
const summaryPart = summary ? `: ${summary}` : "";
|
|
489
|
-
parts.push(`- ${story.name}${summaryPart}`);
|
|
490
|
-
}
|
|
491
|
-
parts.push("");
|
|
430
|
+
for (const story of remainingStories) {
|
|
431
|
+
const summary = extractSummary(story);
|
|
432
|
+
const summaryPart = summary ? `: ${summary}` : "";
|
|
433
|
+
parts.push(`- ${story.name}${summaryPart}`);
|
|
492
434
|
}
|
|
435
|
+
parts.push("");
|
|
493
436
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
}
|
|
511
|
-
let propLine = ` ${propName}`;
|
|
512
|
-
if (!isRequired) propLine += "?";
|
|
513
|
-
propLine += `: ${type}`;
|
|
514
|
-
if (hasDefault) propLine += ` = ${propInfo.defaultValue}`;
|
|
515
|
-
propLine += ";";
|
|
516
|
-
parts.push(propLine);
|
|
437
|
+
}
|
|
438
|
+
if (parsedDocgen) {
|
|
439
|
+
const propEntries = Object.entries(parsedDocgen.props);
|
|
440
|
+
if (propEntries.length > 0) {
|
|
441
|
+
parts.push("## Props");
|
|
442
|
+
parts.push("");
|
|
443
|
+
parts.push("```");
|
|
444
|
+
parts.push("export type Props = {");
|
|
445
|
+
for (const [propName, propInfo] of propEntries) {
|
|
446
|
+
const type = propInfo.type ?? "any";
|
|
447
|
+
const isRequired = propInfo.required ?? true;
|
|
448
|
+
const hasDefault = propInfo.defaultValue !== void 0;
|
|
449
|
+
if (propInfo.description !== void 0) {
|
|
450
|
+
parts.push(" /**");
|
|
451
|
+
parts.push(` ${propInfo.description}`);
|
|
452
|
+
parts.push(" */");
|
|
517
453
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
454
|
+
let propLine = ` ${propName}`;
|
|
455
|
+
if (!isRequired) propLine += "?";
|
|
456
|
+
propLine += `: ${type}`;
|
|
457
|
+
if (hasDefault) propLine += ` = ${propInfo.defaultValue}`;
|
|
458
|
+
propLine += ";";
|
|
459
|
+
parts.push(propLine);
|
|
521
460
|
}
|
|
461
|
+
parts.push("}");
|
|
462
|
+
parts.push("```");
|
|
463
|
+
parts.push("");
|
|
522
464
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
465
|
+
}
|
|
466
|
+
if (componentManifest.docs && Object.keys(componentManifest.docs).length > 0) {
|
|
467
|
+
const docsWithContent = Object.values(componentManifest.docs).filter((doc) => doc.content.trim().length > 0);
|
|
468
|
+
if (docsWithContent.length > 0) {
|
|
469
|
+
parts.push("## Docs");
|
|
470
|
+
parts.push("");
|
|
471
|
+
for (const doc of docsWithContent) {
|
|
472
|
+
parts.push(`### ${doc.name}`);
|
|
473
|
+
parts.push("");
|
|
474
|
+
parts.push(doc.content);
|
|
527
475
|
parts.push("");
|
|
528
|
-
for (const doc of docsWithContent) {
|
|
529
|
-
parts.push(`### ${doc.name}`);
|
|
530
|
-
parts.push("");
|
|
531
|
-
parts.push(doc.content);
|
|
532
|
-
parts.push("");
|
|
533
|
-
}
|
|
534
476
|
}
|
|
535
477
|
}
|
|
536
|
-
return parts.join("\n").trim();
|
|
537
|
-
},
|
|
538
|
-
formatDocsManifest(doc) {
|
|
539
|
-
return dedent`# ${doc.title}
|
|
540
|
-
|
|
541
|
-
${doc.content}`;
|
|
542
|
-
},
|
|
543
|
-
formatManifestsToLists(manifests) {
|
|
544
|
-
const parts = [];
|
|
545
|
-
parts.push("# Components");
|
|
546
|
-
parts.push("");
|
|
547
|
-
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
548
|
-
const summary = extractSummary(component);
|
|
549
|
-
if (summary) parts.push(`- ${component.name} (${component.id}): ${summary}`);
|
|
550
|
-
else parts.push(`- ${component.name} (${component.id})`);
|
|
551
|
-
}
|
|
552
|
-
parts.push("");
|
|
553
|
-
if (!manifests.docsManifest) return parts.join("\n").trim();
|
|
554
|
-
parts.push("# Docs");
|
|
555
|
-
parts.push("");
|
|
556
|
-
for (const doc of Object.values(manifests.docsManifest.docs)) {
|
|
557
|
-
const summary = doc.summary ?? extractDocsSummary(doc.content);
|
|
558
|
-
parts.push(`- ${doc.title} (${doc.id})${summary ? `: ${summary}` : ""}`);
|
|
559
|
-
}
|
|
560
|
-
return parts.join("\n").trim();
|
|
561
|
-
},
|
|
562
|
-
formatStoryDocumentation(componentManifest, storyName) {
|
|
563
|
-
const story = componentManifest.stories?.find((s) => s.name === storyName);
|
|
564
|
-
if (!story || !story.snippet) return "";
|
|
565
|
-
const parts = [];
|
|
566
|
-
parts.push(`# ${componentManifest.name} - ${story.name}`);
|
|
567
|
-
parts.push("");
|
|
568
|
-
parts.push(...formatStoryContent(story, componentManifest.import));
|
|
569
|
-
return parts.join("\n").trim();
|
|
570
478
|
}
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
//#endregion
|
|
574
|
-
//#region src/utils/format-manifest.ts
|
|
575
|
-
const formatters = {
|
|
576
|
-
xml: xmlFormatter,
|
|
577
|
-
markdown: markdownFormatter
|
|
578
|
-
};
|
|
479
|
+
return parts.join("\n").trim();
|
|
480
|
+
}
|
|
579
481
|
/**
|
|
580
|
-
* Format a single
|
|
581
|
-
* @param componentManifest - The component manifest to format
|
|
582
|
-
* @param format - The desired output format (defaults to 'markdown')
|
|
583
|
-
* @returns Formatted string representation of the component
|
|
482
|
+
* Format a single doc manifest into markdown.
|
|
584
483
|
*/
|
|
585
|
-
function
|
|
586
|
-
return
|
|
484
|
+
function formatDocsManifest(doc) {
|
|
485
|
+
return dedent`# ${doc.title}
|
|
486
|
+
|
|
487
|
+
${doc.content}`;
|
|
587
488
|
}
|
|
588
489
|
/**
|
|
589
|
-
* Format a
|
|
590
|
-
* @param
|
|
591
|
-
* @
|
|
592
|
-
* @param format - The desired output format (defaults to 'markdown')
|
|
593
|
-
* @returns Formatted string representation of the story
|
|
490
|
+
* Format a component manifest map into a markdown list.
|
|
491
|
+
* @param manifest - The component manifest map to format
|
|
492
|
+
* @returns Formatted string representation of the component list
|
|
594
493
|
*/
|
|
595
|
-
function
|
|
596
|
-
|
|
494
|
+
function formatManifestsToLists(manifests) {
|
|
495
|
+
const parts = [];
|
|
496
|
+
parts.push("# Components");
|
|
497
|
+
parts.push("");
|
|
498
|
+
for (const component of Object.values(manifests.componentManifest.components)) parts.push(formatComponentLine(component));
|
|
499
|
+
parts.push("");
|
|
500
|
+
if (!manifests.docsManifest) return parts.join("\n").trim();
|
|
501
|
+
parts.push("# Docs");
|
|
502
|
+
parts.push("");
|
|
503
|
+
for (const doc of Object.values(manifests.docsManifest.docs)) parts.push(formatDocLine(doc));
|
|
504
|
+
return parts.join("\n").trim();
|
|
597
505
|
}
|
|
598
506
|
/**
|
|
599
|
-
* Format
|
|
507
|
+
* Format multi-source manifests into grouped lists.
|
|
508
|
+
* Each source is displayed with a heading and its components/docs listed underneath.
|
|
509
|
+
* @param manifests - The multi-source manifests to format
|
|
510
|
+
* @returns Formatted string representation grouped by source
|
|
600
511
|
*/
|
|
601
|
-
function
|
|
602
|
-
|
|
512
|
+
function formatMultiSourceManifestsToLists(manifests) {
|
|
513
|
+
const parts = [];
|
|
514
|
+
for (const { source, componentManifest, docsManifest, error } of manifests) {
|
|
515
|
+
parts.push(`# ${source.title}`);
|
|
516
|
+
parts.push(`id: ${source.id}`);
|
|
517
|
+
parts.push("");
|
|
518
|
+
if (error) {
|
|
519
|
+
parts.push(`error: ${error}`);
|
|
520
|
+
parts.push("");
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
const components = Object.values(componentManifest.components);
|
|
524
|
+
if (components.length > 0) {
|
|
525
|
+
parts.push("## Components");
|
|
526
|
+
parts.push("");
|
|
527
|
+
for (const component of components) parts.push(formatComponentLine(component));
|
|
528
|
+
parts.push("");
|
|
529
|
+
}
|
|
530
|
+
if (docsManifest && Object.keys(docsManifest.docs).length > 0) {
|
|
531
|
+
parts.push("## Docs");
|
|
532
|
+
parts.push("");
|
|
533
|
+
for (const doc of Object.values(docsManifest.docs)) parts.push(formatDocLine(doc));
|
|
534
|
+
parts.push("");
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return parts.join("\n").trim();
|
|
603
538
|
}
|
|
604
539
|
/**
|
|
605
|
-
* Format a
|
|
540
|
+
* Format a single story's documentation.
|
|
606
541
|
*/
|
|
607
|
-
function
|
|
608
|
-
|
|
542
|
+
function formatStoryDocumentation(componentManifest, storyName) {
|
|
543
|
+
const story = componentManifest.stories?.find((s) => s.name === storyName);
|
|
544
|
+
if (!story || !story.snippet) return "";
|
|
545
|
+
const parts = [];
|
|
546
|
+
parts.push(`# ${componentManifest.name} - ${story.name}`);
|
|
547
|
+
parts.push("");
|
|
548
|
+
parts.push(...formatStoryContent(story, componentManifest.import));
|
|
549
|
+
return parts.join("\n").trim();
|
|
609
550
|
}
|
|
610
551
|
|
|
611
552
|
//#endregion
|
|
@@ -619,10 +560,29 @@ async function addListAllDocumentationTool(server, enabled) {
|
|
|
619
560
|
enabled
|
|
620
561
|
}, async () => {
|
|
621
562
|
try {
|
|
622
|
-
const
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
563
|
+
const ctx = server.ctx.custom;
|
|
564
|
+
if (ctx?.sources?.some((s) => s.url)) {
|
|
565
|
+
const multiSourceManifests = await getMultiSourceManifests(ctx.sources, ctx.request, ctx.manifestProvider);
|
|
566
|
+
const lists$1 = formatMultiSourceManifestsToLists(multiSourceManifests);
|
|
567
|
+
const firstSuccess = multiSourceManifests.find((m) => !m.error);
|
|
568
|
+
if (firstSuccess) await ctx.onListAllDocumentation?.({
|
|
569
|
+
context: ctx,
|
|
570
|
+
manifests: {
|
|
571
|
+
componentManifest: firstSuccess.componentManifest,
|
|
572
|
+
docsManifest: firstSuccess.docsManifest
|
|
573
|
+
},
|
|
574
|
+
resultText: lists$1,
|
|
575
|
+
sources: multiSourceManifests
|
|
576
|
+
});
|
|
577
|
+
return { content: [{
|
|
578
|
+
type: "text",
|
|
579
|
+
text: lists$1
|
|
580
|
+
}] };
|
|
581
|
+
}
|
|
582
|
+
const manifests = await getManifests(ctx?.request, ctx?.manifestProvider);
|
|
583
|
+
const lists = formatManifestsToLists(manifests);
|
|
584
|
+
await ctx?.onListAllDocumentation?.({
|
|
585
|
+
context: ctx,
|
|
626
586
|
manifests,
|
|
627
587
|
resultText: lists
|
|
628
588
|
});
|
|
@@ -670,10 +630,9 @@ async function addGetStoryDocumentationTool(server, enabled) {
|
|
|
670
630
|
isError: true
|
|
671
631
|
};
|
|
672
632
|
}
|
|
673
|
-
const format = server.ctx.custom?.format ?? "markdown";
|
|
674
633
|
return { content: [{
|
|
675
634
|
type: "text",
|
|
676
|
-
text: formatStoryDocumentation(component, input.storyName
|
|
635
|
+
text: formatStoryDocumentation(component, input.storyName)
|
|
677
636
|
}] };
|
|
678
637
|
} catch (error) {
|
|
679
638
|
return errorToMCPContent(error);
|
|
@@ -684,8 +643,13 @@ async function addGetStoryDocumentationTool(server, enabled) {
|
|
|
684
643
|
//#endregion
|
|
685
644
|
//#region src/tools/get-documentation.ts
|
|
686
645
|
const GET_TOOL_NAME = "get-documentation";
|
|
687
|
-
const
|
|
688
|
-
|
|
646
|
+
const BaseInput = { id: v.pipe(v.string(), v.description("The component or docs entry ID (e.g., \"button\")")) };
|
|
647
|
+
const StorybookIdField = { storybookId: v.pipe(v.string(), v.description("The Storybook source ID (e.g., \"local\", \"tetra\"). Required when multiple Storybooks are composed. See list-all-documentation for available sources.")) };
|
|
648
|
+
async function addGetDocumentationTool(server, enabled, options) {
|
|
649
|
+
const schema = options?.multiSource ? v.object({
|
|
650
|
+
...BaseInput,
|
|
651
|
+
...StorybookIdField
|
|
652
|
+
}) : v.object(BaseInput);
|
|
689
653
|
server.tool({
|
|
690
654
|
name: GET_TOOL_NAME,
|
|
691
655
|
title: "Get Documentation",
|
|
@@ -694,31 +658,53 @@ async function addGetDocumentationTool(server, enabled) {
|
|
|
694
658
|
Returns the first ${MAX_STORIES_TO_SHOW} stories with code snippets showing how props are used, plus TypeScript prop definitions. Call this before using a component to avoid hallucinating prop names, types, or valid combinations. Stories reveal real prop usage patterns, interactions, and edge cases that type definitions alone don't show. If the example stories don't show the prop you need, use the ${GET_STORY_TOOL_NAME} tool to fetch the story documentation for the specific story variant you need.
|
|
695
659
|
|
|
696
660
|
Example: id="button" returns Primary, Secondary, Large stories with code like <Button variant="primary" size="large"> showing actual prop combinations.`,
|
|
697
|
-
schema
|
|
661
|
+
schema,
|
|
698
662
|
enabled
|
|
699
663
|
}, async (input) => {
|
|
700
664
|
try {
|
|
701
|
-
const
|
|
702
|
-
const
|
|
703
|
-
const
|
|
665
|
+
const ctx = server.ctx.custom;
|
|
666
|
+
const { id, storybookId } = input;
|
|
667
|
+
const sources = ctx?.sources;
|
|
668
|
+
const isMultiSource = sources && sources.some((s) => s.url);
|
|
669
|
+
let source;
|
|
670
|
+
if (isMultiSource) {
|
|
671
|
+
if (!storybookId) return {
|
|
672
|
+
content: [{
|
|
673
|
+
type: "text",
|
|
674
|
+
text: `storybookId is required. Available sources: ${sources.map((s) => s.id).join(", ")}. Use the ${LIST_TOOL_NAME} tool to see available sources.`
|
|
675
|
+
}],
|
|
676
|
+
isError: true
|
|
677
|
+
};
|
|
678
|
+
source = sources.find((s) => s.id === storybookId);
|
|
679
|
+
if (!source) return {
|
|
680
|
+
content: [{
|
|
681
|
+
type: "text",
|
|
682
|
+
text: `Storybook source not found: "${storybookId}". Available sources: ${sources.map((s) => s.id).join(", ")}. Use the ${LIST_TOOL_NAME} tool to see available sources.`
|
|
683
|
+
}],
|
|
684
|
+
isError: true
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
const { componentManifest, docsManifest } = await getManifests(ctx?.request, ctx?.manifestProvider, source);
|
|
688
|
+
const component = componentManifest.components[id];
|
|
689
|
+
const docsEntry = docsManifest?.docs[id];
|
|
704
690
|
if (!component && !docsEntry) {
|
|
705
|
-
|
|
706
|
-
|
|
691
|
+
const suffix = storybookId ? ` in source "${storybookId}"` : "";
|
|
692
|
+
await ctx?.onGetDocumentation?.({
|
|
693
|
+
context: ctx,
|
|
707
694
|
input
|
|
708
695
|
});
|
|
709
696
|
return {
|
|
710
697
|
content: [{
|
|
711
698
|
type: "text",
|
|
712
|
-
text: `Component or Docs Entry not found: "${
|
|
699
|
+
text: `Component or Docs Entry not found: "${id}"${suffix}. Use the ${LIST_TOOL_NAME} tool to see available components and documentation entries.`
|
|
713
700
|
}],
|
|
714
701
|
isError: true
|
|
715
702
|
};
|
|
716
703
|
}
|
|
717
704
|
const documentation = component ?? docsEntry;
|
|
718
|
-
const
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
context: server.ctx.custom,
|
|
705
|
+
const text = component ? formatComponentManifest(documentation) : formatDocsManifest(documentation);
|
|
706
|
+
await ctx?.onGetDocumentation?.({
|
|
707
|
+
context: ctx,
|
|
722
708
|
input,
|
|
723
709
|
foundDocumentation: documentation,
|
|
724
710
|
resultText: text
|
|
@@ -753,7 +739,6 @@ const createStorybookMcpHandler = async (options = {}) => {
|
|
|
753
739
|
return (async (req, context) => {
|
|
754
740
|
return await transport.respond(req, {
|
|
755
741
|
request: req,
|
|
756
|
-
format: context?.format ?? options.format ?? "markdown",
|
|
757
742
|
manifestProvider: context?.manifestProvider ?? options.manifestProvider,
|
|
758
743
|
onListAllDocumentation: context?.onListAllDocumentation ?? options.onListAllDocumentation,
|
|
759
744
|
onGetDocumentation: context?.onGetDocumentation ?? options.onGetDocumentation
|
|
@@ -762,4 +747,4 @@ const createStorybookMcpHandler = async (options = {}) => {
|
|
|
762
747
|
};
|
|
763
748
|
|
|
764
749
|
//#endregion
|
|
765
|
-
export { COMPONENT_MANIFEST_PATH, DOCS_MANIFEST_PATH, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
|
|
750
|
+
export { COMPONENT_MANIFEST_PATH, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storybook/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@tmcp/transport-stdio": "^0.4.1",
|
|
35
35
|
"react-docgen": "^8.0.2",
|
|
36
|
+
"react-docgen-typescript": "^2.4.0",
|
|
36
37
|
"srvx": "^0.8.16",
|
|
37
38
|
"tinyexec": "^1.0.2"
|
|
38
39
|
},
|