@storybook/mcp 0.5.1 → 0.6.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/README.md CHANGED
@@ -1,3 +1,350 @@
1
- # `@storybook/mcp`
1
+ # Storybook MCP
2
2
 
3
- The hype is real.
3
+ Reusable MCP package for Storybook component and docs knowledge.
4
+
5
+ Learn more about Storybook at [storybook.js.org](https://storybook.js.org/?ref=readme).
6
+
7
+ ## Self-hosting `@storybook/mcp`
8
+
9
+ ### Prerequisites
10
+
11
+ - Node.js 20+
12
+ - A [manifests](https://storybook.js.org/docs/next/ai/manifests) source containing:
13
+ - `components.json` (required)
14
+ - `docs.json` (optional)
15
+
16
+ ### Example implementation
17
+
18
+ Reference implementation: [apps/self-host-mcp/server.ts](../../apps/self-host-mcp/server.ts)
19
+
20
+ The example repo demonstrates self-hosting patterns for both a Node.js process and a Netlify Function.
21
+
22
+ #### Minimal implementation
23
+
24
+ ```ts
25
+ import { createStorybookMcpHandler } from '@storybook/mcp';
26
+
27
+ const storybookMcpHandler = await createStorybookMcpHandler();
28
+
29
+ export async function handleRequest(request: Request): Promise<Response> {
30
+ if (new URL(request.url).pathname === '/mcp') {
31
+ return storybookMcpHandler(request);
32
+ }
33
+
34
+ return new Response('Not found', { status: 404 });
35
+ }
36
+ ```
37
+
38
+ #### With custom manifest source
39
+
40
+ Use `manifestProvider` when your manifests are not available from the same origin/path layout:
41
+
42
+ ```ts
43
+ const storybookMcpHandler = await createStorybookMcpHandler({
44
+ manifestProvider: async (_request, path) => {
45
+ return asyncReadManifestFromSomewhere(path);
46
+ },
47
+ });
48
+ ```
49
+
50
+ ### API reference
51
+
52
+ #### `createStorybookMcpHandler`
53
+
54
+ Type:
55
+
56
+ ```ts
57
+ (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
58
+
59
+ type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
60
+ ```
61
+
62
+ Creates and configures an MCP HTTP handler with all built-in docs tools registered.
63
+
64
+ ##### Parameters
65
+
66
+ ###### `options`
67
+
68
+ Type: [`StorybookMcpHandlerOptions`](#handler-options-and-request-context)
69
+
70
+ Default: `{}`
71
+
72
+ Server-level configuration. The handler uses this at creation time and as a fallback for per-request context.
73
+
74
+ ##### Returns
75
+
76
+ Type: `Promise<Handler>`
77
+
78
+ A fetch-compatible request handler for your `/mcp` endpoint.
79
+
80
+ ##### Behavior
81
+
82
+ - Registers these MCP tools:
83
+ - [`list-all-documentation`](https://storybook.js.org/docs/next/ai/mcp/overview/#list-all-documentation)
84
+ - [`get-documentation`](https://storybook.js.org/docs/next/ai/mcp/overview/#get-documentation)
85
+ - [`get-documentation-for-story`](https://storybook.js.org/docs/next/ai/mcp/overview/#get-documentation-for-story)
86
+ - Uses HTTP transport from [`@tmcp/transport-http`](https://github.com/paoloricciuti/tmcp/).
87
+ - For each request, the handler always passes the current `Request` as `context.request`.
88
+ - Per-request `context` overrides handler-level `options` for:
89
+ - [`manifestProvider`](#manifestprovider)
90
+ - [`onListAllDocumentation`](#onlistalldocumentation)
91
+ - [`onGetDocumentation`](#ongetdocumentation)
92
+
93
+ > Note: [`onSessionInitialize`](#handler-options-and-request-context) can only be set at handler creation time (in `options`).
94
+
95
+ ##### Example
96
+
97
+ ```ts
98
+ import { createStorybookMcpHandler } from '@storybook/mcp';
99
+
100
+ const mcp = await createStorybookMcpHandler({
101
+ manifestProvider: async (_request, path) => {
102
+ return await fetchManifest(path);
103
+ },
104
+ });
105
+
106
+ export async function handleRequest(request: Request) {
107
+ if (new URL(request.url).pathname !== '/mcp') {
108
+ return new Response('Not found', { status: 404 });
109
+ }
110
+
111
+ return mcp(request);
112
+ }
113
+ ```
114
+
115
+ #### Handler options and request context
116
+
117
+ `@storybook/mcp` uses the same core fields in two places:
118
+
119
+ - Handler creation (`createStorybookMcpHandler(options)`)
120
+ - Per-request override (`handler(request, context)`)
121
+
122
+ Type:
123
+
124
+ ```ts
125
+ type StorybookContext = {
126
+ request?: Request;
127
+ manifestProvider?: (
128
+ request: Request | undefined,
129
+ path: string,
130
+ source?: Source,
131
+ ) => Promise<string>;
132
+ sources?: Source[];
133
+ onListAllDocumentation?: (params: {
134
+ context: StorybookContext;
135
+ manifests: AllManifests;
136
+ resultText: string;
137
+ sources?: SourceManifests[];
138
+ }) => void | Promise<void>;
139
+ onGetDocumentation?: (
140
+ params:
141
+ | {
142
+ context: StorybookContext;
143
+ input: { id: string; storybookId?: string };
144
+ foundDocumentation: ComponentManifest | Doc;
145
+ resultText: string;
146
+ }
147
+ | {
148
+ context: StorybookContext;
149
+ input: { id: string; storybookId?: string };
150
+ },
151
+ ) => void | Promise<void>;
152
+ };
153
+
154
+ type StorybookMcpHandlerOptions = StorybookContext & {
155
+ onSessionInitialize?: (initializeRequestParams: InitializeRequestParams) => void | Promise<void>;
156
+ };
157
+ ```
158
+
159
+ > [!NOTE]
160
+ > `onSessionInitialize` is only used when provided at handler creation time (`createStorybookMcpHandler(options)`). It is ignored in per-request `context`.
161
+ >
162
+ > `InitializeRequestParams` is the [`tmcp`](https://github.com/paoloricciuti/tmcp/) initialize payload type, and its exact structure may change in patch versions. Prefer treating it as an opaque protocol payload unless you need specific fields.
163
+
164
+ ##### `manifestProvider`
165
+
166
+ Type:
167
+
168
+ ```ts
169
+ (request: Request | undefined, path: string, source?: Source) => Promise<string>;
170
+ ```
171
+
172
+ Primary extension point for production setups.
173
+
174
+ Use this when manifests are not available at the default same-origin paths. Your function returns the raw JSON string for each requested manifest path.
175
+
176
+ For a real customization example (switching between HTTP and filesystem-backed manifest loading), see the [Example implementation](#example-implementation) section above.
177
+
178
+ Manifest paths requested by built-in tools:
179
+
180
+ - `./manifests/components.json` (required)
181
+ - `./manifests/docs.json` (optional)
182
+
183
+ ##### `request`
184
+
185
+ Type: `Request`
186
+
187
+ The incoming HTTP request for the current call, as a Web Fetch API (`WHATWG`) `Request`.
188
+
189
+ This is **not** a Node.js `http.IncomingMessage`. In Node runtimes, pass a fetch-compatible `Request` (for example, Node's global `Request` from Undici in modern Node versions), or convert your server's native request object before calling the handler.
190
+
191
+ `createStorybookMcpHandler` automatically sets this field when you invoke the returned handler with `(request, context?)`.
192
+
193
+ ##### `onListAllDocumentation`
194
+
195
+ Type:
196
+
197
+ ```ts
198
+ (params: {
199
+ context: StorybookContext;
200
+ manifests: AllManifests;
201
+ resultText: string;
202
+ sources?: SourceManifests[];
203
+ }) => void | Promise<void>
204
+ ```
205
+
206
+ Optional callback after `list-all-documentation` resolves successfully.
207
+
208
+ ##### `onGetDocumentation`
209
+
210
+ Type:
211
+
212
+ ```ts
213
+ (
214
+ params:
215
+ | {
216
+ context: StorybookContext;
217
+ input: { id: string; storybookId?: string };
218
+ foundDocumentation: ComponentManifest | Doc;
219
+ resultText: string;
220
+ }
221
+ | {
222
+ context: StorybookContext;
223
+ input: { id: string; storybookId?: string };
224
+ },
225
+ ) => void | Promise<void>
226
+ ```
227
+
228
+ Optional callback after `get-documentation` runs:
229
+
230
+ - When a component/docs entry is found, receives `foundDocumentation` and `resultText`.
231
+ - When not found, receives only `context` and `input`.
232
+
233
+ ##### `sources`
234
+
235
+ Type: `Source[]`
236
+
237
+ Optional multi-source configuration for composing multiple Storybook MCP sources. This is supported but relatively uncommon for most user setups.
238
+
239
+ #### `Source`
240
+
241
+ Type:
242
+
243
+ ```ts
244
+ type Source = {
245
+ id: string;
246
+ title: string;
247
+ url?: string;
248
+ };
249
+ ```
250
+
251
+ Represents one Storybook source in multi-source mode.
252
+
253
+ #### `SourceManifests`
254
+
255
+ Type:
256
+
257
+ ```ts
258
+ type SourceManifests = {
259
+ source: Source;
260
+ componentManifest: ComponentManifestMap;
261
+ docsManifest?: DocsManifestMap;
262
+ error?: string;
263
+ };
264
+ ```
265
+
266
+ Represents fetched manifests (or an error) for a single source.
267
+
268
+ #### Tool registration exports
269
+
270
+ Use these when you want to build your own [`tmcp`](https://github.com/paoloricciuti/tmcp/) server instead of using `createStorybookMcpHandler`, while still reusing Storybook's docs tools.
271
+
272
+ This approach is useful when you need to:
273
+
274
+ - register Storybook tools alongside your own custom tools,
275
+ - customize transport/session setup yourself,
276
+ - or conditionally enable tools based on your own server context.
277
+
278
+ > [!IMPORTANT]
279
+ > These composition helpers are built for `tmcp`'s `McpServer` and **cannot** be directly composed into a server built with the [official MCP TypeScript SDK (`@modelcontextprotocol/sdk`)](https://github.com/modelcontextprotocol/typescript-sdk).
280
+
281
+ Minimal composition example:
282
+
283
+ ```ts
284
+ import { McpServer } from 'tmcp';
285
+ import { ValibotJsonSchemaAdapter } from '@tmcp/adapter-valibot';
286
+ import {
287
+ addGetStoryDocumentationTool,
288
+ addGetDocumentationTool,
289
+ addListAllDocumentationTool,
290
+ type StorybookContext,
291
+ } from '@storybook/mcp';
292
+
293
+ const adapter = new ValibotJsonSchemaAdapter();
294
+ const server = new McpServer(
295
+ { name: 'custom-mcp', version: '1.0.0' },
296
+ {
297
+ adapter,
298
+ capabilities: {
299
+ tools: { listChanged: true },
300
+ },
301
+ },
302
+ ).withContext<StorybookContext>();
303
+
304
+ await addListAllDocumentationTool(server);
305
+ await addGetDocumentationTool(server);
306
+ await addGetStoryDocumentationTool(server);
307
+ ```
308
+
309
+ After registration, wire your own transport and pass `StorybookContext` per request so tools can resolve manifests (`request`, `manifestProvider`, and optional `sources`).
310
+
311
+ #### `addListAllDocumentationTool`
312
+
313
+ Type:
314
+
315
+ ```ts
316
+ (server: McpServer<any, StorybookContext>, enabled?: () => boolean | Promise<boolean>) =>
317
+ Promise<void>;
318
+ ```
319
+
320
+ Registers the [list tool](https://storybook.js.org/docs/next/ai/mcp/overview/#list-all-documentation) that returns all component/docs IDs from manifests.
321
+
322
+ #### `addGetDocumentationTool`
323
+
324
+ Type:
325
+
326
+ ```ts
327
+ (
328
+ server: McpServer<any, StorybookContext>,
329
+ enabled?: () => boolean | Promise<boolean>,
330
+ options?: { multiSource?: boolean },
331
+ ) => Promise<void>;
332
+ ```
333
+
334
+ Registers [documentation lookup](https://storybook.js.org/docs/next/ai/mcp/overview/#get-documentation) by component/docs `id`.
335
+
336
+ When `options.multiSource` is `true`, the tool schema requires `storybookId` input.
337
+
338
+ #### `addGetStoryDocumentationTool`
339
+
340
+ Type:
341
+
342
+ ```ts
343
+ (
344
+ server: McpServer<any, StorybookContext>,
345
+ enabled?: () => boolean | Promise<boolean>,
346
+ options?: { multiSource?: boolean },
347
+ ) => Promise<void>;
348
+ ```
349
+
350
+ Registers [story-level documentation lookup](https://storybook.js.org/docs/next/ai/mcp/overview/#get-documentation-for-story) for a specific story variant by `componentId` and `storyName`.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { t as instructions_default } from "./instructions-Cl0vcW2j.js";
1
2
  import { McpServer } from "tmcp";
2
3
  import * as v from "valibot";
3
4
 
@@ -269,4 +270,4 @@ interface StorybookMcpHandlerOptions extends StorybookContext {
269
270
  type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
270
271
  declare const createStorybookMcpHandler: (options?: StorybookMcpHandlerOptions) => Promise<Handler>;
271
272
  //#endregion
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 };
273
+ export { COMPONENT_MANIFEST_PATH, type ComponentManifest, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, instructions_default as STORYBOOK_MCP_INSTRUCTIONS, type Source, type SourceManifests, type StorybookContext, StorybookMcpHandlerOptions, addGetDocumentationTool, addGetStoryDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { t as instructions_default } from "./instructions-Cl0vcW2j.js";
1
2
  import { McpServer } from "tmcp";
2
3
  import { ValibotJsonSchemaAdapter } from "@tmcp/adapter-valibot";
3
4
  import { HttpTransport } from "@tmcp/transport-http";
@@ -5,7 +6,7 @@ import * as v from "valibot";
5
6
 
6
7
  //#region package.json
7
8
  var name = "@storybook/mcp";
8
- var version = "0.5.1";
9
+ var version = "0.6.1";
9
10
  var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
10
11
 
11
12
  //#endregion
@@ -760,6 +761,7 @@ Example: id="button" returns Primary, Secondary, Large stories with code like <B
760
761
  //#endregion
761
762
  //#region src/index.ts
762
763
  const createStorybookMcpHandler = async (options = {}) => {
764
+ const { onSessionInitialize, ...defaultContext } = options;
763
765
  const adapter = new ValibotJsonSchemaAdapter();
764
766
  const server = new McpServer({
765
767
  name,
@@ -767,22 +769,22 @@ const createStorybookMcpHandler = async (options = {}) => {
767
769
  description
768
770
  }, {
769
771
  adapter,
772
+ instructions: instructions_default,
770
773
  capabilities: { tools: { listChanged: true } }
771
774
  }).withContext();
772
- if (options.onSessionInitialize) server.on("initialize", options.onSessionInitialize);
775
+ if (onSessionInitialize) server.on("initialize", onSessionInitialize);
773
776
  await addListAllDocumentationTool(server);
774
777
  await addGetStoryDocumentationTool(server);
775
778
  await addGetDocumentationTool(server);
776
779
  const transport = new HttpTransport(server, { path: null });
777
780
  return (async (req, context) => {
778
781
  return await transport.respond(req, {
779
- request: req,
780
- manifestProvider: context?.manifestProvider ?? options.manifestProvider,
781
- onListAllDocumentation: context?.onListAllDocumentation ?? options.onListAllDocumentation,
782
- onGetDocumentation: context?.onGetDocumentation ?? options.onGetDocumentation
782
+ ...defaultContext,
783
+ ...context,
784
+ request: req
783
785
  });
784
786
  });
785
787
  };
786
788
 
787
789
  //#endregion
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 };
790
+ export { COMPONENT_MANIFEST_PATH, ComponentManifestMap, DOCS_MANIFEST_PATH, DocsManifestMap, GET_STORY_TOOL_NAME, GET_TOOL_NAME, LIST_TOOL_NAME, instructions_default as STORYBOOK_MCP_INSTRUCTIONS, addGetDocumentationTool, addGetStoryDocumentationTool, addListAllDocumentationTool, createStorybookMcpHandler, getMultiSourceManifests };
@@ -0,0 +1,5 @@
1
+ //#region src/instructions.md
2
+ var instructions_default = "## Documentation Workflow\n\n1. Call **list-all-documentation** once at the start of the task to discover available component and docs IDs.\n2. Call **get-documentation** with an `id` from that list to retrieve full component docs, props, usage examples, and stories.\n3. Call **get-documentation-for-story** when you need additional docs from a specific story variant that was not included in the initial component documentation.\n\nUse `withStoryIds: true` on **list-all-documentation** when you also need story IDs for inputs to other tools.\n\n## Verification Rules\n\n- Never assume component props, variants, or API shape. Retrieve documentation before using a component.\n- If a component or prop is not documented, do not invent it. Report that it was not found.\n- Only reference IDs returned by **list-all-documentation**. Do not guess IDs.\n\n## Multi-Source Requests\n\n- When multiple Storybook sources are configured, **list-all-documentation** returns entries from all sources.\n- Use `storybookId` in **get-documentation** when you need to scope a request to one source.\n";
3
+
4
+ //#endregion
5
+ export { instructions_default as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/mcp",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "MCP server that serves knowledge about your components based on your Storybook stories and documentation",
5
5
  "keywords": [
6
6
  "mcp",