AgentSkillsCN

claude-code:hook

当你需要配置、创建或排查 Claude Code 钩子时,请使用此技能。包括设置 PreToolUse 钩子、PostToolUse 钩子、UserPromptSubmit 钩子,调试钩子失败问题,或在 Claude Code 内部进行任何自动化操作。例如,“我想在每次编辑文件前运行测试”、“我的钩子没有触发”、“2 个钩子中只有 1 个运行了”、“我该如何创建一个能用 jq 格式化 JSON 输出的钩子?”

SKILL.md
--- frontmatter
name: claude-code:hook
description: Use this skill when you need to configure, create, or troubleshoot Claude Code hooks. This includes setting up PreToolUse hooks, PostToolUse hooks, UserPromptSubmit hooks, debugging hook failures, or any automation within Claude Code. Examples include "I want to run tests before every file edit", "My hook isn't firing", "1 out of 2 hooks ran", or "How do I create a hook that formats JSON output with jq?"
allowed-tools: [Read, Write, Edit, Glob, Grep, Bash, WebFetch(domain:docs.anthropic.com)]

Claude Code Hooks

Reference for creating and configuring Claude Code hooks. When uncertain about syntax or features, use the Task tool with subagent_type='claude-code-guide' to consult official documentation.

Hook Types

TypeTriggerUse Cases
PreToolUseBefore tool executionValidate inputs, block operations, modify parameters
PostToolUseAfter tool completesCheck results, run linters, provide feedback
UserPromptSubmitWhen user sends messagePre-process input, add context
StopSession endsCleanup, save state
SubagentStopSubagent completesProcess results
PreCompactBefore context compactionSave important state
NotificationSystem notificationLog events

Configuration Files

  • ~/.claude/settings.json - User-level (global)
  • .claude/settings.json - Project-level
  • .claude/settings.local.json - Local (not committed)
  • Plugin hooks: plugins/<name>/hooks/hooks.json

Hook Structure

json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "bun ./hooks/biome"
          }
        ]
      }
    ]
  }
}

Matcher Patterns:

  • Simple: "Write", "Edit"
  • Multiple: "Edit|Write|MultiEdit"
  • With args: "Bash(npm:*)", "Bash(osascript:*)|Bash(open:*)"
  • MCP tools: "mcp__linear__create_issue"
  • Plugin MCP tools: "mcp__plugin_<plugin>_<namespace>__<tool>"
  • Both patterns: "mcp__linear__create_issue|mcp__plugin_linear_linear__create_issue"

Hook Input

Commands receive JSON on stdin:

json
{
  "tool_name": "Write",
  "tool_input": {
    "file_path": "/path/to/file.ts",
    "content": "..."
  },
  "cwd": "/project/root",
  "session_id": "...",
  "transcript_path": "..."
}

Parse in shell:

bash
input=$(cat)
file_path=$(echo "$input" | jq -r '.tool_input.file_path')

Or use @constellos/claude-code-kit/runners:

typescript
import { readStdinJson, writeStdoutJson } from "@constellos/claude-code-kit/runners";
const input = await readStdinJson<PreToolUseHookInput>();

Hook Output

PreToolUse - Control execution:

json
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "Use gh cli instead"}}
json
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "updatedInput": {"state": "Todo"}}}

PostToolUse - Provide feedback:

json
{"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext": "Lint errors found..."}}

Exit with no output to allow without modification.

Script Storage

Store complex hooks in .claude/hooks/ or project hooks/ directory:

code
.claude/
├── settings.json
└── hooks/
    └── my-hook.ts

Reference with:

json
"command": "bun $CLAUDE_PROJECT_DIR/.claude/hooks/my-hook.ts"

Examples

See these repositories for hook implementations:

Debugging

For troubleshooting hook failures, see debugging.