コンテンツにスキップ

ツール

ツールを使うことで、エージェントは 行動を実行 できます。たとえばデータの取得、外部 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 / retrieval 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 スキーマまたは raw JSON スキーマオブジェクト。Zod を使用すると strict モードが自動で有効になります
strictNotrue (デフォルト) の場合、引数の検証に失敗すると SDK はモデルエラーを返します。曖昧なマッチングを許可する場合は false に設定します
executeYes(args, context) => string | Promise<string> – ビジネスロジック。第 2 引数には省略可能な RunContext が渡されます
errorFunctionNo内部エラーをユーザー向けメッセージへ変換するカスタムハンドラー (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;
},
});

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 パラメーターを持つ関数ツールを生成
  • ツール呼び出し時にサブエージェントをその入力で実行
  • 最後のメッセージ、または 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 は有用な文字列を返すだけにし、例外は投げない
  • ツールは単一責務 – 小さく再利用可能なツールに分割するとモデルの推論精度が向上