@storybook/mcp 0.4.0 → 0.5.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 CHANGED
@@ -1,18 +1,13 @@
1
1
  import { McpServer } from "tmcp";
2
2
  import * as v from "valibot";
3
- import { Documentation } from "react-docgen";
4
- import { ComponentDoc } from "react-docgen-typescript";
5
3
 
6
4
  //#region src/types.d.ts
7
5
  /**
8
6
  * Represents a single Storybook source (local or remote).
9
7
  */
10
8
  type Source = {
11
- /** Unique identifier for this source (e.g., 'local', 'tetra') */
12
- id: string;
13
- /** Human-readable title (e.g., 'Local', 'Tetra Design System') */
14
- title: string;
15
- /** Remote URL, undefined for local source */
9
+ /** Unique identifier for this source (e.g., 'local', 'tetra') */id: string; /** Human-readable title (e.g., 'Local', 'Tetra Design System') */
10
+ title: string; /** Remote URL, undefined for local source */
16
11
  url?: string;
17
12
  };
18
13
  /**
@@ -21,8 +16,7 @@ type Source = {
21
16
  type SourceManifests = {
22
17
  source: Source;
23
18
  componentManifest: ComponentManifestMap;
24
- docsManifest?: DocsManifestMap;
25
- /** Error message if fetching this source failed */
19
+ docsManifest?: DocsManifestMap; /** Error message if fetching this source failed */
26
20
  error?: string;
27
21
  };
28
22
  /**
@@ -56,8 +50,7 @@ type StorybookContext = {
56
50
  onListAllDocumentation?: (params: {
57
51
  context: StorybookContext;
58
52
  manifests: AllManifests;
59
- resultText: string;
60
- /** Present in multi-source mode — all source manifests including errors */
53
+ resultText: string; /** Present in multi-source mode — all source manifests including errors */
61
54
  sources?: SourceManifests[];
62
55
  }) => void | Promise<void>;
63
56
  /**
@@ -95,13 +88,6 @@ declare const Doc: v.ObjectSchema<{
95
88
  }, undefined>, undefined>;
96
89
  }, undefined>;
97
90
  type Doc = v.InferOutput<typeof Doc>;
98
- /**
99
- * Component documentation from react-docgen-typescript, extended with export name.
100
- * Matches the shape produced by Storybook's manifest generator.
101
- */
102
- type ComponentDocWithExportName = ComponentDoc & {
103
- exportName: string;
104
- };
105
91
  declare const ComponentManifest: v.ObjectSchema<{
106
92
  readonly id: v.StringSchema<undefined>;
107
93
  readonly path: v.StringSchema<undefined>;
@@ -119,8 +105,8 @@ declare const ComponentManifest: v.ObjectSchema<{
119
105
  readonly message: v.StringSchema<undefined>;
120
106
  }, undefined>, undefined>;
121
107
  }, undefined>, undefined>, undefined>;
122
- readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
123
- readonly reactDocgenTypescript: v.OptionalSchema<v.CustomSchema<ComponentDocWithExportName, undefined>, undefined>;
108
+ readonly reactDocgen: v.OptionalSchema<v.AnySchema, undefined>;
109
+ readonly reactDocgenTypescript: v.OptionalSchema<v.AnySchema, undefined>;
124
110
  readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
125
111
  readonly id: v.StringSchema<undefined>;
126
112
  readonly name: v.StringSchema<undefined>;
@@ -161,8 +147,8 @@ declare const ComponentManifestMap: v.ObjectSchema<{
161
147
  readonly message: v.StringSchema<undefined>;
162
148
  }, undefined>, undefined>;
163
149
  }, undefined>, undefined>, undefined>;
164
- readonly reactDocgen: v.OptionalSchema<v.CustomSchema<Documentation, undefined>, undefined>;
165
- readonly reactDocgenTypescript: v.OptionalSchema<v.CustomSchema<ComponentDocWithExportName, undefined>, undefined>;
150
+ readonly reactDocgen: v.OptionalSchema<v.AnySchema, undefined>;
151
+ readonly reactDocgenTypescript: v.OptionalSchema<v.AnySchema, undefined>;
166
152
  readonly docs: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
