MCPInstall

hooks

Claude Code の Hook 作成・管理ガイド。PreToolUse、PostToolUse、Stop等のフック実装。 使用タイミング: (1) 危険操作をブロックしたい時 (2) ツール実行後に提案を出したい時 (3) セッション終了時にクリーンアップしたい時 (4) Claude Codeをカスタマイズしたい時。 トリガー例: 「フックを作って」「PreToolUseフック」「危険操作をブロック」 「コミット後に提案」「Claude Codeのhooks」

Installs
66
Last 24h
—
Trending rank
—
Security
Not audited

Install

Works with every agent the skills CLI supports.

npx skills add https://github.com/kimny1143/claude-code-template --skill hooks
  1. 01

    Install

    Run the command in your project. The CLI asks which agents to install for.

  2. 02

    Describe the task

    Ask your agent for something this skill covers. It loads hooks automatically when relevant.

  3. 03

    Review the result

    The agent follows the SKILL.md steps, scripts and references bundled with the skill.

SKILL.md

The instructions your agent reads when the skill is triggered.

hooks - Claude Code Hooks

Claude Code の Hook 作成・管理ガイド。


概要

Hooks はツール実行の前後に自動で実行されるスクリプト。

タイプタイミング用途
PreToolUseツール実行前検証、危険操作ブロック
PostToolUseツール実行後提案、ログ記録
Stop会話終了時クリーンアップ

Hook 作成

1. スクリプト作成

#!/bin/bash
# .claude/hooks/my-hook.sh

# 環境変数
# TOOL_NAME: ツール名 (Write, Edit, Bash, etc.)
# TOOL_INPUT: JSON形式の入力パラメータ

# 終了コード
# 0: 成功(許可)
# 1: 警告(許可、メッセージ表示)
# 2: ブロック(実行を停止)

# 例: 危険なコマンドをブロック
if echo "$TOOL_INPUT" | grep -q "rm -rf /"; then
  echo "🚫 危険なコマンドをブロックしました" >&2
  exit 2
fi

exit 0

2. 実行権限付与

chmod +x .claude/hooks/my-hook.sh

3. 設定ファイルに登録

// .claude/settings.local.json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "/path/to/project/.claude/hooks/my-hook.sh"
          }
        ]
      }
    ]
  }
}

実用的な Hook 例

PreToolUse: 危険操作ブロック

#!/bin/bash
# validate-dangerous-ops.sh

case "$TOOL_NAME" in
  "Bash")
    COMMAND=$(echo "$TOOL_INPUT" | jq -r '.command // empty')

    # force push をブロック
    if echo "$COMMAND" | grep -qE 'git push.*(--force|-f).*(main|master)'; then
      echo "🚫 main/master への force push は禁止です" >&2
      exit 2
    fi
    ;;

  "Write"|"Edit")
    FILE_PATH=$(echo "$TOOL_INPUT" | jq -r '.file_path // empty')

    # .env ファイル編集を警告
    if echo "$FILE_PATH" | grep -qE '\.env'; then
      echo "⚠️ 環境変数ファイルを編集しようとしています" >&2
      exit 1  # 警告のみ
    fi
    ;;
esac

exit 0

PostToolUse: コミット後の提案

#!/bin/bash
# suggest-after-commit.sh

# git commit 後のみ発火
if ! echo "$TOOL_INPUT" | grep -q "git commit"; then
  exit 0
fi

# 変更ファイルをチェック
CHANGED=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null)

if echo "$CHANGED" | grep -q "schema"; then
  echo "" >&2
  echo "💡 スキーマが変更されました" >&2
  echo "   マイグレーションファイルの作成を検討してください" >&2
  exit 1
fi

exit 0

Stop: セッション終了時クリーンアップ

#!/bin/bash
# cleanup-on-stop.sh

# マージ済みブランチの確認
MERGED=$(git branch --merged main | grep -v main | grep -v '\*')

if [ -n "$MERGED" ]; then
  echo "" >&2
  echo "🧹 マージ済みブランチがあります:" >&2
  echo "$MERGED" >&2
  echo "   削除を検討してください: git branch -d <branch>" >&2
  exit 1
fi

exit 0

