← スキル一覧に戻る

adding-hooks
by kkhys
My personal marketplace for Claude Code.
⭐ 1🍴 0📅 2026年1月21日
SKILL.md
name: adding-hooks description: Add and configure Claude Code hooks for event-driven automation. Use when setting up hooks, creating hook scripts, or automating tool-related workflows.
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
| Event | When | Common Use |
|---|---|---|
PreToolUse | Before tool execution | Validation, blocking |
PostToolUse | After tool completion | Formatting, logging |
PermissionRequest | Permission dialog shown | Auto-approve/deny |
UserPromptSubmit | User sends prompt | Context injection |
Stop | Agent finishes | Task completion check |
SubagentStop | Subagent finishes | Subagent validation |
SessionStart | Session begins | Environment setup |
SessionEnd | Session ends | Cleanup tasks |
Notification | Notification sent | Alerts |
PreCompact | Before compact | Custom 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: Write ≠ write
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
| Code | Meaning | Behavior |
|---|---|---|
| 0 | Success | Continue execution |
| 2 | Block | Block tool/prompt, show stderr to Claude |
| Other | Non-blocking error | Show 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
| Variable | Description |
|---|---|
CLAUDE_PROJECT_DIR | Project root directory |
CLAUDE_PLUGIN_ROOT | Plugin directory (plugins only) |
CLAUDE_ENV_FILE | Env file path (SessionStart only) |
CLAUDE_CODE_REMOTE | "true" if remote environment |
Security Best Practices
- Quote variables:
"$VAR"not$VAR - Validate input: Don't trust stdin blindly
- Block path traversal: Check for
..in paths - Use absolute paths: Full script paths
- 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:
Writenotwrite - Missing executable permission:
chmod +x script.sh
Resources
- events.md - Detailed event documentation
- scripts.md - Hook script examples
- json-output.md - JSON output schema
スコア
総合スコア
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
レビュー
💬
レビュー機能は近日公開予定です