Cloudflare Agents 从入门到精通
附录

§22 人类在环:审批流与四种模式

22.1 先给决策树

文档给了清晰的选型:

是Workflow多步? ──是──▶ waitForApproval
   │否
是MCP服务器? ──是──▶ elicitInput
   │否
是聊天交互? ──是──▶ 要浏览器能力? ──是──▶ onToolCall(客户端)
                         │否
                         └──▶ needsApproval(服务端)
    │否
简单确认 ──▶ State + WebSocket

22.2 聊天工具审批

processPayment: tool({
  description: "Process a payment",
  inputSchema: z.object({ amount: z.number(), recipient: z.string() }),
  needsApproval: async ({ amount }) => amount > 100, // 超 100 要批
  execute: async ({ amount, recipient }) => charge(amount, recipient),
}),

客户端用 addToolApprovalResponse 回应批准或拒绝。被拒可改 addToolOutput({ state: "output-error", errorText }) 给模型自定义原因。

22.3 MCP 内请求结构化输入

const userInput = await this.server.server.elicitInput({
  message: "By how much do you want to increase the counter?",
  requestedSchema: { type: "object", properties: { amount: { type: "number" } }, required: ["amount"] },
}, { relatedRequestId: extra.requestId });
核心建议
危险操作(付款、删号)一律加审批。审批不是体验负担,是上线底线。四种模式按决策树选,别混用。

人类在环讲完。下一章讲一个 Cloudflare 独一份的能力:让 Agent 自己付钱。