ClaudeMod
Back to browse
Hooks

Claude Code Hooks Collection

karanb192's comprehensive hooks library — pre/post tool hooks for git automation, test runners, linting enforcement, session logging, and notification alerts.

Karan B321 starsAdded 1 months ago

claude-code-hooks

🪝 Ready-to-use hooks for Claude Code — safety, automation, notifications, and more.

GitHub stars License: MIT Tests

🎬 Quick Demo

Protecting SecretsBlocking Dangerous Commands
Hook blocking .env readHook blocking dangerous commands

A growing collection of tested, documented hooks you can copy, paste, and customize.


📑 Table of Contents


🪝 Hooks

Pre-Tool-Use

Runs before Claude executes a tool. Can block or modify the operation.

HookMatcherDescription
block-dangerous-commandsBashBlocks dangerous shell commands (rm -rf ~, fork bombs, curl|sh)
protect-secretsRead|Edit|Write|BashPrevents reading/modifying/exfiltrating sensitive files

Post-Tool-Use

Runs after Claude executes a tool. Can react to results.

HookMatcherDescription
auto-stageEdit|WriteAutomatically git stages files after Claude modifies them

Notification

Fires when Claude needs user attention.

HookMatcherDescription
notify-permissionpermission_prompt|idle_promptSends Slack alerts when Claude needs input

Utils

Tools to help you build and debug hooks.

ToolLanguageDescription
event-loggerPythonLogs all hook events to inspect payload structures

💡 Building a new hook? Use event-logger.py to discover what data Claude Code provides for each event before writing your own hooks.


🚀 Quick Start

1. Copy the hook script:

mkdir -p ~/.claude/hooks
cp hook-scripts/pre-tool-use/block-dangerous-commands.js ~/.claude/hooks/

2. Add to .claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node ~/.claude/hooks/block-dangerous-commands.js"
          }
        ]
      }
    ]
  }
}

3. Restart Claude Code — the hook is now active.

💡 Tip: Use multiple hooks together. Combine block-dangerous-commands + protect-secrets for comprehensive safety.


🛡️ Safety Levels

Security hooks support configurable safety levels:

LevelWhat's BlockedUse Case
criticalCatastrophic only (rm -rf ~, fork bombs, dd to disk)Maximum flexibility
high+ Risky (force push main, secrets exposure, git reset --hard)Recommended
strict+ Cautionary (any force push, sudo rm, docker prune)Maximum safety

To change: Edit the SAFETY_LEVEL constant at the top of each hook.

const SAFETY_LEVEL = 'strict'; // or 'critical', 'high'

🧪 Testing

All hooks include comprehensive tests:

# Run all tests
npm test

# Run specific hook tests
node --test hook-scripts/tests/pre-tool-use/block-dangerous-commands.test.js

Test coverage:

  • ✅ Unit tests for core functions
  • ✅ Integration tests for stdin/stdout flow
  • ✅ Config validation tests

📖 Configuration Reference

See the official Claude Code hooks documentation for:

  • All hook events and their lifecycles
  • Input/output JSON formats
  • Matcher patterns
  • Environment variables

🤝 Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

Ideas for new hooks:

HookEventDescription
protect-testsPreToolUseBlock test deletion/disabling
auto-formatPostToolUseRun prettier/black/gofmt after edits
branch-guardPreToolUseBlock changes on main/master branch
context-snapshotPreCompactPreserve context before compaction
session-summaryStopGenerate summary on session end
ntfy-notifyNotificationFree mobile push via ntfy.sh
discord-notifyNotificationDiscord webhook alerts
cost-trackerPostToolUseTrack token usage and estimate costs
tts-alertsNotificationVoice notifications via say/espeak
rules-injectorUserPromptSubmitAuto-inject CLAUDE.md rules
rate-limiterPreToolUseLimit tool calls per minute
context-injectorSessionStartInject project context on session start

📄 License

MIT © karanb192

More Hooks

Hooks

Multi-Agent Observability Hooks

Hooks for observing and debugging multi-agent Claude Code sessions — trace inter-agent messages, log tool calls, visualize agent graphs, and detect loops.

observabilitymulti-agenttracing+3
by Disler
GitHub
Hooks

TypeScript Claude Hooks

TypeScript-specific Claude Code hooks — run tsc type checking, ESLint, and Prettier enforcement after every edit so type errors never accumulate during a session.

typescripteslintprettier+3
by bartolli
GitHub
Hooks

Claude Hooks SDK

PHP/Laravel-style hooks SDK for Claude Code — define, compose, and test hooks with a clean API, middleware chaining, and built-in test helpers.

phplaravelsdk+3
by Beyond Code
GitHub

Command Palette

Search for a command to run...