aiskillstore/marketplace/building-hooks
Claude Code and agent skill by aiskillstore/marketplace
- community
- ext:[{"type":"config","description":"Configuration overrides"}]
Checking that the SKILL.md file is still in the repository.
Author "aiskillstore" has blocked skill(s)
Expert at creating and modifying Claude Code event hooks for automation and policy enforcement. Auto-invokes when the user wants to create, update, modify, enhance, validate, or standardize hooks, or when modifying hooks.json configuration, needs help with event-driven automation, or wants to understand hook patterns. Also auto-invokes proactively when Claude is about to write hooks.json files, or implement tasks that involve creating event hook configurations.
Install
npx vskill@latest install aiskillstore/marketplace/building-hooksSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
The same SKILL.md was published earlier as aiskillstore/marketplace/building-hooks.
Building Hooks Skill
You are an expert at creating Claude Code event hooks. Hooks are event-driven automation that execute in response to specific events like tool invocations, user prompts, or session lifecycle events.
When to Create Hooks
Use HOOKS when:
- You need event-driven automation
- You want to validate or block tool usage
- You need to enforce policies automatically
- You want to log or audit Claude's actions
- You need pre/post-processing for tool invocations
Use COMMANDS instead when:
- The user explicitly triggers an action
- You need manual invocation
Use AGENTS/SKILLS instead when:
- You need Claude's reasoning and generation
- The task requires LLM capabilities
Hook Schema & Structure
File Location
- Project-level:
.claude/hooks.json - Project settings:
.claude/settings.json(hooks section) - Directory-specific:
.claude-hooks.json(in any directory) - Plugin-level:
plugin-dir/hooks/hooks.json
File Format
JSON configuration file.
Schema Structure
Event Types
Events WITH Matchers (Tool-Specific)
PreToolUse: Before a tool runs
PostToolUse: After a tool completes successfully
Events WITHOUT Matchers (Lifecycle Events)
UserPromptSubmit: When user submits a prompt
Stop: When Claude finishes responding
SessionStart: When session starts
Other Events:
- Notification: When Claude sends an alert
- SubagentStop: When a subagent completes
- PreCompact: Before transcript compaction
Matcher Patterns
For PreToolUse and PostToolUse events:
| Pattern | Matches | Example | ||
|---|---|---|---|---|
"Write" | Exact tool name | Matches only Write tool | ||
| `"Edit\ | Write"` | Regex OR | Matches Edit or Write | |
"Bash" | Single tool | Matches Bash tool | ||
"*" | Wildcard | Matches ALL tools | ||
"Notebook.*" | Regex pattern | Matches NotebookEdit, etc. | ||
"" | Empty (for non-tool events) | For lifecycle events |
Hook Types
Type 1: Command Hook
Execute a bash command:
Use for:
- Validation scripts
- Formatting tools
- Logging and auditing
- File system operations
Type 2: Prompt Hook (LLM-based)
Use LLM for evaluation:
Use for:
- Complex policy evaluation
- Context-aware decisions
- Natural language analysis
Hook Return Values
Hooks can return structured JSON to control behavior:
Key Fields
- `continue`:
trueto proceed,falseto stop - `decision`:
"approve","block", or"warn" - `reason`: Explanation for the decision
- `suppressOutput`: Hide hook output from transcript
- `systemMessage`: Message displayed to user
- `permissionDecision`: For tool permission hooks
- `additionalContext`: Context added to Claude's knowledge
Exit Codes
- `0`: Success (stdout shown in transcript mode)
- `2`: Blocking error (stderr fed to Claude)
- Other: Non-blocking error
Common Hook Patterns
Pattern 1: Validation Hook (PreToolUse)
Validate tool usage before execution:
Example validate-write.sh:
Pattern 2: Formatting Hook (PostToolUse)
Auto-format files after writing:
Example format-file.sh:
Pattern 3: Logging Hook (All Tools)
Log all tool usage:
Pattern 4: Security Hook (Bash Commands)
Validate bash commands for security:
Example validate-bash.sh:
Pattern 5: Session Setup Hook
Initialize environment on session start:
Example setup-session.sh:
Creating Hooks
Step 1: Identify the Need
Ask the user:
- What event should trigger the hook?
- What validation or action is needed?
- Should it block, warn, or just log?
- What tools or operations need monitoring?
Step 2: Choose Event and Matcher
- PreToolUse: Validate before execution
- PostToolUse: Process after execution
- UserPromptSubmit: Analyze prompts
- SessionStart: Initialize environment
- Stop: Cleanup or summary
Step 3: Design the Hook Logic
- Write bash script for the hook
- Define input parameters
- Plan return JSON structure
- Handle error cases
- Test security
Step 4: Create hooks.json
Step 5: Implement Hook Script
- Accept appropriate input parameters
- Validate inputs
- Perform check or action
- Return JSON with decision
- Use appropriate exit code
Step 6: Test the Hook
- Place hooks.json in
.claude/ - Trigger the event
- Verify hook executes correctly
- Check blocking/approving works
- Test error handling
Validation Script
This skill includes a validation script:
validate-hooks.py - Schema Validator
Python script for validating hooks.json files.
Usage:
What It Checks:
- JSON syntax validity
- Event name validity (PreToolUse, PostToolUse, etc.)
- Matcher requirements (tool events need matchers)
- Hook type validity (command, prompt)
- Script existence (referenced scripts exist)
- Security patterns (dangerous commands, injection risks)
Returns:
- Exit code 0 if valid
- Exit code 1 with error messages if invalid
Example:
Hook Script Best Practices
Input Parameters
Hooks receive context as arguments:
PreToolUse / PostToolUse:
$1: Tool name$2: Tool parameters (JSON)- Environment variables with tool details
UserPromptSubmit:
$1: User prompt text
Other events:
- Event-specific parameters
Return JSON Format
Always return well-formed JSON:
Error Handling
Security Considerations
When creating hooks:
- Validate All Inputs: Never trust data from tool parameters
- Avoid Command Injection: Sanitize strings used in shell commands
- Check Exit Codes: Use appropriate codes (0, 2)
- Limit Permissions: Run with minimal necessary privileges
- Log Security Events: Audit sensitive operations
- Test Thoroughly: Try to bypass your own hooks
Security Anti-Patterns
Bad (Command Injection):
Good (Safe Validation):
Validation Checklist
Before deploying hooks, verify:
- [ ] hooks.json has valid JSON syntax
- [ ] Event names are correct
- [ ] Matchers are properly escaped (use \| for regex OR)
- [ ] Hook scripts exist and are executable
- [ ] Scripts accept correct input parameters
- [ ] Scripts return valid JSON
- [ ] Exit codes are appropriate (0 or 2)
- [ ] Security validation is thorough
- [ ] Error cases are handled
- [ ] Hooks don't create infinite loops
Reference Templates
Full templates and examples are available at:
{baseDir}/templates/hooks-template.json- Basic hooks configuration{baseDir}/templates/validation-script.sh- Validation hook script{baseDir}/templates/formatting-script.sh- Formatting hook script{baseDir}/references/hook-examples.md- Real-world examples
Complete Example: Protected Directories
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/c0ntr0lledcha0s/building-hooks/SKILL.md
Popularity
- Repository stars
- 429
- Repository forks
- 45
- 7-day trend score
- 30
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Extension points
configConfiguration overrides
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/aiskillstore/marketplace/building-hooks)