generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 16
Fix internal stdio-based MCP server #85
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@storybook/mcp': patch | ||
| --- | ||
|
|
||
| Allow undefined request in server context when using custom manifestProvider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /** | ||
| * Integration tests for the stdio MCP server in bin.ts | ||
| * | ||
| * These tests spawn the bin.ts process as a child process and communicate | ||
| * with it via stdin/stdout, simulating how an MCP client would interact | ||
| * with the server in production. | ||
| */ | ||
| import { describe, it, expect, beforeAll, afterAll } from 'vitest'; | ||
| import { x } from 'tinyexec'; | ||
| import { resolve, dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import type { ChildProcess } from 'node:child_process'; | ||
|
|
||
| /** | ||
| * Helper to send a JSON-RPC request and wait for the response | ||
| */ | ||
| async function sendRequest( | ||
| child: ChildProcess, | ||
| stdoutData: string[], | ||
| request: unknown, | ||
| requestId: number, | ||
| timeoutMs = 10_000, | ||
| ): Promise<unknown> { | ||
| // Send request | ||
| child.stdin?.write(JSON.stringify(request) + '\n'); | ||
|
|
||
| // Wait for response with timeout | ||
| const { promise, resolve, reject } = Promise.withResolvers<void>(); | ||
| const timeout = setTimeout(() => { | ||
| reject(new Error(`Timeout waiting for response to request ${requestId}`)); | ||
| }, timeoutMs); | ||
|
|
||
| const checkResponse = () => { | ||
| const allData = stdoutData.join(''); | ||
| if (allData.includes(`"id":${requestId}`)) { | ||
| clearTimeout(timeout); | ||
| resolve(); | ||
| } else { | ||
| setTimeout(checkResponse, 50); | ||
| } | ||
| }; | ||
| checkResponse(); | ||
|
|
||
| await promise; | ||
|
|
||
| // Parse and return the response | ||
| const allData = stdoutData.join(''); | ||
| const lines = allData.split('\n').filter((line) => line.trim()); | ||
| const responseLine = lines.find((line) => { | ||
| try { | ||
| const parsed = JSON.parse(line); | ||
| return parsed.id === requestId; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| if (!responseLine) { | ||
| throw new Error(`No response found for request ${requestId}`); | ||
| } | ||
|
|
||
| return JSON.parse(responseLine); | ||
| } | ||
|
|
||
| describe('bin.ts stdio MCP server', () => { | ||
| let child: ChildProcess; | ||
| let stdoutData: string[] = []; | ||
| let stderrData: string[] = []; | ||
|
|
||
| beforeAll(() => { | ||
| const currentDir = dirname(fileURLToPath(import.meta.url)); | ||
| const binPath = resolve(currentDir, './bin.ts'); | ||
| const fixturePath = resolve( | ||
| currentDir, | ||
| './fixtures/full-manifest.fixture.json', | ||
| ); | ||
|
|
||
| const proc = x('node', [binPath, '--manifestPath', fixturePath]); | ||
|
|
||
| child = proc.process as ChildProcess; | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Collect stdout for later assertions | ||
| child.stdout?.on('data', (chunk) => { | ||
| stdoutData.push(chunk.toString()); | ||
| }); | ||
|
|
||
| // Collect stderr for debugging | ||
| child.stderr?.on('data', (chunk) => { | ||
| stderrData.push(chunk.toString()); | ||
| }); | ||
|
|
||
| child.on('error', (err) => { | ||
| console.error('Process error:', err); | ||
| }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| child.kill(); | ||
| }); | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('should respond to initialize request', async () => { | ||
| const request = { | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'initialize', | ||
| params: { | ||
| protocolVersion: '2024-11-05', | ||
| capabilities: {}, | ||
| clientInfo: { | ||
| name: 'test-client', | ||
| version: '1.0.0', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const response = await sendRequest(child, stdoutData, request, 1); | ||
|
|
||
| expect(response).toMatchObject({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| result: { | ||
| protocolVersion: '2024-11-05', | ||
| capabilities: { | ||
| tools: { | ||
| listChanged: true, | ||
| }, | ||
| }, | ||
| serverInfo: { | ||
| name: '@storybook/mcp', | ||
| }, | ||
| }, | ||
| }); | ||
| }, 15000); | ||
|
|
||
| it('should list available tools', async () => { | ||
| const request = { | ||
| jsonrpc: '2.0', | ||
| id: 2, | ||
| method: 'tools/list', | ||
| params: {}, | ||
| }; | ||
|
|
||
| const response = await sendRequest(child, stdoutData, request, 2); | ||
|
|
||
| expect(response).toMatchObject({ | ||
| jsonrpc: '2.0', | ||
| id: 2, | ||
| result: { | ||
| tools: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| name: 'list-all-components', | ||
| }), | ||
| expect.objectContaining({ | ||
| name: 'get-component-documentation', | ||
| }), | ||
| ]), | ||
| }, | ||
| }); | ||
| }, 15000); | ||
|
|
||
| it('should execute list-all-components tool', async () => { | ||
| const request = { | ||
| jsonrpc: '2.0', | ||
| id: 3, | ||
| method: 'tools/call', | ||
| params: { | ||
| name: 'list-all-components', | ||
| arguments: {}, | ||
| }, | ||
| }; | ||
|
|
||
| const response = await sendRequest(child, stdoutData, request, 3); | ||
|
|
||
| expect(response).toMatchObject({ | ||
| jsonrpc: '2.0', | ||
| id: 3, | ||
| result: { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: expect.stringContaining('<components>'), | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| }, 15000); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.