hook-development

[工具]开发Claude Code钩子的指南——响应工具调用和通知自动执行的shell命令。当用户想要为自动化工作流(如代码检查、格式化、通知或自定义验证)创建、调试或优化钩子时使用此功能。支持PreToolUse、PostToolUse、Stop、SubagentStop、SessionStart、SessionEnd、UserPromptSubmit、PreCompact、Notification事件。

📖 中文导读:[工具]开发Claude Code钩子的指南——响应工具调用和通知自动执行的shell命令。当用户想要为自动化工作流(如代码检查、格式化、通知或自定义验证)创建、调试或优化钩子时使用此功能。支持PreToolUse、PostToolUse、Stop、SubagentStop、SessionStart、SessionEnd、UserPromptSubmit、PreCompact、Notification事件。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: hook-development description: "[Utility] Guide for developing Claude Code hooks - shell commands that execute automatically in response to tool calls and notifications. Use this when users want to create, debug, or optimize hooks for automating workflows like linting, formatting, notifications, or custom validations. Supports PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification events."

Hook Development for Claude Code Plugins

Overview

Hooks are event-driven automation scripts that execute in response to Claude Code events. Use hooks to validate operations, enforce policies, add context, and integrate external tools into workflows.

Hook Types

Use LLM-driven decision making for context-aware validation.

Command Hooks

Execute bash commands for deterministic checks.

Hook Events

Event When Use For
PreToolUse Before tool Validation, modification
PostToolUse After tool Feedback, logging
UserPromptSubmit User input Context, validation
Stop Agent stopping Completeness check
SubagentStop Subagent done Task validation
SessionStart Session begins Context loading
SessionEnd Session ends Cleanup, logging
PreCompact Before compact Preserve context
Notification User notified Logging, reactions

Environment Variables

  • $CLAUDE_PROJECT_DIR - Project root path
  • $CLAUDE_PLUGIN_ROOT - Plugin directory
  • $CLAUDE_ENV_FILE - SessionStart only: persist env vars here

Best Practices

  • Use prompt-based hooks for complex logic
  • Use ${CLAUDE_PLUGIN_ROOT} for portability
  • Validate all inputs in command hooks
  • Set appropriate timeouts