スキル一覧に戻る
filipexyz

claude-code-hooks

by filipexyz

0🍴 0📅 2025年12月31日
GitHubで見るManusで実行

SKILL.md


name: claude-code-hooks description: Implement Claude Code hooks for deterministic control over agent behavior. Use when creating custom hooks for notifications, auto-formatting, logging, feedback, permissions, or lifecycle events.

Claude Code Hooks

Hooks are shell commands that execute at specific points in Claude Code's lifecycle, providing deterministic control over behavior.

Hook Events

EventWhen it runsCan block?
PreToolUseBefore tool executionYes
PermissionRequestWhen permission dialog shownYes
PostToolUseAfter tool completesFeedback only
UserPromptSubmitWhen user submits promptYes
NotificationWhen notification sentNo
StopWhen agent finishesCan continue
SubagentStopWhen subagent finishesCan continue
PreCompactBefore compact operationNo
SessionStartSession starts/resumesNo
SessionEndSession endsNo

Configuration

Hooks are defined in settings files:

  • ~/.claude/settings.json - User settings (all projects)
  • .claude/settings.json - Project settings
  • .claude/settings.local.json - Local settings (not committed)

Basic Structure

{
  "hooks": {
    "EventName": [
      {
        "matcher": "ToolPattern",
        "hooks": [
          {
            "type": "command",
            "command": "your-command-here",
            "timeout": 60
          }
        ]
      }
    ]
  }
}

Matcher patterns:

  • Exact match: Write, Bash, Read
  • Regex: Edit|Write, Notebook.*, mcp__memory__.*
  • Match all: * or ""

Hook Input

Hooks receive JSON via stdin:

{
  "session_id": "abc123",
  "transcript_path": "/path/to/transcript.jsonl",
  "cwd": "/current/directory",
  "permission_mode": "default",
  "hook_event_name": "PreToolUse",
  "tool_name": "Write",
  "tool_input": { "file_path": "/path/to/file", "content": "..." },
  "tool_use_id": "toolu_01ABC..."
}

Hook Output

Exit Codes

  • 0: Success (stdout shown in verbose mode)
  • 2: Blocking error (stderr fed back to Claude)
  • Other: Non-blocking error (stderr shown in verbose mode)

JSON Output (exit code 0)

{
  "decision": "block",
  "reason": "Explanation for Claude",
  "continue": true,
  "stopReason": "Message when continue=false",
  "systemMessage": "Warning for user"
}

Quick Examples

Log Bash Commands

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{
          "type": "command",
          "command": "jq -r '.tool_input.command' >> ~/.claude/bash.log"
        }]
      }
    ]
  }
}

Auto-Format on Save

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{
          "type": "command",
          "command": "jq -r '.tool_input.file_path' | xargs -I{} sh -c 'echo \"{}\" | grep -q \"\\.ts$\" && npx prettier --write \"{}\"'"
        }]
      }
    ]
  }
}

Block Sensitive Files

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{
          "type": "command",
          "command": "jq -e '.tool_input.file_path | test(\"\\\\.env|secrets|credentials\")' > /dev/null && echo 'Cannot modify sensitive files' >&2 && exit 2 || exit 0"
        }]
      }
    ]
  }
}

Environment Variables

  • CLAUDE_PROJECT_DIR - Absolute path to project root
  • CLAUDE_PLUGIN_ROOT - Plugin directory (for plugin hooks)
  • CLAUDE_ENV_FILE - File to persist env vars (SessionStart only)
  • CLAUDE_CODE_REMOTE - "true" if running in remote/web environment

References

スコア

総合スコア

45/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

0/5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です