@storybook/mcp 0.2.0 → 0.2.2

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
@@ -38,9 +38,10 @@ type StorybookContext = {
38
38
  onListAllDocumentation?: (params: {
39
39
  context: StorybookContext;
40
40
  manifests: AllManifests;
41
+ resultText: string;
41
42
  }) => void | Promise<void>;
42
43
  /**
43
- * Optional handler called when get-component-documentation tool is invoked.
44
+ * Optional handler called when get-documentation tool is invoked.
44
45
  * Receives the context, input parameters, and the found component (if any).
45
46
  */
46
47
  onGetDocumentation?: (params: {
@@ -48,8 +49,13 @@ type StorybookContext = {
48
49
  input: {
49
50
  id: string;
50
51
  };
51
- foundDocumentation?: ComponentManifest | Doc;
52
- }) => void | Promise<void>;
52
+ } & ({
53
+ foundDocumentation: ComponentManifest | Doc;
54
+ resultText: string;
55
+ } | {
56
+ foundDocumentation?: never;
57
+ resultText?: never;
58
+ })) => void | Promise<void>;
53
59
  };
54
60
  /**
55
61
  * A docs entry represents MDX documentation that can be attached to a component
@@ -74,7 +80,9 @@ declare const ComponentManifest: v.ObjectSchema<{
74
80
  readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
75
81
  readonly import: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
76
82
  readonly stories: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
83
+ readonly id: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
77
84
  readonly snippet: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
85
+ readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
78
86
  readonly name: v.StringSchema<undefined>;
79
87
  readonly description: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
80
88
  readonly jsDocTags: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
@@ -113,7 +121,9 @@ declare const ComponentManifestMap: v.ObjectSchema<{
113
121
  readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
114
122
  readonly import: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
115
123
  readonly stories: v.OptionalSchema<v.ArraySchema<v.ObjectSchema<{
124
+ readonly id: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
116
125
  readonly snippet: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
126
+ readonly summary: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
117
127
  readonly name: v.StringSchema<undefined>;
118
128
  readonly description: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
119
129
  readonly jsDocTags: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
@@ -178,6 +188,10 @@ declare function addListAllDocumentationTool(server: McpServer<any, StorybookCon
178
188
  declare const GET_TOOL_NAME = "get-documentation";
179
189
  declare function addGetDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']): Promise<void>;
180
190
  //#endregion
191
+ //#region src/tools/get-documentation-for-story.d.ts
192
+ declare const GET_STORY_TOOL_NAME = "get-documentation-for-story";
193
+ declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']): Promise<void>;
194
+ //#endregion
181
195
  //#region src/utils/get-manifest.d.ts
182
196
  /**
183
197
  * The paths to the manifest files relative to the Storybook build
@@ -223,4 +237,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
223
237
  type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
224
238
  declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
225
239
  //#endregion
226
- export { COMPONENT_MANIFEST_PATH, type ComponentManifest, type ComponentManifestMap, DOCS_MANIFEST_PATH, GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, StorybookMcpHandlerOptions, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
240
+ export { COMPONENT_MANIFEST_PATH, type ComponentManifest, type ComponentManifestMap, DOCS_MANIFEST_PATH, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, type StorybookContext, StorybookMcpHandlerOptions, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, 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.2.0";
8
+ var version = "0.2.2";
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
- snippet: v.optional(v.string())
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
@@ -172,6 +174,11 @@ async function defaultManifestProvider(request, path) {
172
174
  * Maximum length for a summary before truncation.
173
175
  */
174
176
  const MAX_SUMMARY_LENGTH = 90;
177
+ /**
178
+ * Maximum number of stories to show in full detail in component manifests.
179
+ * Remaining stories will be shown as names only.
180
+ */
181
+ const MAX_STORIES_TO_SHOW = 3;
175
182
 
176
183
  //#endregion
177
184
  //#region src/utils/manifest-formatter/extract-docs-summary.ts