167
153
  readonly id: v.StringSchema<undefined>;
168
154
  readonly name: v.StringSchema<undefined>;
@@ -222,7 +208,9 @@ declare function addGetDocumentationTool(server: McpServer<any, StorybookContext
222
208
  //#endregion
223
209
  //#region src/tools/get-documentation-for-story.d.ts
224
210
  declare const GET_STORY_TOOL_NAME = "get-documentation-for-story";
225
- declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']): Promise<void>;
211
+ declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled'], options?: {
212
+ multiSource?: boolean;
213
+ }): Promise<void>;
226
214
  //#endregion
227
215
  //#region src/utils/get-manifest.d.ts
228
216
  /**
@@ -281,4 +269,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
281
269
  type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
282
270
  declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
283
271
  //#endregion
284
- export { COMPONENT_MANIFEST_PATH, type ComponentManifest, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, type Source, type SourceManifests, type StorybookContext, StorybookMcpHandlerOptions, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
272
+ export { COMPONENT_MANIFEST_PATH, type ComponentManifest, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, type Source, type SourceManifests, type StorybookContext, StorybookMcpHandlerOptions, addGetDocumentationTool, addGetStoryDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import * as v from "valibot";
5
5
 
6
6
  //#region package.json
7
7
  var name = "@storybook/mcp";
8
- var version = "0.4.0";
8
+ var version = "0.5.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
@@ -47,8 +47,8 @@ const ComponentManifest = v.object({
47
47
  summary: v.optional(v.string()),
48
48
  import: v.optional(v.string()),
49
49
  stories: v.optional(v.array(Story)),
50
- reactDocgen: v.optional(v.custom(() => true)),
51
- reactDocgenTypescript: v.optional(v.custom(() => true)),
50
+ reactDocgen: v.optional(v.any()),
51
+ reactDocgenTypescript: v.optional(v.any()),
52
52
  docs: v.optional(v.record(v.string(), Doc))
53
53
  });
54
54
  const ComponentManifestMap = v.object({
@@ -63,6 +63,11 @@ const DocsManifestMap = v.object({
63
63
  v: v.number(),
64
64
  docs: v.record(v.string(), Doc)
65
65
  });
66
+ /**
67
+ * Shared Valibot field for the storybookId input, used in multi-source mode.
68
+ * Reused across tools that support source selection.
69
+ */
70
+ const StorybookIdField = { storybookId: v.pipe(v.string(), v.description("The Storybook source ID (e.g., \"local\", \"tetra\"). Required when multiple Storybooks are composed. See list-all-documentation for available sources.")) };
66
71
 
67
72
  //#endregion
68
73
  //#region src/utils/get-manifest.ts
