api-gateway-mcp-server
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAPI Gateway Remote MCP Server (Public Preview)
API Gateway 远程 MCP 服务器(Public Preview)
This skill converts new or existing REST APIs on Google Cloud API Gateway into remote Model Context Protocol (MCP) servers (, Public Preview). By annotating an OpenAPI 3.x (3.0.x or 3.1.x) specification with and , API Gateway translates MCP JSON-RPC 2.0 requests (, , , ) into standard HTTP REST requests to your backend while enforcing existing authentication and quota policies.
POST https://{default_hostname}/mcpx-google-api-management.mcpx-google-mcp-toolinitializenotifications/initializedtools/listtools/call此 skill 可将 Google Cloud API Gateway 上新建或现有的 REST API 转换为远程 Model Context Protocol (MCP) 服务器(,Public Preview)。通过在 OpenAPI 3.x(3.0.x 或 3.1.x)规范中使用 和 进行标注,API Gateway 会将 MCP JSON-RPC 2.0 请求(、、、)转换为发往后端的标准 HTTP REST 请求,同时执行现有的身份验证和配额策略。
POST https://{default_hostname}/mcpx-google-api-management.mcpx-google-mcp-toolinitializenotifications/initializedtools/listtools/callPrerequisites
前置条件
- Install the Google Cloud CLI and the validator dependency:
- Install the Google Cloud CLI. Then update it to the latest version with . An old CLI can fail when it uploads an OpenAPI 3.x API config.
gcloud components update - Install Python 3.10 or later and for the local OpenAPI MCP validator script:
PyYAMLbashpip install "PyYAML>=6.0,<7.0"
- Install the Google Cloud CLI. Then update it to the latest version with
- Authenticate your environment:
- Local CLI session:
gcloud auth login - Application Default Credentials (SDKs / ADK):
gcloud auth application-default login
- Local CLI session:
- Select or create a project, confirm billing, and pick a supported gateway location:
- Select an existing project () or create one (
gcloud config set project {project_id}).gcloud projects create {project_id} - Confirm that an active billing account is linked to . API Gateway has a free monthly allowance of API calls for each billing account, but you still need an active billing account to deploy. Calls above the allowance and network data transfer out are charged. For current prices, see API Gateway pricing.
{project_id} - Use as the default
us-central1for gateway commands. For the other supported regions, see API Gateway deployment model.--location
- Select an existing project (
- Enable the required Google Cloud APIs:
For API hub discovery (optional), enable the API hub API in the API hub host project:bash
gcloud services enable \ apigateway.googleapis.com \ servicemanagement.googleapis.com \ servicecontrol.googleapis.com \ --project={project_id}. To query Agent Registry (optional), also rungcloud services enable apihub.googleapis.com --project={hub_project_id}.gcloud services enable agentregistry.googleapis.com --project={project_id} - Verify the IAM roles (use the most restrictive role for each step):
- To create and update the API, API configs, and gateways: API Gateway Admin () on
roles/apigateway.admin. To run the{project_id}commands: Service Usage Admin (gcloud services enable) on that project.roles/serviceusage.serviceUsageAdmin - To reference the gateway service account in an API config: Service Account User () on
roles/iam.serviceAccountUser.{gateway_sa_name}@{project_id}.iam.gserviceaccount.com - For a Cloud Run backend: grant the gateway service account the Cloud Run Invoker role () on the service.
roles/run.invoker - To mint test ID tokens as a caller service account: Service Account Token Creator () on
roles/iam.serviceAccountTokenCreator.{caller_sa_email} - Reference: Configure the development environment.
- To create and update the API, API configs, and gateways: API Gateway Admin (
- 安装 Google Cloud CLI 和验证器依赖:
- 安装 Google Cloud CLI。然后使用 将其更新至最新版本。旧版 CLI 在上传 OpenAPI 3.x API config 时可能会失败。
gcloud components update - 安装 Python 3.10 或更高版本,以及用于本地 OpenAPI MCP 验证器脚本的 :
PyYAMLbashpip install "PyYAML>=6.0,<7.0"
- 安装 Google Cloud CLI。然后使用
- 验证环境身份:
- 本地 CLI 会话:
gcloud auth login - Application Default Credentials(SDK / ADK):
gcloud auth application-default login
- 本地 CLI 会话:
- 选择或创建项目、确认结算账号,并选择受支持的 gateway 位置:
- 选择现有项目()或创建项目(
gcloud config set project {project_id})。gcloud projects create {project_id} - 确认 已关联有效的结算账号。每个结算账号均有免费的月度 API 调用额度,但部署时仍需有效的结算账号。超出额度的调用和出站网络数据传输会计费。有关当前价格,请参阅 API Gateway pricing。
{project_id} - 对 gateway 命令使用 作为默认
us-central1。有关其他受支持的区域,请参阅 API Gateway deployment model。--location
- 选择现有项目(
- 启用所需的 Google Cloud API:
如需使用 API hub 发现功能(可选),请在 API hub 所在项目中启用 API hub API:bash
gcloud services enable \ apigateway.googleapis.com \ servicemanagement.googleapis.com \ servicecontrol.googleapis.com \ --project={project_id}。如需查询 Agent Registry(可选),还需运行gcloud services enable apihub.googleapis.com --project={hub_project_id}。gcloud services enable agentregistry.googleapis.com --project={project_id} - 验证 IAM 角色(每一步均使用权限范围最小的角色):
- 要创建和更新 API、API config 与 gateway:在 上授予 API Gateway Admin(
{project_id})。要在该项目中运行roles/apigateway.admin命令:授予 Service Usage Admin(gcloud services enable)。roles/serviceusage.serviceUsageAdmin - 要在 API config 中引用 gateway service account:在 上授予 Service Account User(
{gateway_sa_name}@{project_id}.iam.gserviceaccount.com)。roles/iam.serviceAccountUser - 对于 Cloud Run 后端:在该服务上向 gateway service account 授予 Cloud Run Invoker 角色()。
roles/run.invoker - 要以调用方 service account 签发测试 ID token:在 上授予 Service Account Token Creator(
{caller_sa_email})。roles/iam.serviceAccountTokenCreator - 参考:Configure the development environment。
- 要创建和更新 API、API config 与 gateway:在
Decision Matrix: MCP Server Configuration and Client Integration
决策矩阵:MCP 服务器配置与客户端集成
| Task | Required Mechanism | Critical Guardrail | Detailed Reference |
|---|---|---|---|
Migrate OpenAPI 2.0 ( | Root | NEVER add MCP extensions to an OpenAPI 2.0 spec. Rationale: MCP is not supported for OpenAPI 2.0. Use OpenAPI 3.0.x or 3.1.x, and deploy the spec with | OpenAPI 2.0 to 3.x Migration |
| Optimize tool names, descriptions, and schemas for LLM tool selection | | Every operation MUST have an | OpenAPI Agent Tool Optimizer |
Secure | | | OpenAPI 2.0 to 3.x Migration |
Connect Google ADK ( | | No separate registration step is needed. After you attach the API Gateway project to API hub, the MCP server appears in API hub as | ADK and API Hub Discovery |
Verify | | Keep | MCP Verification and Troubleshooting |
| 任务 | 必需机制 | 关键约束 | 详细参考 |
|---|---|---|---|
迁移 OpenAPI 2.0( | 根级 | 绝不要向 OpenAPI 2.0 规范添加 MCP 扩展。**理由:**MCP 不支持 OpenAPI 2.0。请使用 OpenAPI 3.0.x 或 3.1.x,并通过 | OpenAPI 2.0 to 3.x Migration |
| 优化工具名称、描述和 schema,以支持 LLM 工具选择 | | 每个操作都必须有 | OpenAPI Agent Tool Optimizer |
保护 | | | OpenAPI 2.0 to 3.x Migration |
连接 Google ADK( | | 无需单独的注册步骤。将 API Gateway 项目关联到 API hub 后,MCP 服务器会自动显示在 API hub 中,名称为 | ADK and API Hub Discovery |
验证 | 使用 | 请将 | MCP Verification and Troubleshooting |
Execution Workflow and Validation Loop
执行工作流与验证循环
- Upgrade and annotate the OpenAPI 3.x specification:
- The references in this skill contain the conversion rules: the Swagger 2.0 Construct Conversion Map covers each Swagger 2.0 construct, and the Complete Converted Example shows a finished spec with a Google ID token scheme. Use them before you search other sources.
- Follow OpenAPI 2.0 to 3.x Migration and OpenAPI Agent Tool Optimizer to set to a 3.0.x or 3.1.x version (the examples use
openapi), remove unsupported OpenAPI 2.0 fields (3.0.4), definex-google-allow(withx-google-api-management.backendsorjwtAudience), configuredisableAuthwith exactly one JWT or API key scheme (add a JWT scheme to the rootx-google-api-management.mcp.tools-list.securitylist too, in the same requirement object as any API key scheme), authorsecuritydescriptions stating when and why to invoke each tool, and opt out administrative routes (x-google-mcp-tool).x-google-mcp-tool: false - Before you deploy, give the user a short summary that states each of these points, and get approval:
- MCP is not supported for OpenAPI 2.0, so you converted the spec to OpenAPI 3.x.
- Which operations you exposed as tools, and which you opted out with . Opted-out operations stay on REST.
x-google-mcp-tool: false - If uses a JWT scheme, the root
tools/listentry makes every REST operation without its ownsecurityrequire the JWT. The gateway rejects REST callers without it.security - The MCP endpoint is , at the host root even when the spec sets a
https://{default_hostname}/mcpbase path.servers - The limits that apply: only ,
GET,POST,PUT, andPATCHoperations can be tools, a gateway serves at most 1,000 tools, request parameters must be primitive types, API Gateway rejects complex nested schemas and OpenAPI 3.1 type lists (such asDELETE) when you create the API config, tools cannot returntype: ["string", "null"], and MCP is stateless with UTF-8 text responses only.HTTP 204
- Give the same points when the user only asks for the converted spec or for advice, without a deploy.
- Preserve exact quota metric names in across API configs so existing gateways do not fail with HTTP 500
x-google-api-management.metrics.Failed to call Service Control Quota
- Run the deterministic MCP OpenAPI validation loop before creating the API config:
- Execute on
scripts/validate_mcp_openapi.py:{candidate_openapi_spec}bashpython3 scripts/validate_mcp_openapi.py --spec={candidate_openapi_spec} - If exits with code
scripts/validate_mcp_openapi.py(1), fix every reported error instatus: "FAIL"(such as OpenAPI 2.0 syntax,{candidate_openapi_spec}, path-levelx-google-allow, undefined backend names, an operation without anx-google-mcp-tool, missing descriptions, invalid or duplicate tool names, a header parameter with the reservedoperationIdprefix, ax-google-scheme that is neither antools-list.securityJWT nor anoauth2scheme, a JWTapiKeyscheme missing from the roottools-list.securitylist, alternative (OR) security requirements with an API key, or ansecurityoperation in an MCP spec) and re-run the script until it exits with codex-google-model-router(0). If it exits with codestatus: "PASS", fix the file path or YAML syntax error printed to stderr.2 - Read every warning (such as an response, a request parameter that is not a primitive type, or operations that an
HTTP 204object exposes by default). Fix it, or tell the user why you keep it.mcp
- Execute
- Create the API (if new), create the API config, deploy the gateway, and enable the managed service (request human approval before updating a production gateway):
(For a brand-new gateway, runbash
gcloud api-gateway apis create {api_id} --project={project_id} gcloud api-gateway api-configs create {config_id} \ --api={api_id} \ --openapi-spec={candidate_openapi_spec} \ --backend-auth-service-account={gateway_sa_name}@{project_id}.iam.gserviceaccount.com \ --project={project_id} gcloud api-gateway gateways update {gateway_id} \ --api={api_id} \ --api-config={config_id} \ --location={region} \ --project={project_id} gcloud services enable $(gcloud api-gateway apis describe {api_id} --project={project_id} --format="value(managedService)") --project={project_id}instead ofgcloud api-gateway gateways create {gateway_id} ...).gateways update- Run one at a time per API. Wait for it to finish before you start the next one, because API Gateway creates only one config for an API at a time. A complex API config can take up to ten minutes to create.
api-configs create - If clients use API keys from other projects, enable the managed service in each of those projects too. API Gateway rejects an API key unless the API is enabled in the project that created the key.
- Run one
- Verify the JSON-RPC 2.0 lifecycle and connect ADK / API hub:
- Send every probe to , even when the spec sets a
https://{default_hostname}/mcpbase path.servers - An HTML means that the gateway may not be deployed correctly. Check that the gateway state is
404 Page not foundand that the last create or update finished, and retry before you change the config. During a gateway update, some requests can still reach the previous API config. A JSON 404 (ACTIVE) means that the active config does not define the path.The current request is not defined by this API. - If requires a JWT, mint a Google ID token whose audience is in
tools/list:x-google-auth.audiences. Ifgcloud auth print-identity-token --impersonate-service-account={caller_sa_email} --audiences={jwt_audience}requires an API key, send it in thetools/listheader.x-api-key - Run the ,
initialize,notifications/initialized, andtools/listverification probes in MCP Verification and Troubleshooting.tools/call - Connect Google ADK () and verify automatic catalog discovery in Apigee API hub (
McpToolset) and Agent Registry using ADK and API Hub Discovery.{your_api} (MCP)
- Send every probe to
- 升级并标注 OpenAPI 3.x 规范:
- 此 skill 中的参考资料包含转换规则:Swagger 2.0 Construct Conversion Map 涵盖每种 Swagger 2.0 构造,Complete Converted Example 展示了一个带有 Google ID token 方案的完整转换后示例。在查找其他来源之前,请先使用这些资料。
- 按照 OpenAPI 2.0 to 3.x Migration 和 OpenAPI Agent Tool Optimizer 操作:将 设置为 3.0.x 或 3.1.x 版本(示例使用
openapi),移除不支持的 OpenAPI 2.0 字段(3.0.4),定义x-google-allow(使用x-google-api-management.backends或jwtAudience),使用恰好一种 JWT 或 API key 方案配置disableAuth(如果使用 JWT 方案,还需将其添加到根级x-google-api-management.mcp.tools-list.security列表中,并与任何 API key 方案放在同一个要求对象内),编写security描述以说明何时以及为何调用各个工具,并排除管理类路由(x-google-mcp-tool)。x-google-mcp-tool: false - 部署之前,向用户提供一份简短摘要并获得批准:
- MCP 不支持 OpenAPI 2.0,因此已将规范转换为 OpenAPI 3.x。
- 列出公开为工具的操作,以及通过 排除的操作。被排除的操作仍保留在 REST 中。
x-google-mcp-tool: false - 如果 使用 JWT 方案,根级
tools/list条目会使每个未自行定义security的 REST 操作都要求提供该 JWT。Gateway 会拒绝未提供该 JWT 的 REST 调用方。security - MCP 端点为 。即使规范设置了
https://{default_hostname}/mcp基础路径,该端点仍位于主机根路径。servers - 适用的限制包括:只有 、
GET、POST、PUT和PATCH操作可以作为工具;一个 gateway 最多提供 1,000 个工具;请求参数必须为原始类型;创建 API config 时,API Gateway 会拒绝复杂的嵌套 schema 和 OpenAPI 3.1 类型列表(如DELETE);工具不能返回type: ["string", "null"];MCP 无状态,且仅支持 UTF-8 文本响应。HTTP 204
- 即使用户只要求转换后的规范或相关建议,而不要求部署,也应提供上述相同信息。
- 在各个 API config 之间保留 中完全一致的配额指标名称,避免现有 gateway 因 HTTP 500
x-google-api-management.metrics而失败。Failed to call Service Control Quota
- 创建 API config 之前,运行确定性的 MCP OpenAPI 验证循环:
- 对 执行
{candidate_openapi_spec}:scripts/validate_mcp_openapi.pybashpython3 scripts/validate_mcp_openapi.py --spec={candidate_openapi_spec} - 如果 的退出代码为
scripts/validate_mcp_openapi.py(1),请修复status: "FAIL"中报告的每项错误,例如 OpenAPI 2.0 语法、{candidate_openapi_spec}、路径级x-google-allow、未定义的 backend 名称、缺少x-google-mcp-tool的操作、缺少描述、工具名称无效或重复、带有保留operationId前缀的 header 参数、既不是x-google-JWT 也不是oauth2方案的apiKey方案、未出现在根级tools-list.security列表中的 JWTsecurity方案、包含 API key 的替代(OR)安全要求,或 MCP 规范中的tools-list.security操作。然后重新运行脚本,直到其退出代码为x-google-model-router(0)。如果退出代码为status: "PASS",请修复 stderr 中打印的文件路径或 YAML 语法错误。2 - 阅读每条警告,例如 响应、非原始类型的请求参数,或被
HTTP 204对象默认公开的操作。修复这些问题,或告知用户保留它们的原因。mcp
- 对
- 创建 API(如果尚不存在)、创建 API config、部署 gateway,并启用托管服务(更新生产 gateway 前必须获得人工批准):
(对于全新 gateway,请运行bash
gcloud api-gateway apis create {api_id} --project={project_id} gcloud api-gateway api-configs create {config_id} \ --api={api_id} \ --openapi-spec={candidate_openapi_spec} \ --backend-auth-service-account={gateway_sa_name}@{project_id}.iam.gserviceaccount.com \ --project={project_id} gcloud api-gateway gateways update {gateway_id} \ --api={api_id} \ --api-config={config_id} \ --location={region} \ --project={project_id} gcloud services enable $(gcloud api-gateway apis describe {api_id} --project={project_id} --format="value(managedService)") --project={project_id},而不是gcloud api-gateway gateways create {gateway_id} ...。)gateways update- 每个 API 每次只能运行一个 。请等待其完成后,再开始下一个,因为 API Gateway 一次只能为一个 API 创建一个 config。复杂的 API config 最长可能需要十分钟才能创建完毕。
api-configs create - 如果客户端使用来自其他项目的 API key,也必须在每个相关项目中启用托管服务。除非在创建密钥的项目中启用了该 API,否则 API Gateway 会拒绝该 API key。
- 每个 API 每次只能运行一个
- 验证 JSON-RPC 2.0 生命周期并连接 ADK / API hub:
- 即使规范设置了 基础路径,也必须将所有探测请求发送到
servers。https://{default_hostname}/mcp - HTML 格式的 表示 gateway 可能未正确部署。请检查 gateway 状态是否为
404 Page not found,以及最后一次创建或更新是否已完成,然后重试,再考虑修改配置。在 gateway 更新期间,部分请求仍可能到达之前的 API config。JSON 格式的 404(ACTIVE)表示当前生效的配置未定义该路径。The current request is not defined by this API. - 如果 要求使用 JWT,请签发受众包含在
tools/list中的 Google ID token:x-google-auth.audiences。如果gcloud auth print-identity-token --impersonate-service-account={caller_sa_email} --audiences={jwt_audience}要求使用 API key,请通过tools/list请求头发送。x-api-key - 按照 MCP Verification and Troubleshooting 运行 、
initialize、notifications/initialized和tools/list验证探测。tools/call - 连接 Google ADK(),并按照 ADK and API Hub Discovery 验证在 Apigee API hub(
McpToolset)和 Agent Registry 中的自动目录发现。{your_api} (MCP)
- 即使规范设置了
Pre-Deployment Checklist
部署前检查清单
- Specification version is or
openapi: 3.0.x(not3.1.x), omitsswagger: "2.0", and does not containx-google-allow.x-google-model-router - returns
python3 scripts/validate_mcp_openapi.py --spec={candidate_openapi_spec}(exit codestatus: "PASS").0 - Every operation has an , and all MCP operations are in one OpenAPI document.
operationId - names exactly one scheme: an
x-google-api-management.mcp.tools-list.securityJWT scheme withoauth2that the rootx-google-authlist also references, or ansecurityscheme withapiKeyandin: header.name: x-api-key - If uses a JWT scheme, the user knows that REST operations without their own
tools/listnow require that JWT.security - Administrative, health-check, and endpoints are opted out with
HTTP 204, and opted-out admin paths remain protected on REST.x-google-mcp-tool: false - MCP clients call (not
https://{default_hostname}/mcp), nest{basepath}/mcpJSON request bodies insidetools/call, and sendparams.arguments.body.MCP-Protocol-Version: 2025-11-25
- 规范版本为 或
openapi: 3.0.x(而不是3.1.x),不包含swagger: "2.0",也不包含x-google-allow。x-google-model-router - 返回
python3 scripts/validate_mcp_openapi.py --spec={candidate_openapi_spec}(退出代码为status: "PASS")。0 - 每个操作都有 ,且所有 MCP 操作均位于同一个 OpenAPI 文档中。
operationId - 恰好指定一种方案:一种带
x-google-api-management.mcp.tools-list.security且根级x-google-auth列表也引用的securityJWT 方案,或一种oauth2且in: header的name: x-api-key方案。apiKey - 如果 使用 JWT 方案,用户知悉未自行定义
tools/list的 REST 操作现在也需要该 JWT。security - 管理类、健康检查和 端点均已通过
HTTP 204排除,且被排除的管理类路径在 REST 中仍受保护。x-google-mcp-tool: false - MCP 客户端调用 (而不是
https://{default_hostname}/mcp),将{basepath}/mcp的 JSON 请求正文嵌套在tools/call内,并发送params.arguments.body。MCP-Protocol-Version: 2025-11-25
Resource Cleanup
资源清理
To avoid ongoing charges when tearing down a test or staging environment, request human approval and delete the gateway, API config, API, and service account in dependency order:
bash
gcloud api-gateway gateways delete {gateway_id} --location={region} --project={project_id}
gcloud api-gateway api-configs delete {config_id} --api={api_id} --project={project_id}
gcloud api-gateway apis delete {api_id} --project={project_id}
gcloud iam service-accounts delete {gateway_sa_name}@{project_id}.iam.gserviceaccount.com --project={project_id}为避免在拆除测试或预发布环境后继续产生费用,请先获得人工批准,然后按照依赖顺序删除 gateway、API config、API 和 service account:
bash
gcloud api-gateway gateways delete {gateway_id} --location={region} --project={project_id}
gcloud api-gateway api-configs delete {config_id} --api={api_id} --project={project_id}
gcloud api-gateway apis delete {api_id} --project={project_id}
gcloud iam service-accounts delete {gateway_sa_name}@{project_id}.iam.gserviceaccount.com --project={project_id}