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

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

启用 Memorystore for Valkey API 后,系统会启用 Memorystore for Valkey 远程 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 Valkey 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 Valkey 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 Valkey MCP 服务器所需的权限,请让您的管理员为您授予要在其中使用 Memorystore for Valkey MCP 服务器的项目的以下 IAM 角色:

  • 进行 MCP 工具调用: MCP Tool User (roles/mcp.toolUser)
  • 创建 Memorystore for Valkey 实例: Memorystore Admin (roles/memorystore.admin)
  • 创建 Memorystore for Valkey 用户: Memorystore Admin (roles/memorystore.admin)
  • 获取 Memorystore for Valkey 实例或列出项目中的所有 Memorystore for Valkey 实例: Memorystore Viewer (roles/memorystore.viewer)
  • 列出 Memorystore for Valkey 用户: Memorystore Viewer (roles/memorystore.viewer)
  • 管理服务使用政策: Service Usage Admin (roles/serviceusage.serviceUsageAdmin)

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

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

所需权限

使用 Memorystore for Valkey MCP 服务器需要以下权限:

  • 获取有关服务使用政策的信息: serviceusage.mcppolicy.get
  • 更新服务使用政策: serviceusage.mcppolicy.update
  • 进行 MCP 工具调用: mcp.tools.call
  • 创建 Memorystore for Valkey 实例: memorystore.instances.create
  • 列出 Memorystore for Valkey 实例: memorystore.instances.list
  • 获取有关 Memorystore for Valkey 实例的信息: memorystore.instances.get
  • 克隆 Memorystore for Valkey 实例: memorystore.instances.create
  • 更新 Memorystore for Valkey 实例: memorystore.instances.update
  • 备份 Memorystore for Valkey 实例: memorystore.instances.backup
  • 删除 Memorystore for Valkey 实例: memorystore.instances.delete

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

身份验证和授权

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

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

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

Memorystore for Valkey MCP OAuth 范围

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

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

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

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

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

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

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

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

如需更通用的指导,请参阅以下资源:

可用的工具

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

列出工具

使用 MCP Inspector 列出工具,或将 tools/list HTTP 请求直接发送到 Memorystore for Valkey 远程 MCP 服务器。tools/list 方法不需要身份验证。

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

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

示例应用场景

以下是 Memorystore for Valkey MCP 服务器的示例应用场景:

“为什么要创建启用了 IAM 身份验证的 Memorystore for Valkey 区域级实例?”

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

“为什么要查看特定区域中的所有活跃 Memorystore for Valkey 实例?”

通过列出这些实例,您可以确保资源与当前架构匹配。Memorystore for Valkey MCP 服务器的 AI 智能体使用 list_instances MCP 工具检索指定区域中实例的格式化列表。

“为什么要从特定区域中的 Memorystore for Valkey 实例检索连接端点和运营元数据?”

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

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

如需显著提高这些应用的 CPU 容量和内存吞吐量,您可以通过增加实例的分片数来扩缩 Memorystore for Valkey 实例。Memorystore for Valkey MCP 服务器的 AI 智能体使用 update_instance MCP 工具更新实例的分片数。

“如何保护数据免受 Memorystore for Valkey 实例或其所在区域可能发生的故障的影响?”

创建 Memorystore for Valkey 实例的备份。如果发生区域级或实例故障,您可以将数据恢复到新实例以恢复运营。Memorystore for Valkey MCP 服务器的 AI 智能体使用 backup_instance MCP 工具创建实例的备份。

可选的安全配置

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

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

Model Armor

Model Armor 是一项服务,旨在增强 AI 应用的安全性。 Google Cloud 它通过主动过滤 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 的使用