Skip to content

Commit c5439b7

Browse files
CopilotJReinhold
andcommitted
feat: add MCP server-level instructions to both packages
Co-authored-by: JReinhold <5678122+JReinhold@users.noreply.github.com>
1 parent 282e37e commit c5439b7

11 files changed

Lines changed: 128 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@storybook/mcp': minor
3+
'@storybook/addon-mcp': minor
4+
---
5+
6+
Add MCP server-level instructions to both packages
7+
8+
Both `@storybook/mcp` and `@storybook/addon-mcp` now include server instructions in the MCP `initialize` response. These instructions guide agents on how to use the available tools effectively without requiring explicit prompting from users.
9+
10+
**`@storybook/mcp` instructions include:**
11+
12+
- Tool workflow: when to use `list-all-documentation` vs `get-documentation` vs `get-documentation-for-story`
13+
- Anti-hallucination rules: never assume component props or API shape
14+
- Multi-source behavior guidance
15+
16+
**`@storybook/addon-mcp` instructions compose the `@storybook/mcp` baseline and add:**
17+
18+
- Always call `get-storybook-story-instructions` before writing or editing stories
19+
- `preview-stories` usage expectations
20+
- `run-story-tests` workflow expectations
21+
- Toolset availability guidance (`dev`, `docs`, `test`)
22+
23+
The `@storybook/mcp` package also exports `STORYBOOK_MCP_INSTRUCTIONS` for consumers who want to reuse or extend the base instructions.

packages/addon-mcp/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ For clients not listed above, consult their documentation for MCP server configu
134134

135135
## Usage
136136

137-
This addon provides MCP tools that your agent can use. The goal is that the agent uses these tools automatically when doing UI development, but agents are unreliable and unpredictable, so sometimes you might need to explicitly tell it to use the tools.
137+
This addon provides MCP tools that your agent can use. When an MCP client connects, the server sends instructions in the `initialize` response to guide agents on how to use the tools effectively — including when to call `get-storybook-story-instructions` before editing stories, how to use preview URLs, and when documentation tools are available. These instructions help agents use the correct workflow implicitly without needing explicit prompting.
138+
139+
The goal is that the agent uses these tools automatically when doing UI development, but agents are unreliable and unpredictable, so sometimes you might need to explicitly tell it to use the tools.
138140

139141
**If you are prompting from an IDE like VSCode or Cursor, be sure to use `Agent` mode and `sonnet-4.5` or better.**
140142

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Storybook Addon MCP Server Instructions
2+
3+
This server provides tools to help you build, preview, and test Storybook UI components.
4+
5+
## Before Writing or Editing Stories
6+
7+
Always call **get-storybook-story-instructions** first to get framework-specific guidance. This tool returns the correct imports, patterns, and conventions for the current project. Do not skip this step.
8+
9+
## Story Preview Workflow
10+
11+
After writing or modifying a component or story, call **preview-stories** to retrieve the live preview URLs. Always include these URLs in your response so the user can verify the visual output.
12+
13+
## Story Testing Workflow
14+
15+
When tests are available (run-story-tests tool is present), run tests after writing or changing stories. Fix any failures before reporting success. Do not report stories as complete if tests are failing.
16+
17+
## Component Documentation Workflow (docs toolset)
18+
19+
When the docs toolset is available:
20+
21+
1. Call **list-all-documentation** once to discover available components and docs IDs.
22+
2. Call **get-documentation** with a specific ID to retrieve full component props, usage examples, and stories.
23+
3. Call **get-documentation-for-story** when you need documentation scoped to a specific story variant.
24+
4. Never assume prop names, variants, or component API — always retrieve documentation first.
25+
5. Only reference IDs returned by list-all-documentation. Do not guess IDs.
26+
27+
## Toolset Availability
28+
29+
Tools are grouped into toolsets. Check which tools are available before assuming a workflow is possible:
30+
31+
- **dev**: preview-stories, get-storybook-story-instructions
32+
- **docs**: list-all-documentation, get-documentation, get-documentation-for-story
33+
- **test**: run-story-tests
34+
35+
Toolsets can be restricted per-request via the `X-MCP-Toolsets` header. When a toolset's tools are not listed, that toolset is disabled for this request.

packages/addon-mcp/src/mcp-handler.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ describe('mcpServerHandler', () => {
278278
},
279279
});
280280
expect(parsedResponse.result.serverInfo.version).toBeDefined();
281+
expect(parsedResponse.result.instructions).toBeDefined();
282+
expect(parsedResponse.result.instructions).toContain('get-storybook-story-instructions');
283+
expect(parsedResponse.result.instructions).toContain('list-all-documentation');
281284
});
282285

