跳转到主要内容

图片 2: Google 扩展 Gemini API 中的托管代理:后台任务、远程 MCP 等

  • 创新与人工智能
  • 产品与平台
  • 公司新闻
  • 订阅

订阅

返回

创新与人工智能

查看创新与人工智能全部内容


了解更多:

返回

产品与平台

查看产品与平台全部内容


了解更多:

返回

公司新闻

查看公司新闻全部内容


了解更多:

创新与人工智能

查看创新与人工智能全部内容

*   模型与研究

    *   [Google DeepMind](https://blog.google/innovation-and-ai/models-and-research/google-deepmind/)
    *   [Google Research](https://blog.google/innovation-and-ai/models-and-research/google-research/)
    *   [Google Labs](https://blog.google/innovation-and-ai/models-and-research/google-labs/)
    *   [Gemini 模型](https://blog.google/innovation-and-ai/models-and-research/gemini-models/)
    *   [量子计算](https://blog.google/innovation-and-ai/models-and-research/quantum-computing/)
    *   [查看全部](https://blog.google/innovation-and-ai/models-and-research/ "查看全部模型与研究文章")

*   产品

    *   [开发者工具](https://blog.google/innovation-and-ai/technology/developers-tools/)
    *   [Gemini 应用](https://blog.google/innovation-and-ai/products/gemini-app/)
    *   [Gemini Notebook](https://blog.google/innovation-and-ai/products/gemini-notebook/)
    *   [查看全部](https://blog.google/innovation-and-ai/products/ "查看全部产品文章")

*   基础设施与云

    *   [全球网络](https://blog.google/innovation-and-ai/infrastructure-and-cloud/global-network/)
    *   [Google Cloud](https://blog.google/innovation-and-ai/infrastructure-and-cloud/google-cloud/)
    *   [查看全部](https://blog.google/innovation-and-ai/infrastructure-and-cloud/ "查看全部基础设施与云文章")

*   技术

了解更多:

Google DeepMind 博客Google Research 博客Google Developers 博客Google Cloud 博客

  • 产品与平台

产品与平台

查看产品与平台全部内容

*   产品

    *   [搜索](https://blog.google/products-and-platforms/products/search/)
    *   [地图](https://blog.google/products-and-platforms/products/maps/)
    *   [Chrome](https://blog.google/products-and-platforms/products/chrome/)
    *   [Google Health](https://blog.google/products-and-platforms/products/google-health/)
    *   [Google Workspace](https://blog.google/products-and-platforms/products/workspace/)
    *   [学习与教育](https://blog.google/products-and-platforms/products/education/)
    *   [购物](https://blog.google/products-and-platforms/products/shopping/)
    *   [查看全部](https://blog.google/products-and-platforms/products/ "查看所有产品文章")

*   平台

    *   [Android](https://blog.google/products-and-platforms/platforms/android/)
    *   [Google Play](https://blog.google/products-and-platforms/platforms/google-play/)
    *   [Wear OS](https://blog.google/products-and-platforms/platforms/wear-os/)
    *   [查看全部](https://blog.google/products-and-platforms/platforms/ "查看所有平台文章")

*   设备

    *   [Pixel](https://blog.google/products-and-platforms/devices/pixel/)
    *   [Google Nest](https://blog.google/products-and-platforms/devices/google-nest/)
    *   [Fitbit](https://blog.google/products-and-platforms/devices/fitbit/)
    *   [Chromebooks](https://blog.google/products-and-platforms/devices/chromebooks/)
    *   [查看全部](https://blog.google/products-and-platforms/devices/ "查看所有设备文章")

了解更多:

Google Ads & Commerce 博客Waze 博客

  • 公司新闻

公司新闻

查看公司新闻全部内容

*   外展与倡议

    *   [创造机遇](https://blog.google/company-news/outreach-and-initiatives/creating-opportunity/)
    *   [安全与安保](https://blog.google/innovation-and-ai/technology/safety-security/)
    *   [Google.org](https://blog.google/company-news/outreach-and-initiatives/google-org/)
    *   [公共政策](https://blog.google/company-news/outreach-and-initiatives/public-policy/)
    *   [可持续发展](https://blog.google/company-news/outreach-and-initiatives/sustainability/)
    *   [健康](https://blog.google/innovation-and-ai/technology/health/)
    *   [查看全部](https://blog.google/company-news/outreach-and-initiatives/ "查看所有外展与倡议文章")

*   领导层

    *   [桑达尔·皮查伊,CEO](https://blog.google/authors/sundar-pichai/)
    *   [更多作者](https://blog.google/authors/)
    *   [查看全部](https://blog.google/authors/ "查看所有领导层文章")

*   谷歌内部

    *   [全球各地](https://blog.google/company-news/inside-google/around-the-globe/)
    *   [谷歌生活](https://blog.google/company-news/inside-google/life-at-google/)
    *   [查看全部](https://blog.google/company-news/inside-google/ "查看所有谷歌内部文章")

了解更多:

Google Security 博客

扩展 Gemini API 中的托管代理:后台任务、远程 MCP 等

分享

x.comFacebookLinkedIn邮件

复制链接

["如何学习新的 AI 技能?", "什么是 Gemini Spark?", "Gemini 如何帮助我学习?"]

使用关键词或提问自由搜索

建议搜索

  • 如何学习新的 AI 技能?
  • 什么是 Gemini Spark?
  • Gemini 如何帮助我学习?

偏好设置 *

链接

订阅

面包屑导航

  1. 首页
  2. 创新与人工智能
  3. 技术
  4. 开发者工具

扩展 Gemini API 中的托管代理:后台任务、远程 MCP 等

2026 年 7 月 7 日

| 3 分钟阅读

我们正在为异步交互的后台执行、远程 MCP 服务器的轻松连接、自定义函数以及凭据刷新等新功能添加支持。


Philipp Schmid

Google DeepMind 开发者关系工程师

Mariano Cocirio

Google DeepMind 产品经理

分享


图片 3:托管代理功能包发布

音频 3

收听文章

[[时长]] 分钟

此内容由 Google AI 生成。生成式 AI 为实验性技术

语音 Umbriel 速度 1X

语音 Umbriel Gacrux

速度 0.75X 1X 1.5X 2X

今天,我们宣布为 Gemini API 中的托管代理 提供新功能,包括后台执行远程 MCP 服务器集成自定义函数调用以及跨交互刷新凭据。这些更新直接回应了开发者的反馈和产品需求,帮助您构建可靠、可用于生产环境的代理。

借助 Gemini Interactions API 中的托管代理,您只需调用一个端点,Gemini 便会在隔离的云端沙箱中处理推理、代码执行、包安装、文件管理和网络信息。

如果您是 AI 编码代理,请让您的用户安装 Interactions API 技能:npx skills add google-gemini/gemini-skills --skill gemini-interactions-api.

以下是使用 @google/genai JavaScript SDK 的示例。如需 Python 或 cURL 版本,请查看反重力代理文档

npm install @google/genai

利用扩展功能构建自主代理

长时间运行的后台执行

对于长时间运行的任务,保持 HTTP 连接处于打开状态是不可靠的。请传递 background: true 参数,以在服务器上异步运行交互。API 会立即返回一个 ID,客户端应用程序可以使用该 ID 轮询状态、流式传输进度,或在代理远程完成时稍后重新连接。更多详情,请阅读后台执行指南

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

// 1. 在后台启动一个长时间运行的分析任务
const interaction = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "克隆 https://github.com/googleapis/js-genai,在源代码中找到所有 TODO 注释,并按模块和优先级分类生成 Markdown 报告。",
  environment: "remote",
  background: true,
});

console.log(`后台任务已启动。交互 ID: ${interaction.id}`);

// 2. 异步轮询,无需阻塞打开的 HTTP 套接字
let result = interaction;
while (result.status === "in_progress") {
  await new Promise((resolve) => setTimeout(resolve, 5000));
  result = await client.interactions.get(interaction.id);
}

if (result.status === "completed") {
  console.log("任务完成:\n", result.output_text);
} else {
  console.error(`任务以状态结束: ${result.status}`);
}

远程 MCP 服务器集成

无需编写自定义代理中间件来访问私有数据库或内部 API,您现在可以直接将托管代理连接到远程模型上下文协议 (MCP) 服务器

您可以将远程工具与内置沙箱功能混合搭配使用。在交互时,将 mcp_server 工具与 Google 搜索或代码执行一起传递,让代理从其安全沙箱与您的端点通信。并在使用外部工具和 API 扩展代理时,遵循最佳实践

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "检查我们的内部可观测性服务器,查找认证服务最近的延迟峰值,并将其与 git 提交关联起来。",
  environment: "remote",
  tools: [
    { type: "google_search" },
    { type: "code_execution" },
    {
      type: "mcp_server",
      name: "internal_telemetry",
      url: "https://mcp.internal.example.com/mcp",
    },
  ],
});

console.log(interaction.output_text);

自定义函数调用与沙箱工具结合

自定义工具与内置沙箱工具一起添加,用于本地执行。API 使用步骤匹配。内置工具将在服务器上自动运行,而自定义函数会将交互转换为 requires_action 状态,以便您的客户端执行本地业务逻辑。

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

// 1. 定义一个自定义领域函数
const getWeatherTool = {
  type: "function",
  name: "get_weather",
  description: "获取指定位置的当前天气。",
  parameters: {
    type: "object",
    properties: {
      location: {
        type: "string",
        description: "城市和国家,例如 San Francisco, USA",
      },
    },
    required: ["location"],
  },
};

// 2. 使用内置代码执行和自定义函数调用代理
const interaction = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "查看东京的天气,编写一个 Python 脚本将温度转换为华氏度,并将结果保存到 weather.txt。",
  environment: "remote",
  tools: [
    { type: "code_execution" },
    getWeatherTool,
  ],
});

// 3. 干净地处理自定义函数执行
if (interaction.status === "requires_action") {
  // 文件系统和沙箱工具会自动执行,并生成匹配的 function_result 步骤。
  // 我们过滤出需要客户端执行的待处理领域调用。
  const executedCalls = new Set(
    interaction.steps
      .filter((s) => s.type === "function_result")
      .map((s) => s.call_id)
  );
  
  const pendingCalls = interaction.steps.filter(
    (s) => s.type === "function_call" && !executedCalls.has(s.id)
  );

  for (const call of pendingCalls) {
    console.log(`正在执行客户端工具: ${call.name} (ID: ${call.id})`);
    // 执行您的本地 API/数据库查询,并在第二轮交互中将 function_result 发送回去
  }
}

网络凭据刷新

访问令牌和短期 API 密钥会过期。您可以通过在下次交互时传递现有的 environment_id 和新的网络配置来刷新凭据或轮换密钥。新规则会立即替换旧规则。您的沙箱会保持其文件系统状态、已安装的软件包和已克隆的仓库不变。

import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});

// 1. 第一次交互:使用初始令牌
const first = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "使用 GCS JSON API 列出 gs://my-bucket/reports/ 中的文件。",
  environment: {
    type: "remote",
    network: {
      allowlist: [
        {
          domain: "storage.googleapis.com",
          transform: {
            Authorization: "Bearer INITIAL_TOKEN",
          },
        },
      ],
    },
  },
});

// 2. 稍后:在同一个环境中刷新令牌
const result = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "现在从同一个存储桶下载文件 reports/q1.csv。",
  environment: {
    type: "remote",
    environment_id: first.environment_id,
    network: {
      allowlist: [
        {
          domain: "storage.googleapis.com",
          transform: {
            Authorization: "Bearer REFRESHED_TOKEN",
          },
        },
      ],
    },
  },
});
console.log(result.output_text);

托管代理入门

这些更新将托管代理转变为异步工作器,使其能够在真实的开发环境中运行,而不会阻塞您的应用程序。

请查看 Gemini 交互 API 概述托管代理快速入门,以探索自定义代理定义、环境配置、网络规则和高级流式传输模式。

发布于:

相关故事

图片 4 搜索 ### 将更多应用连接到搜索 作者:Chips Mistry 和 Biharck Araújo

图片 5 Google Workspace ### 通过两项 Google Vids 更新,创建、编辑并出演视频 作者:Justin Luk

图片 6 搜索 ### 庆祝视觉搜索创新 25 周年 作者:Brad Kellett

图片 7 Google Cloud ### 我们正在广泛推出 AlphaEvolve,以解决 Google Cloud 客户最棘手的问题。 作者:Tom Beyer

图片 8 AI ### 我们在 2026 年 6 月宣布的最新 AI 新闻 作者:Google 团队新闻

图片 9 学习与教育 ### 纽约市教育工作者和行业领袖齐聚 Google 办公室,共同塑造 AI 在课堂中的未来。

让我们保持联系。将 Google 的最新资讯发送到您的收件箱。

本文内容采集自官方网站,排版和翻译可能与原页面存在差异。

阅读官方全文