コンテンツにスキップ

ツール

ツールは、エージェントが アクションを実行 できるようにします。データ取得、外部 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)
import { Agent, webSearchTool, fileSearchTool } from '@openai/agents';
const agent = new Agent({
name: 'Travel assistant',
tools: [webSearchTool(), fileSearchTool('VS_ID')],
});

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


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

Zod パラメーターを用いた関数ツール
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 スキーマまたは 元 JSON スキーマオブジェクトのいずれか。Zod のパラメーターを使うと自動的に strict モードが有効になります
strictいいえtrue(デフォルト)の場合、引数が検証に失敗すると SDK はモデルエラーを返します。あいまい一致にするには false に設定します
executeはい(args, context) => string | Promise<string> – ビジネスロジック。2 番目の引数は任意で、RunContext です
errorFunctionいいえ内部エラーを ユーザー に見える文字列に変換するためのカスタムハンドラー (context, error) => string

無効または不完全な入力をモデルに推測させたい場合は、 元 JSON スキーマ を使うときに strict モードを無効化できます:

非 strict な JSON スキーマツール
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() を使います:

エージェントをツールとして
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 を使って サーバー を起動・接続します:

ローカル MCP サーバー
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],
});

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


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


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