Skip to content

Hooks 事件速查表 ​

事件类型 ​

事件触发时机可阻止stdin 数据
PreToolUse工具执行前✅ (exit 2)tool_name, tool_input
PostToolUse工具执行后❌tool_name, tool_input, tool_output
SessionStart会话启动❌session_id, model
StopClaude 停止❌--
TaskCreated任务创建❌task_id, task_subject
Notification通知事件❌message
TeammateIdleTeammate 空闲❌teammate_id
TaskCompleted任务完成❌task_id

处理器类型 ​

类型语法适用场景
command"command": "bash script.sh"Shell 脚本
http"url": "https://..."Webhook
prompt"prompt": "Check if..."语义判断
agent"agent": "reviewer"子 Agent

Exit Code ​

Code含义
0通过
1脚本出错(不阻止)
2阻止工具执行

Matcher 模式 ​

"Bash"              → 精确匹配
"Write|Edit"         → 匹配多个工具
"mcp__github__.*"    → 正则匹配

环境变量 ​

$CLAUDE_PROJECT_DIR  → 项目根目录
$CLAUDE_SESSION_ID   → 当前会话 ID
$CLAUDE_MODEL        → 当前模型

配置示例 ​

json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{"type": "command", "command": "bash .claude/hooks/check.sh"}]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [{"type": "command", "command": "bash .claude/hooks/lint.sh"}]
      }
    ],
    "Stop": [
      {
        "hooks": [{"type": "command", "command": "bash .claude/hooks/notify.sh"}]
      }
    ]
  }
}

基于 MIT 许可发布