DeepSeek Harness: ツールの使用と承認戦略
最終更新:2026-08-31
ツールは Agent の「手足」——ツールがなければ、Agent は「話す」ことしかできません;ツールがあれば、Agent はファイルの読み書き、コマンドの実行、コードの検索、計画の作成ができます。DSH のツールシステムは Cordis プラグインアーキテクチャ上に構築されています——すべてのツールはプラグインであり、拡張、置換、組み合わせが可能です。
📋 前提知識:05-modes.md の完了、4つの動作モードに精通していること
1. 学習内容
- DSH 内蔵ツール一覧と機能
- ツール実行パイプラインの3つの段階
- 各ツールのユースケースとサンプル
- ツール承認ポリシーの設定
- カスタムツールの作成方法
2. 内蔵ツールの概要
(1) ツール概要
graph TB
subgraph DSHTools[DSH Built-in Tools]
FE[file_edit<br/>File Read/Write & Edit]
SH[shell<br/>Shell Command Execution]
SR[search<br/>Code & File Search]
SK[skills<br/>Skill Invocation]
PL[plan<br/>Plan Creation & Tracking]
SB[sandbox<br/>Sandbox Environment Management]
end
(2) ツール機能比較
| ツール | 機能 | セキュリティレベル | 承認が必要 |
|---|---|---|---|
| file_edit | ファイルの作成、読み取り、編集、削除 | 🔴 高 | はい |
| shell | Shell コマンドの実行 | 🔴 高 | はい |
| search | ファイルとコード内容の検索 | 🟢 低 | いいえ |
| skills | 定義済みスキルテンプレートの呼び出し | 🟡 中 | 状況による |
| plan | 実行計画の作成と追跡 | 🟢 低 | いいえ |
| sandbox | サンドボックス環境の管理 | 🟡 中 | はい |
3. file_edit — ファイル操作ツール
(1) サポートされる操作
file_edit は最もよく使われるツールで、4つの操作をサポートしています:
| 操作 | 説明 | 承認が必要 |
|---|---|---|
read |
ファイル内容の読み取り | 承認不要 |
create |
新規ファイルの作成 | 承認必要 |
edit |
既存ファイルの編集 | 承認必要 |
delete |
ファイルの削除 | 承認必要 |
(2) ファイルの読み取り
▶ サンプル 1:ファイル内容の読み取り
// Agent's file_edit call parameters
{
action: "read",
path: "src/config.ts",
encoding: "utf-8"
}
読み取り後、Agent はファイル内容を自動的に分析します:
🤖 Agent:
🔍 Using tool: file_edit (read)
→ Path: src/config.ts
→ Size: 1.2KB
この設定ファイルは3つの設定をエクスポートしています:
- DATABASE_URL:データベース接続文字列
- PORT:サービスポート(デフォルト 3000)
- LOG_LEVEL:ログレベル(デフォルト info)
(3) ファイルの作成
▶ サンプル 2:新規ファイルの作成
// Agent calls file_edit to create a file
{
action: "create",
path: "src/utils/logger.ts",
content: "export function log(level: string, msg: string) {\n const ts = new Date().toISOString();\n console.log(`[${ts}] [${level}] ${msg}`);\n}"
}
ファイル作成は承認ポップアップをトリガーし、ユーザーの確認後にのみファイルが書き込まれます。
(4) ファイルの編集
▶ サンプル 3:ファイルの編集(diff モード)
DSH のファイル編集は diff モードを使用し、変更が必要な部分のみを修正します:
// Agent calls file_edit to edit a file
{
action: "edit",
path: "src/app.ts",
changes: [
{
type: "insert",
line: 5,
content: "import { log } from './utils/logger';"
},
{
type: "replace",
line: 23,
oldContent: "console.log('Server started');",
newContent: "log('info', 'Server started');"
}
]
}
承認ポップアップに diff ビューが表示されます:
⚠️ Approval Required: Edit file src/app.ts
+5 | import { log } from './utils/logger';
-23| console.log('Server started');
+23| log('info', 'Server started');
[Allow] [Always] [Deny]
(5) 可逆的な編集
すべての file_edit の変更は可逆的です。DSH は編集前に自動的にファイルスナップショットを保存します:
graph LR
A[Pre-edit Snapshot] --> B[Apply Edits]
B --> C[Post-edit State]
C -->|Rollback| A
4. shell — Shell コマンドツール
(1) 基本的な使用方法
▶ サンプル 4:安全なコマンドの実行
// Agent executes ls command
{
command: "ls -la src/",
cwd: "/home/alice/project",
timeout: 30000
}
(2) コマンドのセキュリティ分類
DSH は Shell コマンドを危険度レベルで分類します:
| レベル | コマンド例 | 承認ポリシー |
|---|---|---|
| 安全 | ls, cat, grep, head, wc |
自動許可 |
| 中程度 | npm install, git add, mkdir |
承認必要 |
| 危険 | rm, chmod, sudo, dd |
承認+確認必要 |
| 禁止 | rm -rf /, mkfs, > /dev/sda |
自動拒否 |
▶ サンプル 5:中リスクコマンドの実行
// Agent executes npm view (view package info, moderate risk)
{
command: "npm view jsonwebtoken",
cwd: "/home/alice/project",
timeout: 120000
}
承認ポップアップ:
⚠️ Approval Required: Execute shell command
Command: npm view jsonwebtoken
Working directory: /home/alice/project
Estimated packages: 1
[Allow] [Always for npm] [Deny]
(3) タイムアウトと中断
// Shell tool parameters
interface ShellParams {
command: string;
cwd?: string;
timeout?: number; // Timeout in milliseconds, default 30000
env?: Record<string, string>; // Additional environment variables
}
長時間実行されるコマンドはタイムアウトで中断されます:
🤖 Agent:
🔧 Using tool: shell
→ Command: npm run build
→ Timeout: 120000ms
⏱️ Build completed in 45s
→ Output: Build successful. 15 files generated.
5. search — 検索ツール
(1) 検索モード
検索ツールは複数の検索モードをサポートしています:
| モード | 説明 | 例 |
|---|---|---|
| ファイル検索 | ファイル名/パスで検索 | *.test.ts |
| 内容検索 | コンテンツ正規表現で検索 | import.*from |
| シンボル検索 | 関数/クラス定義を検索 | class UserService |
▶ サンプル 6:ファイルの検索
// Search for all test files
{
pattern: "*.test.ts",
type: "file",
maxResults: 50
}
▶ サンプル 7:コード内容の検索
// Search for all import statements
{
pattern: "import.*from 'express'",
type: "content",
filePattern: "*.ts",
maxResults: 100
}
(2) 検索結果の表示
🤖 Agent:
🔍 Using tool: search
→ Pattern: import.*from 'express'
→ Type: content
→ Results: 8 matches
Found in:
src/app.ts:1 — import express from 'express';
src/routes/users.ts:3 — import express from 'express';
src/routes/auth.ts:2 — import express from 'express';
...
6. skills — スキルツール
(1) スキルの概念
スキルは定義済みのタスクテンプレートで、一般的な操作の完全なワークフローをカプセル化します:
graph LR
USER[User Request] --> SK[Skill Template]
SK --> T1[Tool Call 1]
SK --> T2[Tool Call 2]
SK --> T3[Tool Call 3]
(2) 内蔵スキル
| スキル | 説明 | 含まれる操作 |
|---|---|---|
| add-test | 関数にテストを追加 | search → file_edit (create) |
| refactor | 関数/クラスの抽出 | file_edit (read) → file_edit (edit × N) |
| debug | エラーのデバッグ | search → shell → file_edit |
| document | ドキュメントコメントの追加 | file_edit (read) → file_edit (edit) |
▶ サンプル 8:スキルの呼び出し
// Invoke add-test skill
{
skill: "add-test",
params: {
target: "src/utils/format.ts::formatDate",
framework: "jest"
}
}
7. plan — 計画ツール
(1) 計画の作成と追跡
plan ツールはマルチステップタスクの実行計画の作成と追跡に使用されます:
▶ サンプル 9:実行計画の作成
// Create a plan
{
action: "create",
steps: [
{ id: 1, desc: "Install dependencies", tool: "shell" },
{ id: 2, desc: "Create auth module", tool: "file_edit" },
{ id: 3, desc: "Update app.ts", tool: "file_edit" },
{ id: 4, desc: "Write tests", tool: "file_edit" },
{ id: 5, desc: "Run tests", tool: "shell" }
]
}
▶ サンプル 10:計画ステータスの更新
// Mark step as complete
{
action: "update",
stepId: 1,
status: "completed",
result: "Installed jsonwebtoken, bcryptjs"
}
(2) plan ツールと PTC モード
plan ツールは PTC モードの基盤です:
graph TD
PTC[PTC Mode] --> PLAN[plan Tool Creates Plan]
PLAN --> USER[User Reviews]
USER --> EXEC[Execute Steps Per Plan]
EXEC --> UPDATE[plan Tool Updates Status]
UPDATE --> DONE{All Complete?}
DONE -->|No| EXEC
DONE -->|Yes| REPORT[Output Summary]
8. ツール実行パイプライン
(1) 3段階パイプライン
すべてのツール呼び出しは3つの段階を経ます:
graph LR
PRE[pre-execute<br/>パラメータ検証<br/>権限チェック<br/>承認ポップアップ] --> EXEC[execute<br/>実際の実行<br/>出力のキャプチャ] --> POST[post-execute<br/>ログ記録<br/>イベント発行<br/>ステータス更新]
▶ サンプル 11:パイプライン疑似コード
async function executeToolPipeline(tool: Tool, params: Params): Promise<Result> {
// Stage 1: pre-execute
const preResult = await preExecute(tool, params);
if (preResult.denied) {
throw new ToolDeniedError(preResult.reason);
}
// Stage 2: execute
const result = await tool.execute(params);
// Stage 3: post-execute
await postExecute(tool, params, result);
ctx.emit('tool.executed', { tool: tool.name, params, result });
return result;
}
(2) pre-execute 段階
pre-execute は検証と承認を処理します:
interface PreExecuteResult {
allowed: boolean;
reason?: string;
modifiedParams?: Params;
}
| チェック項目 | 説明 |
|---|---|
| パラメータ検証 | パラメータのフォーマットと型が正しいか |
| 権限チェック | ユーザーがこの操作を実行する権限があるか |
| 承認ポップアップ | 危険な操作にユーザーの確認が必要か |
| サンドボックスチェック | 操作がワークスペースの範囲内か |
(3) post-execute 段階
post-execute は記録と通知を処理します:
interface PostExecuteAction {
log: boolean; // Record to session log
emit: boolean; // Emit event
updateTrajectory: boolean; // Update Trajectory
notifyUI: boolean; // Notify Web UI update
}
9. ツール承認ポリシー
(1) ポリシー設定
▶ サンプル 12:承認ポリシー設定
# dsh.config.yaml
approval:
# グローバルデフォルトポリシー
default: ask
# ツールごとの設定
tools:
file_edit:
read: always # 読み取りは常に許可
create: ask # 作成は承認が必要
edit: ask # 編集は承認が必要
delete: ask_with_confirm # 削除は二重確認が必要
shell:
safe: always # 安全なコマンドは常に許可
moderate: ask # 中程度のコマンドは承認が必要
dangerous: deny # 危険なコマンドは自動拒否
search:
default: always # 検索は常に許可
skills:
default: ask # スキル呼び出しは承認が必要
plan:
default: always # 計画は常に許可
(2) 承認モードの説明
| モード | 説明 | ユースケース |
|---|---|---|
always |
常に許可、ポップアップなし | 安全な操作 |
ask |
承認が必要、ポップアップで確認 | 危険な操作 |
ask_with_confirm |
二重確認が必要 | 極めて危険な操作 |
deny |
自動拒否 | 決して許可してはいけない操作 |
10. カスタムツール入門
(1) カスタムツールの作成
DSH ツールは Cordis プラグインで、TypeScript で記述します:
▶ サンプル 13:カスタム HTTP リクエストツール
import { definePlugin } from '@deepseek-ai/dsh';
export default definePlugin({
name: 'tool-http-request',
version: '1.0.0',
contribute(ctx) {
ctx.registerTool({
name: 'http_request',
description: 'Make HTTP requests to external APIs',
parameters: {
type: 'object',
properties: {
url: { type: 'string', description: 'Request URL' },
method: { type: 'string', enum: ['GET', 'POST', 'PUT', 'DELETE'] },
headers: { type: 'object', description: 'Request headers' },
body: { type: 'string', description: 'Request body' }
},
required: ['url', 'method']
},
async execute(params) {
const response = await fetch(params.url, {
method: params.method,
headers: params.headers,
body: params.body
});
return {
status: response.status,
body: await response.text()
};
}
});
}
});
(2) カスタムツールの登録
カスタムツールプラグインはプロジェクトの .dsh/plugins/ ディレクトリに配置します:
.dsh/
└── plugins/
└── tool-http-request/
├── index.ts
└── package.json
または設定ファイルで指定:
# dsh.config.yaml
plugins:
- path: "./custom-tools/http-request"
- path: "./custom-tools/database-query"
(3) カスタムツールの承認
カスタムツールにも承認ポリシーを定義する必要があります:
ctx.registerTool({
name: 'http_request',
// ...
approval: {
level: 'ask', // Default requires approval
rules: [
{ match: { method: 'GET' }, level: 'always' }, // GET requests auto-allowed
{ match: { method: 'POST' }, level: 'ask' }, // POST requires approval
{ match: { method: 'DELETE' }, level: 'deny' } // DELETE auto-denied
]
}
});
❓ よくある質問
tools.disabled: ["shell"] を設定して、指定したツールを無効にできます。📖 まとめ
- DSH には6つの内蔵ツール:file_edit、shell、search、skills、plan、sandbox
- ツール実行には3段階パイプライン:pre-execute → execute → post-execute
- file_edit は読み取り/書き込み/作成/編集をサポート;すべての変更は可逆的
- shell はコマンドをセキュリティレベルで分類:安全/中程度/危険/禁止
- search はファイル名/内容/シンボル検索モードをサポート
- 承認ポリシーはツールと操作タイプごとに設定で細かく制御
- カスタムツールは本質的に Cordis プラグインで、TypeScript で記述
📝 練習問題
1. ⭐ 基礎:DSH Agent を使って以下の操作を完了してください:1) search ツールでプロジェクト内のすべての TypeScript ファイルを検索;2) file_edit でそのうち1つを読み取り。両方のツール呼び出しのパラメータと結果を記録すること。
2. ⭐⭐ 応用:承認ポリシーを設定し、file_edit の読み取り操作は自動許可、作成/編集操作は承認必要、削除操作は二重確認必要にしてください。各操作をテストして承認ポリシーが機能していることを確認すること。
3. ⭐⭐⭐ チャレンジ:カスタムツールプラグインを作成して、現在の Git リポジトリの最新5コミットをクエリする(git log -5 --oneline を呼び出す)ツールを定義し、DSH に登録して、Agent に正常に呼び出させること。