Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-pianos-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@storybook/mcp': patch
---

Forward `sources` through `createStorybookMcpHandler()` into the per-request transport context.
87 changes: 85 additions & 2 deletions packages/mcp/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect, vi, afterEach } from 'vitest';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { createStorybookMcpHandler } from './index.ts';
import type { StorybookContext } from './types.ts';
import smallManifestFixture from '../fixtures/small-manifest.fixture.json' with { type: 'json' };
import smallDocsManifestFixture from '../fixtures/small-docs-manifest.fixture.json' with { type: 'json' };

Expand Down Expand Up @@ -42,12 +43,15 @@ describe('createStorybookMcpHandler', () => {
/**
* Helper to setup client with a mock fetch that routes to our handler
*/
async function setupClient(handler: Awaited<ReturnType<typeof createStorybookMcpHandler>>) {
async function setupClient(
handler: Awaited<ReturnType<typeof createStorybookMcpHandler>>,
context?: StorybookContext,
) {
// Mock global fetch to route to our handler
fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
const request = new Request(url, init);
return await handler(request);
return await handler(request, context);
});
(global as any).fetch = fetchMock;

Expand Down Expand Up @@ -168,6 +172,85 @@ describe('createStorybookMcpHandler', () => {
});
});

it('should forward handler-level sources into the transport context', async () => {
const manifestProvider = createManifestProviderMockWithDocs();
const sources = [
{ id: 'local', title: 'Local' },
{ id: 'remote', title: 'Remote', url: 'https://example.com/storybook' },
];

const handler = await createStorybookMcpHandler({
manifestProvider,
sources,
});
await setupClient(handler);

await client.callTool({
name: 'list-all-documentation',
arguments: {},
});

expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/components.json',
sources[0],
);
expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/docs.json',
sources[0],
);
expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/components.json',
sources[1],
);
expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/docs.json',
sources[1],
);
});

it('should allow per-request sources to override handler-level sources', async () => {
const manifestProvider = createManifestProviderMockWithDocs();
const handlerSources = [
{ id: 'handler', title: 'Handler', url: 'https://handler.example.com' },
];
const requestSources = [
{ id: 'request', title: 'Request', url: 'https://request.example.com' },
];

const handler = await createStorybookMcpHandler({
manifestProvider,
sources: handlerSources,
});
await setupClient(handler, {
sources: requestSources,
});

await client.callTool({
name: 'list-all-documentation',
arguments: {},
});

expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/components.json',
requestSources[0],
);
expect(manifestProvider).toHaveBeenCalledWith(
expect.any(Request),
'./manifests/docs.json',
requestSources[0],
);
expect(manifestProvider).not.toHaveBeenCalledWith(
expect.any(Request),
expect.any(String),
handlerSources[0],
);
});

it('should call onListAllDocumentation handler when tool is invoked', async () => {
const onListAllDocumentation = vi.fn();
const manifestProvider = createManifestProviderMock();
Expand Down
10 changes: 5 additions & 5 deletions packages/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Handler = (req: Request, context?: StorybookContext) => Promise<Response>;
export const createStorybookMcpHandler = async (
options: StorybookMcpHandlerOptions = {},
): Promise<Handler> => {
const { onSessionInitialize, ...defaultContext } = options;
const adapter = new ValibotJsonSchemaAdapter();
const server = new McpServer(
{
Expand All @@ -92,8 +93,8 @@ export const createStorybookMcpHandler = async (
},
).withContext<StorybookContext>();

if (options.onSessionInitialize) {
server.on('initialize', options.onSessionInitialize);
if (onSessionInitialize) {
server.on('initialize', onSessionInitialize);
}

await addListAllDocumentationTool(server);
Expand All @@ -104,10 +105,9 @@ export const createStorybookMcpHandler = async (

return (async (req, context) => {
return await transport.respond(req, {
...defaultContext,
...context,
request: req,
manifestProvider: context?.manifestProvider ?? options.manifestProvider,
onListAllDocumentation: context?.onListAllDocumentation ?? options.onListAllDocumentation,
onGetDocumentation: context?.onGetDocumentation ?? options.onGetDocumentation,
});
Comment on lines 107 to 111
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spreading ...context over ...defaultContext changes the precedence semantics compared to the previous nullish-coalescing merge: if a caller passes a per-request context where fields like manifestProvider, sources, onListAllDocumentation, or onGetDocumentation are present but undefined (e.g. via object-spread), this will now overwrite the handler-level defaults and can fall back to the default provider unexpectedly. Consider merging per-field with nullish coalescing (or a merge helper that ignores undefined) so handler-level options remain the default unless the request explicitly supplies a defined override.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay actually.

}) as Handler;
};
Loading