@storybook/mcp 0.1.1 → 0.2.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 +81 -15
- package/dist/index.js +189 -68
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -32,25 +32,42 @@ type StorybookContext = {
|
|
|
32
32
|
*/
|
|
33
33
|
manifestProvider?: (request: Request | undefined, path: string) => Promise<string>;
|
|
34
34
|
/**
|
|
35
|
-
* Optional handler called when list-all-
|
|
35
|
+
* Optional handler called when list-all-documentation tool is invoked.
|
|
36
36
|
* Receives the context and the component manifest.
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
onListAllDocumentation?: (params: {
|
|
39
39
|
context: StorybookContext;
|
|
40
|
-
|
|
40
|
+
manifests: AllManifests;
|
|
41
41
|
}) => void | Promise<void>;
|
|
42
42
|
/**
|
|
43
43
|
* Optional handler called when get-component-documentation tool is invoked.
|
|
44
44
|
* Receives the context, input parameters, and the found component (if any).
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
onGetDocumentation?: (params: {
|
|
47
47
|
context: StorybookContext;
|
|
48
48
|
input: {
|
|
49
|
-
|
|
49
|
+
id: string;
|
|
50
50
|
};
|
|
51
|
-
|
|
51
|
+
foundDocumentation?: ComponentManifest | Doc;
|
|
52
52
|
}) => void | Promise<void>;
|
|
53
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* A docs entry represents MDX documentation that can be attached to a component
|
|
56
|
+
* or standalone (unattached).
|
|
57
|
+
*/
|
|
58
|
+
declare const Doc: v.ObjectSchema<{
|
|
59
|
+
readonly id: v.StringSchema<undefined>;
|
|
60
|
+
readonly name: v.StringSchema<undefined>;
|
|
61
|
+
readonly title: v.StringSchema<undefined>;
|
|
62
|
+
readonly path: v.StringSchema<undefined>;
|
|
63
|
+
readonly content: v.StringSchema<undefined>;
|
|
64
|
+
readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
65
|
+
readonly error: v.OptionalSchema<v.ObjectSchema<{
|
|
66
|
+
readonly name: v.StringSchema<undefined>;
|
|
67
|
+
readonly message: v.StringSchema<undefined>;
|
|
68
|
+
}, undefined>, undefined>;
|
|
69
|
+
}, undefined>;
|
|
70
|
+
type Doc = v.InferOutput<typeof Doc>;
|
|
54
71
|
declare const ComponentManifest: v.ObjectSchema<{
|
|
55
72
|
readonly id: v.StringSchema<undefined>;
|
|
56
73
|
readonly path: v.StringSchema<undefined>;
|
|
@@ -67,6 +84,18 @@ declare const ComponentManifest: v.ObjectSchema<{
|
|
|
67
84
|
}, undefined>, undefined>;
|
|
68
85
|
}, undefined>, undefined>, undefined>;
|
|
69
86
|
readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
|
|
87
|
+
readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
|
|
88
|
+
readonly id: v.StringSchema<undefined>;
|
|
89
|
+
readonly name: v.StringSchema<undefined>;
|
|
90
|
+
readonly title: v.StringSchema<undefined>;
|
|
91
|
+
readonly path: v.StringSchema<undefined>;
|
|
92
|
+
readonly content: v.StringSchema<undefined>;
|
|
93
|
+
readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
94
|
+
readonly error: v.OptionalSchema<v.ObjectSchema<{
|
|
95
|
+
readonly name: v.StringSchema<undefined>;
|
|
96
|
+
readonly message: v.StringSchema<undefined>;
|
|
97
|
+
}, undefined>, undefined>;
|
|
98
|
+
}, undefined>, undefined>, undefined>;
|
|
70
99
|
readonly name: v.StringSchema<undefined>;
|
|
71
100
|
readonly description: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
72
101
|
readonly jsDocTags: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
|
|
@@ -94,6 +123,18 @@ declare const ComponentManifestMap: v.ObjectSchema<{
|
|
|
94
123
|
}, undefined>, undefined>;
|
|
95
124
|
}, undefined>, undefined>, undefined>;
|
|
96
125
|
readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
|
|
126
|
+
readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
|
|
127
|
+
readonly id: v.StringSchema<undefined>;
|
|
128
|
+
readonly name: v.StringSchema<undefined>;
|
|
129
|
+
readonly title: v.StringSchema<undefined>;
|
|
130
|
+
readonly path: v.StringSchema<undefined>;
|
|
131
|
+
readonly content: v.StringSchema<undefined>;
|
|
132
|
+
readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
133
|
+
readonly error: v.OptionalSchema<v.ObjectSchema<{
|
|
134
|
+
readonly name: v.StringSchema<undefined>;
|
|
135
|
+
readonly message: v.StringSchema<undefined>;
|
|
136
|
+
}, undefined>, undefined>;
|
|
137
|
+
}, undefined>, undefined>, undefined>;
|
|
97
138
|
readonly name: v.StringSchema<undefined>;
|
|
98
139
|
readonly description: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
99
140
|
readonly jsDocTags: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
|
|
@@ -104,20 +145,45 @@ declare const ComponentManifestMap: v.ObjectSchema<{
|
|
|
104
145
|
}, undefined>, undefined>;
|
|
105
146
|
}, undefined>;
|
|
106
147
|
type ComponentManifestMap = v.InferOutput<typeof ComponentManifestMap>;
|
|
148
|
+
/**
|
|
149
|
+
* Manifest for unattached/standalone documentation entries.
|
|
150
|
+
* Served at /manifests/docs.json
|
|
151
|
+
*/
|
|
152
|
+
declare const DocsManifestMap: v.ObjectSchema<{
|
|
153
|
+
readonly v: v.NumberSchema<undefined>;
|
|
154
|
+
readonly docs: v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
|
|
155
|
+
readonly id: v.StringSchema<undefined>;
|
|
156
|
+
readonly name: v.StringSchema<undefined>;
|
|
157
|
+
readonly title: v.StringSchema<undefined>;
|
|
158
|
+
readonly path: v.StringSchema<undefined>;
|
|
159
|
+
readonly content: v.StringSchema<undefined>;
|
|
160
|
+
readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
161
|
+
readonly error: v.OptionalSchema<v.ObjectSchema<{
|
|
162
|
+
readonly name: v.StringSchema<undefined>;
|
|
163
|
+
readonly message: v.StringSchema<undefined>;
|
|
164
|
+
}, undefined>, undefined>;
|
|
165
|
+
}, undefined>, undefined>;
|
|
166
|
+
}, undefined>;
|
|
167
|
+
type DocsManifestMap = v.InferOutput<typeof DocsManifestMap>;
|
|
168
|
+
type AllManifests = {
|
|
169
|
+
componentManifest: ComponentManifestMap;
|
|
170
|
+
docsManifest?: DocsManifestMap;
|
|
171
|
+
};
|
|
107
172
|
//#endregion
|
|
108
|
-
//#region src/tools/list-all-
|
|
109
|
-
declare const LIST_TOOL_NAME = "list-all-
|
|
110
|
-
declare function
|
|
173
|
+
//#region src/tools/list-all-documentation.d.ts
|
|
174
|
+
declare const LIST_TOOL_NAME = "list-all-documentation";
|
|
175
|
+
declare function addListAllDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']): Promise<void>;
|
|
111
176
|
//#endregion
|
|
112
|
-
//#region src/tools/get-
|
|
113
|
-
declare const GET_TOOL_NAME = "get-
|
|
114
|
-
declare function
|
|
177
|
+
//#region src/tools/get-documentation.d.ts
|
|
178
|
+
declare const GET_TOOL_NAME = "get-documentation";
|
|
179
|
+
declare function addGetDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']): Promise<void>;
|
|
115
180
|
//#endregion
|
|
116
181
|
//#region src/utils/get-manifest.d.ts
|
|
117
182
|
/**
|
|
118
|
-
* The
|
|
183
|
+
* The paths to the manifest files relative to the Storybook build
|
|
119
184
|
*/
|
|
120
|
-
declare const
|
|
185
|
+
declare const COMPONENT_MANIFEST_PATH = "./manifests/components.json";
|
|
186
|
+
declare const DOCS_MANIFEST_PATH = "./manifests/docs.json";
|
|
121
187
|
//#endregion
|
|
122
188
|
//#region src/index.d.ts
|
|
123
189
|
type InitializeRequestParams = {
|
|
@@ -157,4 +223,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
|
|
|
157
223
|
type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
|
|
158
224
|
declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
|
|
159
225
|
//#endregion
|
|
160
|
-
export { type ComponentManifest, type ComponentManifestMap, GET_TOOL_NAME, LIST_TOOL_NAME,
|
|
226
|
+
export { COMPONENT_MANIFEST_PATH, type ComponentManifest, type ComponentManifestMap, DOCS_MANIFEST_PATH, GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, StorybookMcpHandlerOptions, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
|
package/dist/index.js
CHANGED
|
@@ -5,25 +5,39 @@ 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.2.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
|
|
12
12
|
//#region src/types.ts
|
|
13
13
|
const JSDocTag = v.record(v.string(), v.array(v.string()));
|
|
14
|
+
const Error$1 = v.object({
|
|
15
|
+
name: v.string(),
|
|
16
|
+
message: v.string()
|
|
17
|
+
});
|
|
14
18
|
const BaseManifest = v.object({
|
|
15
19
|
name: v.string(),
|
|
16
20
|
description: v.optional(v.string()),
|
|
17
21
|
jsDocTags: v.optional(JSDocTag),
|
|
18
|
-
error: v.optional(
|
|
19
|
-
name: v.string(),
|
|
20
|
-
message: v.string()
|
|
21
|
-
}))
|
|
22
|
+
error: v.optional(Error$1)
|
|
22
23
|
});
|
|
23
24
|
const Story = v.object({
|
|
24
25
|
...BaseManifest.entries,
|
|
25
26
|
snippet: v.optional(v.string())
|
|
26
27
|
});
|
|
28
|
+
/**
|
|
29
|
+
* A docs entry represents MDX documentation that can be attached to a component
|
|
30
|
+
* or standalone (unattached).
|
|
31
|
+
*/
|
|
32
|
+
const Doc = v.object({
|
|
33
|
+
id: v.string(),
|
|
34
|
+
name: v.string(),
|
|
35
|
+
title: v.string(),
|
|
36
|
+
path: v.string(),
|
|
37
|
+
content: v.string(),
|
|
38
|
+
summary: v.optional(v.string()),
|
|
39
|
+
error: v.optional(Error$1)
|
|
40
|
+
});
|
|
27
41
|
const ComponentManifest = v.object({
|
|
28
42
|
...BaseManifest.entries,
|
|
29
43
|
id: v.string(),
|
|
@@ -31,19 +45,29 @@ const ComponentManifest = v.object({
|
|
|
31
45
|
summary: v.optional(v.string()),
|
|
32
46
|
import: v.optional(v.string()),
|
|
33
47
|
stories: v.optional(v.array(Story)),
|
|
34
|
-
reactDocgen: v.optional(v.custom(() => true))
|
|
48
|
+
reactDocgen: v.optional(v.custom(() => true)),
|
|
49
|
+
docs: v.optional(v.record(v.string(), Doc))
|
|
35
50
|
});
|
|
36
51
|
const ComponentManifestMap = v.object({
|
|
37
52
|
v: v.number(),
|
|
38
53
|
components: v.record(v.string(), ComponentManifest)
|
|
39
54
|
});
|
|
55
|
+
/**
|
|
56
|
+
* Manifest for unattached/standalone documentation entries.
|
|
57
|
+
* Served at /manifests/docs.json
|
|
58
|
+
*/
|
|
59
|
+
const DocsManifestMap = v.object({
|
|
60
|
+
v: v.number(),
|
|
61
|
+
docs: v.record(v.string(), Doc)
|
|
62
|
+
});
|
|
40
63
|
|
|
41
64
|
//#endregion
|
|
42
65
|
//#region src/utils/get-manifest.ts
|
|
43
66
|
/**
|
|
44
|
-
* The
|
|
67
|
+
* The paths to the manifest files relative to the Storybook build
|
|
45
68
|
*/
|
|
46
|
-
const
|
|
69
|
+
const COMPONENT_MANIFEST_PATH = "./manifests/components.json";
|
|
70
|
+
const DOCS_MANIFEST_PATH = "./manifests/docs.json";
|
|
47
71
|
/**
|
|
48
72
|
* Error thrown when getting or parsing a manifest fails
|
|
49
73
|
*/
|
|
@@ -78,25 +102,46 @@ const errorToMCPContent = (error) => {
|
|
|
78
102
|
};
|
|
79
103
|
};
|
|
80
104
|
/**
|
|
81
|
-
*
|
|
105
|
+
* Parses a JSON string and validates it against a Valibot schema
|
|
106
|
+
*/
|
|
107
|
+
function parseManifest({ jsonString, schema, name: name$1, url }) {
|
|
108
|
+
try {
|
|
109
|
+
return v.parse(v.pipe(v.string(), v.parseJson(), schema), jsonString);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
throw new ManifestGetError(`Failed to parse ${name$1} manifest:
|
|
112
|
+
${error instanceof v.ValiError ? error.issues.map((i) => i.message).join("\n") : String(error)}`, url);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Gets component and docs manifest from a request or using a custom provider
|
|
82
117
|
*
|
|
83
118
|
* @param request - The HTTP request to get the manifest for (optional when using custom manifestProvider)
|
|
84
119
|
* @param manifestProvider - Optional custom function to get the manifest
|
|
85
120
|
* @returns A promise that resolves to the parsed ComponentManifestMap
|
|
86
121
|
* @throws {ManifestGetError} If getting the manifest fails or the response is invalid
|
|
87
122
|
*/
|
|
88
|
-
async function
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
123
|
+
async function getManifests(request, manifestProvider) {
|
|
124
|
+
const provider = manifestProvider ?? defaultManifestProvider;
|
|
125
|
+
const [componentResult, docsResult] = await Promise.allSettled([provider(request, COMPONENT_MANIFEST_PATH), provider(request, DOCS_MANIFEST_PATH)]);
|
|
126
|
+
const getUrl = (path) => request ? getManifestUrlFromRequest(request, path) : "Unknown manifest source";
|
|
127
|
+
if (componentResult.status === "rejected") throw new ManifestGetError(`Failed to get component manifest: ${componentResult.reason instanceof Error ? componentResult.reason.message : String(componentResult.reason)}`, getUrl(COMPONENT_MANIFEST_PATH), componentResult.reason instanceof Error ? componentResult.reason : void 0);
|
|
128
|
+
const componentManifest = parseManifest({
|
|
129
|
+
jsonString: componentResult.value,
|
|
130
|
+
schema: ComponentManifestMap,
|
|
131
|
+
name: "component",
|
|
132
|
+
url: getUrl(COMPONENT_MANIFEST_PATH)
|
|
133
|
+
});
|
|
134
|
+
if (Object.keys(componentManifest.components).length === 0) throw new ManifestGetError(`No components found in the manifest`, getUrl(COMPONENT_MANIFEST_PATH));
|
|
135
|
+
if (docsResult.status === "rejected") return { componentManifest };
|
|
136
|
+
return {
|
|
137
|
+
componentManifest,
|
|
138
|
+
docsManifest: parseManifest({
|
|
139
|
+
jsonString: docsResult.value,
|
|
140
|
+
schema: DocsManifestMap,
|
|
141
|
+
name: "docs",
|
|
142
|
+
url: getUrl(DOCS_MANIFEST_PATH)
|
|
143
|
+
})
|
|
144
|
+
};
|
|
100
145
|
}
|
|
101
146
|
/**
|
|
102
147
|
* Constructs the manifest URL from a request by replacing /mcp with the provided path
|
|
@@ -121,6 +166,47 @@ async function defaultManifestProvider(request, path) {
|
|
|
121
166
|
return response.text();
|
|
122
167
|
}
|
|
123
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
|
+
|
|
124
210
|
//#endregion
|
|
125
211
|
//#region src/utils/dedent.ts
|
|
126
212
|
/**
|
|
@@ -210,7 +296,6 @@ const parseReactDocgen = (reactDocgen) => {
|
|
|
210
296
|
|
|
211
297
|
//#endregion
|
|
212
298
|
//#region src/utils/manifest-formatter/xml.ts
|
|
213
|
-
const MAX_SUMMARY_LENGTH$1 = 90;
|
|
214
299
|
/**
|
|
215
300
|
* XML formatter for component manifests.
|
|
216
301
|
* Formats component data into XML structure with tags like <component>, <props>, etc.
|
|
@@ -261,27 +346,47 @@ const xmlFormatter = {
|
|
|
261
346
|
parts.push("</component>");
|
|
262
347
|
return parts.join("\n");
|
|
263
348
|
},
|
|
264
|
-
|
|
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) {
|
|
265
358
|
const parts = [];
|
|
266
359
|
parts.push("<components>");
|
|
267
|
-
for (const component of Object.values(
|
|
360
|
+
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
268
361
|
parts.push(dedent`<component>
|
|
269
362
|
<id>${component.id}</id>
|
|
270
363
|
<name>${component.name}</name>`);
|
|
271
|
-
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH
|
|
364
|
+
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH ? `${component.description.slice(0, MAX_SUMMARY_LENGTH)}...` : component.description : void 0);
|
|
272
365
|
if (summary) parts.push(dedent`<summary>
|
|
273
366
|
${summary}
|
|
274
367
|
</summary>`);
|
|
275
368
|
parts.push("</component>");
|
|
276
369
|
}
|
|
277
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>");
|
|
278
384
|
return parts.join("\n");
|
|
279
385
|
}
|
|
280
386
|
};
|
|
281
387
|
|
|
282
388
|
//#endregion
|
|
283
389
|
//#region src/utils/manifest-formatter/markdown.ts
|
|
284
|
-
const MAX_SUMMARY_LENGTH = 90;
|
|
285
390
|
/**
|
|
286
391
|
* Markdown formatter for component manifests.
|
|
287
392
|
* Formats component data into token-efficient markdown with headers, lists, and tables.
|
|
@@ -351,16 +456,28 @@ const markdownFormatter = {
|
|
|
351
456
|
}
|
|
352
457
|
return parts.join("\n").trim();
|
|
353
458
|
},
|
|
354
|
-
|
|
459
|
+
formatDocsManifest(doc) {
|
|
460
|
+
return dedent`# ${doc.title}
|
|
461
|
+
|
|
462
|
+
${doc.content}`;
|
|
463
|
+
},
|
|
464
|
+
formatManifestsToLists(manifests) {
|
|
355
465
|
const parts = [];
|
|
356
466
|
parts.push("# Components");
|
|
357
467
|
parts.push("");
|
|
358
|
-
for (const component of Object.values(
|
|
468
|
+
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
359
469
|
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH ? `${component.description.slice(0, MAX_SUMMARY_LENGTH)}...` : component.description : void 0);
|
|
360
470
|
if (summary) parts.push(`- ${component.name} (${component.id}): ${summary}`);
|
|
361
471
|
else parts.push(`- ${component.name} (${component.id})`);
|
|
362
472
|
}
|
|
363
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
|
+
}
|
|
364
481
|
return parts.join("\n").trim();
|
|
365
482
|
}
|
|
366
483
|
};
|
|
@@ -373,43 +490,43 @@ const formatters = {
|
|
|
373
490
|
};
|
|
374
491
|
/**
|
|
375
492
|
* Format a single component manifest.
|
|
376
|
-
* @param componentManifest - The component manifest to format
|
|
377
|
-
* @param format - The desired output format (defaults to 'markdown')
|
|
378
|
-
* @returns Formatted string representation of the component
|
|
379
493
|
*/
|
|
380
494
|
function formatComponentManifest(componentManifest, format = "markdown") {
|
|
381
495
|
return formatters[format].formatComponentManifest(componentManifest);
|
|
382
496
|
}
|
|
383
497
|
/**
|
|
384
|
-
* Format a
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
498
|
+
* Format a single docs manifest.
|
|
499
|
+
*/
|
|
500
|
+
function formatDocsManifest(doc, format = "markdown") {
|
|
501
|
+
return formatters[format].formatDocsManifest(doc);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Format a component manifest and optionally a docs manifest into lists.
|
|
388
505
|
*/
|
|
389
|
-
function
|
|
390
|
-
return formatters[format].
|
|
506
|
+
function formatManifestsToLists(manifests, format = "markdown") {
|
|
507
|
+
return formatters[format].formatManifestsToLists(manifests);
|
|
391
508
|
}
|
|
392
509
|
|
|
393
510
|
//#endregion
|
|
394
|
-
//#region src/tools/list-all-
|
|
395
|
-
const LIST_TOOL_NAME = "list-all-
|
|
396
|
-
async function
|
|
511
|
+
//#region src/tools/list-all-documentation.ts
|
|
512
|
+
const LIST_TOOL_NAME = "list-all-documentation";
|
|
513
|
+
async function addListAllDocumentationTool(server, enabled) {
|
|
397
514
|
server.tool({
|
|
398
515
|
name: LIST_TOOL_NAME,
|
|
399
|
-
title: "List All
|
|
400
|
-
description: "List all available UI components from the
|
|
516
|
+
title: "List All Documentation",
|
|
517
|
+
description: "List all available UI components and documentation entries from the Storybook",
|
|
401
518
|
enabled
|
|
402
519
|
}, async () => {
|
|
403
520
|
try {
|
|
404
|
-
const
|
|
405
|
-
const
|
|
406
|
-
await server.ctx.custom?.
|
|
521
|
+
const manifests = await getManifests(server.ctx.custom?.request, server.ctx.custom?.manifestProvider);
|
|
522
|
+
const lists = formatManifestsToLists(manifests, server.ctx.custom?.format ?? "markdown");
|
|
523
|
+
await server.ctx.custom?.onListAllDocumentation?.({
|
|
407
524
|
context: server.ctx.custom,
|
|
408
|
-
|
|
525
|
+
manifests
|
|
409
526
|
});
|
|
410
527
|
return { content: [{
|
|
411
528
|
type: "text",
|
|
412
|
-
text:
|
|
529
|
+
text: lists
|
|
413
530
|
}] };
|
|
414
531
|
} catch (error) {
|
|
415
532
|
return errorToMCPContent(error);
|
|
@@ -418,40 +535,44 @@ async function addListAllComponentsTool(server, enabled) {
|
|
|
418
535
|
}
|
|
419
536
|
|
|
420
537
|
//#endregion
|
|
421
|
-
//#region src/tools/get-
|
|
422
|
-
const GET_TOOL_NAME = "get-
|
|
423
|
-
const
|
|
424
|
-
async function
|
|
538
|
+
//#region src/tools/get-documentation.ts
|
|
539
|
+
const GET_TOOL_NAME = "get-documentation";
|
|
540
|
+
const GetDocumentationInput = v.object({ id: v.string() });
|
|
541
|
+
async function addGetDocumentationTool(server, enabled) {
|
|
425
542
|
server.tool({
|
|
426
543
|
name: GET_TOOL_NAME,
|
|
427
|
-
title: "Get Documentation
|
|
428
|
-
description: "Get detailed documentation for a specific UI component",
|
|
429
|
-
schema:
|
|
544
|
+
title: "Get Documentation",
|
|
545
|
+
description: "Get detailed documentation for a specific UI component or docs entry",
|
|
546
|
+
schema: GetDocumentationInput,
|
|
430
547
|
enabled
|
|
431
548
|
}, async (input) => {
|
|
432
549
|
try {
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
550
|
+
const { componentManifest, docsManifest } = await getManifests(server.ctx.custom?.request, server.ctx.custom?.manifestProvider);
|
|
551
|
+
const component = componentManifest.components[input.id];
|
|
552
|
+
const docsEntry = docsManifest?.docs[input.id];
|
|
553
|
+
if (!component && !docsEntry) {
|
|
554
|
+
await server.ctx.custom?.onGetDocumentation?.({
|
|
436
555
|
context: server.ctx.custom,
|
|
437
|
-
input
|
|
556
|
+
input
|
|
438
557
|
});
|
|
439
558
|
return {
|
|
440
559
|
content: [{
|
|
441
560
|
type: "text",
|
|
442
|
-
text: `Component not found: "${input.
|
|
561
|
+
text: `Component or Docs Entry not found: "${input.id}". Use the ${LIST_TOOL_NAME} tool to see available components and documentation entries.`
|
|
443
562
|
}],
|
|
444
563
|
isError: true
|
|
445
564
|
};
|
|
446
565
|
}
|
|
447
|
-
|
|
566
|
+
const documentation = component ?? docsEntry;
|
|
567
|
+
await server.ctx.custom?.onGetDocumentation?.({
|
|
448
568
|
context: server.ctx.custom,
|
|
449
|
-
input
|
|
450
|
-
|
|
569
|
+
input,
|
|
570
|
+
foundDocumentation: documentation
|
|
451
571
|
});
|
|
572
|
+
const format = server.ctx.custom?.format ?? "markdown";
|
|
452
573
|
return { content: [{
|
|
453
574
|
type: "text",
|
|
454
|
-
text: formatComponentManifest(
|
|
575
|
+
text: component ? formatComponentManifest(documentation, format) : formatDocsManifest(documentation, format)
|
|
455
576
|
}] };
|
|
456
577
|
} catch (error) {
|
|
457
578
|
return errorToMCPContent(error);
|
|
@@ -472,19 +593,19 @@ const createStorybookMcpHandler = async (options = {}) => {
|
|
|
472
593
|
capabilities: { tools: { listChanged: true } }
|
|
473
594
|
}).withContext();
|
|
474
595
|
if (options.onSessionInitialize) server.on("initialize", options.onSessionInitialize);
|
|
475
|
-
await
|
|
476
|
-
await
|
|
596
|
+
await addListAllDocumentationTool(server);
|
|
597
|
+
await addGetDocumentationTool(server);
|
|
477
598
|
const transport = new HttpTransport(server, { path: null });
|
|
478
599
|
return (async (req, context) => {
|
|
479
600
|
return await transport.respond(req, {
|
|
480
601
|
request: req,
|
|
481
602
|
format: context?.format ?? options.format ?? "markdown",
|
|
482
603
|
manifestProvider: context?.manifestProvider ?? options.manifestProvider,
|
|
483
|
-
|
|
484
|
-
|
|
604
|
+
onListAllDocumentation: context?.onListAllDocumentation ?? options.onListAllDocumentation,
|
|
605
|
+
onGetDocumentation: context?.onGetDocumentation ?? options.onGetDocumentation
|
|
485
606
|
});
|
|
486
607
|
});
|
|
487
608
|
};
|
|
488
609
|
|
|
489
610
|
//#endregion
|
|
490
|
-
export { GET_TOOL_NAME, LIST_TOOL_NAME,
|
|
611
|
+
export { COMPONENT_MANIFEST_PATH, DOCS_MANIFEST_PATH, GET_TOOL_NAME, LIST_TOOL_NAME, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
|