注册 MCP 服务器

Agent Registry 通过注册和管理 模型上下文协议 (MCP) 服务器端点及其公开的 特定 工具,充当集中式目录。

本文档介绍了如何在 Agent Registry 中注册 MCP 服务器,以及如何管理其工具规范。

自动发现 Google Cloud MCP 服务器

官方 Google 和 Google Cloud 远程 MCP 服务器会自动注册 并提取到 Agent Registry 中。如需查看可用的 Google 和 Google Cloud 远程 MCP 服务器,请参阅MCP 服务器文档中的 Google Cloud 支持的产品。

当您在项目中启用受支持的 Google Cloud API(例如 Compute Engine API)时,相应的 MCP 服务器及其工具会立即注册,并可在 Agent Registry 中被发现。您无需为这些服务器手动配置或上传工具规范。

Google 管理的 MCP 服务器的作用域和绑定

Google 管理的远程 MCP 服务器会自动在项目的 global 位置注册。由于这些内置 Google 服务器位于全局位置,因此您必须通过指定 --region=global 标志,在全局作用域内为其应用任何 Identity and Access Management (IAM) 绑定。这些全局远程 MCP 服务器不支持区域级 IAM 绑定(例如 --region=us-central1),并且会返回 NOT_FOUND 错误。

从 GKE 注册 MCP 服务器

您可以通过将 registry.gke.io/functional-type: "MCP_SERVER" 标签添加到 GKE 部署中,为部署在 Google Kubernetes Engine (GKE) 上的自定义 MCP 服务器配置自动注册。

registry.gke.io/functional-type: "MCP_SERVER"标签。

如需让 Agent Registry 执行内省扫描并发现您的 MCP 工具,您的部署还必须包含声明服务器的端点网址和功能详细信息的注解。

如需启用经过身份验证的通信,请通过将 代理身份添加到 Pod 规范中来配置 添加 iam.gke.io/spiffe-identity-type: agent-identity 注解。

以下示例展示了使用这些配置的 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 to the registry
    registry.gke.io/functional-type: "MCP_SERVER"
  annotations:
    # A list of 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/my-project/my-mcp-server:1.0.0

应用部署时,GKE 会自动尝试从服务器获取工具规范,并将工具直接注册到 Agent Registry 数据模型中。Agent Registry 会将您的部署名称用作 MCP 服务器的显示名称。

从 Cloud Run 注册 MCP 服务器

您可以在部署自定义 MCP 服务器时指定 --functional-type=mcp-server 标志,为部署在 Cloud Run 中的自定义 MCP 服务器配置自动注册。

当您使用 --functional-type=mcp-server 部署 Cloud Run 服务时,该服务会注册到 Agent Registry 中的 MCP 服务器目录。在注册期间,Cloud Run 会注册服务器名称和类型 (MCP_SERVER)。

您还可以使用 --identity-type 标志指定身份类型。例如,指定 agent-identity 或 service-account 身份。如果未指定,工作负载默认使用服务帐号身份。

如需了解如何在 Cloud Run 中部署 MCP 服务器服务,请参阅 为 Cloud Run 配置 Agent Platform 功能。

如需了解详情,请参阅 在 Cloud Run 上托管 MCP 服务器和 对 MCP 服务器进行身份验证。

自动注册只会发现部署在同一 Google Cloud 项目中的资源。

注册外部 MCP 服务器

如需管理和重复使用外部服务器或自定义 API 提供的工具,您必须明确注册 MCP 服务器。

手动注册 MCP 服务器时,Agent Registry 会注册端点。 不过,它不会自动内省服务器以确定哪些工具可用。如需让用户发现 MCP 服务器上可用的确切工具,您必须在注册期间通过上传 toolspec.json 文件来提供工具规范。

准备工作

开始之前,请先设置 Agent Registry。您需要 项目 ID 才能执行 这些任务。

如需使用本文档中的 gcloud CLI 命令,请确保您已 设置 Google Cloud CLI 环境。

所需的角色

如需获得在 Agent Registry 中手动注册 MCP 服务器所需的权限,请让您的管理员为您授予项目的Agent Registry API Editor (roles/agentregistry.editor) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

准备工具规范

创建一个 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:

    前往 Agent Registry

  2. 在项目选择器中,选择您在其中 设置 Agent Registry的 Google Cloud 项目。

  3. 选择 MCP 服务器 标签页。

  4. 点击添加 MCP 服务器 。

  5. 在 MCP 服务器详细信息 面板中,输入显示名称、说明和地理区域。

  6. 在工具规范 部分中,输入 MCP 服务器端点网址,然后粘贴 toolspec.json 文件的内容。

    (可选)点击此面板中的导入工具 ,从网址导入 MCP 工具。只有当 MCP 服务器的端点网址在互联网上公开托管时,此选项才有效。您无法使用此功能从私下托管的 MCP 服务器导入工具。

  7. 点击下一步 。

    选择 MCP 工具 面板会显示 MCP 服务器中的工具列表,其中包含名称和说明。

  8. 选择您要包含的所有 MCP 工具。您可以按名称或值过滤列表中的工具。

  9. 点击保存 。

gcloud

您使用的规范标志决定了您的服务所属的资源集合。如需查看标志与资源集合的完整映射, 请参阅 API 资源。

使用工具规范注册 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。
  • 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

您配置的规范块(例如 mcp_server_spec)决定了您的服务所属的资源集合。如需查看规范与资源集合的完整映射,请参阅 API 资源。

如需注册 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 Server resource name."
  value       = google_agent_registry_service.mcp_server.registry_resource
}

替换以下内容:

  • REGION:注册表区域。
  • SERVER_NAME:您要为 MCP 服务器提供的名称,例如 custom-mcp-server。
  • DISPLAY_NAME:您要为服务器提供的易记名称,例如 Custom MCP Server。
  • SERVER_URL:MCP 服务器的端点网址,例如 https://api.example.com/mcp。

验证注册情况

列出、过滤和查看新注册的 MCP 服务器的详细信息。 如需确认 MCP 服务器已成功注册,您可以 列出注册表中的服务器。