|
| 1 | +--- |
| 2 | +applyTo: 'packages/mcp/**' |
| 3 | +--- |
| 4 | + |
| 5 | +# Copilot Instructions for @storybook/mcp |
| 6 | + |
| 7 | +## Project Overview |
| 8 | + |
| 9 | +This is a Model Context Protocol (MCP) server for Storybook that serves knowledge about components based on Storybook stories and documentation. The project is built with TypeScript and provides an HTTP transport endpoint for MCP communication. |
| 10 | + |
| 11 | +## Architecture |
| 12 | + |
| 13 | +### Key Components |
| 14 | + |
| 15 | +- **MCP Server**: Built using the `tmcp` library with HTTP transport |
| 16 | +- **Tools System**: Extensible tool registration system (currently includes `list_all_components`) |
| 17 | +- **Schema Validation**: Uses Valibot for JSON schema validation via `@tmcp/adapter-valibot` |
| 18 | +- **HTTP Transport**: Provides HTTP-based MCP communication via `@tmcp/transport-http` |
| 19 | + |
| 20 | +### File Structure |
| 21 | + |
| 22 | +``` |
| 23 | +src/ |
| 24 | + index.ts # Main entry point - exports createStorybookMcpHandler |
| 25 | + serve.ts # Development server setup |
| 26 | + tools/ |
| 27 | + list.ts # Tool definitions (e.g., list_all_components) |
| 28 | +``` |
| 29 | + |
| 30 | +### Key Design Patterns |
| 31 | + |
| 32 | +1. **Factory Pattern**: `createStorybookMcpHandler()` creates configured handler instances |
| 33 | +2. **Tool Registration**: Tools are added to the server using `server.tool()` method |
| 34 | +3. **Async Handler**: Returns a Promise-based request handler compatible with standard HTTP servers |
| 35 | + |
| 36 | +## Development Workflow |
| 37 | + |
| 38 | +### Prerequisites |
| 39 | + |
| 40 | +- Node.js 24+ (see `.nvmrc`) |
| 41 | +- pnpm 10.18.3+ (specified in `packageManager` field) |
| 42 | + |
| 43 | +### Setup |
| 44 | + |
| 45 | +```bash |
| 46 | +pnpm install |
| 47 | +``` |
| 48 | + |
| 49 | +### Build |
| 50 | + |
| 51 | +```bash |
| 52 | +pnpm build |
| 53 | +``` |
| 54 | + |
| 55 | +Builds the project using `tsdown` (rolldown-based bundler). Output goes to `dist/` directory. |
| 56 | + |
| 57 | +### Development |
| 58 | + |
| 59 | +```bash |
| 60 | +pnpm dev |
| 61 | +``` |
| 62 | + |
| 63 | +Runs the development server with hot reload using Node's `--watch` flag. |
| 64 | + |
| 65 | +### Formatting |
| 66 | + |
| 67 | +```bash |
| 68 | +pnpm format |
| 69 | +``` |
| 70 | + |
| 71 | +Formats code using Prettier. |
| 72 | + |
| 73 | +To check formatting without applying changes: |
| 74 | + |
| 75 | +```bash |
| 76 | +pnpm format --check |
| 77 | +``` |
| 78 | + |
| 79 | +### Testing |
| 80 | + |
| 81 | +```bash |
| 82 | +pnpm test run |
| 83 | +``` |
| 84 | + |
| 85 | +Or with coverage enabled: |
| 86 | + |
| 87 | +```bash |
| 88 | +pnpm test run --coverage |
| 89 | +``` |
| 90 | + |
| 91 | +### Inspector Tool |
| 92 | + |
| 93 | +```bash |
| 94 | +pnpm inspect |
| 95 | +``` |
| 96 | + |
| 97 | +Launches the MCP inspector for debugging the MCP server using the configuration in `.mcp.json`. |
| 98 | + |
| 99 | +## Code Style and Conventions |
| 100 | + |
| 101 | +### TypeScript Configuration |
| 102 | + |
| 103 | +- Uses `@tsconfig/node24` and `@tsconfig/node-ts` as base configs |
| 104 | +- Module system: ESM with `"type": "module"` in package.json |
| 105 | +- Module resolution: `bundler` mode |
| 106 | +- Module format: `preserve` |
| 107 | + |
| 108 | +### Code Style |
| 109 | + |
| 110 | +- Use Prettier for formatting (config: `.prettierignore`) |
| 111 | +- Prefer async/await over callbacks |
| 112 | +- Export types and interfaces explicitly |
| 113 | +- Use descriptive variable and function names |
| 114 | +- **Always include file extensions in imports** (e.g., `import { foo } from './bar.ts'`, not `./bar`), except when importing packages. |
| 115 | + |
| 116 | +### Naming Conventions |
| 117 | + |
| 118 | +- Constants: SCREAMING_SNAKE_CASE (e.g., `LIST_TOOL_NAME`) |
| 119 | +- Functions: camelCase (e.g., `createStorybookMcpHandler`, `addListTool`) |
| 120 | +- Types/Interfaces: PascalCase (e.g., `StorybookMcpHandlerOptions`, `Handler`) |
| 121 | + |
| 122 | +## Important Files |
| 123 | + |
| 124 | +### Configuration Files |
| 125 | + |
| 126 | +- `package.json` - Project metadata and scripts |
| 127 | +- `tsconfig.json` - TypeScript configuration |
| 128 | +- `tsdown.config.ts` - Build tool configuration |
| 129 | +- `.mcp.json` - MCP inspector configuration |
| 130 | +- `.nvmrc` - Node version specification |
| 131 | + |
| 132 | +### Source Files |
| 133 | + |
| 134 | +- `src/index.ts` - Main library entry point (exported API) |
| 135 | +- `src/tools/list.ts` - Tool definitions |
| 136 | +- `serve.ts` - Development server (not included in distribution) |
| 137 | + |
| 138 | +### Build Artifacts |
| 139 | + |
| 140 | +- `dist/` - Build output (gitignored, included in npm package) |
| 141 | +- `pnpm-lock.yaml` - Dependency lock file |
| 142 | + |
| 143 | +## Adding New Tools |
| 144 | + |
| 145 | +To add a new MCP tool: |
| 146 | + |
| 147 | +1. Create a new file in `src/tools/` (e.g., `src/tools/my-tool.ts`) |
| 148 | +2. Export a constant for the tool name |
| 149 | +3. Export an async function that adds the tool to the server: |
| 150 | + ```typescript |
| 151 | + export async function addMyTool(server: McpServer) { |
| 152 | + server.tool( |
| 153 | + { |
| 154 | + name: 'my_tool_name', |
| 155 | + description: 'Tool description', |
| 156 | + }, |
| 157 | + () => ({ |
| 158 | + content: [{ type: 'text', text: 'result' }], |
| 159 | + }), |
| 160 | + ); |
| 161 | + } |
| 162 | + ``` |
| 163 | +4. Import and call the function in `src/index.ts` after `addListTool(server)` |
| 164 | + |
| 165 | +## MCP Protocol |
| 166 | + |
| 167 | +This server implements the Model Context Protocol (MCP) specification: |
| 168 | + |
| 169 | +- **Transport**: HTTP-based transport |
| 170 | +- **Capabilities**: Supports dynamic tool listing (`tools: { listChanged: true }`) |
| 171 | +- **Schema Validation**: Uses Valibot for request/response validation |
| 172 | + |
| 173 | +## Dependencies |
| 174 | + |
| 175 | +### Runtime (Production) |
| 176 | + |
| 177 | +None - this is a library with peer/dev dependencies only. |
| 178 | + |
| 179 | +### Development |
| 180 | + |
| 181 | +- `tmcp` - MCP server implementation |
| 182 | +- `@tmcp/adapter-valibot` - Valibot schema adapter for MCP |
| 183 | +- `@tmcp/transport-http` - HTTP transport for MCP |
| 184 | +- `valibot` - Schema validation |
| 185 | +- `srvx` - HTTP server for development |
| 186 | +- `tsdown` - Build tool |
| 187 | +- `typescript` - TypeScript compiler |
| 188 | + |
| 189 | +## Release Process |
| 190 | + |
| 191 | +The project uses Changesets for version management: |
| 192 | + |
| 193 | +```bash |
| 194 | +pnpm changeset # Create a changeset |
| 195 | +pnpm release # Build and publish to npm |
| 196 | +``` |
| 197 | + |
| 198 | +Releases are automated via GitHub Actions (see `.github/workflows/release.yml`). |
| 199 | + |
| 200 | +## Documentation resources |
| 201 | + |
| 202 | +When working with the MCP server/tools related stuff, refer to the following resources: |
| 203 | + |
| 204 | +- https://github.com/paoloricciuti/tmcp/tree/main/packages/tmcp |
| 205 | +- https://github.com/paoloricciuti/tmcp/tree/main/packages/transport-http |
| 206 | +- https://github.com/paoloricciuti/tmcp |
| 207 | + |
| 208 | +When working on data validation, refer to the following resources: |
| 209 | + |
| 210 | +- https://valibot.dev/ |
| 211 | +- https://github.com/paoloricciuti/tmcp/tree/main/packages/adapter-valibot |
| 212 | + |
| 213 | +## Notes for AI Assistants |
| 214 | + |
| 215 | +- Prefer test-driven development when possible, and continously use test coverage to verify test quality |
| 216 | +- When adding features, prefer minimal changes to existing code |
| 217 | +- Follow the established patterns for tool registration |
| 218 | +- Use TypeScript types from the `tmcp` package |
| 219 | +- Ensure all exports are properly typed |
| 220 | +- Update this file when making significant architectural changes |
| 221 | +- The project uses ESM modules exclusively |
| 222 | +- Build artifacts in `dist/` should not be committed to git |
| 223 | +- When modifying package.json scripts, ensure they work with pnpm |
0 commit comments