註冊 MCP 伺服器

Agent Registry 可讓您自動或手動註冊 Model Context Protocol (MCP) 伺服器,方便開發人員和自動化調度管理代理在 AI 生態系統中,探索及叫用標準化工具。

如果自訂 MCP 伺服器部署在 Google Kubernetes Engine (GKE) 或 Cloud Run,自動註冊功能只會探索部署在管理專案中的資源,且該專案必須啟用 Agent Registry。如要註冊部署在其他 Google Cloud 專案或託管於外部的自訂 MCP 伺服器,請參閱「註冊外部 MCP 伺服器」。

自動探索 MCP 伺服器

Google 和 Google Cloud 遠端 MCP 伺服器會自動註冊並擷取至 Agent Registry。

在專案中啟用支援 MCP 的 Google Cloud API (例如 Compute Engine API) 時,系統會立即註冊對應的 MCP 伺服器及其工具,並在代理註冊資料庫中開放探索。您不必手動設定或上傳這些伺服器的工具規格。

位置和條件

Agent Registry 自動註冊 Google 或 Google Cloud遠端 MCP 伺服器時,只會在 global 位置建立 McpServer 資源,不會在 us-central1 等個別區域建立。如果服務提供區域端點 (REP),Agent Registry 會在單一 global McpServer 資源的 interfaces 欄位中列出這些區域網址。

由於自動註冊的資源僅存在於 global 中,因此在查詢伺服器或設定 Identity-Aware Proxy (IAP) 存取權政策時,請留意下列條件:

  • 針對自動註冊的伺服器使用 global:使用 --location=global 在 Agent Registry 查詢伺服器,或使用 --region=global 套用 IAP 政策時,請指定 global 位置。如果您指定 us-central1 等區域,指令會傳回 NOT_FOUND 錯誤,因為根據預設,該區域沒有區域 McpServer 資源。
  • 手動註冊區域以設定資料落地:如要將登錄檔中繼資料保留在特定區域,請在該區域手動註冊 MCP 伺服器及其區域端點網址,然後在該區域套用 IAP 政策。

詳情請參閱「區域和全球位置的控管範圍」和「在區域位置找不到遠端 MCP 伺服器」。

事前準備

  1. 設定 Agent Registry。
  2. 取得已啟用 Agent Registry 的管理專案專案 ID。

  3. 如要使用本文件中的 Google Cloud CLI 指令,請設定 gcloud CLI 環境。

必要的角色

如要取得在 Agent Registry 註冊 MCP 伺服器所需的權限,請要求管理員在已啟用 Agent Registry 的管理專案中,授予您 Agent Registry API 編輯者 (roles/agentregistry.editor) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

從 GKE 註冊 MCP 伺服器

如要註冊部署在 GKE 的自訂 MCP 伺服器,請在部署資訊清單中新增必要的 Kubernetes 標籤和註解。以下範例顯示設定為自動註冊的 GKE MCP 伺服器部署資訊清單:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-mcp-server
  labels:
    # GKE takes this label and registers the deployment as an MCP server
    registry.gke.io/functional-type: "MCP_SERVER"
  annotations:
    # Endpoint URLs where the GKE controller can access this MCP server
    modelcontextprotocol.info/urls: |
      - https://my-mcp-server.default.svc.cluster.local/mcp
    # Defines structural capabilities for the MCP server card
    modelcontextprotocol.info/capabilities: |
      card:
        endpoint: "/mcp"
        protocol: "HTTP"
spec:
  selector:
    matchLabels:
      app: my-mcp-server
  template:
    metadata:
      labels:
        app: my-mcp-server
      annotations:
        # Agent Identity annotation for identity and access management
        iam.gke.io/spiffe-identity-type: agent-identity
    spec:
      containers:
      - name: server
        image: gcr.io/PROJECT_ID/my-mcp-server:1.0.0

將 PROJECT_ID 替換為 Google Cloud 專案 ID。

如要設定及套用 Deployment 資訊清單,請按照下列步驟操作:

  1. 在 Deployment 資訊清單的 metadata.labels 中,新增 registry.gke.io/functional-type: "MCP_SERVER" 標籤。

  2. 在 Deployment 資訊清單的 metadata.annotations 中,提供 modelcontextprotocol.info/urls 和 modelcontextprotocol.info/capabilities 註解,宣告伺服器的端點網址和功能詳細資料,啟用 MCP 工具的自動探索功能。

  3. 在 Pod 範本的 spec.template.metadata.annotations 中,使用 iam.gke.io/spiffe-identity-type: agent-identity 註解設定代理程式身分,啟用經過驗證的通訊。

  4. 將 Deployment 資訊清單套用至叢集。

    套用部署作業時,GKE 會自動嘗試從伺服器取得工具規格,並直接在 Agent Registry 的唯讀 McpServer 探索資源中,註冊伺服器及其工具。Agent Registry 會使用部署作業名稱做為 MCP 伺服器的顯示名稱。

從 Cloud Run 註冊 MCP 伺服器

將自訂 MCP 伺服器部署為 Cloud Run 服務時,您可以指定 --functional-type=mcp-server 旗標,自動註冊這些伺服器。--functional-type=mcp-server 旗標會將 Cloud Run 服務識別為 MCP 伺服器,而非標準容器化工作負載,指示 Cloud Run 向 Agent Registry 註冊伺服器。註冊期間,Cloud Run 會自動將伺服器名稱和 MCP_SERVER 類型新增至註冊資料庫。

您也可以使用 --identity-type 旗標指定身分類型。例如指定 agent-identity 或 service-account 身分。如未指定,工作負載預設會使用服務帳戶身分。

