基础模板
基础模板提供了完整的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开发流程
安装依赖
bashnpm install开发模式
bashnpm run dev运行测试
bashnpm test代码检查
bashnpm 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;
// 计算逻辑...
},
};自定义开发
- 在
src/tools/目录中添加新工具 - 在
src/index.ts中注册工具 - 编写测试用例
- 运行
npm run dev进行开发