AgentSkillsCN

hook-development

【实用】开发Claude Code钩子的指南——在工具调用和通知时自动执行的Shell命令。当用户希望创建、调试或优化钩子以自动化linting、格式化、通知或自定义验证等流程时使用。支持PreToolUse、PostToolUse、Stop、SubagentStop、SessionStart、SessionEnd、UserPromptSubmit、PreCompact、Notification事件。

SKILL.md
--- frontmatter
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

Prompt-Based Hooks (Recommended)

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

Command Hooks

Execute bash commands for deterministic checks.

Hook Events

EventWhenUse For
PreToolUseBefore toolValidation, modification
PostToolUseAfter toolFeedback, logging
UserPromptSubmitUser inputContext, validation
StopAgent stoppingCompleteness check
SubagentStopSubagent doneTask validation
SessionStartSession beginsContext loading
SessionEndSession endsCleanup, logging
PreCompactBefore compactPreserve context
NotificationUser notifiedLogging, 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