@storybook/mcp 0.6.0 → 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.
Files changed (3) hide show
  1. package/README.md +349 -2
  2. package/dist/index.js +1 -1
  3. package/package.json +1 -1
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.js CHANGED
@@ -6,7 +6,7 @@ import * as v from "valibot";
6
6
 
7
7
  //#region package.json
8
8
  var name = "@storybook/mcp";
9
- var version = "0.6.0";
9
+ var version = "0.6.1";
10
10
  var description = "MCP server that serves knowledge about your components based on your Storybook stories and documentation";
11
11
 
12
12
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/mcp",
3
- "version": "0.6.0",
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",