使用远程 MCP 服务器从 AI 应用连接到 Memorystore for Redis Cluster

本文档介绍了如何使用 Memorystore for Redis Cluster 远程 Model Context Protocol (MCP) 服务器连接到 AI 应用,包括 Gemini CLI、ChatGPT、Claude 和您正在开发的自定义应用。借助 Memorystore for Redis Cluster 远程 MCP 服务器,您可以在支持 AI 的开发环境和 AI 智能体平台中管理 Memorystore for Redis Cluster 中的集群和备份。

启用 Memorystore for Redis Cluster API 后,系统会启用 Memorystore for Redis Cluster 远程 MCP 服务器。

Model Context Protocol (MCP) 可规范大语言模型 (LLM) 和 AI 应用或代理连接到外部数据源的方式。借助 MCP 服务器,您可以使用其工具、资源和提示来执行操作,并从其后端服务获取更新后的数据。

本地 MCP 服务器和远程 MCP 服务器有何区别?

本地 MCP 服务器
通常在本地机器上运行,并使用标准输入和输出流 (stdio) 在同一设备上的服务之间进行通信。
远程 MCP 服务器
在服务的基础设施上运行,并向 AI 应用提供 HTTP 端点,以实现 AI MCP 客户端与 MCP 服务器之间的通信。如需详细了解 MCP 架构,请参阅 MCP 架构

Google 和 Google Cloud 远程 MCP 服务器

Google 和 Google Cloud 远程 MCP 服务器具有以下功能和优势:

  • 简化、集中化的发现。
  • 托管式全球或区域 HTTP 端点。
  • 精细授权。
  • 借助 Model Armor 保护,可选择性地确保提示和回答的安全性。
  • 集中式审核日志记录。

如需了解其他 MCP 服务器,以及适用于 Google Cloud MCP 服务器的安全性和治理控制措施,请参阅 Google Cloud MCP 服务器概览

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Enable the Memorystore for Redis Cluster API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 安装 Google Cloud CLI。

  6. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  7. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  10. Enable the Memorystore for Redis Cluster API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  11. 安装 Google Cloud CLI。

  12. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  13. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

所需的角色

如需获得使用 Memorystore for Redis Cluster MCP 服务器所需的权限,请让您的管理员为您授予您想要使用 Memorystore for Redis Cluster MCP 服务器的项目的以下 IAM 角色:

  • 进行 MCP 工具调用: MCP Tool User (roles/mcp.toolUser)
  • 在 Memorystore for Redis Cluster 中创建集群: Redis Admin (roles/redis.admin)
  • 获取集群或列出项目中的所有集群:Redis Viewer (roles/redis.viewer)
  • 管理服务使用情况政策: Service Usage Admin (roles/serviceusage.serviceUsageAdmin)

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

这些预定义角色包含使用 Memorystore for Redis Cluster MCP 服务器所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

如需使用 Memorystore for Redis Cluster MCP 服务器,您需要拥有以下权限:

  • 获取有关服务使用政策的信息: serviceusage.mcppolicy.get
  • 更新服务使用政策: serviceusage.mcppolicy.update
  • 进行 MCP 工具调用: mcp.tools.call
  • 创建集群: redis.clusters.create
  • 列出集群: redis.clusters.list
  • 获取有关集群的信息: redis.clusters.get
  • 克隆集群: redis.clusters.create
  • 更新集群: redis.clusters.update
  • 备份集群: redis.clusters.backup
  • 删除集群: redis.clusters.delete
  • 创建用户: redis.users.create
  • 列出用户: redis.users.list
  • 获取用户信息: redis.users.get
  • 更新用户: redis.users.update
  • 删除用户: redis.users.delete

您也可以使用自定义角色或其他预定义角色来获取这些权限。

身份验证和授权

Memorystore for Redis Cluster MCP 服务器使用 OAuth 2.0 协议和 Identity and Access Management (IAM) 进行身份验证和授权。支持所有 Google Cloud 身份用于向 MCP 服务器进行身份验证。

Memorystore for Redis Cluster 远程 MCP 服务器接受 API 密钥。

我们建议使用 MCP 工具为代理创建单独的身份,以便控制和监控对资源的访问权限。如需详细了解身份验证,请参阅向 Google 和 Google Cloud MCP 服务器进行身份验证