@@ -241,7 +248,7 @@ function dedent(templ, ...values) {
241
248
  if (typeof value === "string" && value.includes("\n")) indentedValue = String(value).split("\n").map((str, i$1) => {
242
249
  return i$1 === 0 ? str : `${endentation}${str}`;
243
250
  }).join("\n");
244
- string += `${indentedValue}${strings[i + 1]}`;
251
+ string += `${String(indentedValue)}${strings[i + 1]}`;
245
252
  });
246
253
  return string;
247
254
  }
@@ -309,20 +316,32 @@ const xmlFormatter = {
309
316
  if (componentManifest.description) parts.push(dedent`<description>
310
317
  ${componentManifest.description}
311
318
  </description>`);
312
- if (componentManifest.stories && componentManifest.stories.length > 0) for (const story of componentManifest.stories) {
313
- if (!story.snippet) continue;
314
- const storyParts = [];
315
- storyParts.push(dedent`<story>
319
+ if (componentManifest.stories && componentManifest.stories.length > 0) {
320
+ const storiesWithSnippets = componentManifest.stories.filter((s) => s.snippet);
321
+ const storiesToShow = storiesWithSnippets.slice(0, MAX_STORIES_TO_SHOW);
322
+ const remainingStories = storiesWithSnippets.slice(MAX_STORIES_TO_SHOW);
323
+ for (const story of storiesToShow) {
324
+ const storyParts = [];
325
+ storyParts.push(dedent`<story>
316
326
  <story_name>${story.name.replace(/([A-Z])/g, " $1").trim()}</story_name>`);
317
- if (story.description) storyParts.push(dedent`<story_description>
327
+ if (story.description) storyParts.push(dedent`<story_description>
318
328
  ${story.description}
319
329
  </story_description>`);
320
- storyParts.push("<story_code>");
321
- if (componentManifest.import) storyParts.push(`${componentManifest.import}\n`);
322
- storyParts.push(dedent`${story.snippet}
330
+ storyParts.push("<story_code>");
331
+ if (componentManifest.import) storyParts.push(`${componentManifest.import}\n`);
332
+ storyParts.push(dedent`${story.snippet}
323
333
  </story_code>
324
334
  </story>`);
325
- parts.push(storyParts.join("\n"));
335
+ parts.push(storyParts.join("\n"));
336
+ }
337
+ if (remainingStories.length > 0) {
338
+ parts.push("<other_stories>");
339
+ for (const story of remainingStories) {
340
+ const storyName = story.name.replace(/([A-Z])/g, " $1").trim();
341
+ parts.push(dedent`<story_name>${storyName}</story_name>`);
342
+ }
343
+ parts.push("</other_stories>");
344
+ }
326
345
  }
327
346
  if (componentManifest.reactDocgen) {
328
347
  const parsedDocgen = parseReactDocgen(componentManifest.reactDocgen);
@@ -382,12 +401,57 @@ const xmlFormatter = {
382
401
  }
383
402
  parts.push("</docs>");
384
403
  return parts.join("\n");
404
+ },
405
+ formatStoryDocumentation(componentManifest, storyName) {
406
+ const story = componentManifest.stories?.find((s) => s.name === storyName);
407
+ if (!story || !story.snippet) return "";
408
+ const parts = [];
409
+ const displayStoryName = story.name.replace(/([A-Z])/g, " $1").trim();
410
+ parts.push(dedent`<story_documentation>
411
+ <component_name>${componentManifest.name}</component_name>
412
+ <story_name>${displayStoryName}</story_name>`);
413
+ if (story.description) parts.push(dedent`<story_description>
414
+ ${story.description}
415
+ </story_description>`);
416
+ parts.push("<story_code>");
417
+ if (componentManifest.import) parts.push(`${componentManifest.import}\n`);
418
+ parts.push(dedent`${story.snippet}
419
+ </story_code>
420
+ </story_documentation>`);
421
+ return parts.join("\n");
385
422
  }
386
423
  };
387
424
 
388
425
  //#endregion
389
426
  //#region src/utils/manifest-formatter/markdown.ts
390
427
  /**
428
+ * Extracts a summary from an object with optional summary and description fields.
429
+ * Prefers summary if available, otherwise truncates description to maxLength.
430
+ */
431
+ function extractSummary(item, maxLength = MAX_SUMMARY_LENGTH) {
432
+ if (item.summary) return item.summary;
433
+ if (item.description) return item.description.length > maxLength ? `${item.description.slice(0, maxLength)}...` : item.description;
434
+ }
435
+ /**
436
+ * Formats a story's content (description + code snippet) into markdown.
437
+ * Reusable helper for both formatComponentManifest and formatStoryDocumentation.
438
+ */
439
+ function formatStoryContent(story, importStatement) {
440
+ const parts = [];
441
+ if (story.description) {
442
+ parts.push(story.description);
443
+ parts.push("");
444
+ }
445
+ parts.push("```");
446
+ if (importStatement) {
447
+ parts.push(importStatement);
448
+ parts.push("");
449
+ }
450
+ parts.push(story.snippet ?? "");
451
+ parts.push("```");
452
+ return parts;
453
+ }
454
+ /**
391
455
  * Markdown formatter for component manifests.
392
456
  * Formats component data into token-efficient markdown with headers, lists, and tables.
393
457
  * Uses adaptive formatting based on prop complexity to optimize token usage.
@@ -406,22 +470,24 @@ const markdownFormatter = {
406
470
  if (componentManifest.stories && componentManifest.stories.length > 0) {
407
471
  parts.push("## Stories");
408
472
  parts.push("");
409
- for (const story of componentManifest.stories) {
410
- if (!story.snippet) continue;
411
- const storyName = story.name.replace(/([A-Z])/g, " $1").trim();
412
- parts.push(`### ${storyName}`);
413
- if (story.description) {
414
- parts.push("");
415
- parts.push(story.description);
416
- }
473
+ const storiesWithSnippets = componentManifest.stories.filter((s) => s.snippet);
474
+ const hasProps = componentManifest.reactDocgen && Object.keys(componentManifest.reactDocgen.props ?? {}).length > 0;
475
+ const storiesToShow = hasProps ? storiesWithSnippets.slice(0, MAX_STORIES_TO_SHOW) : storiesWithSnippets;
476
+ const remainingStories = hasProps ? storiesWithSnippets.slice(MAX_STORIES_TO_SHOW) : [];
477
+ for (const story of storiesToShow) {
478
+ parts.push(`### ${story.name}`);
417
479
  parts.push("");
418
- parts.push("```");
419
- if (componentManifest.import) {
420
- parts.push(componentManifest.import);
421
- parts.push("");
480
+ parts.push(...formatStoryContent(story, componentManifest.import));
481
+ parts.push("");
482
+ }
483
+ if (remainingStories.length > 0) {
484
+ if (storiesToShow.length > 0) parts.push("### Other Stories");
485
+ parts.push("");
486
+ for (const story of remainingStories) {
487
+ const summary = extractSummary(story);
488
+ const summaryPart = summary ? `: ${summary}` : "";
489
+ parts.push(`- ${story.name}${summaryPart}`);
422
490
  }
423
- parts.push(story.snippet);
424
- parts.push("```");
425
491
  parts.push("");
426
492
  }
427
493
  }
@@ -454,6 +520,19 @@ const markdownFormatter = {
454
520
  parts.push("");
455
521
  }
456
522
  }
523
+ if (componentManifest.docs && Object.keys(componentManifest.docs).length > 0) {
524
+ const docsWithContent = Object.values(componentManifest.docs).filter((doc) => doc.content.trim().length > 0);
525
+ if (docsWithContent.length > 0) {
526
+ parts.push("## Docs");
527
+ parts.push("");
528
+ for (const doc of docsWithContent) {
529
+ parts.push(`### ${doc.name}`);
530
+ parts.push("");
531
+ parts.push(doc.content);
532
+ parts.push("");
533
+ }
534
+ }
535
+ }
457
536
  return parts.join("\n").trim();
458
537
  },
459
538
  formatDocsManifest(doc) {
@@ -466,7 +545,7 @@ const markdownFormatter = {
466
545
  parts.push("# Components");
467
546
  parts.push("");
468
547
  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);
548
+ const summary = extractSummary(component);
470
549
  if (summary) parts.push(`- ${component.name} (${component.id}): ${summary}`);
471
550
  else parts.push(`- ${component.name} (${component.id})`);
472
551
  }
@@ -479,6 +558,15 @@ const markdownFormatter = {
479
558
  parts.push(`- ${doc.title} (${doc.id})${summary ? `: ${summary}` : ""}`);
480
559
  }
481
560
  return parts.join("\n").trim();
561
+ },
562
+ formatStoryDocumentation(componentManifest, storyName) {
563
+ const story = componentManifest.stories?.find((s) => s.name === storyName);
564
+ if (!story || !story.snippet) return "";
565
+ const parts = [];
566
+ parts.push(`# ${componentManifest.name} - ${story.name}`);
567
+ parts.push("");
568
+ parts.push(...formatStoryContent(story, componentManifest.import));
569
+ return parts.join("\n").trim();
482
570
  }
483
571
  };
484
572
 
@@ -489,12 +577,25 @@ const formatters = {
489
577
  markdown: markdownFormatter
490
578
  };
491
579
  /**
492
- * Format a single component manifest.
580
+ * Format a single component manifest in summary mode (optimized for token usage).
581
+ * @param componentManifest - The component manifest to format
582
+ * @param format - The desired output format (defaults to 'markdown')
583
+ * @returns Formatted string representation of the component
493
584
  */
494
585
  function formatComponentManifest(componentManifest, format = "markdown") {
495
586
  return formatters[format].formatComponentManifest(componentManifest);
496
587
  }
497
588
  /**
589
+ * Format a single story's documentation.
590
+ * @param componentManifest - The component manifest containing the story
591
+ * @param storyName - The name of the story to format
592
+ * @param format - The desired output format (defaults to 'markdown')
593
+ * @returns Formatted string representation of the story
594
+ */
595
+ function formatStoryDocumentation(componentManifest, storyName, format = "markdown") {
596
+ return formatters[format].formatStoryDocumentation(componentManifest, storyName);
597
+ }
598
+ /**
498
599
  * Format a single docs manifest.
499
600
  */
500
601
  function formatDocsManifest(doc, format = "markdown") {
@@ -522,7 +623,8 @@ async function addListAllDocumentationTool(server, enabled) {
522
623
  const lists = formatManifestsToLists(manifests, server.ctx.custom?.format ?? "markdown");
523
624
  await server.ctx.custom?.onListAllDocumentation?.({
524
625
  context: server.ctx.custom,
525
- manifests
626
+ manifests,
627
+ resultText: lists
526
628
  });
527
629
  return { content: [{
528
630
  type: "text",
@@ -534,6 +636,51 @@ async function addListAllDocumentationTool(server, enabled) {
534
636
  });
535
637
  }
536
638
 
639
+ //#endregion
640
+ //#region src/tools/get-documentation-for-story.ts
641
+ const GET_STORY_TOOL_NAME = "get-documentation-for-story";
642
+ const GetStoryDocumentationInput = v.object({
643
+ componentId: v.string(),
644
+ storyName: v.string()
645
+ });
646
+ async function addGetStoryDocumentationTool(server, enabled) {
647
+ server.tool({
648
+ name: GET_STORY_TOOL_NAME,
649
+ title: "Get Documentation for Story",
650
+ 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.",
651
+ schema: GetStoryDocumentationInput,
652
+ enabled
653
+ }, async (input) => {
654
+ try {
655
+ const component = (await getManifests(server.ctx.custom?.request, server.ctx.custom?.manifestProvider)).componentManifest?.components[input.componentId];
656
+ if (!component) return {
657
+ content: [{
658
+ type: "text",
659
+ text: `Component not found: "${input.componentId}". Use the list-all-documentation tool to see available components.`
660
+ }],
661
+ isError: true
662
+ };
663
+ if (!component.stories?.find((s) => s.name === input.storyName)) {
664
+ const availableStories = component.stories?.map((s) => s.name).join(", ") ?? "none";
665
+ return {
666
+ content: [{
667
+ type: "text",
668
+ text: `Story "${input.storyName}" not found for component "${input.componentId}". Available stories: ${availableStories}`
669
+ }],
670
+ isError: true
671
+ };
672
+ }
673
+ const format = server.ctx.custom?.format ?? "markdown";
674
+ return { content: [{
675
+ type: "text",
676
+ text: formatStoryDocumentation(component, input.storyName, format)
677
+ }] };
678
+ } catch (error) {
679
+ return errorToMCPContent(error);
680
+ }
681
+ });
682
+ }
683
+
537
684
  //#endregion
538
685
  //#region src/tools/get-documentation.ts
539
686
  const GET_TOOL_NAME = "get-documentation";
@@ -542,7 +689,11 @@ async function addGetDocumentationTool(server, enabled) {
542
689
  server.tool({
543
690
  name: GET_TOOL_NAME,
544
691
  title: "Get Documentation",
545
- description: "Get detailed documentation for a specific UI component or docs entry",
692
+ description: `Get documentation for a UI component or docs entry.
693
+
694
+ Returns the first ${MAX_STORIES_TO_SHOW} stories with code snippets showing how props are used, plus TypeScript prop definitions. Call this before using a component to avoid hallucinating prop names, types, or valid combinations. Stories reveal real prop usage patterns, interactions, and edge cases that type definitions alone don't show. If the example stories don't show the prop you need, use the ${GET_STORY_TOOL_NAME} tool to fetch the story documentation for the specific story variant you need.
695
+
696
+ Example: id="button" returns Primary, Secondary, Large stories with code like <Button variant="primary" size="large"> showing actual prop combinations.`,
546
697
  schema: GetDocumentationInput,
547
698
  enabled
548
699
  }, async (input) => {
@@ -564,15 +715,17 @@ async function addGetDocumentationTool(server, enabled) {
564
715
  };
565
716
  }
566
717
  const documentation = component ?? docsEntry;
718
+ const format = server.ctx.custom?.format ?? "markdown";
719
+ const text = component ? formatComponentManifest(documentation, format) : formatDocsManifest(documentation, format);
567
720
  await server.ctx.custom?.onGetDocumentation?.({
568
721
  context: server.ctx.custom,
569
722
  input,
570
- foundDocumentation: documentation
723
+ foundDocumentation: documentation,
724
+ resultText: text
571
725
  });
572
- const format = server.ctx.custom?.format ?? "markdown";
573
726
  return { content: [{
574
727
  type: "text",
575
- text: component ? formatComponentManifest(documentation, format) : formatDocsManifest(documentation, format)
728
+ text
576
729
  }] };
577
730
  } catch (error) {
578
731
  return errorToMCPContent(error);
@@ -594,6 +747,7 @@ const createStorybookMcpHandler = async (options = {}) => {
594
747
  }).withContext();
595
748
  if (options.onSessionInitialize) server.on("initialize", options.onSessionInitialize);
596
749
  await addListAllDocumentationTool(server);
750
+ await addGetStoryDocumentationTool(server);
597
751
  await addGetDocumentationTool(server);
598
752
  const transport = new HttpTransport(server, { path: null });
599
753
  return (async (req, context) => {
@@ -608,4 +762,4 @@ const createStorybookMcpHandler = async (options = {}) => {
608
762
  };
609
763
 
610
764
  //#endregion
611
- export { COMPONENT_MANIFEST_PATH, DOCS_MANIFEST_PATH, GET_TOOL_NAME, LIST_TOOL_NAME, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
765
+ export { COMPONENT_MANIFEST_PATH, DOCS_MANIFEST_PATH, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler };
package/package.json CHANGED
@@ -1,18 +1,21 @@
1
1
  {
2
2
  "name": "@storybook/mcp",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
5
5
  "keywords": [
6
- "storybook",
7
- "mcp"
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
- "license": "MIT",
15
- "author": "Storybook Core Team",
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",