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