Memorystore for Redis Cluster MCP OAuth 范围

OAuth 2.0 使用范围和凭证来确定经过身份验证的主账号是否有权对资源执行特定操作。如需详细了解 Google 的 OAuth 2.0 范围,请参阅使用 OAuth 2.0 访问 Google API

Memorystore for Redis Cluster 具有以下 MCP 工具 OAuth 范围:

范围 URI 说明
https://www.googleapis.com/auth/redis.read-write 创建、列出、备份、更新和删除集群。

配置 MCP 客户端以使用 Memorystore for Redis Cluster MCP 服务器

Claude 或 Gemini CLI 等 AI 应用和代理可以实例化连接到单个 MCP 服务器的 MCP 客户端。一个 AI 应用可以有多个连接到不同 MCP 服务器的客户端。如需连接到远程 MCP 服务器,MCP 客户端必须至少知道远程 MCP 服务器的网址。

在 AI 应用中,寻找连接到远程 MCP 服务器的方式。系统会提示您输入服务器的详细信息,例如名称和网址。

对于 Memorystore for Redis Cluster MCP 服务器,请根据需要输入以下内容:

  • 服务器名称:Memorystore for Redis Cluster MCP 服务器
  • 服务器网址端点:https://redis.googleapis.com/mcp
  • 传输:HTTP
  • 身份验证详细信息:您可以根据所需的身份验证方式,输入 Google Cloud 凭证、OAuth 客户端 ID 和密钥,或代理身份和凭证。如需详细了解身份验证,请参阅向 Google 和 Google Cloud MCP 服务器进行身份验证
  • OAuth 范围:您在连接到 Memorystore for Redis Cluster MCP 服务器时要使用的 OAuth 2.0 范围

如需查看特定于主机的指导,请参阅以下内容:

如需更一般的指导,请参阅以下资源:

可用的工具

如需查看 Memorystore for Redis Cluster MCP 服务器的可用 MCP 工具的详细信息及其说明,请参阅 Memorystore for Redis Cluster MCP 参考文档

列出工具

使用 MCP 检查器列出工具,或直接向 Memorystore for Redis Cluster 远程 MCP 服务器发送 tools/list HTTP 请求。tools/list 方法不需要进行身份验证。

POST /mcp HTTP/1.1
Host: redis.googleapis.com
Content-Type: application/json

{
  "jsonrpc": "2.0",
  "method": "tools/list",
}

示例应用场景

以下是 Memorystore for Redis Cluster MCP 服务器的示例使用情形:

“为什么要创建启用了 IAM 身份验证的地区级集群?”

创建此类集群可消除静态密码,转而使用集中式短期凭据,以实现高度安全、区域性的工作负载。Memorystore for Redis Cluster MCP 服务器的 AI 智能体使用 create_cluster MCP 工具创建集群。

“为什么要查看特定区域中的所有有效集群?”

通过列出这些集群,您可以确保资源与当前架构相符。Memorystore for Redis Cluster MCP 服务器的 AI 智能体使用 list_clusters MCP 工具检索指定区域中集群的格式化列表。

“为什么您要从特定区域的集群检索连接端点和操作元数据?”

您需要此信息才能进行应用集成和系统维护。 Memorystore for Redis Cluster MCP 服务器的 AI 智能体使用 get_cluster MCP 工具检索有关集群的信息,例如其发现端点、分片数和副本数。

“如何针对数据密集型应用优化 Memorystore for Redis Cluster?”

如需大幅提升这些应用的 CPU 容量和内存吞吐量,您可以通过增加集群的分片数来扩缩集群。Memorystore for Redis Cluster MCP 服务器的 AI 智能体使用 update_cluster MCP 工具更新集群的分片数量。

“如何保护数据免受集群或其所在区域可能发生的故障的影响?”

创建集群的备份。如果发生区域级或集群故障,您可以将数据恢复到新集群,以继续运行。Memorystore for Redis Cluster MCP 服务器的 AI 智能体使用 backup_cluster MCP 工具创建集群备份。

可选的安全配置

