M

MCP网关

@Timandes/mcp-gateway
0 Stars 29 次浏览 Timandes 更新于 2026-08-23

一种将模型上下文协议(MCP)工具调用转换为传统HTTP API请求的服务器,通过可配置的映射,允许现有的HTTP API集成到MCP领域中。

MCP 服务配置

复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用

{
  "mcpServers": {
    "mcp-gateway": {
      "args": [
        "run",
        "mcp-gateway",
        "--transport=stdio"
      ],
      "command": "uv"
    }
  }
}

服务介绍

MCP 网关

MCP (Model Context Protocol) 网关可以将 MCP 工具调用转换为传统的 HTTP API 请求。它可以提供一种可配置的方式来让现有的 HTTP API 进入 MCP 领域。

入门指南

config.example.yaml 创建配置文件:

$ cp config.example.yaml config.yaml

编辑 config.yaml 文件,将所有 API 映射到 MCP 工具。

然后使用 SSE 传输启动它:

$ uv run mcp-gateway
INFO:     Started server process [15400]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:3001 (Press CTRL+C to quit)

默认端口是 3001。

服务器控制

更改端口

在命令行中提供参数 --port=<port_no> 将更改 SSE 传输的端口。

使用 3002 端口启动网关:

$ uv run mcp-gateway --port=3002
INFO:     Started server process [15400]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:3002 (Press CTRL+C to quit)

stdio 传输

在命令行中提供参数 --transport=stdio 将更改为 stdio 传输。

例如:

$ uv run mcp-gateway --transport=stdio

手动以 stdio 传输方式启动网关是没有意义的。你可以在 Cursor 或 Cline 中进行如下配置:

{
    "mcpServers": {
        "mcp-gateway": {
          "command": "uv",
          "args": ["run", "mcp-gateway", "--transport=stdio"]
        }
      }
}

或者通过 MCP Inspector 使用表单值进行配置:

项目
传输类型 STDIO
命令 uv
参数 run mcp-gateway --transport=stdio

配置文件

配置 YAML 文件包含两个部分:servertoolsserver 定义了网关服务器使用的基信息。tools 定义了从单一 MCP 工具到 HTTP API 请求的映射。

server:
  name: rest-amap-server
  config:
    apiKey: foo
tools:
- name: maps-geo
  description: "将详细的结构化地址转换为经纬度坐标。支持对地标性名胜景区、建筑物名称解析为经纬度坐标"
  args:
  - name: address
    description: "待解析的结构化地址信息"
    required: true
  - name: city
    description: "指定查询的城市"
    required: false
  requestTemplate:
    url: "https://restapi.amap.com/v3/geocode/geo?key={{.config.apiKey}}&address={{.args.address}}&city={{.args.city}}&source=ts_mcp"
    method: GET
    headers:
    - key: x-api-key
      value: "{{.config.apiKey}}"
    - key: Content-Type
      value: application/json
  responseTemplate:
    body: |
      # 地理编码信息
      {{- range $index, $geo := .Geocodes }}
      ## 地点 {{add $index 1}}

      - **国家**: {{ $geo.Country }}
      - **省份**: {{ $geo.Province }}
      - **城市**: {{ $geo.City }}
      - **城市代码**: {{ $geo.Citycode }}
      - **区/县**: {{ $geo.District }}
      - **街道**: {{ $geo.Street }}
      - **门牌号**: {{ $geo.Number }}
      - **行政编码**: {{ $geo.Adcode }}
      - **坐标**: {{ $geo.Location }}
      - **级别**: {{ $geo.Level }}
      {{- end }}

服务器

项目 描述
name 服务器名称
config 可以通过模板中的变量 {{.config.xxx}} 引用的键/值对

工具

tools 是 MCP 工具映射列表。单个工具属性定义如下:

项目 描述
name 工具名称(函数名),提供给 LLM。
description 工具描述,通过此描述LLM可以理解该工具的功能。
args 工具参数(函数参数)。
requestTemplate 到目标HTTP API的请求映射。
responseTemplate 目标HTTP API响应的响应映射。

单个参数属性定义如下:

项目 类型 描述
name 参数名称,提供给 LLM。
description 参数描述,通过此描述LLM可以理解和决定应填写什么值。
required 布尔值 是否为必填参数。

请求模板属性定义如下:

项目 描述
method HTTP 方法
url 目标 HTTP API URL 模板
headers HTTP 头部

HTTP 头部定义如下:

项目 描述
key 头部键
value 头部值模板

响应模板属性定义如下:

项目 描述
body 响应体模板

贡献

欢迎各种形式的贡献。