@@ -107,11 +112,11 @@ const errorToMCPContent = (error) => {
107
112
  /**
108
113
  * Parses a JSON string and validates it against a Valibot schema
109
114
  */
110
- function parseManifest({ jsonString, schema, name: name$1, url }) {
115
+ function parseManifest({ jsonString, schema, name, url }) {
111
116
  try {
112
117
  return v.parse(v.pipe(v.string(), v.parseJson(), schema), jsonString);
113
118
  } catch (error) {
114
- throw new ManifestGetError(`Failed to parse ${name$1} manifest:
119
+ throw new ManifestGetError(`Failed to parse ${name} manifest:
115
120
  ${error instanceof v.ValiError ? error.issues.map((i) => i.message).join("\n") : String(error)}`, url);
116
121
  }
117
122
  }
@@ -301,8 +306,8 @@ function dedent(templ, ...values) {
301
306
  const endentations = string.match(/(?:^|\n)( *)$/);
302
307
  const endentation = endentations ? endentations[1] : "";
303
308
  let indentedValue = value;
304
- if (typeof value === "string" && value.includes("\n")) indentedValue = String(value).split("\n").map((str, i$1) => {
305
- return i$1 === 0 ? str : `${endentation}${str}`;
309
+ if (typeof value === "string" && value.includes("\n")) indentedValue = String(value).split("\n").map((str, i) => {
310
+ return i === 0 ? str : `${endentation}${str}`;
306
311
  }).join("\n");
307
312
  string += `${String(indentedValue)}${strings[i + 1]}`;
308
313
  });
@@ -363,6 +368,9 @@ function formatDocLine(doc) {
363
368
  const summary = doc.summary ?? extractDocsSummary(doc.content);
364
369
  return `- ${doc.title} (${doc.id})${summary ? `: ${summary}` : ""}`;
365
370
  }
371
+ function formatStorySubLine(story) {
372
+ return ` - ${story.name}` + (story.id ? ` (${story.id})` : "");
373
+ }
366
374
  /**
367
375
  * Extracts a summary from an object with optional summary and description fields.
368
376
  * Prefers summary if available, otherwise truncates description to maxLength.
@@ -421,6 +429,10 @@ function formatComponentManifest(componentManifest) {
421
429
  for (const story of storiesToShow) {
422
430
  parts.push(`### ${story.name}`);
423
431
  parts.push("");
432
+ if (story.id) {
433
+ parts.push(`Story ID: ${story.id}`);
434
+ parts.push("");
435
+ }
424
436
  parts.push(...formatStoryContent(story, componentManifest.import));
425
437
  parts.push("");
426
438
  }
@@ -430,7 +442,8 @@ function formatComponentManifest(componentManifest) {
430
442
  for (const story of remainingStories) {
431
443
  const summary = extractSummary(story);
432
444
  const summaryPart = summary ? `: ${summary}` : "";
433
- parts.push(`- ${story.name}${summaryPart}`);
445
+ const storyLabel = story.id ? `${story.name} (${story.id})` : story.name;
446
+ parts.push(`- ${storyLabel}${summaryPart}`);
434
447
  }
435
448
  parts.push("");
436
449
  }
@@ -491,11 +504,14 @@ function formatDocsManifest(doc) {
491
504
  * @param manifest - The component manifest map to format
492
505
  * @returns Formatted string representation of the component list
493
506
  */
494
- function formatManifestsToLists(manifests) {
507
+ function formatManifestsToLists(manifests, options = {}) {
495
508
  const parts = [];
496
509
  parts.push("# Components");
497
510
  parts.push("");
498
- for (const component of Object.values(manifests.componentManifest.components)) parts.push(formatComponentLine(component));
511
+ for (const component of Object.values(manifests.componentManifest.components)) {
512
+ parts.push(formatComponentLine(component));
513
+ if (options.withStoryIds) for (const story of component.stories ?? []) parts.push(formatStorySubLine(story));
514
+ }
499
515
  parts.push("");
500
516
  if (!manifests.docsManifest) return parts.join("\n").trim();
501
517
  parts.push("# Docs");
@@ -503,13 +519,7 @@ function formatManifestsToLists(manifests) {
503
519
  for (const doc of Object.values(manifests.docsManifest.docs)) parts.push(formatDocLine(doc));
504
520
  return parts.join("\n").trim();
505
521
  }
506
- /**
507
- * Format multi-source manifests into grouped lists.
508
- * Each source is displayed with a heading and its components/docs listed underneath.
509
- * @param manifests - The multi-source manifests to format
510
- * @returns Formatted string representation grouped by source
511
- */
512
- function formatMultiSourceManifestsToLists(manifests) {
522
+ function formatMultiSourceManifestsToLists(manifests, options = {}) {
513
523
  const parts = [];
514
524
  for (const { source, componentManifest, docsManifest, error } of manifests) {
515
525
  parts.push(`# ${source.title}`);
@@ -524,7 +534,10 @@ function formatMultiSourceManifestsToLists(manifests) {
524
534
  if (components.length > 0) {
525
535
  parts.push("## Components");
526
536
  parts.push("");
527
- for (const component of components) parts.push(formatComponentLine(component));
537
+ for (const component of components) {
538
+ parts.push(formatComponentLine(component));
539
+ if (options.withStoryIds) for (const story of component.stories ?? []) parts.push(formatStorySubLine(story));
540
+ }
528
541
  parts.push("");
529
542
  }
530
543
  if (docsManifest && Object.keys(docsManifest.docs).length > 0) {
@@ -552,18 +565,21 @@ function formatStoryDocumentation(componentManifest, storyName) {
552
565
  //#endregion
553
566
  //#region src/tools/list-all-documentation.ts
554
567
  const LIST_TOOL_NAME = "list-all-documentation";
568
+ const ListAllDocumentationInput = v.object({ withStoryIds: v.optional(v.pipe(v.boolean(), v.description("When true, includes story sub-bullets under each component with story name and story ID. Use this to discover IDs for downstream story-focused workflows without filesystem lookup.")), false) });
555
569
  async function addListAllDocumentationTool(server, enabled) {
556
570
  server.tool({
557
571
  name: LIST_TOOL_NAME,
558
572
  title: "List All Documentation",
559
573
  description: "List all available UI components and documentation entries from the Storybook",
574
+ schema: ListAllDocumentationInput,
560
575
  enabled
561
- }, async () => {
576
+ }, async (input) => {
562
577
  try {
563
578
  const ctx = server.ctx.custom;
579
+ const withStoryIds = input.withStoryIds ?? false;
564
580
  if (ctx?.sources?.some((s) => s.url)) {
565
581
  const multiSourceManifests = await getMultiSourceManifests(ctx.sources, ctx.request, ctx.manifestProvider);
566
- const lists$1 = formatMultiSourceManifestsToLists(multiSourceManifests);
582
+ const lists = formatMultiSourceManifestsToLists(multiSourceManifests, { withStoryIds });
567
583
  const firstSuccess = multiSourceManifests.find((m) => !m.error);
568
584
  if (firstSuccess) await ctx.onListAllDocumentation?.({
569
585
  context: ctx,
@@ -571,16 +587,16 @@ async function addListAllDocumentationTool(server, enabled) {
571
587
  componentManifest: firstSuccess.componentManifest,
572
588
  docsManifest: firstSuccess.docsManifest
573
589
  },
574
- resultText: lists$1,
590
+ resultText: lists,
575
591
  sources: multiSourceManifests
576
592
  });
577
593
  return { content: [{
578
594
  type: "text",
579
- text: lists$1
595
+ text: lists
580
596
  }] };
581
597
  }
582
598
  const manifests = await getManifests(ctx?.request, ctx?.manifestProvider);
583
- const lists = formatManifestsToLists(manifests);
599
+ const lists = formatManifestsToLists(manifests, { withStoryIds });
584
600
  await ctx?.onListAllDocumentation?.({
585
601
  context: ctx,
586
602
  manifests,
@@ -599,40 +615,63 @@ async function addListAllDocumentationTool(server, enabled) {
599
615
  //#endregion
600
616
  //#region src/tools/get-documentation-for-story.ts
601
617
  const GET_STORY_TOOL_NAME = "get-documentation-for-story";
602
- const GetStoryDocumentationInput = v.object({
618
+ const BaseInput$1 = {
603
619
  componentId: v.string(),
604
620
  storyName: v.string()
605
- });
606
- async function addGetStoryDocumentationTool(server, enabled) {
621
+ };
622
+ async function addGetStoryDocumentationTool(server, enabled, options) {
623
+ const schema = options?.multiSource ? v.object({
624
+ ...BaseInput$1,
625
+ ...StorybookIdField
626
+ }) : v.object(BaseInput$1);
607
627
  server.tool({
608
628
  name: GET_STORY_TOOL_NAME,
609
629
  title: "Get Documentation for Story",
610
630
  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.",
611
- schema: GetStoryDocumentationInput,
631
+ schema,
612
632
  enabled
613
633
  }, async (input) => {
614
634
  try {
615
- const component = (await getManifests(server.ctx.custom?.request, server.ctx.custom?.manifestProvider)).componentManifest?.components[input.componentId];
616
- if (!component) return {
617
- content: [{
618
- type: "text",
619
- text: `Component not found: "${input.componentId}". Use the list-all-documentation tool to see available components.`
620
- }],
621
- isError: true
622
- };
623
- if (!component.stories?.find((s) => s.name === input.storyName)) {
624
- const availableStories = component.stories?.map((s) => s.name).join(", ") ?? "none";
625
- return {
635
+ const ctx = server.ctx.custom;
636
+ const { componentId, storyName, storybookId } = input;
637
+ const sources = ctx?.sources;
638
+ const isMultiSource = sources && sources.some((s) => s.url);
639
+ let source;
640
+ if (isMultiSource) {
641
+ if (!storybookId) return {
626
642
  content: [{
627
643
  type: "text",
628
- text: `Story "${input.storyName}" not found for component "${input.componentId}". Available stories: ${availableStories}`
644
+ text: `storybookId is required. Available sources: ${sources.map((s) => s.id).join(", ")}. Use the ${LIST_TOOL_NAME} tool to see available sources.`
645
+ }],
646
+ isError: true
647
+ };
648
+ source = sources.find((s) => s.id === storybookId);
649
+ if (!source) return {
650
+ content: [{
651
+ type: "text",
652
+ text: `Storybook source not found: "${storybookId}". Available sources: ${sources.map((s) => s.id).join(", ")}. Use the ${LIST_TOOL_NAME} tool to see available sources.`
629
653
  }],
630
654
  isError: true
631
655
  };
632
656
  }
657
+ const component = (await getManifests(ctx?.request, ctx?.manifestProvider, source)).componentManifest?.components[componentId];
658
+ if (!component) return {
659
+ content: [{
660
+ type: "text",
661
+ text: `Component not found: "${componentId}". Use the list-all-documentation tool to see available components.`
662
+ }],
663
+ isError: true
664
+ };
665
+ if (!component.stories?.find((s) => s.name === storyName)) return {
666
+ content: [{
667
+ type: "text",
668
+ text: `Story "${storyName}" not found for component "${componentId}". Available stories: ${component.stories?.map((s) => s.name).join(", ") ?? "none"}`
669
+ }],
670
+ isError: true
671
+ };
633
672
  return { content: [{
634
673
  type: "text",
635
- text: formatStoryDocumentation(component, input.storyName)
674
+ text: formatStoryDocumentation(component, storyName)
636
675
  }] };
637
676
  } catch (error) {
638
677
  return errorToMCPContent(error);
@@ -644,7 +683,6 @@ async function addGetStoryDocumentationTool(server, enabled) {
644
683
  //#region src/tools/get-documentation.ts
645
684
  const GET_TOOL_NAME = "get-documentation";
646
685
  const BaseInput = { id: v.pipe(v.string(), v.description("The component or docs entry ID (e.g., \"button\")")) };
647
- const StorybookIdField = { storybookId: v.pipe(v.string(), v.description("The Storybook source ID (e.g., \"local\", \"tetra\"). Required when multiple Storybooks are composed. See list-all-documentation for available sources.")) };
648
686
  async function addGetDocumentationTool(server, enabled, options) {
649
687
  const schema = options?.multiSource ? v.object({
650
688
  ...BaseInput,
@@ -655,7 +693,7 @@ async function addGetDocumentationTool(server, enabled, options) {
655
693
  title: "Get Documentation",
656
694
  description: `Get documentation for a UI component or docs entry.
657
695
 
658
- 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.
696
+ Returns the first ${MAX_STORIES_TO_SHOW} stories (including story IDs) 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.
659
697
 
660
698
  Example: id="button" returns Primary, Secondary, Large stories with code like <Button variant="primary" size="large"> showing actual prop combinations.`,
661
699
  schema,
@@ -747,4 +785,4 @@ const createStorybookMcpHandler = async (options = {}) => {
747
785
  };
748
786
 
749
787
  //#endregion
750
- export { COMPONENT_MANIFEST_PATH, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, addGetStoryDocumentationTool as addGetComponentStoryDocumentationTool, addGetDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
788
+ export { COMPONENT_MANIFEST_PATH, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, addGetDocumentationTool, addGetStoryDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/mcp",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
5
5
  "keywords": [
6
6
  "mcp",