@storybook/mcp 0.0.3 → 0.0.5

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 CHANGED
@@ -1,5 +1,6 @@
1
1
  import { McpServer } from "tmcp";
2
- import "valibot";
2
+ import * as v from "valibot";
3
+ import { Documentation } from "react-docgen";
3
4
 
4
5
  //#region src/types.d.ts
5
6
 
@@ -18,7 +19,64 @@ interface StorybookContext extends Record<string, unknown> {
18
19
  * The function receives the source URL and should return the manifest as a string.
19
20
  */
20
21
  manifestProvider?: (source: string) => Promise<string>;
22
+ /**
23
+ * Optional handler called when list-all-components tool is invoked.
24
+ * Receives the context and the component manifest.
25
+ */
26
+ onListAllComponents?: (params: {
27
+ context: StorybookContext;
28
+ manifest: ComponentManifestMap;
29
+ }) => void | Promise<void>;
30
+ /**
31
+ * Optional handler called when get-component-documentation tool is invoked.
32
+ * Receives the context, input parameters, and the found components.
33
+ */
34
+ onGetComponentDocumentation?: (params: {
35
+ context: StorybookContext;
36
+ input: {
37
+ componentIds: string[];
38
+ };
39
+ foundComponents: ComponentManifest[];
40
+ notFoundIds: string[];
41
+ }) => void | Promise<void>;
21
42
  }
43
+ declare const ComponentManifest: v.ObjectSchema<{
44
+ readonly id: v.StringSchema<undefined>;
45
+ readonly summary: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
46
+ readonly examples: v.ExactOptionalSchema<v.ArraySchema<v.ObjectSchema<{
47
+ readonly snippet: v.StringSchema<undefined>;
48
+ readonly name: v.StringSchema<undefined>;
49
+ readonly description: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
50
+ readonly import: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
51
+ readonly jsDocTags: v.ExactOptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
52
+ }, undefined>, undefined>, undefined>;
53
+ readonly reactDocgen: v.ExactOptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
54
+ readonly name: v.StringSchema<undefined>;
55
+ readonly description: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
56
+ readonly import: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
57
+ readonly jsDocTags: v.ExactOptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
58
+ }, undefined>;
59
+ type ComponentManifest = v.InferOutput<typeof ComponentManifest>;
60
+ declare const ComponentManifestMap: v.ObjectSchema<{
61
+ readonly v: v.NumberSchema<undefined>;
62
+ readonly components: v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
63
+ readonly id: v.StringSchema<undefined>;
64
+ readonly summary: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
65
+ readonly examples: v.ExactOptionalSchema<v.ArraySchema<v.ObjectSchema<{
66
+ readonly snippet: v.StringSchema<undefined>;
67
+ readonly name: v.StringSchema<undefined>;
68
+ readonly description: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
69
+ readonly import: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
70
+ readonly jsDocTags: v.ExactOptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
71
+ }, undefined>, undefined>, undefined>;
72
+ readonly reactDocgen: v.ExactOptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
73
+ readonly name: v.StringSchema<undefined>;
74
+ readonly description: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
75
+ readonly import: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
76
+ readonly jsDocTags: v.ExactOptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
77
+ }, undefined>, undefined>;
78
+ }, undefined>;
79
+ type ComponentManifestMap = v.InferOutput<typeof ComponentManifestMap>;
22
80
  //#endregion
23
81
  //#region src/tools/list-all-components.d.ts
24
82
  declare const LIST_TOOL_NAME = "list-all-components";
@@ -29,7 +87,41 @@ declare const GET_TOOL_NAME = "get-component-documentation";
29
87
  declare function addGetComponentDocumentationTool(server: McpServer<any, StorybookContext>): Promise<void>;
30
88
  //#endregion
31
89
  //#region src/index.d.ts
90
+ type InitializeRequestParams = {
91
+ protocolVersion: string;
92
+ capabilities: {
93
+ experimental?: {} | undefined;
94
+ sampling?: {} | undefined;
95
+ elicitation?: {} | undefined;
96
+ roots?: {
97
+ listChanged?: boolean | undefined;
98
+ } | undefined;
99
+ };
100
+ clientInfo: {
101
+ icons?: {
102
+ src: string;
103
+ mimeType?: string | undefined;
104
+ sizes?: string[] | undefined;
105
+ }[] | undefined;
106
+ version: string;
107
+ websiteUrl?: string | undefined;
108
+ name: string;
109
+ title?: string | undefined;
110
+ };
111
+ };
112
+ /**
113
+ * Options for creating a Storybook MCP handler.
114
+ * Extends StorybookContext with server-level configuration.
115
+ */
116
+ interface StorybookMcpHandlerOptions extends StorybookContext {
117
+ /**
118
+ * Optional handler called when an MCP session is initialized.
119
+ * This is only valid at the handler creation level, not per-request.
120
+ * Receives the initialize request parameters from the MCP protocol.
121
+ */
122
+ onSessionInitialize?: (initializeRequestParams: InitializeRequestParams) => void | Promise<void>;
123
+ }
32
124
  type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
