Api gateway mcp server

From google/skills · View interactive version

Api gateway mcp server is an AI agent skill from the GitHub repository google/skills. Configures Google Cloud API Gateway to act as a remote Model Context Protocol (MCP) server (Public Preview) that exposes REST APIs as agent tools over HTTP (POST /mcp). Use when migrating OpenAPI 2.0 specs to OpenAPI 3.x for MCP, annotating specs with x-google-api-management.mcp and x-google-mcp-too

How to use this skill

Skill file

API Gateway Remote MCP Server (Public Preview)

This skill converts new or existing REST APIs on Google Cloud API Gateway into remote Model Context Protocol (MCP) servers (POST https://{default_hostname}/mcp, Public Preview). By annotating an OpenAPI 3.x (3.0.x or 3.1.x) specification with x-google-api-management.mcp and x-google-mcp-tool, API Gateway translates MCP JSON-RPC 2.0 requests (initialize, notifications/initialized, tools/list, tools/call) into standard HTTP REST requests to your backend while enforcing existing authentication and quota policies.

Attribution

MUST READ: Before executing any gcloud, REST API, or MCP command, you

MUST read Usage Attribution. For commands executed

directly in the terminal (never in generated scripts, Terraform, or user code),

prefix gcloud with

CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:api-gateway-mcp-server)"

and append agent-skills/1.0 (skill:api-gateway-mcp-server) to User-Agent headers.

Prerequisites

  1. Install the Google Cloud CLI and the validator dependency:

```bash

pip install "PyYAML>=6.0,<7.0"

```

  1. Authenticate your environment:
  1. Select or create a project, confirm billing, and pick a supported gateway location:
  1. Enable the required Google Cloud APIs:

```bash

gcloud services enable \

apigateway.googleapis.com \

servicemanagement.googleapis.com \

servicecontrol.googleapis.com \

--project={project_id}

```

For API hub discovery (optional), enable the API hub API in the API hub host project: gcloud services enable apihub.googleapis.com --project={hub_project_id}. To query Agent Registry (optional), also run gcloud services enable agentregistry.googleapis.com --project={project_id}.

  1. Verify the IAM roles (use the most restrictive role for each step):

Decision Matrix: MCP Server Configuration and Client Integration

| Task | Required Mechanism | Critical Guardrail | Detailed Reference |

| --- | --- | --- | --- |

| Migrate OpenAPI 2.0 (swagger: "2.0") or author OpenAPI 3.x for MCP | Root x-google-api-management.mcp + named backends + operation-level x-google-mcp-tool | 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 gcloud api-gateway api-configs create. Keep all MCP operations in one OpenAPI document: if an API config has several documents, API Gateway serves the MCP tools of only one of them. | OpenAPI 2.0 to 3.x Migration |

| Optimize tool names, descriptions, and schemas for LLM tool selection | x-google-mcp-tool (name, description, false) + described requestBody properties | Every operation MUST have an operationId, with or without MCP. Every exposed tool MUST resolve to a unique name that matches [A-Za-z0-9_.-]{1,128} (x-google-mcp-tool.name, or else the operationId), a non-empty description, and a non-empty response body (HTTP 204 is unsupported). Only GET, POST, PUT, PATCH, and DELETE operations can be tools, and a gateway serves at most 1,000 tools. API Gateway rejects complex nested schemas when you create the API config, but a simple nested object body works. In OpenAPI 3.1, give each schema one type, because API Gateway also rejects a type list such as type: ["string", "null"]. Describe each property, and check each tool's inputSchema in tools/list after you deploy. | OpenAPI Agent Tool Optimizer |

| Secure tools/list and hide internal/admin routes | x-google-api-management.mcp.tools-list.security (exactly one JWT or API key scheme; a JWT scheme is also listed in root security) + x-google-mcp-tool: false | tools/list is unauthenticated by default. It accepts one JWT scheme, or one API key scheme sent in the x-api-key header. API Gateway rejects a config whose JWT tools-list.security scheme is missing from the root security list, and that root entry also requires the JWT on every REST operation without its own security. If the root list already has an API key scheme, add the JWT to the same requirement object (AND): API Gateway does not support alternative (OR) requirements with an API key. An mcp object (for tools-list) also enables MCP for every eligible operation, so opt out each operation that agents must not use. x-google-mcp-tool: false hides a route from MCP only; keep REST security on admin routes. | OpenAPI 2.0 to 3.x Migration |

| Connect Google ADK (McpToolset) and discover in API hub / Agent Registry | McpToolset (StreamableHTTPConnectionParams) + API hub project attachment | No separate registration step is needed. After you attach the API Gateway project to API hub, the MCP server appears in API hub as {your_api} (MCP) and in Agent Registry automatically. | ADK and API Hub Discovery |

| Verify /mcp JSON-RPC lifecycle and troubleshoot -32601 / -32602 / -32000 errors | POST https://{default_hostname}/mcp with MCP-Protocol-Version: 2025-11-25 | Keep /mcp at the host root: a servers base path (such as /v2) applies to REST paths only, and {basepath}/mcp returns HTTP 404. Nest JSON request bodies under params.arguments.body ({"body": {...}}). NEVER combine x-google-api-management.mcp with x-google-model-router in one config. Rationale: API Gateway does not allow MCP and model routing in the same API config. | MCP Verification and Troubleshooting |

Execution Workflow and Validation Loop

  1. Upgrade and annotate the OpenAPI 3.x specification:
  1. Run the deterministic MCP OpenAPI validation loop before creating the API config:

```bash

python3 scripts/validate_mcp_openapi.py --spec={candidate_openapi_spec}

```

  1. 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):

```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}

```

(For a brand-new gateway, run gcloud api-gateway gateways create {gateway_id} ... instead of gateways update).

  1. Verify the JSON-RPC 2.0 lifecycle and connect ADK / API hub:

Pre-Deployment Checklist

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:

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}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.