claude-code-hooks

Claude Code hook system for pre/post tool execution. Triggers on: hooks, PreToolUse, PostToolUse, hook script, tool validation, audit logging.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "claude-code-hooks" with this command: npx skills add 0xdarkmatter/claude-mods/0xdarkmatter-claude-mods-claude-code-hooks

Claude Code Hooks

Execute custom scripts before/after Claude Code tool invocations.

Quick Reference

EventWhenHas Matcher
PreToolUseBefore tool executionYes
PostToolUseAfter tool completesYes
PermissionRequestPermission dialog shownYes
NotificationNotifications sentYes
UserPromptSubmitUser submits promptNo
StopAgent finishesNo
SubagentStopSubagent finishesNo
PreCompactBefore context compactionNo
SessionStartSession begins/resumesNo
SessionEndSession endsNo

Basic Configuration

Add to ~/.claude/settings.json or .claude/settings.local.json:

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{
        "type": "command",
        "command": "$CLAUDE_PROJECT_DIR/hooks/validate.sh",
        "timeout": 5000
      }]
    }]
  }
}

Matcher Patterns

PatternMatches
"Write"Only Write tool
"*" or ""All tools
"mcp__*"All MCP tools
"Bash"Bash commands

Hook Script Requirements

#!/bin/bash
# Receives JSON via stdin: { "tool_name": "...", "tool_input": {...} }
INPUT=$(cat)
TOOL=$(echo "$INPUT" | jq -r '.tool_name')

# Exit codes:
# 0 = Success (continue)
# 2 = Block with error (stderr shown to Claude)
# Other = Non-blocking error

Common Use Cases

Use CaseEventExample
Validate inputsPreToolUseBlock dangerous commands
Audit loggingPostToolUseLog all tool usage
Custom approvalPermissionRequestSlack notification
Session initSessionStartLoad project context

Security Checklist

  • Quote all variables: "$VAR" not $VAR
  • Validate paths (no .. traversal)
  • Use $CLAUDE_PROJECT_DIR for paths
  • Set reasonable timeouts
  • Handle jq parsing errors

Troubleshooting

# Debug hook loading
claude --debug

# List registered hooks
/hooks

# Test script manually
echo '{"tool_name":"Bash"}' | ./hooks/validate.sh

Official Documentation

Additional Resources

  • ./references/hook-events.md - All events with input/output schemas
  • ./references/configuration.md - Advanced config patterns
  • ./references/security-ops.md - Production security

See Also: claude-code-debug for troubleshooting, claude-code-headless for CLI automation

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Security

security-patterns

No summary provided by upstream source.

Repository SourceNeeds Review
Security

security-ops

No summary provided by upstream source.

Repository SourceNeeds Review
General

file-search

No summary provided by upstream source.

Repository SourceNeeds Review