コンテンツにスキップ

ツール

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

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

OpenAIResponsesModel を使用すると、以下の組み込みツールを追加できます:

ツール型文字列目的
Web 検索'web_search'インターネット検索
ファイル / リトリーバル検索'file_search'OpenAI がホストするベクトルストアを検索
コンピュータ操作'computer'GUI 操作を自動化
Code Interpreter'code_interpreter'サンドボックス環境でコードを実行
画像生成'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.`;
},
});
フィールド必須説明
nameNo省略時は関数名 (例: get_weather) が使用されます
descriptionYesLLM に表示される、わかりやすい説明文
parametersYesZod スキーマまたは元の JSON スキーマオブジェクト。Zod スキーマを使うと strict モードが自動で有効になります
strictNotrue (デフォルト) で、引数の検証に失敗した場合 SDK はモデルエラーを返します。曖昧な入力を許可したい場合は false に設定
executeYes(args, context) => string | Promise<string> – ビジネスロジックを実装します。第 2 引数は省略可能な RunContext
errorFunctionNo内部エラーをユーザー向けメッセージに変換するカスタムハンドラ (context, error) => string

無効または部分的な入力をモデルに 推測 させたい場合は、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;
},
});

3. エージェントをツールとして使用

Section titled “3. エージェントをツールとして使用”

会話を完全にハンドオフすることなく、あるエージェントが別のエージェントを 支援 してほしい場合は 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 という 1 つのパラメーターを持つ関数ツールを生成
  • ツールが呼び出されるとサブエージェントをその入力で実行
  • 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_choice, toolUseBehavior など) の制御については、エージェント を参照してください。


  • 短く明示的な説明 – ツールが 何をするかいつ使うか を記述
  • 入力を検証 – 可能な限り Zod スキーマで厳格な JSON 検証を行う
  • エラーハンドラで副作用を避けるerrorFunction は有用な文字列を返すだけにし、例外を投げない
  • ツールは単一責務 – 小さく構成可能なツールの方がモデルの推論精度が向上