33
- declare const createStorybookMcpHandler: (options?: StorybookContext) => Promise<Handler>;
125
+ declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
34
126
  //#endregion
35
- export { GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, addGetComponentDocumentationTool, addListAllComponentsTool, createStorybookMcpHandler };
127
+ export { type ComponentManifest, type ComponentManifestMap, GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, StorybookMcpHandlerOptions, addGetComponentDocumentationTool, addListAllComponentsTool, createStorybookMcpHandler };
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.0.3";
8
+ var version = "0.0.5";
9
9
  var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
10
10
 
11
11
  //#endregion
@@ -26,7 +26,7 @@ const ComponentManifest = v.object({
26
26
  id: v.string(),
27
27
  summary: v.exactOptional(v.string()),
28
28
  examples: v.exactOptional(v.array(Example)),
29
- props: v.exactOptional(v.any())
29
+ reactDocgen: v.exactOptional(v.custom(() => true))
30
30
  });
31
31
  const ComponentManifestMap = v.object({
32
32
  v: v.number(),
@@ -125,6 +125,54 @@ function dedent(templ, ...values) {
125
125
  return string;
126
126
  }
127
127
 
128
+ //#endregion
129
+ //#region src/utils/parse-react-docgen.ts
130
+ function serializeTsType(tsType) {
131
+ if (!tsType) return void 0;
132
+ if ("raw" in tsType && typeof tsType.raw === "string" && tsType.raw.trim().length > 0) return tsType.raw;
133
+ if (!tsType.name) return void 0;
134
+ if ("elements" in tsType) {
135
+ const serializeElements = () => (tsType.elements ?? []).map((el) => serializeTsType(el) ?? "unknown");
136
+ switch (tsType.name) {
137
+ case "union": return serializeElements().join(" | ");
138
+ case "intersection": return serializeElements().join(" & ");
139
+ case "Array": return `${serializeTsType((tsType.elements ?? [])[0]) ?? "unknown"}[]`;
140
+ case "tuple": return `[${serializeElements().join(", ")}]`;
141
+ }
142
+ }
143
+ if ("value" in tsType && tsType.name === "literal") return tsType.value;
144
+ if ("signature" in tsType && tsType.name === "signature") {
145
+ if (tsType.type === "function") {
146
+ const args = (tsType.signature?.arguments ?? []).map((a) => {
147
+ const argType = serializeTsType(a.type) ?? "any";
148
+ return `${a.name}: ${argType}`;
149
+ });
150
+ const ret = serializeTsType(tsType.signature?.return) ?? "void";
151
+ return `(${args.join(", ")}) => ${ret}`;
152
+ }
153
+ if (tsType.type === "object") return `{ ${(tsType.signature?.properties ?? []).map((p) => {
154
+ const req = Boolean(p.value?.required);
155
+ const propType = serializeTsType(p.value) ?? "any";
156
+ return `${p.key}${req ? "" : "?"}: ${propType}`;
157
+ }).join("; ")} }`;
158
+ return "unknown";
159
+ }
160
+ if ("elements" in tsType) {
161
+ const inner = (tsType.elements ?? []).map((el) => serializeTsType(el) ?? "unknown");
162
+ if (inner.length > 0) return `${tsType.name}<${inner.join(", ")}>`;
163
+ }
164
+ return tsType.name;
165
+ }
166
+ const parseReactDocgen = (reactDocgen) => {
167
+ const props = reactDocgen?.props ?? {};
168
+ return { props: Object.fromEntries(Object.entries(props).map(([propName, prop]) => [propName, {
169
+ description: prop.description,
170
+ type: serializeTsType(prop.tsType ?? prop.type),
171
+ defaultValue: prop.defaultValue?.value,
172
+ required: prop.required
173
+ }])) };
174
+ };
175
+
128
176
  //#endregion
129
177
  //#region src/utils/format-manifest.ts
130
178
  function formatComponentManifest(componentManifest) {
@@ -150,6 +198,25 @@ function formatComponentManifest(componentManifest) {
150
198
  </example>`);
151
199
  parts.push(exampleParts.join("\n"));
152
200
  }
201
+ if (componentManifest.reactDocgen) {
202
+ const parsedDocgen = parseReactDocgen(componentManifest.reactDocgen);
203
+ const propEntries = Object.entries(parsedDocgen.props);
204
+ if (propEntries.length > 0) {
205
+ parts.push("<props>");
206
+ for (const [propName, propInfo] of propEntries) {
207
+ parts.push(dedent`<prop>
208
+ <prop_name>${propName}</prop_name>`);
209
+ if (propInfo.description !== void 0) parts.push(dedent`<prop_description>
210
+ ${propInfo.description}
211
+ </prop_description>`);
212
+ if (propInfo.type !== void 0) parts.push(dedent`<prop_type>${propInfo.type}</prop_type>`);
213
+ if (propInfo.required !== void 0) parts.push(dedent`<prop_required>${propInfo.required}</prop_required>`);
214
+ if (propInfo.defaultValue !== void 0) parts.push(dedent`<prop_default>${propInfo.defaultValue}</prop_default>`);
215
+ parts.push("</prop>");
216
+ }
217
+ parts.push("</props>");
218
+ }
219
+ }
153
220
  parts.push("</component>");
154
221
  return parts.join("\n");
155
222
  }
@@ -181,9 +248,15 @@ async function addListAllComponentsTool(server) {
181
248
  description: "List all available UI components from the component library"
182
249
  }, async () => {
183
250
  try {
251
+ const manifest = await getManifest(server.ctx.custom?.source, server.ctx.custom?.manifestProvider);
252
+ const componentList = formatComponentManifestMapToList(manifest);
253
+ await server.ctx.custom?.onListAllComponents?.({
254
+ context: server.ctx.custom,
255
+ manifest
256
+ });
184
257
  return { content: [{
185
258
  type: "text",
186
- text: formatComponentManifestMapToList(await getManifest(server.ctx.custom?.source, server.ctx.custom?.manifestProvider))
259
+ text: componentList
187
260
  }] };
188
261
  } catch (error) {
189
262
  return errorToMCPContent(error);
@@ -206,12 +279,14 @@ async function addGetComponentDocumentationTool(server) {
206
279
  const manifest = await getManifest(server.ctx.custom?.source, server.ctx.custom?.manifestProvider);
207
280
  const content = [];
208
281
  const notFoundIds = [];
282
+ const foundComponents = [];
209
283
  for (const componentId of input.componentIds) {
210
284
  const component = manifest.components[componentId];
211
285
  if (!component) {
212
286
  notFoundIds.push(componentId);
213
287
  continue;
214
288
  }
289
+ foundComponents.push(component);
215
290
  content.push({
216
291
  type: "text",
217
292
  text: formatComponentManifest(component)
@@ -226,6 +301,12 @@ async function addGetComponentDocumentationTool(server) {
226
301
  text: `${severity}: ${componentWord} not found: ${notFoundIds.join(", ")}`
227
302
  });
228
303
  }
304
+ await server.ctx.custom?.onGetComponentDocumentation?.({
305
+ context: server.ctx.custom,
306
+ input: { componentIds: input.componentIds },
307
+ foundComponents,
308
+ notFoundIds
309
+ });
229
310
  return {
230
311
  content,
231
312
  ...allNotFound && { isError: true }
@@ -248,15 +329,16 @@ const createStorybookMcpHandler = async (options = {}) => {
248
329
  adapter,
249
330
  capabilities: { tools: { listChanged: true } }
250
331
  }).withContext();
332
+ if (options.onSessionInitialize) server.on("initialize", options.onSessionInitialize);
251
333
  await addListAllComponentsTool(server);
252
334
  await addGetComponentDocumentationTool(server);
253
335
  const transport = new HttpTransport(server, { path: null });
254
336
  return (async (req, context) => {
255
- const source = context?.source ?? options.source;
256
- const manifestProvider = context?.manifestProvider ?? options.manifestProvider;
257
337
  return await transport.respond(req, {
258
- source,
259
- manifestProvider
338
+ source: context?.source ?? options.source,
339
+ manifestProvider: context?.manifestProvider ?? options.manifestProvider,
340
+ onListAllComponents: context?.onListAllComponents ?? options.onListAllComponents,
341
+ onGetComponentDocumentation: context?.onGetComponentDocumentation ?? options.onGetComponentDocumentation
260
342
  });
261
343
  });
262
344
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/mcp",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
5
5
  "keywords": [
6
6
  "storybook",
@@ -36,10 +36,11 @@
36
36
  "@tsconfig/node24": "^24.0.1",
37
37
  "@types/node": "20.19.0",
38
38
  "@vitest/coverage-v8": "3.2.4",
39
+ "react-docgen": "^8.0.2",
39
40
  "srvx": "^0.8.16",
40
41
  "tsdown": "^0.15.9",
41
42
  "typescript": "^5.9.3",
42
- "vitest": "^3.2.4"
43
+ "vitest": "3.2.4"
43
44
  },
44
45
  "scripts": {
45
46
  "build": "tsdown",