跳转到内容
主站 新闻 控制台

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

· Google AI
GoogleGemini

2026 年 7 月 7 日

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

Philipp Schmid

Google DeepMind 开发者关系工程师

Mariano Cocirio

Google DeepMind 产品经理

Managed agents feature bundle launch

收听文章

[[duration]] 分钟

本文内容由 Google AI 生成。生成式 AI 仍处于实验阶段

今天,我们宣布为 Gemini API 中的托管代理(Managed Agents) 增加新能力,包括 后台执行远程 MCP 服务器集成自定义函数调用 以及 跨交互刷新凭据。这些更新直接回应了开发者反馈和产品需求,帮助你构建可靠、可投入生产的智能体。

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

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

下面是使用 @google/genai JavaScript SDK 的示例。关于 Python 或 cURL,请查看 Antigravity agent 文档

构建具备扩展能力的自主智能体

长时间运行的后台执行

对于长时间任务,保持 HTTP 连接一直打开并不可靠。传入 background: true 可在服务器端异步运行交互。API 会立即返回一个 ID,客户端应用可以用它轮询状态、流式获取进度,或在智能体远程完成时稍后重新连接。更多详情请参阅 后台执行指南

import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// 1. 在后台启动一项长时间分析
const interaction = await client.interactions.create({
agent: "antigravity-preview-05-2026",
input: "Clone https://github.com/googleapis/js-genai, find all TODO comments in the source code, and categorize them by module and priority in a markdown report.",
environment: "remote",
background: true,
});
console.log(`后台任务已启动。交互 ID: ${interaction.id}`);
// 2. 异步轮询,不阻塞打开的 HTTP socket
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 服务器集成

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

你可以将远程工具与内置沙箱能力混合使用。在交互时传入 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: "Check our internal observability server for recent latency spikes in the auth service and correlate them with git commits.",
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 使用 step matching。内置工具会在服务器端自动运行,而自定义函数会将交互切换为 requires_action,由你的客户端执行本地业务逻辑。

import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// 1. 定义一个自定义领域函数
const getWeatherTool = {
type: "function",
name: "get_weather",
description: "Gets the current weather for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and country, e.g. San Francisco, USA",
},
},
required: ["location"],
},
};
// 2. 同时使用内置代码执行和自定义函数调用智能体
const interaction = await client.interactions.create({
agent: "antigravity-preview-05-2026",
input: "Check the weather in Tokyo, write a Python script to convert the temperature to Fahrenheit, and save the result to 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/数据库查询,并在第 2 轮把 function_result 返回
}
}

网络凭据刷新

访问令牌和短期 API 密钥会过期。你可以通过传入现有的 environment_id,并配合新的网络配置来刷新凭据或轮换密钥。