設定構造

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit|Bash",  // 正規表現でマッチ
        "hooks": [
          { "type": "command", "command": "/path/to/hook.sh" }
        ]
      }
    ],
    "PostToolUse": [
      {
        "hooks": [
          { "type": "command", "command": "/path/to/hook.sh" }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "/path/to/hook.sh" }
        ]
      }
    ]
  }
}

デバッグ

# 直接実行してテスト
TOOL_NAME="Bash" TOOL_INPUT='{"command":"git push --force main"}' ./my-hook.sh
echo $?  # 終了コード確認

ベストプラクティス

  1. 非ブロッキング: 長時間処理は避ける
  2. stderr に出力: stdout ではなく stderr に出力
  3. 提案のみ: 自動実行せず、人間に判断を委ねる
  4. jq でパース: JSON パースには jq を使用
  5. フェイルセーフ: jq がない場合は許可(exit 0)

FAQ

What does the hooks skill do?

Claude Code の Hook 作成・管理ガイド。PreToolUse、PostToolUse、Stop等のフック実装。 使用タイミング: (1) 危険操作をブロックしたい時 (2) ツール実行後に提案を出したい時 (3) セッション終了時にクリーンアップしたい時 (4) Claude Codeをカスタマイズしたい時。 トリガー例: 「フックを作って」「PreToolUseフック」「危険操作をブロック」 「コミット後に提案」「Claude Codeのhooks」

How do I install hooks?

Run npx skills add https://github.com/kimny1143/claude-code-template --skill hooks in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/hooks/ (personal) or .claude/skills/hooks/ (project).

Which AI agents can use hooks?

Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.

Is hooks safe to install?

No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.

Who maintains hooks?

It is published by kimny1143 in kimny1143/claude-code-template. It has 66 recorded installs.

More from kimny1143/claude-code-template

View all →

ui-ux-pro-max

kimny1143/claude-code-template

UI/UX design intelligence. Plan, build, design, implement, review, improve UI/UX code. Styles: image-first, editorial design, minimalism, dark mode, responsive. Projects: landing page, dashboard, SaaS, mobile app.

Frontend & Design4.7K installs

remotion

kimny1143/claude-code-template

汎用Remotion動画制作スキル。プロモーション動画、マスコットアニメーション、 LP用動画、チュートリアル動画、SNS短尺動画の制作に使用。 トリガー: "プロモ動画", "アニメーション", "動画を作って", "Remotion", "動画コンテンツ", "ビデオ制作", "モーショングラフィックス", "LP動画", "チュートリアル動画", "OGP動画", "動画素材" Remotionを使ったReactベースの動画生成全般でこのスキルを使うこと。 プロモーション映像やアニメーション素材の話が出たら積極的にこのスキルを参照すること。

Media & Creative162 installs

ux-psychology

kimny1143/claude-code-template

Open the skill to read its SKILL.md instructions.

Frontend & Design114 installs

copywriting

kimny1143/claude-code-template

Open the skill to read its SKILL.md instructions.

Marketing & SEO89 installs

ios-app-store-submission

kimny1143/claude-code-template

Open the skill to read its SKILL.md instructions.

Mobile88 installs

marketing-psychology

kimny1143/claude-code-template

When the user wants to apply psychological principles, mental models, or behavioral science to marketing. Also use when the user mentions 'psychology,' 'mental models,' 'cognitive bias,' 'persuasion,' 'behavioral science,' 'why people buy,' 'decision-making,' or 'consumer behavior.' This skill provides 70+ mental models organized for marketing application.

Marketing & SEO82 installs

Popular other skills

View category →

hyperframes-cli

heygen-com/hyperframes

The HyperFrames CLI by job - scaffold, check, look at frames, edit the timeline, preview, render locally or in the cloud, publish, project history - plus reading its JSON and recovering from build or render failures.

1.3M installs

triage

mattpocock/skills

Move issues and external PRs through a state machine of triage roles, categorise, verify, grill if needed, and write agent-ready briefs.

1M installs

ask-matt

mattpocock/skills

Ask which skill or flow fits your situation. A router over the skills in this repo.

778.4K installs

to-questionnaire

mattpocock/skills

Turn a decision you can't fully answer into a questionnaire for someone else to fill in.

508.9K installs

wait-what

mattpocock/skills

Stop. That last message did not land: re-pitch it.

459.7K installs

loop-me

mattpocock/skills

Grill me about specs for the workflows I want to build, within this workspace.

447.9K installs