工具
工具让智能体能够执行操作——获取数据、调用外部 API、执行代码,甚至使用计算机。JavaScript/TypeScript SDK 支持六类工具:
在阅读过智能体并明确应由哪个智能体负责该任务、且希望为其赋予能力后,再阅读本页。如果您仍在不同委派模式之间做选择,请参阅智能体编排。
- OpenAI 托管工具——与模型一起在 OpenAI 服务器上运行。(Web 搜索、文件搜索、Code Interpreter、图像生成、工具搜索)
- 内置执行工具——由 SDK 提供、在模型之外执行的工具。(computer use 和 apply_patch 在本地运行;shell 可在本地或托管容器中运行)
- 函数工具——用 JSON schema 封装任意本地函数,以便 LLM 调用它。
- Agents as tools——将整个智能体作为可调用工具暴露出来。
- MCP 服务器——连接一个 Model Context Protocol 服务器(本地或远程)。
- 实验性:Codex 工具——将 Codex SDK 封装为函数工具,以运行感知工作区的任务。
本指南的其余部分将先介绍每类工具,然后总结跨类别的工具选择与提示编写建议。
1. 托管工具(OpenAI Responses API)
Section titled “1. 托管工具(OpenAI Responses API)”使用 OpenAIResponsesModel 时,您可以添加以下内置工具:
| 工具 | 类型字符串 | 用途 |
|---|---|---|
| Web 搜索 | 'web_search' | 互联网搜索。 |
| 文件 / 检索搜索 | 'file_search' | 查询托管在 OpenAI 上的向量存储。 |
| Code Interpreter | 'code_interpreter' | 在沙箱环境中运行代码。 |
| 图像生成 | 'image_generation' | 根据文本生成图像。 |
| 工具搜索 | 'tool_search' | 在运行时加载延迟函数工具、命名空间或可搜索的 MCP 工具。 |
import { Agent, codeInterpreterTool, fileSearchTool, imageGenerationTool, webSearchTool,} from '@openai/agents';
const agent = new Agent({ name: 'Travel assistant', tools: [ webSearchTool({ searchContextSize: 'medium' }), fileSearchTool('VS_ID', { maxNumResults: 3 }), codeInterpreterTool(), imageGenerationTool({ size: '1024x1024' }), ],});SDK 提供了返回托管工具定义的辅助函数:
| 辅助函数 | 说明 |
|---|---|
webSearchTool(options?) | 提供适合 JS 的选项,例如 searchContextSize、userLocation 和 filters.allowedDomains。 |
fileSearchTool(ids, options?) | 第一个参数接收一个或多个向量存储 ID,另可传入 maxNumResults、includeSearchResults、rankingOptions 和过滤器等选项。 |
codeInterpreterTool(options?) | 未提供 container 时,默认使用自动管理的容器。 |
imageGenerationTool(options?) | 支持图像生成配置,例如 model、size、quality、background、inputFidelity、inputImageMask、moderation、outputCompression、partialImages 和输出格式。 |
toolSearchTool(options?) | 添加内置的 tool_search 辅助工具。可与设置了 deferLoading: true 的延迟函数工具或托管 MCP 工具配合使用。默认支持托管执行,也可通过 execution: 'client' 加上 execute 使用客户端执行。 |
这些辅助函数会将 JavaScript/TypeScript 友好的选项名映射到底层 OpenAI Responses API 工具负载。完整工具 schema 以及排序选项、语义过滤器等高级选项,请参阅官方OpenAI 工具指南;当前内置工具搜索流程和模型可用性,请参阅官方工具搜索指南。
2. 内置执行工具
Section titled “2. 内置执行工具”这些工具内置于 SDK 中,但执行发生在模型响应之外:
- 计算机操作——实现
Computer接口并将其传给computerTool()。这始终针对您提供的本地Computer实现运行。 - Shell——可以提供本地
Shell实现,或通过shellTool({ environment })配置托管容器环境。 - Apply patch——实现
Editor接口并将其传给applyPatchTool()。这始终针对您提供的本地Editor实现运行。
工具调用仍由模型发起请求,但具体工作由您的应用程序或已配置的执行环境完成。
import { Agent, applyPatchTool, computerTool, shellTool, Computer, Editor, Shell,} from '@openai/agents';
const computer: Computer = { environment: 'browser', dimensions: [1024, 768], screenshot: async () => '', click: async () => {}, doubleClick: async () => {}, scroll: async () => {}, type: async () => {}, wait: async () => {}, move: async () => {}, keypress: async () => {}, drag: async () => {},};
const shell: Shell = { run: async () => ({ output: [ { stdout: '', stderr: '', outcome: { type: 'exit', exitCode: 0 }, }, ], }),};
const editor: Editor = { createFile: async () => ({ status: 'completed' }), updateFile: async () => ({ status: 'completed' }), deleteFile: async () => ({ status: 'completed' }),};
const agent = new Agent({ name: 'Local tools agent', model: 'gpt-5.4', tools: [ computerTool({ computer }), shellTool({ shell, needsApproval: true }), applyPatchTool({ editor, needsApproval: true }), ],});Computer 工具细节
Section titled “Computer 工具细节”computerTool() 可以接受以下任一形式:
- 一个具体的
Computer实例。 - 一个为每次运行创建
Computer的初始化函数。 - 一个包含
{ create, dispose }的提供者对象,用于需要按运行范围进行初始化与清理的场景。
要使用 OpenAI 当前的计算机操作路径,请设置支持计算机操作的模型,例如 gpt-5.4。当请求模型显式指定时,SDK 会发送 GA 内置的 computer 工具格式。如果生效模型仍来自存储的提示或其他旧集成,SDK 会继续使用旧版 computer_use_preview 线格式以保持兼容,除非您通过 modelSettings.toolChoice: 'computer' 显式启用 GA 路径。
GA 的计算机调用可以在单个 computer_call 中包含批量 actions[]。SDK 会按顺序执行它们,对每个 action 评估 needsApproval,并将最终截图作为工具输出返回。如果您基于 interruption.rawItem 构建审批 UI,在存在 actions 时请读取它,否则回退到旧版预览项中的 action。
当高影响的计算机操作需要暂停供用户审核时,请使用 needsApproval;如果您希望确认或拒绝某次计算机调用报告的待处理安全检查,请使用 onSafetyCheck。关于模型侧指导和迁移细节,请参阅官方OpenAI 计算机操作指南及其迁移说明。
Shell 工具细节
Section titled “Shell 工具细节”shellTool() 有两种模式:
- 本地模式:提供
shell,并可选提供environment: { type: 'local', skills },以及用于自动审批处理的needsApproval和onApproval。 - 托管容器模式:提供
environment,其中type为container_auto或container_reference。
在本地模式下,environment.skills 允许您通过 name、description 和文件系统 path 挂载本地技能。
在托管容器模式下,可通过以下任一方式配置 shellTool({ environment }):
type: 'container_auto',为当前运行创建托管容器。type: 'container_reference',通过containerId复用现有容器。
托管的 container_auto 环境支持:
networkPolicy,包括带domainSecrets的允许列表。fileIds,用于挂载已上传文件。memoryLimit,用于指定容器大小。skills,可通过skill_reference或内联 zip 包提供。
托管 shell 环境不接受 shell、needsApproval 或 onApproval,因为执行发生在托管容器环境中,而不是您的本地进程中。
完整用法请参见 examples/tools/local-shell.ts、examples/tools/container-shell-skill-ref.ts 和 examples/tools/container-shell-inline-skill.ts。
Apply-patch 工具细节
Section titled “Apply-patch 工具细节”applyPatchTool() 复用了 shellTool() 的本地审批流程:在文件编辑前使用 needsApproval 暂停;如果您希望由应用层回调自动批准或拒绝,则使用 onApproval。
3. 函数工具
Section titled “3. 函数工具”您可以使用 tool() 辅助函数把任意函数变成工具。
import { tool } from '@openai/agents';import { z } from 'zod';
const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', parameters: z.object({ city: z.string() }), async execute({ city }) { return `The weather in ${city} is sunny.`; },});| 字段 | 必需 | 说明 |
|---|---|---|
name | 否 | 默认为函数名(例如 get_weather)。 |
description | 是 | 展示给 LLM 的清晰、可读描述。 |
parameters | 是 | 可以是 Zod schema,也可以是原始 JSON schema 对象。Zod 参数会自动启用严格模式。 |
strict | 否 | 当为 true(默认)时,如果参数验证失败,SDK 会返回模型错误。设为 false 可进行模糊匹配。 |
execute | 是 | (args, context, details) => string | unknown | Promise<...>——您的业务逻辑。非字符串输出会被序列化后提供给模型。context 是可选的 RunContext;details 包含 toolCall、resumeState 和 signal 等元数据。 |
errorFunction | 否 | 自定义处理器 (context, error) => string,用于将内部错误转换为用户可见字符串。 |
timeoutMs | 否 | 每次调用的超时时间(毫秒)。必须大于 0 且小于等于 2147483647。 |
timeoutBehavior | 否 | 超时模式:error_as_result(默认)返回模型可见的超时消息,raise_exception 则抛出 ToolTimeoutError。 |
timeoutErrorFunction | 否 | 当 timeoutBehavior 为 error_as_result 时,用于超时输出的自定义处理器 (context, timeoutError) => string。 |
needsApproval | 否 | 执行前需要人工审批。参见人机协作指南。 |
isEnabled | 否 | 按运行条件性暴露工具;接受布尔值或谓词。 |
inputGuardrails | 否 | 在工具执行前运行的护栏;可拒绝或抛错。参见护栏。 |
outputGuardrails | 否 | 在工具执行后运行的护栏;可拒绝或抛错。参见护栏。 |
函数工具超时
Section titled “函数工具超时”使用 timeoutMs 来限制每次函数工具调用的时间。
timeoutBehavior: 'error_as_result'(默认)会向模型返回Tool '<name>' timed out after <timeoutMs>ms.。timeoutBehavior: 'raise_exception'会抛出ToolTimeoutError,您可以将其作为运行异常的一部分进行捕获。timeoutErrorFunction允许您在error_as_result模式下自定义超时文本。- 超时会中止
details.signal,因此长时间运行的工具在侦听取消信号时可以及时停止。
如果您直接调用函数工具,请使用 invokeFunctionTool 来强制执行与常规智能体运行相同的超时行为。
非严格 JSON schema 工具
Section titled “非严格 JSON schema 工具”如果您需要模型对无效或不完整输入进行猜测,那么在使用原始 JSON schema 时可以禁用严格模式:
import { tool } from '@openai/agents';
interface LooseToolInput { text: string;}
const looseTool = tool({ description: 'Echo input; be forgiving about typos', strict: false, parameters: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: true, }, execute: async (input) => { // because strict is false we need to do our own verification if (typeof input !== 'object' || input === null || !('text' in input)) { return 'Invalid input. Please try again'; } return (input as LooseToolInput).text; },});使用工具搜索进行延迟工具加载
Section titled “使用工具搜索进行延迟工具加载”工具搜索让模型在运行时仅加载其所需的工具定义,而不是预先发送所有 schema。在 SDK 中,这适用于延迟加载的顶层函数工具、toolNamespace() 分组,以及设置了 deferLoading: true 的托管 MCP 工具。
仅在支持该能力的 Responses API 中,将工具搜索与 GPT-5.4 及更新模型版本一起使用。
import { Agent, tool, toolNamespace, toolSearchTool } from '@openai/agents';import { z } from 'zod';
const customerIdParams = z.object({ customerId: z.string().describe('The customer identifier to look up.'),});
// Keep a standalone deferred tool at the top level when it represents a// single searchable capability that does not need a shared namespace.const shippingLookup = tool({ name: 'get_shipping_eta', description: 'Look up a shipment ETA by customer identifier.', parameters: customerIdParams, deferLoading: true, async execute({ customerId }) { return { customerId, eta: '2026-03-07', carrier: 'Priority Express', }; },});
// Group related tools into a namespace when one domain description should// cover several deferred tools and let tool search load them together.const crmTools = toolNamespace({ name: 'crm', description: 'CRM tools for customer profile lookups.', tools: [ tool({ name: 'get_customer_profile', description: 'Fetch a basic customer profile.', parameters: customerIdParams, deferLoading: true, async execute({ customerId }) { return { customerId, tier: 'enterprise', }; }, }), ],});
const agent = new Agent({ name: 'Operations assistant', model: 'gpt-5.4', // Mixing namespaced and top-level deferred tools in one request is supported. tools: [shippingLookup, ...crmTools, toolSearchTool()],});该示例有意混用了两种风格:
shippingLookup保持为顶层,因为它是一个独立的可搜索能力。crmTools使用toolNamespace(),因为相关的 CRM 工具共享一个高层标签和描述。- 在同一请求中混合命名空间和顶层延迟工具是受支持的;工具搜索既可加载如
crm这样的命名空间路径,也可加载如get_shipping_eta这样的顶层路径。
使用工具搜索时:
- 为每个延迟函数工具标记
deferLoading: true。 - 当多个相关工具应共享一个领域描述并作为一组加载时,使用
toolNamespace({ name, description, tools })。 - 当工具是单一独立能力且工具名本身就是良好的搜索目标时,让它保持在顶层。
- 只要任意延迟函数工具或托管 MCP 工具使用了
deferLoading: true,就要在同一个tools数组中添加toolSearchTool()。 - 将
modelSettings.toolChoice保持为'auto'。SDK 会拒绝按名称强制使用内置tool_search工具或某个延迟函数工具。 - 默认使用托管执行。如果设置
toolSearchTool({ execution: 'client', execute }),标准run()循环仅支持内置的{ paths: string[] }客户端查询格式;自定义客户端 schema 需要您自己实现 Responses 循环。 - 一个命名空间可以同时包含即时成员和延迟成员。即时成员无需工具搜索即可调用,而同一命名空间中的延迟成员会按需加载。
- 延迟函数工具和
toolNamespace()仅适用于 Responses。Chat Completions 会拒绝它们,AI SDK 适配器也不支持延迟 Responses 工具加载流程。
4. Agents as tools
Section titled “4. Agents as tools”有时,您希望一个智能体协助另一个智能体,而不是完全接管对话。此时可使用 agent.asTool():
如果您仍在 agent.asTool() 和 handoff() 之间做选择,请对照智能体与智能体编排中的模式说明。
import { Agent } from '@openai/agents';
const summarizer = new Agent({ name: 'Summarizer', instructions: 'Generate a concise summary of the supplied text.',});
const summarizerTool = summarizer.asTool({ toolName: 'summarize_text', toolDescription: 'Generate a concise summary of the supplied text.',});
const mainAgent = new Agent({ name: 'Research assistant', tools: [summarizerTool],});在底层,SDK 会:
- 创建一个只包含
input参数的函数工具。 - 当该工具被调用时,使用该输入运行子智能体。
- 返回最后一条消息,或由
customOutputExtractor提取的输出。
当您将智能体作为工具运行时,Agents SDK 会使用默认设置创建一个 runner,并在函数执行过程中用它来运行该智能体。如果您希望提供 runConfig 或 runOptions 中的任意属性,可以将它们传给 asTool() 方法,以自定义 runner 的行为。
您还可以通过 asTool() 选项在智能体工具上设置 needsApproval 和 isEnabled,以便集成人工干预流程和按条件控制工具可用性。
在 customOutputExtractor 内,可使用 result.agentToolInvocation 检查当前的 Agent.asTool() 调用。在该回调中,结果始终来自 Agent.asTool(),因此 agentToolInvocation 始终已定义,并暴露 toolName、toolCallId 和 toolArguments。常规应用上下文和 toolInput 则使用 result.runContext 获取。此元数据仅限当前嵌套调用作用域,不会序列化到 RunState 中。
import { Agent } from '@openai/agents';
const billingAgent = new Agent({ name: 'Billing Agent', instructions: 'Handle billing questions and subscription changes.',});
const billingTool = billingAgent.asTool({ toolName: 'billing_agent', toolDescription: 'Handles customer billing questions.', customOutputExtractor(result) { console.log('tool', result.agentToolInvocation.toolName); // Direct invoke() calls may not have a model-generated tool call id. console.log('call', result.agentToolInvocation.toolCallId); console.log('args', result.agentToolInvocation.toolArguments);
return String(result.finalOutput ?? ''); },});
const orchestrator = new Agent({ name: 'Support Orchestrator', instructions: 'Delegate billing questions to the billing agent tool.', tools: [billingTool],});agent.asTool() 的高级结构化输入选项:
inputBuilder:将结构化工具参数映射为嵌套智能体输入负载。includeInputSchema:在嵌套运行中包含输入 JSON schema,以获得更强的 schema 感知行为。resumeState:控制恢复嵌套序列化RunState时的上下文协调策略:'merge'(默认)将实时审批/上下文状态合并到序列化状态中,'replace'改为使用当前运行上下文,'preferSerialized'则在恢复时保持序列化上下文不变。
来自智能体工具的流式事件
Section titled “来自智能体工具的流式事件”智能体工具可以将所有嵌套运行事件流式回传给您的应用。请根据您构造工具的方式选择适合的钩子风格:
import { Agent } from '@openai/agents';
const billingAgent = new Agent({ name: 'Billing Agent', instructions: 'Answer billing questions and compute simple charges.',});
const billingTool = billingAgent.asTool({ toolName: 'billing_agent', toolDescription: 'Handles customer billing questions.', // onStream: simplest catch-all when you define the tool inline. onStream: (event) => { console.log(`[onStream] ${event.event.type}`, event); },});
// on(eventName) lets you subscribe selectively (or use '*' for all).billingTool.on('run_item_stream_event', (event) => { console.log('[on run_item_stream_event]', event);});billingTool.on('raw_model_stream_event', (event) => { console.log('[on raw_model_stream_event]', event);});
const orchestrator = new Agent({ name: 'Support Orchestrator', instructions: 'Delegate billing questions to the billing agent tool.', tools: [billingTool],});- 事件类型与
RunStreamEvent['type']一致:raw_model_stream_event、run_item_stream_event、agent_updated_stream_event。 onStream是最简单的“全量捕获”方式,适合内联声明工具时使用(tools: [agent.asTool({ onStream })])。如果您不需要按事件分别路由,使用它即可。on(eventName, handler)允许您选择性订阅(或使用'*'),更适合需要细粒度处理或希望在创建后再附加监听器的场景。- 如果您提供了
onStream或任意on(...)处理器,agent-as-tool 会自动以流式模式运行;否则将保持非流式路径。 - 处理器会并行调用,因此缓慢的
onStream回调不会阻塞on(...)处理器(反之亦然)。 - 当工具通过模型工具调用被触发时,会提供
toolCallId;直接invoke()调用或某些 provider 的特殊行为可能不会提供它。
5. MCP 服务器
Section titled “5. MCP 服务器”您可以通过 Model Context Protocol(MCP) 服务器暴露工具,并将其附加到智能体。例如,您可以使用 MCPServerStdio 来启动并连接到 stdio MCP 服务器:
import { Agent, MCPServerStdio } from '@openai/agents';
const server = new MCPServerStdio({ fullCommand: 'pnpm exec mcp-server-filesystem ./sample_files',});
await server.connect();
const agent = new Agent({ name: 'Assistant', mcpServers: [server],});完整示例请参见 filesystem-example.ts。此外,如果您在寻找 MCP 服务器工具集成的完整指南,请参阅MCP 集成。在管理多个服务器(或处理部分失败)时,请使用 connectMcpServers,并参考MCP 集成中的生命周期管理建议。
6. 实验性:Codex 工具
Section titled “6. 实验性:Codex 工具”@openai/agents-extensions/experimental/codex 提供 codexTool(),这是一个函数工具,会将模型的工具调用路由到 Codex SDK,使智能体能够自主运行工作区范围内的任务(shell、文件编辑、MCP 工具)。该接口为实验性功能,后续可能发生变化。
请先安装依赖:
npm install @openai/agents-extensions @openai/codex-sdk快速开始:
import { Agent } from '@openai/agents';import { codexTool } from '@openai/agents-extensions/experimental/codex';
export const codexAgent = new Agent({ name: 'Codex Agent', instructions: 'Use the codex tool to inspect the workspace and answer the question. When skill names, which usually start with `$`, are mentioned, you must rely on the codex tool to use the skill and answer the question.', tools: [ codexTool({ sandboxMode: 'workspace-write', workingDirectory: '/path/to/repo', defaultThreadOptions: { model: 'gpt-5.4', networkAccessEnabled: true, webSearchEnabled: false, }, }), ],});需要了解的内容:
- 认证:提供
CODEX_API_KEY(推荐)或OPENAI_API_KEY,也可传入codexOptions.apiKey。 - 输入:严格 schema——
inputs至少必须包含一个{ type: 'text', text }或{ type: 'local_image', path }。 - 安全性:将
sandboxMode与workingDirectory搭配使用;如果目录不是 Git 仓库,请设置skipGitRepoCheck。 - 线程:
useRunContextThreadId: true会在runContext.context中读取/存储最新线程 ID,这对于在应用状态中跨轮次复用很有帮助。 - 线程 ID 优先级:工具调用中的
threadId(如果您的 schema 包含它)优先,其次是 run-context 中的线程 ID,最后才是codexTool({ threadId })。 - 运行上下文键:对于
name: 'codex',默认是codexThreadId;对于name: 'engineer'这类名称,则为codexThreadId_<suffix>(规范化后得到codex_engineer)。 - 可变上下文要求:启用
useRunContextThreadId时,请将可变对象或Map作为run(..., { context })传入。 - 命名:工具名会被规范化到
codex命名空间中(engineer会变为codex_engineer),并且同一智能体中的重复 Codex 工具名会被拒绝。 - 流式传输:
onStream会镜像 Codex 事件(推理、命令执行、MCP 工具调用、文件变更、Web 搜索),便于您记录或追踪进度。 - 输出:工具结果包含
response、usage和threadId,并且 Codex token 用量会记录到RunContext中。 - 结构:
outputSchema可以是描述符、JSON schema 对象或 Zod 对象。对于 JSON 对象 schema,additionalProperties必须为false。
运行上下文线程复用示例:
import { Agent, run } from '@openai/agents';import { codexTool } from '@openai/agents-extensions/experimental/codex';
// Derived from codexTool({ name: 'engineer' }) when runContextThreadIdKey is omitted.type ExampleContext = { codexThreadId_engineer?: string;};
const agent = new Agent<ExampleContext>({ name: 'Codex assistant', instructions: 'Use the codex tool for workspace tasks.', tools: [ codexTool({ // `name` is optional for a single Codex tool. // We set it so the run-context key is tool-specific and to avoid collisions when adding more Codex tools. name: 'engineer', // Reuse the same Codex thread across runs that share this context object. useRunContextThreadId: true, sandboxMode: 'workspace-write', workingDirectory: '/path/to/repo', defaultThreadOptions: { model: 'gpt-5.4', approvalPolicy: 'never', }, }), ],});
// The default key for useRunContextThreadId with name=engineer is codexThreadId_engineer.const context: ExampleContext = {};
// First turn creates (or resumes) a Codex thread and stores the thread ID in context.await run(agent, 'Inspect src/tool.ts and summarize it.', { context });// Second turn reuses the same thread because it shares the same context object.await run(agent, 'Now list refactoring opportunities.', { context });
const threadId = context.codexThreadId_engineer;工具策略与最佳实践
Section titled “工具策略与最佳实践”工具使用行为
Section titled “工具使用行为”关于如何控制模型必须在何时、以何种方式使用工具(modelSettings.toolChoice、toolUseBehavior 等),请参阅智能体。
- 简短且明确的描述——说明工具做什么以及何时使用它。
- 校验输入——尽可能使用 Zod schema 进行严格 JSON 校验。
- 避免在错误处理器中产生副作用——
errorFunction应返回有帮助的字符串,而不是抛出异常。 - 一个工具只负责一件事——小而可组合的工具更有利于模型推理。