المهارات hooks-management

hooks-management

آمن ⚙️ الأوامر الخارجية📁 الوصول إلى نظام الملفات🔑 متغيرات البيئة

Create and Manage Claude Code Hooks

Managing Claude Code hooks requires understanding event-driven automation patterns. This skill provides guided workflows and comprehensive documentation for creating, updating, configuring, and troubleshooting hooks for any event type.

يدعم: Claude Codex Code(CC)
📊 71 كافٍ
1

تنزيل ZIP المهارة

2

رفع في Claude

اذهب إلى Settings → Capabilities → Skills → Upload skill

3

فعّل وابدأ الاستخدام

اختبرها

استخدام "hooks-management". Create a new hook that validates JSON input on the PreToolUse event

النتيجة المتوقعة:

  • Hook created at: .claude/hooks/validate-json.py
  • Configuration to add to .claude/settings.local.json:
  • {
  • "hooks": {
  • "PreToolUse": [
  • {
  • "matcher": "*",
  • "hooks": [
  • { "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/validate-json.py" }
  • ]
  • }
  • ]
  • }
  • }
  • Test command: echo '{"tool_name": "Write", "tool_input": {}}' | python .claude/hooks/validate-json.py

استخدام "hooks-management". Why is my hook not executing on the UserPromptSubmit event?

النتيجة المتوقعة:

  • Checking hook configuration...
  • 1. Verify the hook is registered in .claude/settings.local.json
  • 2. Ensure the event name matches exactly: "UserPromptSubmit"
  • 3. Test with echo: echo '{"prompt": "/test"}' | python .claude/hooks/your-hook.py
  • 4. Check that the script has execute permissions
  • 5. Verify exit code is 0 for successful execution

التدقيق الأمني

آمن
v1 • 1/23/2026

Static scanner flagged patterns in documentation markdown files. After manual review, all findings are FALSE POSITIVES. The skill contains only documentation, task workflows, and reference materials for legitimate Claude Code hooks. No actual malicious code or exploit patterns exist.

7
الملفات التي تم فحصها
1,731
الأسطر التي تم تحليلها
3
النتائج
1
إجمالي عمليات التدقيق

عوامل الخطر

⚙️ الأوامر الخارجية (3)
📁 الوصول إلى نظام الملفات (2)
🔑 متغيرات البيئة (1)
تم تدقيقه بواسطة: claude

درجة الجودة

45
الهندسة المعمارية
100
قابلية الصيانة
87
المحتوى
20
المجتمع
100
الأمان
91
الامتثال للمواصفات

ماذا يمكنك بناءه

Build Custom Event Handlers

Create hooks that respond to specific Claude Code events like PreToolUse, PostToolUse, Stop, or UserPromptSubmit. Automate validation, logging, or enforcement of project policies.

Implement Security and Validation Hooks

Create hooks that validate commands before execution, block dangerous operations, log security events, and enforce project security policies automatically.

Enforce Development Workflows

Build hooks that enforce TDD workflows, track phase completion, validate plans before implementation, and ensure consistent development practices across teams.

جرّب هذه الموجهات

Create a Simple Hook
Create a new Claude Code hook that runs on the Stop event. The hook should validate that all tasks in the plan are complete before allowing Claude to stop.
Update an Existing Hook
Update the session_start.py hook to also load user preferences from a config file and set them in the shared cache.
Debug Hook Issues
The post_tool_use hook is not executing correctly. Test it using the echo method to diagnose the issue.
Add Security Validation
Add a security validation module to the pre_tool_use hook that blocks commands containing dangerous patterns like rm -rf or direct path access to /etc.

أفضل الممارسات

  • Always test hooks using echo piped to stdin before deployment - never create temporary test files
  • Use Python over shell scripts when possible for better error handling and cross-platform compatibility
  • Implement idempotent operations so running hooks multiple times produces the same result
  • Return appropriate exit codes: 0 to allow, 2 to block, other non-zero for non-blocking errors

تجنب

  • Never hardcode credentials or secrets in hook scripts
  • Never create hooks that can cause infinite loops or hang indefinitely
  • Never bypass security validations or access controls
  • Never write complex logic in hooks - keep them simple and focused on single responsibilities

الأسئلة المتكررة

What events can I create hooks for in Claude Code?
Claude Code supports hooks for SessionStart, SessionEnd, PreToolUse, PostToolUse, Stop, SubagentStop, UserPromptSubmit, PermissionRequest, PreCompact, and Notification events. Each event has specific input schemas and response conventions.
How do I test a hook without creating test files?
Use the echo command to pipe JSON input directly to your hook script. For example: echo '{"session_id": "test", "tool": "Read"}' | python .claude/hooks/your-hook.py. This tests stdin parsing and output without creating temporary files.
What exit codes should my hook return?
Exit code 0 means the hook succeeded and the action should proceed. Exit code 2 means the hook blocked the action. Other non-zero codes indicate an error but allow the action to continue. The hook communicates decisions via JSON on stdout.
Where do I register my hooks?
Add hook configurations to .claude/settings.local.json (local project settings) or ~/.claude/settings.json (user settings). Use the "hooks" object with event names as keys, and specify matchers and commands for each event.
Can I use environment variables in hooks?
Yes, SessionStart hooks have access to CLAUDE_ENV_FILE for persisting environment variables. All hooks have access to CLAUDE_PROJECT_DIR which points to the project root directory. Standard environment variables are also available.
How do hooks communicate with Claude Code?
Hooks receive JSON input via stdin and send JSON output via stdout. The output format depends on the event type - allow/block decisions, additional context, or suppressOutput flags. Errors and debug info go to stderr using the log() utility.

تفاصيل المطور

المؤلف

Emz1998

الترخيص

MIT

مرجع

main