ツール API
AI インタラクション用のカスタムツールを作成・管理します。ファイル操作、シェルコマンド、ウェブリクエストなどの強力なツールでGemini CLIを拡張しましょう。
組み込みツール
Gemini CLIですぐに使える強力なツール
ファイルシステムツール
ファイルの読み取り、書き込み、管理のためのツール
read_file
ファイルからコンテンツを読み取る
シグネチャ:
read_file(path: string): Promise<string>例:
const content = await tools.read_file('./package.json');
console.log(content);write_file
ファイルにコンテンツを書き込む
シグネチャ:
write_file(path: string, content: string): Promise<void>例:
await tools.write_file('./output.txt', 'Hello, World!');list_files
ディレクトリ内のファイルをリストする
シグネチャ:
list_files(path: string): Promise<string[]>例:
const files = await tools.list_files('./src');
console.log(files);シェルツール
シェルコマンドを実行するためのツール
run_shell_command
シェルコマンドを実行
シグネチャ:
run_shell_command(command: string, options?: ShellOptions): Promise<ShellResult>例:
const result = await tools.run_shell_command('ls -la');
console.log(result.stdout);run_script
スクリプトファイルを実行
シグネチャ:
run_script(scriptPath: string, args?: string[]): Promise<ShellResult>例:
const result = await tools.run_script('./build.sh', ['--production']);Webツール
Webリクエストと検索のためのツール
web_fetch
URLからコンテンツを取得
シグネチャ:
web_fetch(url: string, options?: FetchOptions): Promise<string>例:
const content = await tools.web_fetch('https://api.example.com/data');
console.log(content);web_search
ウェブで情報を検索
シグネチャ:
web_search(query: string, options?: SearchOptions): Promise<SearchResult[]>例:
const results = await tools.web_search('Gemini CLI documentation');
console.log(results);カスタムツール開発
Gemini CLI機能を拡張する独自のツールを作成
カスタムツールの作成
カスタム天気ツールを作成する完全な例です:
import { Tool, ToolDefinition } from '@google/generative-ai-cli';
// カスタムツールを定義
const weatherTool: ToolDefinition = {
name: 'get_weather',
description: '場所の現在の天気を取得',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: '都市と州、例:東京、日本'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: '温度単位'
}
},
required: ['location']
},
execute: async ({ location, unit = 'celsius' }) => {
// あなたの天気APIロジックをここに
const response = await fetch(`https://api.weather.com/v1/current?location=${location}&unit=${unit}`);
const data = await response.json();
return `${location}の現在の天気: ${data.temperature}°${unit === 'celsius' ? 'C' : 'F'}, ${data.description}`;
}
};
// ツールを登録
gemini.registerTool(weatherTool);
// 会話でツールを使用
const response = await gemini.ask("東京の天気はどうですか?", {
tools: ['get_weather']
});ツール登録と管理
Gemini CLIインスタンスでのツール管理
ツールを登録
// 単一のツールを登録
gemini.registerTool(myTool);
// 複数のツールを登録
gemini.registerTools([tool1, tool2, tool3]);
// プラグインから読み込み
gemini.loadPlugin('./my-tools-plugin');ツールのリストと管理
// 利用可能なすべてのツールをリスト
const tools = gemini.listTools();
// ツール情報を取得
const toolInfo = gemini.getTool('tool_name');
// ツールの登録を解除
gemini.unregisterTool('tool_name');ベストプラクティス
効果的なツールを作成するためのガイドライン
ツール設計
- • ツールを単一の責任に集中させる
- • 明確で説明的な名前と説明を提供する
- • 包括的なパラメータスキーマを定義する
- • エラーを優雅に処理し、意味のあるメッセージを提供する
パフォーマンス
- • 非同期操作を適切に実装する
- • 長時間の操作にタイムアウト処理を追加する
- • 適切な場合は結果をキャッシュする
- • 処理前に入力を検証する