如要為新的或現有的 Cloud Run 服務設定自動註冊,請參閱「設定 Cloud Run 的 Gemini Enterprise Agent Platform 功能」。如要在 Cloud Run 部署及保護 MCP 伺服器,請參閱「在 Cloud Run 託管 MCP 伺服器」和「驗證 MCP 伺服器」。

註冊外部 MCP 伺服器

如要管理及重複使用外部伺服器或自訂 API 提供的工具,您必須手動註冊 MCP 伺服器。

手動註冊 MCP 伺服器時,Agent Registry 會註冊端點。不過,它不會自動檢查伺服器,判斷哪些工具可用。如要讓使用者瞭解 MCP 伺服器上有哪些工具,您必須在註冊時上傳 toolspec.json 檔案,提供工具規格。

準備工具規格

建立 toolspec.json 檔案,列出伺服器提供的工具。這個規格檔案的大小上限為 10 KB。

JSON 檔案必須符合 MCP 工具結構定義規格,例如:

{
  "tools": [
    {
      "name": "get_customer_info",
      "description": "Retrieves customer details from the database.",
      "annotations": {
        "title": "Get Customer Info",
        "readOnlyHint": true,
        "idempotentHint": true
      }
    },
    {
      "name": "create_support_ticket",
      "description": "Creates a new support ticket in the issue tracking system.",
      "annotations": {
        "title": "Create Support Ticket",
        "destructiveHint": true,
        "idempotentHint": false,
        "openWorldHint": true
      }
    }
  ]
}

如需詳細的結構需求,請參閱 JSON 結構定義和 REST 參考資料。

註冊伺服器和工具

註冊 MCP 伺服器,並同時上傳工具定義:

控制台

  1. 前往 Google Cloud 控制台的「Agent Registry」:

    前往代理程式註冊頁面

  2. 在專案挑選器中,選取設定 Agent Registry 的 Google Cloud 專案。

  3. 選取「MCP 伺服器」分頁標籤。

  4. 按一下「新增 MCP 伺服器」。

  5. 在「MCP server details」(MCP 伺服器詳細資料) 面板中,輸入顯示名稱、說明和地理區域。

  6. 在「Tool specification」(工具規格) 部分,輸入 MCP 伺服器端點網址,並貼上 toolspec.json 檔案的內容。

    您也可以點選面板中的「匯入工具」,從網址匯入 MCP 工具。只有在 MCP 伺服器的端點網址公開託管於網際網路時,這個選項才有效。您無法使用這項功能,從私人代管的 MCP 伺服器匯入工具。

  7. 點選「下一步」。

    「Select MCP tools」(選取 MCP 工具) 面板會顯示 MCP 伺服器中的工具清單,以及工具的名稱和說明。

  8. 選取要納入的所有 MCP 工具。您可以依名稱或值篩選清單中的工具。

  9. 按一下 [儲存]。

gcloud

您使用的規格旗標會決定元件所屬的資源集合。如需旗標與資源集合的完整對應關係,請參閱「[服務資源和探索資源](/agent-registry/data-model#service-resources)」。

使用 gcloud agent-registry services create 指令,透過工具規格註冊 MCP 伺服器:

gcloud agent-registry services create SERVER_NAME \
    --project=PROJECT_ID \
    --location=REGION \
    --display-name="DISPLAY_NAME" \
    --mcp-server-spec-type=tool-spec \
    --mcp-server-spec-content=toolspec.json \
    --interfaces=url=SERVER_URL,protocolBinding=PROTOCOL

更改下列內容:

  • SERVER_NAME:要為 MCP 伺服器指定的名稱,例如 custom-mcp-server。名稱長度必須介於 4 到 63 個半形字元之間,且只能使用小寫英文字母、數字和連字號。
  • PROJECT_ID:專案 ID。
  • REGION:登錄檔區域。
  • DISPLAY_NAME:您要為伺服器指定的人類可讀名稱,例如 Custom MCP Server。
  • SERVER_URL:MCP 伺服器的端點網址,例如 https://api.example.com/mcp。
  • PROTOCOL:端點的通訊協定繫結。有效值為 jsonrpc、http-json 或 grpc。如果是 MCP 伺服器,這個值通常是 jsonrpc。

Terraform

您設定的規格區塊會決定元件所屬的資源集合。如需規格與資源集合的完整對應關係,請參閱「[服務資源和探索資源](/agent-registry/data-model#service-resources)」。

如要註冊 MCP 伺服器並指定其工具,請使用 mcp_server_spec 區塊設定 google_agent_registry_service 資源:

resource "google_agent_registry_service" "mcp_server" {
  location     = "REGION"
  service_id   = "SERVER_NAME"
  display_name = "DISPLAY_NAME"
  description  = "An external MCP server registered using Terraform."

  interfaces {
    url              = "SERVER_URL"
    protocol_binding = "JSONRPC"
  }

  mcp_server_spec {
    type    = "TOOL_SPEC"
    content = file("toolspec.json")
  }
}

output "server_resource_name" {
  description = "The generated read-only McpServer resource name."
  value       = google_agent_registry_service.mcp_server.registry_resource
}

更改下列內容:

  • REGION:登錄檔區域。
  • SERVER_NAME:要為 MCP 伺服器指定的名稱,例如 custom-mcp-server。名稱長度必須介於 4 到 63 個半形字元之間,且只能使用小寫英文字母、數字和連字號。
  • DISPLAY_NAME:您要為伺服器指定的人類可讀名稱,例如 Custom MCP Server。
  • SERVER_URL:MCP 伺服器的端點網址,例如 https://api.example.com/mcp。

驗證註冊

列出、篩選及查看新註冊 MCP 伺服器的詳細資料。如要確認 MCP 伺服器是否已成功註冊,可以列出儲存庫中的伺服器。