CC Guide
設定

Hooks設定リファレンス

Claude CodeのHookシステム。ツール実行前後の自動処理、イベント種別、終了コード、設定方法を解説。

hooksautomationlifecyclesettings

概要

Hooksはツールの実行前後に自動的に実行されるカスタムコマンドです。settings.jsonhooks ブロックで設定します。コードのフォーマット、型チェック、セキュリティスキャンなどを自動化できます。

イベント種別

基本イベント

イベントタイミングMatcher
PreToolUseツール実行前ツール名(正規表現対応)
PostToolUseツール実行後ツール名(正規表現対応)
Notification通知送信時なし
UserPromptSubmitユーザー入力送信時なし
Stopメインエージェント完了時なし
SubagentStopサブエージェント完了時なし
PreCompactCompact実行前manual / auto
SessionStartセッション開始時startup / resume / clear / compact
SessionEndセッション終了時なし
InstructionsLoadedCLAUDE.md/rules読み込み時session_start, nested_traversal 等

拡張イベント

必要に応じて使用する高度なフックイベントです。

イベントタイミングMatcher
TaskCreatedサブタスク生成時なし
CwdChanged作業ディレクトリ変更時なし
FileChangedファイル変更検出時ファイルパス(正規表現対応)
PermissionDenied権限拒否時ツール名(正規表現対応)

設定構造

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "npx prettier --write $CLAUDE_FILE_PATH"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "npx tsc --noEmit"
          }
        ]
      }
    ]
  }
}

条件付き実行(if フィールド)

if フィールドを使うと、Hook の実行条件を細かく制御できます。条件式が true を返す場合のみ Hook が実行されます。

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "if": "toolInput.command.includes('git push')",
        "hooks": [
          {
            "type": "command",
            "command": "echo 'Push detected - reviewing changes...'"
          }
        ]
      }
    ]
  }
}

利用可能なコンテキスト変数

イベント変数説明
PreToolUsetoolInputツールへの入力パラメータ
PreToolUsetoolName実行されるツール名
PostToolUsetoolInputツールへの入力パラメータ
PostToolUsetoolOutputツールの出力結果
PostToolUsetoolName実行されたツール名

使用例

{
  "matcher": "Edit|Write",
  "if": "toolInput.file_path.endsWith('.ts') || toolInput.file_path.endsWith('.tsx')",
  "hooks": [
    {
      "type": "command",
      "command": "npx tsc --noEmit"
    }
  ]
}

if フィールドを省略した場合、matcher に一致するすべてのイベントで Hook が実行されます。

非同期実行

Hookを非ブロッキングで実行できます。長時間の処理に適しています。

{
  "hooks": [
    {
      "type": "command",
      "command": "long-running-check.sh",
      "async": true,
      "timeout": 60
    }
  ]
}
オプション説明デフォルト
async非ブロッキング実行false
timeoutタイムアウト秒数600

終了コード

Hookの終了コードによってClaudeの動作が変わります。

終了コード動作
0成功。stdoutがユーザーに表示される
2ブロック。stderrがClaudeにフィードバックとして返される
その他非ブロッキングエラー。stderrがユーザーに表示される

終了コード 2 は強力な安全メカニズムです。例えば、機密ファイルの編集をブロックできます。

#!/bin/bash
# secret-guard.sh
if echo "$CLAUDE_FILE_PATH" | grep -q ".env"; then
  echo ".env files must not be edited by AI" >&2
  exit 2
fi

InstructionsLoadedペイロード

InstructionsLoaded イベントのHookには以下の情報が渡されます。

フィールド説明
file_path読み込まれたファイルの絶対パス
memory_typeスコープ: User, Project, Local, Managed
load_reason理由: session_start, nested_traversal 等
globsFrontmatterのglobパターン(省略可)

実用的なHook例

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit",
        "hooks": [
          {
            "type": "command",
            "command": "npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null"
          }
        ]
      }
    ],
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "grep -rn 'console.log' --include='*.ts' --include='*.tsx' src/ && exit 2 || exit 0"
          }
        ]
      }
    ]
  }
}

関連コンテンツ