@storybook/mcp 0.4.1 → 0.5.1
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 +4 -2
- package/dist/index.js +74 -36
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -208,7 +208,9 @@ declare function addGetDocumentationTool(server: McpServer<any, StorybookContext
|
|
|
208
208
|
//#endregion
|
|
209
209
|
//#region src/tools/get-documentation-for-story.d.ts
|
|
210
210
|
declare const GET_STORY_TOOL_NAME = "get-documentation-for-story";
|
|
211
|
-
declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled']
|
|
211
|
+
declare function addGetStoryDocumentationTool(server: McpServer<any, StorybookContext>, enabled?: Parameters<McpServer<any, StorybookContext>['tool']>[0]['enabled'], options?: {
|
|
212
|
+
multiSource?: boolean;
|
|
213
|
+
}): Promise<void>;
|
|
212
214
|
//#endregion
|
|
213
215
|
//#region src/utils/get-manifest.d.ts
|
|
214
216
|
/**
|
|
@@ -267,4 +269,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
|
|
|
267
269
|
type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
|
|
268
270
|
declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
|
|
269
271
|
//#endregion
|
|
270
|
-
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,
|
|
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.
|
|
8
|
+
var version = "0.5.1";
|
|
9
9
|
var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
|
|
10
10
|
|
|
11
11
|
//#endregion
|
|
@@ -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
|
|
@@ -130,7 +135,7 @@ async function getManifests(request, manifestProvider, source) {
|
|
|
130
135
|
const getUrl = (path) => request ? getManifestUrlFromRequest(request, path) : "Unknown manifest source";
|
|
131
136
|
if (componentResult.status === "rejected") {
|
|
132
137
|
const reason = componentResult.reason;
|
|
133
|
-
const hint = reason instanceof ManifestGetError && reason.message.includes("404") ? `\nHint: The Storybook at this URL may not have the component manifest enabled. Add \`features: { experimentalComponentsManifest: true }\` to its main.ts config.` : "";
|
|
138
|
+
const hint = reason instanceof ManifestGetError && reason.message.includes("404") ? `\nHint: The Storybook at this URL may not have the component manifest enabled. Add \`features: { componentsManifest: true }\` (or \`features: { experimentalComponentsManifest: true }\` for older Storybook versions) to its main.ts config.` : "";
|
|
134
139
|
throw new ManifestGetError(`Failed to get component manifest: ${reason instanceof Error ? reason.message : String(reason)}${hint}`, getUrl(COMPONENT_MANIFEST_PATH), reason instanceof Error ? reason : void 0);
|
|
135
140
|
}
|
|
136
141
|
const componentManifest = parseManifest({
|
|
@@ -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
|
-
|
|
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))
|
|
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)
|
|
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 = 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,
|
|
@@ -580,7 +596,7 @@ async function addListAllDocumentationTool(server, enabled) {
|
|
|
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
|
|
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
|
|
631
|
+
schema,
|
|
612
632
|
enabled
|
|
613
633
|
}, async (input) => {
|
|
614
634
|
try {
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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: `
|
|
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,
|
|
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,
|
|
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 };
|