typescript-mcp-server-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerate TypeScript MCP Server
生成TypeScript MCP服务器
Create a complete Model Context Protocol (MCP) server in TypeScript with the following specifications:
创建一个符合以下规范的完整TypeScript版Model Context Protocol (MCP)服务器:
Requirements
需求
- Project Structure: Create a new TypeScript/Node.js project with proper directory structure
- NPM Packages: Include @modelcontextprotocol/sdk, zod@3, and either express (for HTTP) or stdio support
- TypeScript Configuration: Proper tsconfig.json with ES modules support
- Server Type: Choose between HTTP (with Streamable HTTP transport) or stdio-based server
- Tools: Create at least one useful tool with proper schema validation
- Error Handling: Include comprehensive error handling and validation
- 项目结构:创建一个具有合理目录结构的全新TypeScript/Node.js项目
- NPM包:包含@modelcontextprotocol/sdk、zod@3,以及express(用于HTTP)或stdio支持
- TypeScript配置:带有ES模块支持的正确tsconfig.json
- 服务器类型:在HTTP(支持Streamable HTTP传输)或基于stdio的服务器中选择其一
- 工具:创建至少一个带有正确schema校验的实用工具
- 错误处理:包含全面的错误处理和校验机制
Implementation Details
实现细节
Project Setup
项目搭建
- Initialize with and create package.json
npm init - Install dependencies: ,
@modelcontextprotocol/sdk, and transport-specific packageszod@3 - Configure TypeScript with ES modules: in package.json
"type": "module" - Add dev dependencies: or
tsxfor developmentts-node - Create proper .gitignore file
- 使用初始化并创建package.json
npm init - 安装依赖:、
@modelcontextprotocol/sdk以及传输相关的包zod@3 - 在package.json中配置TypeScript以支持ES模块:
"type": "module" - 添加开发依赖:或
tsx用于开发ts-node - 创建合适的.gitignore文件
Server Configuration
服务器配置
- Use class for high-level implementation
McpServer - Set server name and version
- Choose appropriate transport (StreamableHTTPServerTransport or StdioServerTransport)
- For HTTP: set up Express with proper middleware and error handling
- For stdio: use StdioServerTransport directly
- 使用类进行高层实现
McpServer - 设置服务器名称和版本
- 选择合适的传输方式(StreamableHTTPServerTransport或StdioServerTransport)
- 对于HTTP:使用合适的中间件和错误处理搭建Express
- 对于stdio:直接使用StdioServerTransport
Tool Implementation
工具实现
- Use method with descriptive names
registerTool() - Define schemas using zod for input and output validation
- Provide clear and
titlefieldsdescription - Return both and
contentin resultsstructuredContent - Implement proper error handling with try-catch blocks
- Support async operations where appropriate
- 使用方法并设置描述性名称
registerTool() - 使用zod定义输入和输出校验的schema
- 提供清晰的和
title字段description - 在结果中同时返回和
contentstructuredContent - 使用try-catch块实现正确的错误处理
- 酌情支持异步操作
Resource/Prompt Setup (Optional)
资源/提示设置(可选)
- Add resources using with ResourceTemplate for dynamic URIs
registerResource() - Add prompts using with argument schemas
registerPrompt() - Consider adding completion support for better UX
- 使用带ResourceTemplate的添加资源以支持动态URI
registerResource() - 使用带参数schema的添加提示
registerPrompt() - 考虑添加补全支持以提升用户体验
Code Quality
代码质量
- Use TypeScript for type safety
- Follow async/await patterns consistently
- Implement proper cleanup on transport close events
- Use environment variables for configuration
- Add inline comments for complex logic
- Structure code with clear separation of concerns
- 使用TypeScript保证类型安全
- 始终遵循async/await模式
- 在传输关闭事件时实现正确的清理操作
- 使用环境变量进行配置
- 为复杂逻辑添加内联注释
- 通过清晰的关注点分离来组织代码
Example Tool Types to Consider
可考虑的工具类型示例
- Data processing and transformation
- External API integrations
- File system operations (read, search, analyze)
- Database queries
- Text analysis or summarization (with sampling)
- System information retrieval
- 数据处理与转换
- 外部API集成
- 文件系统操作(读取、搜索、分析)
- 数据库查询
- 文本分析或摘要(支持采样)
- 系统信息检索
Configuration Options
配置选项
-
For HTTP Servers:
- Port configuration via environment variables
- CORS setup for browser clients
- Session management (stateless vs stateful)
- DNS rebinding protection for local servers
-
For stdio Servers:
- Proper stdin/stdout handling
- Environment-based configuration
- Process lifecycle management
-
对于HTTP服务器:
- 通过环境变量配置端口
- 为浏览器客户端设置CORS
- 会话管理(无状态 vs 有状态)
- 为本地服务器设置DNS重绑定保护
-
对于stdio服务器:
- 正确处理stdin/stdout
- 基于环境的配置
- 进程生命周期管理
Testing Guidance
测试指南
- Explain how to run the server (or
npm start)npx tsx server.ts - Provide MCP Inspector command:
npx @modelcontextprotocol/inspector - For HTTP servers, include connection URL:
http://localhost:PORT/mcp - Include example tool invocations
- Add troubleshooting tips for common issues
- 说明如何运行服务器(或
npm start)npx tsx server.ts - 提供MCP Inspector命令:
npx @modelcontextprotocol/inspector - 对于HTTP服务器,包含连接URL:
http://localhost:PORT/mcp - 提供工具调用示例
- 添加常见问题的故障排除提示
Additional Features to Consider
可考虑的额外功能
- Sampling support for LLM-powered tools
- User input elicitation for interactive workflows
- Dynamic tool registration with enable/disable capabilities
- Notification debouncing for bulk updates
- Resource links for efficient data references
Generate a complete, production-ready MCP server with comprehensive documentation, type safety, and error handling.
- 为LLM驱动的工具提供采样支持
- 为交互式工作流程添加用户输入引导
- 支持动态工具注册及启用/禁用功能
- 批量更新的通知防抖
- 用于高效数据引用的资源链接
生成一个完整的、可用于生产环境的MCP服务器,包含全面的文档、类型安全和错误处理。