283286
it('should respect disableTelemetry setting', async () => {

packages/addon-mcp/src/mcp-handler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
addListAllDocumentationTool,
99
addGetDocumentationTool,
1010
addGetStoryDocumentationTool,
11+
STORYBOOK_MCP_INSTRUCTIONS,
1112
type Source,
1213
} from '@storybook/mcp';
1314
import type { Options } from 'storybook/internal/types';
@@ -21,6 +22,9 @@ import { addRunStoryTestsTool } from './tools/run-story-tests.ts';
2122
import { estimateTokens } from './utils/estimate-tokens.ts';
2223
import { isAddonA11yEnabled } from './utils/is-addon-a11y-enabled.ts';
2324
import type { CompositionAuth } from './auth/index.ts';
25+
import addonInstructions from './instructions/mcp-server-instructions.md';
26+
27+
const MCP_SERVER_INSTRUCTIONS = `${STORYBOOK_MCP_INSTRUCTIONS}\n\n${addonInstructions}`;
2428

2529
let transport: HttpTransport<AddonContext> | undefined;
2630
let origin: string | undefined;
@@ -41,6 +45,7 @@ const initializeMCPServer = async (options: Options, multiSource?: boolean) => {
4145
},
4246
{
4347
adapter: new ValibotJsonSchemaAdapter(),
48+
instructions: MCP_SERVER_INSTRUCTIONS,
4449
capabilities: {
4550
tools: { listChanged: true },
4651
resources: { listChanged: true },

packages/mcp/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# `@storybook/mcp`
22

33
The hype is real.
4+
5+
## Server Instructions
6+
7+
When an MCP client connects to this server, it receives server-level instructions in the `initialize` response. These instructions guide agents on how to use the available tools effectively:
8+
9+
- **Tool workflow**: When to use `list-all-documentation`, `get-documentation`, and `get-documentation-for-story`
10+
- **Anti-hallucination rules**: Never assume component props or API shape — always retrieve documentation first
11+
- **Multi-source behavior**: How to scope requests when multiple Storybook sources are configured

packages/mcp/bin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ import { addGetDocumentationTool } from './src/tools/get-documentation.ts';
2222
import type { StorybookContext } from './src/types.ts';
2323
import { parseArgs } from 'node:util';
2424
import * as fs from 'node:fs/promises';
25-
import { basename } from 'node:path';
25+
import { readFileSync } from 'node:fs';
26+
import { basename, resolve, dirname } from 'node:path';
27+
import { fileURLToPath } from 'node:url';
28+
29+
const serverInstructions = readFileSync(
30+
resolve(dirname(fileURLToPath(import.meta.url)), './src/instructions.md'),
31+
'utf-8',
32+
);
2633

2734
const adapter = new ValibotJsonSchemaAdapter();
2835
const server = new McpServer(
@@ -33,6 +40,7 @@ const server = new McpServer(
3340
},
3441
{
3542
adapter,
43+
instructions: serverInstructions,
3644
capabilities: {
3745
tools: { listChanged: true },
3846
},

packages/mcp/src/globals.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.md' {
2+
const content: string;
3+
export default content;
4+
}

packages/mcp/src/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ describe('createStorybookMcpHandler', () => {
9595
);
9696
});
9797

98+
it('should include server instructions in initialize response', async () => {
99+
const handler = await createStorybookMcpHandler();
100+
await setupClient(handler);
101+
102+
const instructions = client.getInstructions();
103+
expect(instructions).toBeDefined();
104+
expect(instructions).toContain('list-all-documentation');
105+
expect(instructions).toContain('get-documentation');
106+
expect(instructions).toContain('Anti-Hallucination');
107+
});
108+
98109
it('should call onSessionInitialize handler when provided', async () => {
99110
const onSessionInitialize = vi.fn();
100111
const handler = await createStorybookMcpHandler({ onSessionInitialize });

packages/mcp/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { addListAllDocumentationTool } from './tools/list-all-documentation.ts';
66
import { addGetStoryDocumentationTool } from './tools/get-documentation-for-story.ts';
77
import { addGetDocumentationTool } from './tools/get-documentation.ts';
88
import type { StorybookContext } from './types.ts';
9+
import serverInstructions from './instructions.md';
10+
11+
export { serverInstructions as STORYBOOK_MCP_INSTRUCTIONS };
912

1013
// Export tools for reuse by addon-mcp
1114
export { addListAllDocumentationTool, LIST_TOOL_NAME } from './tools/list-all-documentation.ts';
@@ -82,6 +85,7 @@ export const createStorybookMcpHandler = async (
8285
},
8386
{
8487
adapter,
88+
instructions: serverInstructions,
8589
capabilities: {
8690
tools: { listChanged: true },
8791
},

0 commit comments

Comments
 (0)