由于 MCP 工具可执行各种操作,因此 MCP 会引发新的安全风险和注意事项。为了最大限度地降低这些风险并进行管理, Google Cloud 提供了默认政策和可自定义的政策,用于控制 MCP 工具在 Google Cloud 组织或项目中的使用。

如需详细了解 MCP 安全性和治理,请参阅 AI 安全性

Model Armor

Model Armor 是一项 Google Cloud 服务,旨在增强 AI 应用的安全性。它通过主动过滤 LLM 提示和回答来防范各种风险,并支持 Responsible AI 实践。无论您是在云环境还是外部云服务提供商中部署 AI,Model Armor 都能帮助您防止恶意输入、验证内容安全性、保护敏感数据、保持合规性,并在各种 AI 环境中以一致的方式实施 AI 安全政策。

Model Armor 在特定区域位置提供。如果您为项目启用 Model Armor,并且对该项目的调用来自不受支持的区域,则 Model Armor 会进行跨区域调用。如需了解详情,请参阅 Model Armor 位置

启用 Model Armor

您必须先启用 Model Armor API,然后才能使用 Model Armor。

控制台

  1. 启用 Model Armor API。

    启用 API 所需的角色

    如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (roles/serviceusage.serviceUsageAdmin),该角色包含 serviceusage.services.enable 权限。了解如何授予角色

    启用 API

  2. 选择要启用 Model Armor 的项目。

gcloud

在开始之前,请使用 Google Cloud CLI 和 Model Armor API 按照以下步骤操作:

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  2. 运行以下命令,为 Model Armor 服务设置 API 端点。

    gcloud config set api_endpoint_overrides/modelarmor "https://modelarmor.LOCATION.rep.googleapis.com/"

    LOCATION 替换为您要使用 Model Armor 的区域。

为 Google 和 Google Cloud 远程 MCP 服务器配置保护

为了帮助保护您的 MCP 工具调用和响应,您可以使用 Model Armor 下限设置。基准设置用于定义适用于整个项目的最低安全过滤条件。此配置会对项目中的所有 MCP 工具调用和响应应用一组一致的过滤条件。

设置启用了 MCP 清理功能的 Model Armor 下限设置。如需了解详情,请参阅配置 Model Armor 底价设置

请参阅以下示例命令:

gcloud model-armor floorsettings update \
--full-uri='projects/PROJECT_ID/locations/global/floorSetting' \
--enable-floor-setting-enforcement=TRUE \
--add-integrated-services=GOOGLE_MCP_SERVER \
--google-mcp-server-enforcement-type=INSPECT_AND_BLOCK \
--enable-google-mcp-server-cloud-logging \
--malicious-uri-filter-settings-enforcement=ENABLED \
--add-rai-settings-filters='[{"confidenceLevel": "MEDIUM_AND_ABOVE", "filterType": "DANGEROUS"}]'

请将 PROJECT_ID 替换为您的 Google Cloud 项目 ID。

请注意以下设置:

  • INSPECT_AND_BLOCK:用于检查 Google MCP 服务器的内容并屏蔽与过滤器匹配的提示和响应的强制执行类型。
  • ENABLED:用于启用过滤或强制执行的设置。
  • MEDIUM_AND_ABOVE:Responsible AI - Dangerous 过滤设置的置信度。您可以修改此设置,但较低的值可能会导致出现更多假正例。如需了解详情,请参阅 Model Armor 置信度级别

停用使用 Model Armor 扫描 MCP 流量的功能

如果您想停止使用 Model Armor 扫描 Google MCP 流量,请运行以下命令:

gcloud model-armor floorsettings update \
  --full-uri='projects/PROJECT_ID/locations/global/floorSetting' \
  --remove-integrated-services=GOOGLE_MCP_SERVER

PROJECT_ID 替换为 Google Cloud 项目 ID。

Model Armor 不会扫描项目中的 MCP 流量。

使用 IAM 拒绝政策控制 MCP 使用情况

Identity and Access Management (IAM) 拒绝政策有助于保护 Google Cloud 远程 MCP 服务器。配置这些政策以阻止不需要的 MCP 工具访问。

例如,您可以根据以下条件拒绝或允许访问:

  • 主账号
  • 工具属性(例如只读)
  • 应用的 OAuth 客户端 ID

如需了解详情,请参阅使用 Identity and Access Management 控制 MCP 的使用