$npx -y skills add github/awesome-copilot --skill typescript-mcp-server-generatorGenerate a complete MCP server project in TypeScript with tools, resources, and proper configuration
| 1 | # Generate TypeScript MCP Server |
| 2 | |
| 3 | Create a complete Model Context Protocol (MCP) server in TypeScript with the following specifications: |
| 4 | |
| 5 | ## Requirements |
| 6 | |
| 7 | 1. **Project Structure**: Create a new TypeScript/Node.js project with proper directory structure |
| 8 | 2. **NPM Packages**: Include @modelcontextprotocol/sdk, zod@3, and either express (for HTTP) or stdio support |
| 9 | 3. **TypeScript Configuration**: Proper tsconfig.json with ES modules support |
| 10 | 4. **Server Type**: Choose between HTTP (with Streamable HTTP transport) or stdio-based server |
| 11 | 5. **Tools**: Create at least one useful tool with proper schema validation |
| 12 | 6. **Error Handling**: Include comprehensive error handling and validation |
| 13 | |
| 14 | ## Implementation Details |
| 15 | |
| 16 | ### Project Setup |
| 17 | - Initialize with `npm init` and create package.json |
| 18 | - Install dependencies: `@modelcontextprotocol/sdk`, `zod@3`, and transport-specific packages |
| 19 | - Configure TypeScript with ES modules: `"type": "module"` in package.json |
| 20 | - Add dev dependencies: `tsx` or `ts-node` for development |
| 21 | - Create proper .gitignore file |
| 22 | |
| 23 | ### Server Configuration |
| 24 | - Use `McpServer` class for high-level implementation |
| 25 | - Set server name and version |
| 26 | - Choose appropriate transport (StreamableHTTPServerTransport or StdioServerTransport) |
| 27 | - For HTTP: set up Express with proper middleware and error handling |
| 28 | - For stdio: use StdioServerTransport directly |
| 29 | |
| 30 | ### Tool Implementation |
| 31 | - Use `registerTool()` method with descriptive names |
| 32 | - Define schemas using zod for input and output validation |
| 33 | - Provide clear `title` and `description` fields |
| 34 | - Return both `content` and `structuredContent` in results |
| 35 | - Implement proper error handling with try-catch blocks |
| 36 | - Support async operations where appropriate |
| 37 | |
| 38 | ### Resource/Prompt Setup (Optional) |
| 39 | - Add resources using `registerResource()` with ResourceTemplate for dynamic URIs |
| 40 | - Add prompts using `registerPrompt()` with argument schemas |
| 41 | - Consider adding completion support for better UX |
| 42 | |
| 43 | ### Code Quality |
| 44 | - Use TypeScript for type safety |
| 45 | - Follow async/await patterns consistently |
| 46 | - Implement proper cleanup on transport close events |
| 47 | - Use environment variables for configuration |
| 48 | - Add inline comments for complex logic |
| 49 | - Structure code with clear separation of concerns |
| 50 | |
| 51 | ## Example Tool Types to Consider |
| 52 | - Data processing and transformation |
| 53 | - External API integrations |
| 54 | - File system operations (read, search, analyze) |
| 55 | - Database queries |
| 56 | - Text analysis or summarization (with sampling) |
| 57 | - System information retrieval |
| 58 | |
| 59 | ## Configuration Options |
| 60 | - **For HTTP Servers**: |
| 61 | - Port configuration via environment variables |
| 62 | - CORS setup for browser clients |
| 63 | - Session management (stateless vs stateful) |
| 64 | - DNS rebinding protection for local servers |
| 65 | |
| 66 | - **For stdio Servers**: |
| 67 | - Proper stdin/stdout handling |
| 68 | - Environment-based configuration |
| 69 | - Process lifecycle management |
| 70 | |
| 71 | ## Testing Guidance |
| 72 | - Explain how to run the server (`npm start` or `npx tsx server.ts`) |
| 73 | - Provide MCP Inspector command: `npx @modelcontextprotocol/inspector` |
| 74 | - For HTTP servers, include connection URL: `http://localhost:PORT/mcp` |
| 75 | - Include example tool invocations |
| 76 | - Add troubleshooting tips for common issues |
| 77 | |
| 78 | ## Additional Features to Consider |
| 79 | - Sampling support for LLM-powered tools |
| 80 | - User input elicitation for interactive workflows |
| 81 | - Dynamic tool registration with enable/disable capabilities |
| 82 | - Notification debouncing for bulk updates |
| 83 | - Resource links for efficient data references |
| 84 | |
| 85 | Generate a complete, production-ready MCP server with comprehensive documentation, type safety, and error handling. |