-
Notifications
You must be signed in to change notification settings - Fork 42
remove source API and use the request instead #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
0ae13b5
67d5671
76c5d19
68dcf87
0f9015d
9b8eee8
f88c7ff
7811134
4d913a8
1f74003
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@storybook/mcp': minor | ||
| --- | ||
|
|
||
| Replace the `source` property in the context with `request`. | ||
|
|
||
| Now you don't pass in a source string that might be fetched or handled by your custom `manifestProvider`, but instead you pass in the whole web request. (This is automatically handled if you use the createStorybookMcpHandler() function). | ||
|
|
||
| The default action is now to fetch the manifest from `../manifests/components.json` assuming the server is running at `./mcp`. Your custom `manifestProvider()`-function then also does not get a source string as an argument, but gets the whole web request, that you can use to get information about where to fetch the manifest from. It also gets a second argument, `path`, which it should use to determine which specific manifest to get from a built Storybook. (Currently always `./manifests/components.json`, but in the future it might be other paths too). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,50 @@ src/ | |
| 1. **Factory Pattern**: `createStorybookMcpHandler()` creates configured handler instances | ||
| 2. **Tool Registration**: Tools are added to the server using `server.tool()` method | ||
| 3. **Async Handler**: Returns a Promise-based request handler compatible with standard HTTP servers | ||
| 4. **Request-based Context**: The `Request` object is passed through context to tools, which use it to construct the manifest URL | ||
|
|
||
| ### Manifest Provider API | ||
|
|
||
| The handler accepts a `StorybookContext` with the following key properties: | ||
|
|
||
| - **`request`**: The HTTP `Request` object being processed (automatically passed by the handler) | ||
| - **`manifestProvider`**: Optional custom function `(request: Request, path: string) => Promise<string>` to override default manifest fetching | ||
| - **Parameters**: | ||
| - `request`: The HTTP `Request` object to determine base URL, headers, routing, etc. | ||
| - `path`: The manifest path (currently always `'./manifests/components.json'`) | ||
| - **Responsibility**: The provider determines the "first part" of the URL (base URL/origin) by examining the request. The MCP server provides the path. | ||
| - Default behavior: Constructs URL from request origin, replacing `/mcp` with the provided path | ||
| - Return value should be the manifest JSON as a string | ||
|
|
||
| **Example with custom manifestProvider:** | ||
|
|
||
| ```typescript | ||
| import { createStorybookMcpHandler } from '@storybook/mcp'; | ||
| import { readFile } from 'node:fs/promises'; | ||
|
|
||
| const handler = await createStorybookMcpHandler({ | ||
| manifestProvider: async (request, path) => { | ||
| // Custom logic: read from local filesystem | ||
| // The provider decides on the base path, MCP provides the manifest path | ||
| const basePath = '/path/to/manifests'; | ||
| // Remove leading './' from path if present | ||
| const normalizedPath = path.replace(/^\.\//g, ''); | ||
| const fullPath = `${basePath}/${normalizedPath}`; | ||
| return await readFile(fullPath, 'utf-8'); | ||
| }, | ||
| // Or map requests to different S3 buckets: | ||
| manifestProvider: async (request, path) => { | ||
| const url = new URL(request.url); | ||
| const bucket = url.hostname.includes('staging') | ||
| ? 'staging-bucket' | ||
| : 'prod-bucket'; | ||
| const normalizedPath = path.replace(/^\.\//g, ''); | ||
|
JReinhold marked this conversation as resolved.
Outdated
|
||
| const manifestUrl = `https://${bucket}.s3.amazonaws.com/${normalizedPath}`; | ||
| const response = await fetch(manifestUrl); | ||
| return await response.text(); | ||
| }, | ||
| }); | ||
|
Comment on lines
+66
to
+96
|
||
| ``` | ||
|
|
||
| ### Component Manifest and ReactDocgen Support | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.