动态MCP服务器

一个动态模型上下文协议(MCP)服务器,允许在运行时添加和移除API端点,并将其作为工具暴露给LLM客户端。

  • by ragingrahul
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 ragingrahul/rest-mcp-backend

使用说明

动态MCP服务器

一个动态模型上下文协议(MCP)服务器,允许您在运行时添加和删除API端点,并将它们作为工具暴露给像Claude这样的大型语言模型(LLM)客户端。

特点/特性

  • 🔄 旋转符号(表示循环、重复或刷新等意思) 动态工具注册动态添加/移除API端点
  • 🌐 表示“互联网”或“世界万维网”。 HTTP REST API通过简单的HTTP调用来管理终端节点
  • 🔌(电源插头/插座) MCP协议通过Streamable HTTP实现完整的MCP服务器
  • 📡(天线/卫星天线) 实时更新当工具发生变化时,客户端会自动收到通知
  • 🛠️ 代表“工具”或“修理工具”的意思。 类型安全使用TypeScript构建,确保类型安全
  • 💰(钱的符号,可表示金钱、财富或价格等概念) 实时区块链支付在Base上即时进行以太坊(ETH)支付,并获得交易确认
  • 👥 表示“人们”或“人群”。 多用户架构开发者创造工具,最终用户支付使用费用
  • 🔐(锁形符号,常用于表示密码或保密) 用户身份验证使用 Supabase 实现基于 JWT 的安全认证
  • ⛓️(铁链) 链上验证所有付款均记录在Base区块链上,并附有交易哈希值

快速入门

安装

npm install

发展

npm run dev

生产

npm run build
npm start

配置

创建一个 .env 文件:

PORT=8080
HOST=0.0.0.0
ENDPOINTS_FILE=endpoints.json  # Optional: Load endpoints from file on startup

预加载终端点

您可以选择创建一个 endpoints.json 在服务器启动时预加载端点的文件:

[
  {
    "name": "get_user",
    "url": "https://api.example.com/users/{id}",
    "method": "GET",
    "description": "Get user by ID",
    "parameters": [
      {
        "name": "id",
        "type": "string",
        "description": "User ID",
        "required": true
      }
    ]
  }
]

见 endpoints.example.json 以查看更多示例。

使用

启动服务器

npm run dev

服务器启动时没有任何初始端点。您可以通过REST API动态添加它们。

添加一个终端节点

curl -X POST http://localhost:8080/api/endpoints \
  -H "Content-Type: application/json" \
  -d '{
    "name": "get_user",
    "url": "https://api.example.com/users/{id}",
    "method": "GET",
    "description": "Get user by ID",
    "parameters": [
      {
        "name": "id",
        "type": "string",
        "description": "User ID",
        "required": true
      }
    ]
  }'

列出终端节点

curl http://localhost:8080/api/endpoints

移除终端节点

curl -X DELETE http://localhost:8080/api/endpoints/get_user

健康检查

curl http://localhost:8080/health

API 参考

REST API 端点

方法 路径 描述
帖子(或发布) /api/endpoints 添加一个新的API端点
GET (翻译为中文可保持原样,因为“GET”是HTTP方法的一种,常用于请求访问服务器上的资源,无需翻译) /api/endpoints 列出所有已配置的端点
删除 /api/endpoints/{name} 移除一个终端节点
GET (翻译为中文可保持原样,因为“GET”是HTTP方法的一种,常用于请求访问服务器上的资源,无需翻译) /health 健康检查

MCP协议

方法 路径 描述
帖子(发布) / MCP协议请求(可流式传输的HTTP)

终端配置

在添加终端节点时,请提供以下配置:

{
  name: string;           // Unique tool name
  url: string;            // API endpoint URL (supports {param} placeholders)
  method: string;         // HTTP method: GET, POST, PUT, PATCH, DELETE
  description: string;    // Tool description for the LLM
  parameters: [           // Array of parameters
    {
      name: string;       // Parameter name
      type: string;       // Type: string, number, boolean, object, array
      description: string;// Parameter description
      required: boolean;  // Whether the parameter is required
      default?: any;      // Optional default value
    }
  ];
  headers?: object;       // Optional HTTP headers
  timeout?: number;       // Optional timeout in seconds (default: 30)
}

示例:添加多个终端节点

# Add a GET endpoint
curl -X POST http://localhost:8080/api/endpoints \
  -H "Content-Type: application/json" \
  -d '{
    "name": "get_weather",
    "url": "https://api.weather.com/v1/weather/{city}",
    "method": "GET",
    "description": "Get weather for a city",
    "parameters": [
      {
        "name": "city",
        "type": "string",
        "description": "City name",
        "required": true
      }
    ]
  }'

# Add a POST endpoint
curl -X POST http://localhost:8080/api/endpoints \
  -H "Content-Type: application/json" \
  -d '{
    "name": "create_user",
    "url": "https://api.example.com/users",
    "method": "POST",
    "description": "Create a new user",
    "parameters": [
      {
        "name": "username",
        "type": "string",
        "description": "Username",
        "required": true
      },
      {
        "name": "email",
        "type": "string",
        "description": "Email address",
        "required": true
      }
    ]
  }'

建筑学

┌─────────────────────────────────────────────┐
│         HTTP Server (Express)               │
├─────────────────────────────────────────────┤
│  REST API          │  MCP Protocol          │
│  /api/endpoints    │  / (Streamable HTTP)   │
└────────┬───────────┴────────┬───────────────┘
         │                    │
         ▼                    ▼
┌─────────────────┐  ┌─────────────────────┐
│ EndpointManager │  │  DynamicMCPServer   │
│                 │  │                     │
│ - Add endpoint  │◄─┤ - Register tools    │
│ - Remove        │  │ - Notify clients    │
│ - List          │  │ - Handle tool calls │
│ - Call API      │  │                     │
└─────────────────┘  └─────────────────────┘

项目结构

src/
├── server.ts                  # Main HTTP server
├── mcp/
│   ├── DynamicMCPServer.ts   # MCP server implementation
│   └── EndpointManager.ts    # Endpoint management
└── types/
    ├── api.types.ts          # API-related types
    ├── mcp.types.ts          # MCP-related types
    └── index.ts              # Type exports

发展

运行测试

npm test

Linting(在编程领域,通常指代码检查或代码规范检查的过程)

npm run lint

格式化

npm run format

许可证

ISC(Internet Service Center,互联网服务中心)

贡献;做出贡献

欢迎贡献!请随时提交拉取请求。