Agent 可以响应 MCP 引导式输入(elicitation)请求
现在,使用 addMcpServer 连接到 Model Context Protocol (MCP) 服务器的 Agent 可以处理 引导式输入(elicitation) ↗请求。
引导式输入允许 MCP 服务器在处理工具调用时请求用户输入。表单(Form)模式收集结构化、非敏感的数据。URL 模式在打开带外(out-of-band)流(如第三方授权或支付)之前会征得同意。
sequenceDiagram
participant 用户
participant Agent as Agent (MCP 客户端)
participant 服务器 as MCP 服务器
participant 浏览器
服务器->>Agent: elicitation/create
Agent->>用户: 显示服务器、原因以及输入或 URL
用户->>Agent: 提交、打开、拒绝或取消
Agent->>浏览器: 征得同意后打开 URL(URL 模式)
Agent->>服务器: accept, decline, or cancel
服务器-->>Agent: 可选的 URL 完成通知
在 onStart() 中为您 Agent 支持的每种模式注册一个处理器:
import { Agent } from "agents";
export class MyAgent extends Agent {
onStart() {
this.mcp.configureElicitationHandlers({
form: (request, serverId) => this.forwardToUser(request, serverId),
url: (request, serverId) => this.forwardToUser(request, serverId),
});
}
forwardToUser(request, serverId) {
// Show the request in your UI and resolve after the user responds.
throw new Error(
`Implement elicitation for ${serverId}: ${request.params.message}`,
);
}
}import { Agent } from "agents";
import type { ElicitRequest, ElicitResult } from "agents/mcp";
export class MyAgent extends Agent<Env> {
onStart() {
this.mcp.configureElicitationHandlers({
form: (request, serverId) => this.forwardToUser(request, serverId),
url: (request, serverId) => this.forwardToUser(request, serverId),
});
}
private forwardToUser(
request: ElicitRequest,
serverId: string,
): Promise<ElicitResult> {
// Show the request in your UI and resolve after the user responds.
throw new Error(
`Implement elicitation for ${serverId}: ${request.params.message}`,
);
}
}连接仅通告配置了处理器的模式。没有处理器的 Agent 不通告任何引导式输入能力,这允许服务器使用其回退方案。SDK 会在每次 MCP 服务器注册时存储通告的模式,以便它们在 Durable Object 休眠后仍能存活。回调函数将保留在内存中,并在 onStart() 运行时重新附着。
有关实现细节和浏览器转发模式,请参阅 MCP 客户端引导式输入。mcp-client ↗ 和 mcp-elicitation ↗ 示例实现双端对接。
要更新到此版本:
npm i agents@latestyarn add agents@latestpnpm add agents@latestbun add agents@latest

