コンテンツにスキップ

ツール

ツールはエージェントに アクションの実行 を可能にします。データの取得、外部 API の呼び出し、コードの実行、さらにはコンピュータの使用まで。JavaScript/TypeScript SDK は次の 4 つのカテゴリーをサポートします:

  1. Hosted tools – モデルと並行して OpenAI サーバー上で実行されます(Web 検索、ファイル検索、コンピュータ操作、Code Interpreter、画像生成)
  2. Function tools – 任意のローカル関数を JSON スキーマでラップし、LLM が呼び出せるようにします
  3. Agents as tools – エージェント全体を呼び出し可能なツールとして公開します
  4. Local MCP servers – ローカルで稼働する Model Context Protocol サーバーを接続します

OpenAIResponsesModel を使うと、次の組み込みツールを追加できます:

ツールType 文字列目的
Web search'web_search'インターネット検索
File / retrieval search'file_search'OpenAI 上にホストされたベクトルストアのクエリ
Computer use'computer'GUI 操作の自動化
Code Interpreter'code_interpreter'サンドボックス環境でコードを実行
Image generation'image_generation'テキストに基づく画像生成
Hosted tools
import { Agent, webSearchTool, fileSearchTool } from '@openai/agents';
const agent = new Agent({
name: 'Travel assistant',
tools: [webSearchTool(), fileSearchTool('VS_ID')],
});

正確なパラメーター構成は OpenAI Responses API と一致します。rankingOptions やセマンティックフィルタなどの高度なオプションは公式ドキュメントを参照してください。


tool() ヘルパーで、任意 の関数をツール化できます。

Function tool with Zod parameters
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 スキーマまたは raw な JSON スキーマオブジェクトのいずれか。Zod の parameters を使うと自動的に strict モードが有効化
strictいいえtrue(デフォルト)の場合、引数の検証に失敗すると SDK はモデルエラーを返します。あいまいなマッチングが必要な場合は false に設定
executeはい(args, context) => string | Promise<string> – ビジネスロジック。2 番目の引数は省略可能で、RunContext です
errorFunctionいいえ内部エラーをユーザーに見える文字列へ変換するカスタムハンドラー (context, error) => string

無効または部分的な入力をモデルに推測させたい場合は、raw な JSON スキーマ使用時に strict モードを無効化できます:

Non-strict JSON schema tools
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;
},
});

会話を完全にハンドオフせずに、あるエージェントが別のエージェントを支援させたい場合があります。agent.asTool() を使用します:

Agents as tools
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 パラメーターを持つ function tool を作成
  • ツールが呼び出されたときに、その入力でサブエージェントを実行
  • 最後のメッセージ、または customOutputExtractor で抽出した出力を返却

ローカルの Model Context Protocol サーバー経由でツールを公開し、エージェントに接続できます。MCPServerStdio を使ってサーバーを起動・接続します:

Local MCP server
import { Agent, MCPServerStdio } from '@openai/agents';
const server = new MCPServerStdio({
fullCommand: 'npx -y @modelcontextprotocol/server-filesystem ./sample_files',
});
await server.connect();
const agent = new Agent({
name: 'Assistant',
mcpServers: [server],
});

完全な例は filesystem-example.ts を参照してください。


モデルがいつどのようにツールを使用するか(tool_choicetoolUseBehavior など)の制御は、エージェント を参照してください。


  • 短く明確な説明 – ツールが何をするか、いつ使うかを記述
  • 入力の検証 – 可能であれば Zod スキーマで厳密な JSON 検証を実施
  • エラーハンドラーで副作用を避けるerrorFunction は有用な文字列を返し、例外を投げない
  • ツールごとに単一責任 – 小さく合成可能なツールはモデルの推論を向上