🙆♂️
[Claude Code]カスタムスラッシュコマンドのallowed-toolsが便利なので共有したい
Claude Codeのカスタムスラッシュコマンドには allowed-tools という機能があり、とても便利なので実例とともに紹介します。
カスタムスラッシュコマンドのfront matter設定
claude codeではよく使うプロンプトをカスタムスラッシュコマンドとしてマークダウンファイルで定義できます。
実はClaude Codeのカスタムスラッシュコマンドには書式があり、ファイルの先頭にfront matterを設定することができます。
参考: Slash Commands - File Format | Anthropic Documentation
allowed-toolsとは?
allowed-toolsプロパティ を設定すると、スラッシュコマンド実行中だけ特定のツールを許可することができます。
これにより以下のようなセキュアな運用が可能になります:
- 普段はgit操作を完全に制限
- コミット・プッシュのときだけ自動でgitコマンドを使用許可
- 必要最小限の権限でAIに作業を委任
実例:commit-and-pushコマンド
以下は実際に私が使用している allowed-tools を使ったgit commit ~ push ~ pr作成までを自動化したスラッシュコマンドです。(英語が下手なのはご容赦)
---
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*), Bash(gh pr:*)
description: Create a git commit
---
# commit-and-push command
## Context
- Current git status: !`git status`
- Current git diff (staged and unstaged changes): !`git diff HEAD`
- Current branch: !`git branch --show-current`
- Recent commits: !`git log --oneline -10`
## Your task
Based on the above changes, create a single git commit and push to the remote repository.
git add FILE_NAMES
git commit -m COMMIT_MESSAGE
git push
gh pr create -t TITLE -b BODY --draft
If pre-commit hooks edit some files, you should check status again and add those files before committing.
git status
git add EDITED_FILE_NAMES
git commit -m COMMIT_MESSAGE
...
まとめ
- custom slash commandには書式がある
- 特に
allowed-toolsは便利 - 答えは全てanthoropic公式ドキュメントにある
Discussion