MCP服务器模板

一个用于构建基于Model Context Protocol (MCP)的服务器的TypeScript模板,提供开发、测试和部署的完整工作流。

  • by matracey
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 matracey/mcp-server-template

使用说明

MCP服务器模板

建筑入门模板 主控程序 TypeScript中的(模型上下文协议)服务器

所得

  • 一个可用的MCP服务器及其示例 hello 工具
  • 带有ES模块的TypeScript
  • 使用Vitest进行测试
  • Linting(ESLint)和格式化(Prettier)
  • CI管道和npm发布工作流
  • VS代码、Claude桌面和游标配置示例
┌─────────────────┐     ┌────────────┐     ┌──────────────────┐
│  AI Assistant    │────▶│ MCP Client │────▶│ Your MCP Server  │
│ (Copilot/Claude) │◀────│            │◀────│                  │
└─────────────────┘     └────────────┘     └──────────────────┘

快速开始

1.创建您的项目

npx -y degit matracey/mcp-server-template my-mcp-server
cd my-mcp-server
npm install

2.自定义

  • 更新 name, description,以及 bin 在 package.json
  • 替换示例 hello 工具在 src/server.ts 用你自己的工具
  • 更新中的服务器名称/版本 src/server.ts

3.构建和测试

npm run build
npm test

4.在本地运行

npm run dev    # Run with auto-restart on file changes
npm start      # Run the built version

添加工具

编辑 src/server.ts 并注册新工具:

server.tool(
  'my_tool',
  'Description of what this tool does.',
  {
    param: z.string().describe('Parameter description'),
  },
  async ({ param }) => {
    return {
      content: [{ type: 'text' as const, text: `Result: ${param}` }],
    }
  }
)

模板包括一个示例 hello 工具来演示模式。

客户端配置

发布(或在本地运行)后,配置您的AI客户端:

VS代码(GitHub副本)

添加 .vscode/mcp.json:

{
  "servers": {
    "my-server": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "my-mcp-server"]
    }
  }
}

克劳德桌面版

添加 claude_desktop_config.json:

{
  "mcpServers": {
    "my-server": {
      "command": "npx",
      "args": ["-y", "my-mcp-server"]
    }
  }
}

光标

添加 .cursor/mcp.json:

{
  "mcpServers": {
    "my-server": {
      "command": "npx",
      "args": ["-y", "my-mcp-server"]
    }
  }
}

替换 my-mcp-server 使用您发布的包名称。

项目结构

src/
├── index.ts          # Entry point — stdio transport setup
├── server.ts         # MCP server & tool definitions
├── __tests__/        # Tests (vitest)
└── data/             # Static data (if needed)
scripts/              # Dev-time scripts

发展

先决条件

  • Node.js>=20
  • npm

命令

npm run build           # TypeScript compilation
npm test                # Run tests
npm run test:watch      # Watch mode
npm run test:coverage   # With coverage report
npm run dev             # Run with auto-restart on changes
npm run validate        # Run all CI checks (format, lint, build, test)

贡献

欢迎投稿!请参阅 贡献.md 作为指导方针。

许可证

麻省理工学院——见 许可证 了解详情。