实时通信服务器

MCP Express Server 是一个基于 TypeScript 和 Express.js 的实时通信服务器,支持 MCP 协议和 Server-Sent Events (SSE)。

  • by pr0methevs
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 pr0methevs/mcp-template

使用说明

MCP Express服务器

使用TypeScript、Express.js和服务器发送事件(SSE)进行实时通信的模型上下文协议(MCP)服务器实现。

特性

  • MCP协议支持:实现模型上下文协议规范
  • 苏格兰和南方能源公司运输:使用服务器发送事件进行实时通信
  • TypeScript:完全打字,以获得更好的开发人员体验
  • Express.js:强大的HTTP服务器框架
  • 环境配置:使用dotenv进行配置管理

项目结构

src/
├── index.ts    # Main entry point and Express server setup
├── server.ts   # MCP protocol handling and SSE transport layer
├── tools.ts    # Tool implementations and business logic
└── types.ts    # TypeScript type definitions

安装

npm install

配置

创建一个 .env 根目录中的文件:

cp .env.example .env

编辑 .env 根据您的配置:

PORT=3000
HOST=0.0.0.0
SERVER_NAME=mcp-express-server
SERVER_VERSION=1.0.0

发展

在开发模式下运行并自动重新加载:

npm run dev

构建

将TypeScript编译为JavaScript:

npm run build

生产

运行已编译的服务器:

npm start

API终点

GET /

服务器信息和可用端点

GET /health

健康检查端点

  • 返回服务器状态和活动连接计数

GET /sse

服务器发送事件端点

  • 为实时通知建立持久连接
  • 客户端应在此处连接以接收服务器更新

POST /message

MCP消息端点

  • 发送JSON-RPC 2.0格式的MCP请求
  • 接收MCP响应

可用工具

服务器附带了三个示例工具:

  1. 回声:回复一条消息
  2. 添加:将两个数字相加
  3. 获取时间:返回当前服务器时间

MCP协议使用

初始化连接

curl -X POST http://localhost:3000/message \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2024-11-05",
      "capabilities": {},
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

列出可用工具

curl -X POST http://localhost:3000/message \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/list"
  }'

调用工具

curl -X POST http://localhost:3000/message \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "add",
      "arguments": {
        "a": 5,
        "b": 3
      }
    }
  }'

添加新工具

编辑 src/tools.ts 添加新工具:

export const availableTools: Tool[] = [
  // ... existing tools
  {
    name: 'your_tool',
    description: 'Description of your tool',
    inputSchema: {
      type: 'object',
      properties: {
        param1: {
          type: 'string',
          description: 'Parameter description'
        }
      },
      required: ['param1']
    }
  }
];

// Add implementation in executeTool function
case 'your_tool':
  return {
    content: [{
      type: 'text',
      text: `Result: ${args.param1}`
    }]
  };

许可证

麻省理工学院