Skip to content

高级模板

高级模板提供了完整的生产级MCP项目架构,适合复杂项目和生产环境。

特性

  • ✅ 完整的项目架构
  • ✅ Winston日志系统
  • ✅ 配置管理
  • ✅ 工具注册器
  • ✅ Joi输入验证
  • ✅ 更多示例工具(文件管理、天气、数据库)

项目结构

project/
├── src/
│   ├── index.ts              # 主入口文件
│   ├── tools/                # 工具定义
│   │   ├── hello.ts         # 问候工具
│   │   ├── calculator.ts    # 计算器工具
│   │   ├── fileManager.ts   # 文件管理工具
│   │   ├── weather.ts       # 天气查询工具
│   │   └── database.ts      # 数据库工具
│   └── utils/               # 工具函数
│       ├── logger.ts        # 日志系统
│       ├── configManager.ts # 配置管理
│       └── toolRegistry.ts  # 工具注册器
├── logs/                    # 日志文件
├── config.json              # 配置文件
├── env.example              # 环境变量示例
├── package.json             # 项目配置
├── tsconfig.json            # TypeScript配置
└── README.md                # 项目说明

创建项目

bash
gm-mcp init my-project --template advanced

核心功能

日志系统

使用Winston进行日志管理:

typescript
import winston from 'winston';

export const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.errors({ stack: true }),
    winston.format.json()
  ),
  transports: [
    new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
    new winston.transports.File({ filename: 'logs/combined.log' }),
  ],
});

配置管理

typescript
export class ConfigManager {
  private config: Record<string, any> = {};

  async loadConfig(): Promise<void> {
    // 加载配置逻辑
  }

  get(key: string, defaultValue?: any): any {
    return this.config[key] ?? defaultValue;
  }

  set(key: string, value: any): void {
    this.config[key] = value;
  }
}

工具注册器

typescript
export class ToolRegistry {
  private tools: Map<string, Tool> = new Map();

  register(tool: Tool): void {
    this.tools.set(tool.name, tool);
  }

  getTool(name: string): Tool | undefined {
    return this.tools.get(name);
  }

  getAllTools(): Tool[] {
    return Array.from(this.tools.values());
  }
}

示例工具

文件管理工具

typescript
export const fileManagerTool = {
  name: 'file_manager',
  description: '文件管理工具',
  inputSchema: {
    type: 'object',
    properties: {
      action: {
        type: 'string',
        enum: ['read', 'write', 'exists', 'create_dir'],
        description: '操作类型',
      },
      path: { type: 'string', description: '文件或目录路径' },
      content: { type: 'string', description: '要写入的内容' },
    },
    required: ['action', 'path'],
  },
  handler: async (args: any) => {
    // 文件操作逻辑
  },
};

天气查询工具

typescript
export const weatherTool = {
  name: 'weather',
  description: '天气查询工具(模拟)',
  inputSchema: {
    type: 'object',
    properties: {
      city: { type: 'string', description: '城市名称' },
    },
    required: ['city'],
  },
  handler: async (args: any) => {
    // 天气查询逻辑
  },
};

环境配置

创建 .env 文件:

bash
# 服务器配置
NODE_ENV=development
LOG_LEVEL=info

# 数据库配置
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mcp_db

# API配置
API_KEY=your_api_key_here

开发流程

  1. 安装依赖

    bash
    npm install
  2. 配置环境

    bash
    cp env.example .env
    # 编辑 .env 文件
  3. 开发模式

    bash
    npm run dev
  4. 运行测试

    bash
    npm test

生产部署

  1. 构建项目

    bash
    npm run build
  2. 启动服务

    bash
    npm start

下一步

基于 MIT 许可证发布