@storybook/mcp 0.1.0 → 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 +83 -17
- package/dist/index.js +200 -68
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ type OutputFormat = 'xml' | 'markdown';
|
|
|
11
11
|
* Custom context passed to MCP server and tools.
|
|
12
12
|
* Contains the request object and optional manifest provider.
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
type StorybookContext = {
|
|
15
15
|
/**
|
|
16
16
|
* The incoming HTTP request being processed.
|
|
17
17
|
*/
|
|
@@ -32,25 +32,42 @@ interface StorybookContext extends Record<string, unknown> {
|
|
|
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,8 +166,60 @@ 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
|
|
212
|
+
/**
|
|
213
|
+
* Copied from https://github.com/tamino-martinius/node-ts-dedent/blob/18c4736c79806d7fe78bdaaaf5ae307b79f9574a/src/index.ts
|
|
214
|
+
*
|
|
215
|
+
* Rationale for vendoring instead of using the ts-dedent package:
|
|
216
|
+
* 1. The package has a broken ESM distribution - the IDE auto-imports as
|
|
217
|
+
* `import dedent from 'ts-dedent'` but that breaks at runtime, requiring
|
|
218
|
+
* manual changes to named imports. See:
|
|
219
|
+
* - https://github.com/tamino-martinius/node-ts-dedent/issues/42
|
|
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)
|
|
222
|
+
*/
|
|
126
223
|
function dedent(templ, ...values) {
|
|
127
224
|
let strings = Array.from(typeof templ === "string" ? [templ] : templ);
|
|
128
225
|
strings[strings.length - 1] = strings.at(-1).replace(/\r?\n([\t ]*)$/, "");
|
|
@@ -199,7 +296,6 @@ const parseReactDocgen = (reactDocgen) => {
|
|
|
199
296
|
|
|
200
297
|
//#endregion
|
|
201
298
|
//#region src/utils/manifest-formatter/xml.ts
|
|
202
|
-
const MAX_SUMMARY_LENGTH$1 = 90;
|
|
203
299
|
/**
|
|
204
300
|
* XML formatter for component manifests.
|
|
205
301
|
* Formats component data into XML structure with tags like <component>, <props>, etc.
|
|
@@ -250,27 +346,47 @@ const xmlFormatter = {
|
|
|
250
346
|
parts.push("</component>");
|
|
251
347
|
return parts.join("\n");
|
|
252
348
|
},
|
|
253
|
-
|
|
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) {
|
|
254
358
|
const parts = [];
|
|
255
359
|
parts.push("<components>");
|
|
256
|
-
for (const component of Object.values(
|
|
360
|
+
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
257
361
|
parts.push(dedent`<component>
|
|
258
362
|
<id>${component.id}</id>
|
|
259
363
|
<name>${component.name}</name>`);
|
|
260
|
-
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);
|
|
261
365
|
if (summary) parts.push(dedent`<summary>
|
|
262
366
|
${summary}
|
|
263
367
|
</summary>`);
|
|
264
368
|
parts.push("</component>");
|
|
265
369
|
}
|
|
266
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>");
|
|
267
384
|
return parts.join("\n");
|
|
268
385
|
}
|
|
269
386
|
};
|
|
270
387
|
|
|
271
388
|
//#endregion
|
|
272
389
|
//#region src/utils/manifest-formatter/markdown.ts
|
|
273
|
-
const MAX_SUMMARY_LENGTH = 90;
|
|
274
390
|
/**
|
|
275
391
|
* Markdown formatter for component manifests.
|
|
276
392
|
* Formats component data into token-efficient markdown with headers, lists, and tables.
|
|
@@ -340,16 +456,28 @@ const markdownFormatter = {
|
|
|
340
456
|
}
|
|
341
457
|
return parts.join("\n").trim();
|
|
342
458
|
},
|
|
343
|
-
|
|
459
|
+
formatDocsManifest(doc) {
|
|
460
|
+
return dedent`# ${doc.title}
|
|
461
|
+
|
|
462
|
+
${doc.content}`;
|
|
463
|
+
},
|
|
464
|
+
formatManifestsToLists(manifests) {
|
|
344
465
|
const parts = [];
|
|
345
466
|
parts.push("# Components");
|
|
346
467
|
parts.push("");
|
|
347
|
-
for (const component of Object.values(
|
|
468
|
+
for (const component of Object.values(manifests.componentManifest.components)) {
|
|
348
469
|
const summary = component.summary ?? (component.description ? component.description.length > MAX_SUMMARY_LENGTH ? `${component.description.slice(0, MAX_SUMMARY_LENGTH)}...` : component.description : void 0);
|
|
349
470
|
if (summary) parts.push(`- ${component.name} (${component.id}): ${summary}`);
|
|
350
471
|
else parts.push(`- ${component.name} (${component.id})`);
|
|
351
472
|
}
|
|
352
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
|
+
}
|
|
353
481
|
return parts.join("\n").trim();
|
|
354
482
|
}
|
|
355
483
|
};
|
|
@@ -362,43 +490,43 @@ const formatters = {
|
|
|
362
490
|
};
|
|
363
491
|
/**
|
|
364
492
|
* Format a single component manifest.
|
|
365
|
-
* @param componentManifest - The component manifest to format
|
|
366
|
-
* @param format - The desired output format (defaults to 'markdown')
|
|
367
|
-
* @returns Formatted string representation of the component
|
|
368
493
|
*/
|
|
369
494
|
function formatComponentManifest(componentManifest, format = "markdown") {
|
|
370
495
|
return formatters[format].formatComponentManifest(componentManifest);
|
|
371
496
|
}
|
|
372
497
|
/**
|
|
373
|
-
* Format a
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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.
|
|
377
505
|
*/
|
|
378
|
-
function
|
|
379
|
-
return formatters[format].
|
|
506
|
+
function formatManifestsToLists(manifests, format = "markdown") {
|
|
507
|
+
return formatters[format].formatManifestsToLists(manifests);
|
|
380
508
|
}
|
|
381
509
|
|
|
382
510
|
//#endregion
|
|
383
|
-
//#region src/tools/list-all-
|
|
384
|
-
const LIST_TOOL_NAME = "list-all-
|
|
385
|
-
async function
|
|
511
|
+
//#region src/tools/list-all-documentation.ts
|
|
512
|
+
const LIST_TOOL_NAME = "list-all-documentation";
|
|
513
|
+
async function addListAllDocumentationTool(server, enabled) {
|
|
386
514
|
server.tool({
|
|
387
515
|
name: LIST_TOOL_NAME,
|
|
388
|
-
title: "List All
|
|
389
|
-
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",
|
|
390
518
|
enabled
|
|
391
519
|
}, async () => {
|
|
392
520
|
try {
|
|
393
|
-
const
|
|
394
|
-
const
|
|
395
|
-
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?.({
|
|
396
524
|
context: server.ctx.custom,
|
|
397
|
-
|
|
525
|
+
manifests
|
|
398
526
|
});
|
|
399
527
|
return { content: [{
|
|
400
528
|
type: "text",
|
|
401
|
-
text:
|
|
529
|
+
text: lists
|
|
402
530
|
}] };
|
|
403
531
|
} catch (error) {
|
|
404
532
|
return errorToMCPContent(error);
|
|
@@ -407,40 +535,44 @@ async function addListAllComponentsTool(server, enabled) {
|
|
|
407
535
|
}
|
|
408
536
|
|
|
409
537
|
//#endregion
|
|
410
|
-
//#region src/tools/get-
|
|
411
|
-
const GET_TOOL_NAME = "get-
|
|
412
|
-
const
|
|
413
|
-
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) {
|
|
414
542
|
server.tool({
|
|
415
543
|
name: GET_TOOL_NAME,
|
|
416
|
-
title: "Get Documentation
|
|
417
|
-
description: "Get detailed documentation for a specific UI component",
|
|
418
|
-
schema:
|
|
544
|
+
title: "Get Documentation",
|
|
545
|
+
description: "Get detailed documentation for a specific UI component or docs entry",
|
|
546
|
+
schema: GetDocumentationInput,
|
|
419
547
|
enabled
|
|
420
548
|
}, async (input) => {
|
|
421
549
|
try {
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
|
|
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?.({
|
|
425
555
|
context: server.ctx.custom,
|
|
426
|
-
input
|
|
556
|
+
input
|
|
427
557
|
});
|
|
428
558
|
return {
|
|
429
559
|
content: [{
|
|
430
560
|
type: "text",
|
|
431
|
-
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.`
|
|
432
562
|
}],
|
|
433
563
|
isError: true
|
|
434
564
|
};
|
|
435
565
|
}
|
|
436
|
-
|
|
566
|
+
const documentation = component ?? docsEntry;
|
|
567
|
+
await server.ctx.custom?.onGetDocumentation?.({
|
|
437
568
|
context: server.ctx.custom,
|
|
438
|
-
input
|
|
439
|
-
|
|
569
|
+
input,
|
|
570
|
+
foundDocumentation: documentation
|
|
440
571
|
});
|
|
572
|
+
const format = server.ctx.custom?.format ?? "markdown";
|
|
441
573
|
return { content: [{
|
|
442
574
|
type: "text",
|
|
443
|
-
text: formatComponentManifest(
|
|
575
|
+
text: component ? formatComponentManifest(documentation, format) : formatDocsManifest(documentation, format)
|
|
444
576
|
}] };
|
|
445
577
|
} catch (error) {
|
|
446
578
|
return errorToMCPContent(error);
|
|
@@ -461,19 +593,19 @@ const createStorybookMcpHandler = async (options = {}) => {
|
|
|
461
593
|
capabilities: { tools: { listChanged: true } }
|
|
462
594
|
}).withContext();
|
|
463
595
|
if (options.onSessionInitialize) server.on("initialize", options.onSessionInitialize);
|
|
464
|
-
await
|
|
465
|
-
await
|
|
596
|
+
await addListAllDocumentationTool(server);
|
|
597
|
+
await addGetDocumentationTool(server);
|
|
466
598
|
const transport = new HttpTransport(server, { path: null });
|
|
467
599
|
return (async (req, context) => {
|
|
468
600
|
return await transport.respond(req, {
|
|
469
601
|
request: req,
|
|
470
602
|
format: context?.format ?? options.format ?? "markdown",
|
|
471
603
|
manifestProvider: context?.manifestProvider ?? options.manifestProvider,
|
|
472
|
-
|
|
473
|
-
|
|
604
|
+
onListAllDocumentation: context?.onListAllDocumentation ?? options.onListAllDocumentation,
|
|
605
|
+
onGetDocumentation: context?.onGetDocumentation ?? options.onGetDocumentation
|
|
474
606
|
});
|
|
475
607
|
});
|
|
476
608
|
};
|
|
477
609
|
|
|
478
610
|
//#endregion
|
|
479
|
-
export { GET_TOOL_NAME, LIST_TOOL_NAME,
|
|
611
|
+
export { COMPONENT_MANIFEST_PATH, DOCS_MANIFEST_PATH, GET_TOOL_NAME, LIST_TOOL_NAME, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storybook/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"storybook",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@tmcp/adapter-valibot": "^0.1.4",
|
|
29
29
|
"@tmcp/transport-http": "^0.8.0",
|
|
30
30
|
"tmcp": "^1.16.0",
|
|
31
|
-
"valibot": "1.
|
|
31
|
+
"valibot": "1.2.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@tmcp/transport-stdio": "^0.4.1",
|