Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/clean-bears-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@storybook/addon-mcp': patch
---

Improve `/mcp` HTML response
46 changes: 46 additions & 0 deletions packages/addon-mcp/src/preset.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { Options } from 'storybook/internal/types';
import { experimental_devServer } from './preset.ts';
import * as runStoryTests from './tools/run-story-tests.ts';

describe('experimental_devServer', () => {
let mockApp: any;
Expand Down Expand Up @@ -67,6 +68,51 @@
expect(mockRes.end).toHaveBeenCalledWith(expect.stringContaining('<html'));
});

it('should show Storybook version requirement for addon-vitest and a manual manifest link', async () => {
vi.spyOn(runStoryTests, 'getAddonVitestConstants').mockResolvedValue(undefined);
const manifestEnabledOptions = {
Comment thread
JReinhold marked this conversation as resolved.
presets: {
apply: vi.fn((key: string) => {
if (key === 'features') {
return Promise.resolve({ experimentalComponentsManifest: true });
}
if (key === 'experimental_manifests') {
return Promise.resolve({});
}
return Promise.resolve(undefined);
}),
},
} as unknown as Options;

let getHandler: any;
mockApp.get = vi.fn((path, handler) => {
getHandler = handler;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: This captures the last registered GET handler, which happens to be /mcp today — but only because it's registered after /.well-known/... in preset.ts. If a future change adds another app.get() call after /mcp, this test would silently test the wrong handler.

Other tests in this same file already use a more robust pattern (e.g. line 144):

const handlers: Record<string, any> = {};
mockApp.get = vi.fn((path: string, handler: any) => {
  handlers[path] = handler;
});

await (experimental_devServer as any)(mockApp, manifestEnabledOptions);

const mcpHandler = handlers['/mcp'];
await mcpHandler(mockReq, mockRes);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot this comment makes sense to me, fix it.

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.

Updated the test to capture GET handlers by route and explicitly invoke handlers['/mcp'] instead of relying on the last registered handler. This is in commit e559378, and preset.test.ts passes with the change. Screenshot reference: https://github.com/user-attachments/assets/768de4e3-cc8b-4b37-ad64-5e125c70a42f

});

await (experimental_devServer as any)(mockApp, manifestEnabledOptions);

const mockReq = {
headers: {
accept: 'text/html',
},
} as any;
const mockRes = {
writeHead: vi.fn(),
end: vi.fn(),
} as any;

await getHandler(mockReq, mockRes);

expect(mockRes.end).toHaveBeenCalledWith(
expect.stringContaining('This toolset requires Storybook 10.3.0+ with'),
);
expect(mockRes.end).toHaveBeenCalledWith(
expect.stringContaining(
'View the <a href="/manifests/components.html">component manifest debugger</a>.',
),
);
});
Comment thread
JReinhold marked this conversation as resolved.

it('should handle POST requests as MCP protocol', async () => {
await (experimental_devServer as any)(mockApp, mockOptions);

Expand Down Expand Up @@ -227,7 +273,7 @@
await (experimental_devServer as any)(mockApp, optionsWithRefs);

// The preset should have called presets.apply('refs')
expect(optionsWithRefs.presets.apply).toHaveBeenCalledWith('refs', {});

Check warning on line 276 in packages/addon-mcp/src/preset.test.ts

View workflow job for this annotation

GitHub Actions / Lint

typescript-eslint(unbound-method)

void`, or consider using an arrow function instead.
});

it('should handle refs config returning non-object gracefully', async () => {
Expand Down
18 changes: 8 additions & 10 deletions packages/addon-mcp/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const experimental_devServer: PresetPropertyFn<'experimental_devServer'>
});
}

// Browser request - send HTML with redirect
// Browser request - send HTML
res.writeHead(200, { 'Content-Type': 'text/html' });

let docsNotice = '';
Expand All @@ -137,7 +137,7 @@ export const experimental_devServer: PresetPropertyFn<'experimental_devServer'>

const testNoticeLines = [
!addonVitestConstants &&
`This toolset requires <code>@storybook/addon-vitest</code>. <a target="_blank" href="https://storybook.js.org/docs/writing-tests/test-addon">Learn how to set it up</a>`,
`This toolset requires Storybook 10.3.0+ with <code>@storybook/addon-vitest</code>. <a target="_blank" href="https://storybook.js.org/docs/writing-tests/test-addon">Learn how to set it up</a>`,
!a11yEnabled &&
`Add <code>@storybook/addon-a11y</code> for accessibility testing. <a target="_blank" href="https://storybook.js.org/docs/writing-tests/accessibility-testing">Learn more</a>`,
].filter(Boolean);
Expand All @@ -150,19 +150,17 @@ export const experimental_devServer: PresetPropertyFn<'experimental_devServer'>
: '';

const html = htmlTemplate
.replace(
'{{REDIRECT_META}}',
manifestStatus.available
? // redirect the user to the component manifest page after 10 seconds
'<meta http-equiv="refresh" content="10;url=/manifests/components.html" />'
: // ... or hide the message about redirection
'<style>#redirect-message { display: none; }</style>',
)
.replaceAll('{{DEV_STATUS}}', isDevEnabled ? 'enabled' : 'disabled')
.replaceAll('{{DOCS_STATUS}}', isDocsEnabled ? 'enabled' : 'disabled')
.replace('{{DOCS_NOTICE}}', docsNotice)
.replaceAll('{{TEST_STATUS}}', isTestEnabled ? 'enabled' : 'disabled')
.replace('{{TEST_NOTICE}}', testNotice)
.replace(
'{{MANIFEST_DEBUGGER_LINK}}',
manifestStatus.available
? '<p>View the <a href="/manifests/components.html">component manifest debugger</a>.</p>'
: '',
)
.replace('{{A11Y_BADGE}}', a11yBadge);
res.end(html);
});
Expand Down
17 changes: 1 addition & 16 deletions packages/addon-mcp/src/template.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!doctype html>
<html>
<head>
{{REDIRECT_META}}
<style>
@font-face {
font-family: 'Nunito Sans';
Expand Down Expand Up @@ -233,21 +232,7 @@ <h3>Available Toolsets</h3>
</div>
</div>

<p id="redirect-message">
Automatically redirecting to
<a href="/manifests/components.html">component manifest</a>
in <span id="countdown">10</span> seconds...
</p>
{{MANIFEST_DEBUGGER_LINK}}
</div>
<script>
let countdown = 10;
const countdownElement = document.getElementById('countdown');
if (countdownElement) {
setInterval(() => {
countdown -= 1;
countdownElement.textContent = countdown.toString();
}, 1000);
}
</script>
</body>
</html>
Loading