> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fetchhive.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 自行托管 MCP 网关

> 在本机运行 MCP 网关，让 Cursor、Codex、Claude Code 调用线上 API 或当前分支上的 API

你可以在本地运行 MCP 网关，而不使用托管产品。开源 CLI 会编译 OpenAPI 文档，并通过 Streamable HTTP 或 stdio 提供 MCP 工具。

两种用法同等重要：

* **线上 API** — 编译公开 OpenAPI，把编辑器指向网关。
* **WIP / 分支 API** — 服务已经在本机跑着（`localhost`、Docker 网络）。把网关指到这个源，让 Cursor、Codex 或 Claude Code 直接打你正在改的分支。

## 如何安装？ <a href="#install" id="install" />

项目在 GitHub：[Fetch-Hive/openapi-mcp](https://github.com/Fetch-Hive/openapi-mcp)。可用 Homebrew、Docker、`npx`、curl 安装脚本或 `cargo install`，README 中有当前命令。

## 如何连接本地或分支 API？ <a href="#local-api" id="local-api" />

1. 运行 `mcp-gateway init --allow-private-networks`，允许回环和私有网络。
2. 从仓库里的文件添加规范：`mcp-gateway add-spec --name demo --file ./openapi.yaml --base-url http://127.0.0.1:3000 --insecure-http`。`--base-url` 是当前 checkout，不是生产环境。
3. 启动 `mcp-gateway serve demo`（默认 `http://127.0.0.1:8787/mcp`）。
4. 把 `mcp-gateway inspect demo --client cursor`（或 `codex`、`claude-code`、`vscode`）粘贴到编辑器。`init` 打印的令牌放到 `MCP_GATEWAY_TOKEN`。

访问 localhost 必须加 `--allow-private-networks`。API 走 HTTP 时必须加 `--insecure-http`。

## 如何连接线上 API？ <a href="#live-api" id="live-api" />

1. 运行 `mcp-gateway init`（不要加私有网络标志）。
2. `mcp-gateway add-spec --name demo --url https://…`（或 `--file` 加上 `--base-url https://api.example.com`）。
3. `mcp-gateway serve demo`，再用同样的 `inspect --client` 片段。

`mcp-gateway doctor` 会检查配置、密钥、本地 IR 缓存和 SSRF 策略。

## 能否部署到 Render、Heroku、DigitalOcean 或 Hetzner？

可以。GitHub 仓库提供 Render、Heroku、DigitalOcean App Platform 的指南和一键按钮，以及 Hetzner VPS 的 Compose/cloud-init：[openapi-mcp 部署文档](https://github.com/Fetch-Hive/openapi-mcp/blob/main/docs/deploy/README.md)。部署后把编辑器指向 `https://<host>/mcp`，使用同一个 bearer 令牌。

Vercel 无法运行此服务器（无服务器函数，不是长驻进程）。若不想自己运维 VPS，对**线上** API 请使用[托管 MCP 网关](overview)。托管 MCP 网关无法代理 `localhost` — 分支 API 请在本机使用 CLI。

设置 `MCP_GATEWAY_TOKEN` 和 `MCP_GATEWAY_SPEC_URL`（HTTPS 上的 OpenAPI 文档）。容器会自行读取 `PORT`。

## 与托管 MCP 网关有何不同？

Fetch Hive 上的托管 MCP 网关提供控制台、客户端令牌、套餐配额和公开的 `/g/{slug}` URL，面向线上 API。自行托管时进程和密钥留在你的机器上，也是把编辑器接到本地 checkout 的方式。绑定所有网卡需要 `--expose`。TLS 由你的反向代理负责。

## 相关

* [MCP 网关概览](overview)
* [从 OpenAPI 创建托管 MCP 服务器](create-from-openapi)
* [在智能体中使用托管 MCP 网关](use-with-agents)
