Skip to content

基础模板

基础模板提供了完整的MCP开发环境,适合学习和日常开发。

特性

  • ✅ 基本的MCP服务器结构
  • ✅ TypeScript配置
  • ✅ Jest测试框架
  • ✅ ESLint代码检查
  • ✅ 示例工具(问候、计算器)

项目结构

project/
├── src/
│   ├── index.ts              # 主入口文件
│   ├── tools/                # 工具定义
│   │   ├── hello.ts         # 问候工具
│   │   └── calculator.ts    # 计算器工具
│   └── utils/               # 工具函数
│       └── toolRegistry.ts  # 工具注册器
├── __tests__/               # 测试文件
│   └── tools.test.ts       # 工具测试
├── package.json            # 项目配置
├── tsconfig.json           # TypeScript配置
└── README.md               # 项目说明

创建项目

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

开发流程

  1. 安装依赖

    bash
    npm install
  2. 开发模式

    bash
    npm run dev
  3. 运行测试

    bash
    npm test
  4. 代码检查

    bash
    npm run lint

示例工具

问候工具

typescript
export const helloTool = {
  name: 'hello',
  description: '一个简单的问候工具',
  inputSchema: {
    type: 'object',
    properties: {
      name: {
        type: 'string',
        description: '要问候的人的名字',
      },
    },
    required: ['name'],
  },
  handler: async (args: any) => {
    const name = args.name || 'World';
    return {
      content: [
        {
          type: 'text',
          text: `Hello, ${name}! 欢迎使用MCP服务器!`,
        },
      ],
    };
  },
};

计算器工具

typescript
export const calculatorTool = {
  name: 'calculator',
  description: '简单的计算器工具',
  inputSchema: {
    type: 'object',
    properties: {
      operation: {
        type: 'string',
        enum: ['add', 'subtract', 'multiply', 'divide'],
        description: '运算类型',
      },
      a: { type: 'number', description: '第一个数字' },
      b: { type: 'number', description: '第二个数字' },
    },
    required: ['operation', 'a', 'b'],
  },
  handler: async (args: any) => {
    const { operation, a, b } = args;
    // 计算逻辑...
  },
};

自定义开发

  1. src/tools/ 目录中添加新工具
  2. src/index.ts 中注册工具
  3. 编写测试用例
  4. 运行 npm run dev 进行开发

下一步

基于 MIT 许可证发布