by GowayLee
A Python SDK for claude-code hooks
# Add to your Claude Code skills
git clone https://github.com/GowayLee/cchooks
A lightweight Python Toolkit that makes building Claude Code hooks as simple as writing a few lines of code. Stop worrying about JSON parsing and focus on what your hook should actually do.
New to Claude Code hooks? Check the official docs for the big picture.
Need the full API? See the API Reference for complete documentation.
create_context() handles all the boilerplatepip install cchooks
# or
uv add cchooks
Build a PreToolUse hook that blocks dangerous file writes:
#!/usr/bin/env python3
from cchooks import create_context, PreToolUseContext
c = create_context()
# Determine hook type
assert isinstance(c, PreToolUseContext)
# Block writes to .env files
if c.tool_name == "Write" and ".env" in c.tool_input.get("file_path", ""):
c.output.exit_deny("Nope! .env files are protected")
else:
c.output.exit_success()
Save as hooks/env-guard.py, make executable:
chmod +x hooks/env-guard.py
That's it. No JSON parsing, no validation headaches.
Build each hook type with real examples:
Block dangerous commands before they run:
#!/usr/bin/env python3
from cchooks import create_context, PreToolUseContext
c = create_context()
assert isinstance(c, PreToolUseContext)
# Block rm -rf commands with user warning
if c.tool_name == "Bash" and "rm -rf" in c.tool_input.get("command", ""):
c.output.deny(
reason="You should not execute this command: System protection: rm -rf blocked",
system_message="⚠️ This command could permanently delete files. Please use caution."
)
else:
c.output.allow()
Format Python files after writing:
#!/u...