ツール
ツールはエージェントに アクションの実行 を可能にします。データの取得、外部 API の呼び出し、コードの実行、さらにはコンピュータの使用まで。JavaScript/TypeScript SDK は次の 4 つのカテゴリーをサポートします:
- Hosted tools – モデルと並行して OpenAI サーバー上で実行されます(Web 検索、ファイル検索、コンピュータ操作、Code Interpreter、画像生成)
- Function tools – 任意のローカル関数を JSON スキーマでラップし、LLM が呼び出せるようにします
- Agents as tools – エージェント全体を呼び出し可能なツールとして公開します
- Local MCP servers – ローカルで稼働する Model Context Protocol サーバーを接続します
1. Hosted tools
Section titled “1. Hosted tools”OpenAIResponsesModel
を使うと、次の組み込みツールを追加できます:
ツール | Type 文字列 | 目的 |
---|---|---|
Web search | 'web_search' | インターネット検索 |
File / retrieval search | 'file_search' | OpenAI 上にホストされたベクトルストアのクエリ |
Computer use | 'computer' | GUI 操作の自動化 |
Code Interpreter | 'code_interpreter' | サンドボックス環境でコードを実行 |
Image generation | 'image_generation' | テキストに基づく画像生成 |
import { Agent, webSearchTool, fileSearchTool } from '@openai/agents';
const agent = new Agent({ name: 'Travel assistant', tools: [webSearchTool(), fileSearchTool('VS_ID')],});
正確なパラメーター構成は OpenAI Responses API と一致します。rankingOptions
やセマンティックフィルタなどの高度なオプションは公式ドキュメントを参照してください。
2. Function tools
Section titled “2. Function tools”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.`; },});
オプションリファレンス
Section titled “オプションリファレンス”フィールド | 必須 | 説明 |
---|---|---|
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 |
非 strict な JSON スキーマツール
Section titled “非 strict な JSON スキーマツール”無効または部分的な入力をモデルに推測させたい場合は、raw な JSON スキーマ使用時に strict モードを無効化できます:
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; },});
3. Agents as tools
Section titled “3. Agents as tools”会話を完全にハンドオフせずに、あるエージェントが別のエージェントを支援させたい場合があります。agent.asTool()
を使用します:
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
で抽出した出力を返却
4. Local MCP servers
Section titled “4. Local MCP servers”ローカルの Model Context Protocol サーバー経由でツールを公開し、エージェントに接続できます。MCPServerStdio
を使ってサーバーを起動・接続します:
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
を参照してください。
ツール使用時の挙動
Section titled “ツール使用時の挙動”モデルがいつどのようにツールを使用するか(tool_choice
、toolUseBehavior
など)の制御は、エージェント を参照してください。
ベストプラクティス
Section titled “ベストプラクティス”- 短く明確な説明 – ツールが何をするか、いつ使うかを記述
- 入力の検証 – 可能であれば Zod スキーマで厳密な JSON 検証を実施
- エラーハンドラーで副作用を避ける –
errorFunction
は有用な文字列を返し、例外を投げない - ツールごとに単一責任 – 小さく合成可能なツールはモデルの推論を向上