mcpField
C
claude-code-hooks

claude-code-hooks

github.com/0xdarkmatterAgent workflows7839 installsUpdated 2w ago
Repository

About claude-code-hooks

claude-code-hooks is a skill for configuring hooks around Claude Code tool calls. It helps developers run custom scripts before or after events, with guidance on matchers, configuration, debugging, and script safety.

Key features

  1. 01Run scripts before or after tool calls
  2. 02Match hooks by event and tool
  3. 03Configure permission and session events
  4. 04Receive JSON through standard input
  5. 05Guidance for debugging and script safety

Use cases

  1. 01Block risky commands before Bash execution
  2. 02Log tool calls for auditing
  3. 03Load project context at session start
  4. 04Set up custom permission-request approval

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/0xdarkmatter/claude-mods --skill claude-code-hooks

After installing, type/claude-code-hooksin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

assets/
.gitkeep
references/
configuration.md
security-patterns.md
hook-events.md
scripts/
.gitkeep

Details

From the project README

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

Comments

Markdown supported