|
| 1 | +# MCP TypeScript Server Template |
| 2 | + |
| 3 | +A template repository for building Model Context Protocol (MCP) servers with TypeScript. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🚀 Full TypeScript support with strict mode |
| 8 | +- 🧪 Testing setup with Vitest and coverage reporting |
| 9 | +- 📦 Automated releases with semantic-release |
| 10 | +- 🔄 CI/CD pipelines with GitHub Actions |
| 11 | +- 🏗️ Modular architecture for easy extension |
| 12 | +- 📝 Comprehensive documentation and examples |
| 13 | +- 🛠️ Development tools: Biome for linting/formatting, Husky for Git hooks |
| 14 | +- 🎯 Pre-configured for MCP server development |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +### Using GitHub Template |
| 19 | + |
| 20 | +1. Click "Use this template" button on GitHub |
| 21 | +2. Clone your new repository |
| 22 | +3. Install dependencies: `yarn install` |
| 23 | +4. Start development: `yarn dev` |
| 24 | + |
| 25 | +### Manual Setup |
| 26 | + |
| 27 | +```bash |
| 28 | +# Clone the template |
| 29 | +git clone https://github.com/Mearman/mcp-template.git my-mcp-server |
| 30 | +cd my-mcp-server |
| 31 | + |
| 32 | +# Install dependencies |
| 33 | +yarn install |
| 34 | + |
| 35 | +# Start development |
| 36 | +yarn dev |
| 37 | +``` |
| 38 | + |
| 39 | +## Project Structure |
| 40 | + |
| 41 | +``` |
| 42 | +src/ |
| 43 | +├── index.ts # MCP server entry point |
| 44 | +├── tools/ # Tool implementations |
| 45 | +│ └── example.ts # Example tool |
| 46 | +├── utils/ # Shared utilities |
| 47 | +│ └── validation.ts # Input validation helpers |
| 48 | +└── types.ts # TypeScript type definitions |
| 49 | +``` |
| 50 | + |
| 51 | +## Development |
| 52 | + |
| 53 | +```bash |
| 54 | +# Install dependencies |
| 55 | +yarn install |
| 56 | + |
| 57 | +# Development with hot reload |
| 58 | +yarn dev |
| 59 | + |
| 60 | +# Build TypeScript to JavaScript |
| 61 | +yarn build |
| 62 | + |
| 63 | +# Run production build |
| 64 | +yarn start |
| 65 | + |
| 66 | +# Run tests |
| 67 | +yarn test |
| 68 | + |
| 69 | +# Run tests in watch mode |
| 70 | +yarn test:watch |
| 71 | + |
| 72 | +# Run tests with coverage |
| 73 | +yarn test:coverage |
| 74 | + |
| 75 | +# Lint and format code |
| 76 | +yarn lint |
| 77 | +yarn format |
| 78 | +``` |
| 79 | + |
| 80 | +## Creating Your MCP Server |
| 81 | + |
| 82 | +### 1. Define Your Tools |
| 83 | + |
| 84 | +Create tool implementations in `src/tools/`: |
| 85 | + |
| 86 | +```typescript |
| 87 | +// src/tools/my-tool.ts |
| 88 | +import { z } from 'zod'; |
| 89 | + |
| 90 | +const MyToolSchema = z.object({ |
| 91 | + input: z.string().describe('Tool input'), |
| 92 | +}); |
| 93 | + |
| 94 | +export async function myTool(args: unknown) { |
| 95 | + const { input } = MyToolSchema.parse(args); |
| 96 | + |
| 97 | + // Tool implementation |
| 98 | + return { |
| 99 | + success: true, |
| 100 | + result: `Processed: ${input}`, |
| 101 | + }; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### 2. Register Tools in Server |
| 106 | + |
| 107 | +Update `src/index.ts` to register your tools: |
| 108 | + |
| 109 | +```typescript |
| 110 | +server.setRequestHandler(ListToolsRequestSchema, async () => ({ |
| 111 | + tools: [ |
| 112 | + { |
| 113 | + name: 'my_tool', |
| 114 | + description: 'Description of what my tool does', |
| 115 | + inputSchema: zodToJsonSchema(MyToolSchema), |
| 116 | + }, |
| 117 | + ], |
| 118 | +})); |
| 119 | + |
| 120 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 121 | + switch (request.params.name) { |
| 122 | + case 'my_tool': |
| 123 | + return await myTool(request.params.arguments); |
| 124 | + default: |
| 125 | + throw new Error(`Unknown tool: ${request.params.name}`); |
| 126 | + } |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +### 3. Configure for Claude Desktop |
| 131 | + |
| 132 | +Add to `claude_desktop_config.json`: |
| 133 | + |
| 134 | +```json |
| 135 | +{ |
| 136 | + "mcpServers": { |
| 137 | + "my-mcp-server": { |
| 138 | + "command": "node", |
| 139 | + "args": ["/path/to/my-mcp-server/dist/index.js"], |
| 140 | + "env": {} |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Testing |
| 147 | + |
| 148 | +Write tests for your tools in `src/tools/*.test.ts`: |
| 149 | + |
| 150 | +```typescript |
| 151 | +import { describe, it, expect } from 'vitest'; |
| 152 | +import { myTool } from './my-tool'; |
| 153 | + |
| 154 | +describe('myTool', () => { |
| 155 | + it('should process input correctly', async () => { |
| 156 | + const result = await myTool({ input: 'test' }); |
| 157 | + expect(result.success).toBe(true); |
| 158 | + expect(result.result).toBe('Processed: test'); |
| 159 | + }); |
| 160 | +}); |
| 161 | +``` |
| 162 | + |
| 163 | +## Publishing |
| 164 | + |
| 165 | +### NPM Package |
| 166 | + |
| 167 | +1. Update `package.json` with your package details |
| 168 | +2. Build: `yarn build` |
| 169 | +3. Publish: `npm publish` |
| 170 | + |
| 171 | +### Automated Releases |
| 172 | + |
| 173 | +This template includes semantic-release for automated versioning and publishing: |
| 174 | + |
| 175 | +1. Follow [conventional commits](https://www.conventionalcommits.org/) |
| 176 | +2. Push to main branch |
| 177 | +3. CI/CD will automatically: |
| 178 | + - Determine version bump |
| 179 | + - Update CHANGELOG.md |
| 180 | + - Create GitHub release |
| 181 | + - Publish to npm |
| 182 | + |
| 183 | +## Best Practices |
| 184 | + |
| 185 | +1. **Input Validation**: Always validate tool inputs using Zod schemas |
| 186 | +2. **Error Handling**: Provide clear error messages for debugging |
| 187 | +3. **Testing**: Write comprehensive tests for all tools |
| 188 | +4. **Documentation**: Document each tool's purpose and usage |
| 189 | +5. **Type Safety**: Leverage TypeScript's type system fully |
| 190 | + |
| 191 | +## License |
| 192 | + |
| 193 | +MIT |
| 194 | + |
| 195 | +## Contributing |
| 196 | + |
| 197 | +Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository. |
| 198 | + |
| 199 | +## Resources |
| 200 | + |
| 201 | +- [Model Context Protocol Documentation](https://modelcontextprotocol.io) |
| 202 | +- [MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk) |
| 203 | +- [Creating MCP Servers Guide](https://modelcontextprotocol.io/tutorials/building-servers) |
0 commit comments