MCP服务器模板

一个基于Node.js的MCP服务器模板,支持多种传输协议和开发工具,适用于构建MCP兼容的服务器应用。

  • by Critical-Heros
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 Critical-Heros/critical-heros

使用说明

Node.js MCP服务器模板

Node.js应用程序的模型上下文协议(MCP)服务器模板。此模板为构建支持多种传输协议的MCP兼容服务器提供了基础。

特性

  • 多种传输协议:

    • 命令行界面的标准I/O(stdio)
    • web应用程序的HTTP流传输
    • 服务器发送事件(SSE)用于实时更新
  • TypeScript支持:使用TypeScript构建,以实现类型安全和更好的开发人员体验

  • 开发工具:

    • 开发过程中的热重新加载
    • 与MCP Inspector集成进行调试
    • ESLint和Prettier用于代码质量
    • Husky和lint为Git钩子准备好了
  • 可扩展工具系统:轻松注册自定义MCP工具

先决条件

  • Node.js(推荐最新LTS版本)
  • npm或纱线

安装

npm install

发展

# Start development server with stdio transport
npm run dev

# Start development server with web transport
npm run dev:web

使用dev:web时,服务器将在以下位置可用:

  • 可流式传输的HTTP端点:http://localhost:8401/mcp
  • SSE端点:http://localhost:8401/sse

建筑

npm run build

构建输出将位于构建目录中。

用法

命令行

# Start with stdio transport (default)
node build/index.js

# Start with web transport
node build/index.js web --port 8401

添加自定义工具

可以在src/tools/index.ts文件中添加自定义工具:

server.registerTool(
  'YourToolName',
  {
    title: 'Your Tool Name',
    description: 'Tool description',
    inputSchema: {
      param1: z.string().describe('Parameter description'),
    },
  },
  ({ param1 }) => {
    // Implement tool logic
    return {
      content: [
        {
          type: 'text',
          text: `Your tool response: ${param1}`,
        },
      ],
    }
  },
)

环境变量

  • PORT:指定web传输的端口(默认值:8401)
  • NODE_ENV:设置为生产构建或本地开发

许可证

麻省理工学院