スキル一覧に戻る
kkhys

adding-hooks

by kkhys

My personal marketplace for Claude Code.

1🍴 0📅 2026年1月21日
GitHubで見るManusで実行

SKILL.md


Claude Code Hooks Configuration

Quick Start

First, ask the user where to add hooks:

  • ~/.claude/settings.json - User-scoped (personal, all projects)
  • .claude/settings.json - Project-scoped (shared via git)
  • .claude/settings.local.json - Local project (not committed)
  • Plugin hooks/hooks.json - Plugin-bundled hooks

Basic Structure

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

Hook Events

EventWhenCommon Use
PreToolUseBefore tool executionValidation, blocking
PostToolUseAfter tool completionFormatting, logging
PermissionRequestPermission dialog shownAuto-approve/deny
UserPromptSubmitUser sends promptContext injection
StopAgent finishesTask completion check
SubagentStopSubagent finishesSubagent validation
SessionStartSession beginsEnvironment setup
SessionEndSession endsCleanup tasks
NotificationNotification sentAlerts
PreCompactBefore compactCustom summarization

See events.md for detailed event documentation.

Matchers

For PreToolUse, PostToolUse, PermissionRequest:

  • Exact match: "Write" - matches Write tool only
  • Regex: "Edit|Write" - matches Edit or Write
  • All tools: "*" or "" or omit matcher
  • MCP tools: "mcp__server__tool" pattern

Case-sensitive: Writewrite

Hook Types

Command Hooks

{
  "type": "command",
  "command": "\"$CLAUDE_PROJECT_DIR\"/scripts/check.sh",
  "timeout": 30
}

Prompt Hooks (LLM evaluation)

{
  "type": "prompt",
  "prompt": "Evaluate if Claude should stop: $ARGUMENTS. Check if all tasks complete.",
  "timeout": 30
}

Supported for: Stop, SubagentStop, UserPromptSubmit, PreToolUse, PermissionRequest

Exit Codes

CodeMeaningBehavior
0SuccessContinue execution
2BlockBlock tool/prompt, show stderr to Claude
OtherNon-blocking errorShow stderr, continue

Common Patterns

Auto-format on Write/Edit

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/scripts/format.sh"
          }
        ]
      }
    ]
  }
}

Validate Bash Commands

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/scripts/validate-bash.sh"
          }
        ]
      }
    ]
  }
}

Inject Context on Prompt

{
  "hooks": {
    "UserPromptSubmit": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "echo \"Current time: $(date)\""
          }
        ]
      }
    ]
  }
}

Protect Files

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/scripts/protect-files.sh"
          }
        ]
      }
    ]
  }
}

Hook Script Template

#!/usr/bin/env bash
set -euo pipefail

# Read JSON input from stdin
input=$(cat)

# Parse fields with jq
tool_name=$(echo "$input" | jq -r '.tool_name // empty')
tool_input=$(echo "$input" | jq -r '.tool_input // empty')

# Validation logic here

# Exit 0: success, Exit 2: block with stderr message
exit 0

See scripts.md for script examples.

JSON Output (Advanced)

For structured control, output JSON to stdout (exit code 0):

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "allow",
    "permissionDecisionReason": "Auto-approved"
  }
}

See json-output.md for complete schema.

Environment Variables

VariableDescription
CLAUDE_PROJECT_DIRProject root directory
CLAUDE_PLUGIN_ROOTPlugin directory (plugins only)
CLAUDE_ENV_FILEEnv file path (SessionStart only)
CLAUDE_CODE_REMOTE"true" if remote environment

Security Best Practices

  1. Quote variables: "$VAR" not $VAR
  2. Validate input: Don't trust stdin blindly
  3. Block path traversal: Check for .. in paths
  4. Use absolute paths: Full script paths
  5. Skip sensitive files: Avoid .env, .git/, keys

Debugging

# Check registered hooks
/hooks

# Debug mode for execution details
claude --debug

Common issues:

  • Unescaped quotes in JSON: Use \"
  • Wrong matcher case: Write not write
  • Missing executable permission: chmod +x script.sh

Resources

スコア

総合スコア

50/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
言語

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

+5
タグ

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

0/5

レビュー

💬

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