typescript-mcp-server-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Generate 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

需求

  1. Project Structure: Create a new TypeScript/Node.js project with proper directory structure
  2. NPM Packages: Include @modelcontextprotocol/sdk, zod@3, and either express (for HTTP) or stdio support
  3. TypeScript Configuration: Proper tsconfig.json with ES modules support
  4. Server Type: Choose between HTTP (with Streamable HTTP transport) or stdio-based server
  5. Tools: Create at least one useful tool with proper schema validation
  6. Error Handling: Include comprehensive error handling and validation
  1. 项目结构:创建一个具有合理目录结构的全新TypeScript/Node.js项目
  2. NPM包:包含@modelcontextprotocol/sdk、zod@3,以及express(用于HTTP)或stdio支持
  3. TypeScript配置:带有ES模块支持的正确tsconfig.json
  4. 服务器类型:在HTTP(支持Streamable HTTP传输)或基于stdio的服务器中选择其一
  5. 工具:创建至少一个带有正确schema校验的实用工具
  6. 错误处理:包含全面的错误处理和校验机制

Implementation Details

实现细节

Project Setup

项目搭建

  • Initialize with
    npm init
    and create package.json
  • Install dependencies:
    @modelcontextprotocol/sdk
    ,
    zod@3
    , and transport-specific packages
  • Configure TypeScript with ES modules:
    "type": "module"
    in package.json
  • Add dev dependencies:
    tsx
    or
    ts-node
    for development
  • Create proper .gitignore file
  • 使用
    npm init
    初始化并创建package.json
  • 安装依赖:
    @modelcontextprotocol/sdk
    zod@3
    以及传输相关的包
  • 在package.json中配置TypeScript以支持ES模块:
    "type": "module"
  • 添加开发依赖:
    tsx
    ts-node
    用于开发
  • 创建合适的.gitignore文件

Server Configuration

服务器配置

  • Use
    McpServer
    class for high-level implementation
  • 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
    registerTool()
    method with descriptive names
  • Define schemas using zod for input and output validation
  • Provide clear
    title
    and
    description
    fields
  • Return both
    content
    and
    structuredContent
    in results
  • Implement proper error handling with try-catch blocks
  • Support async operations where appropriate
  • 使用
    registerTool()
    方法并设置描述性名称
  • 使用zod定义输入和输出校验的schema
  • 提供清晰的
    title
    description
    字段
  • 在结果中同时返回
    content
    structuredContent
  • 使用try-catch块实现正确的错误处理
  • 酌情支持异步操作

Resource/Prompt Setup (Optional)

资源/提示设置(可选)

  • Add resources using
    registerResource()
    with ResourceTemplate for dynamic URIs
  • Add prompts using
    registerPrompt()
    with argument schemas
  • Consider adding completion support for better UX
  • 使用带ResourceTemplate的
    registerResource()
    添加资源以支持动态URI
  • 使用带参数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 (
    npm start
    or
    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服务器,包含全面的文档、类型安全和错误处理。