カスタムエージェントとスキル
専門ワークフロー用の .md ベース拡張機能の構築方法。
学ぶこと
Claude Code は固定されたプロダクトではありません。拡張可能なプラットフォームです。特定のディレクトリにMarkdownファイルを配置するだけで、まったく新しい機能を追加できます --- スラッシュコマンド、専門レビュアー、自動化ワークフロー、ドメイン特化アシスタント。
このセッションを終えると、以下を理解できるようになります:
- スキルとエージェントの違い
- スキル(あなたも Claude も起動できるワークフロー)の書き方
- エージェント(Claude が作業を委任するサブエージェント)の書き方
- 両方の設計パターン
- 安全性のためのツール制限
- 呼び出しから実行までのライフサイクル
課題
すべてのチームにはワークフローがあります。デプロイ手順、レビューチェックリスト、テストプロトコル、オンボーディングガイド。この知識は通常、2つの場所に存在します:誰も読まないドキュメントと、一人の頭の中です。
"How do we deploy?" → Ask Sarah
"What's our PR checklist?" → It's in the wiki... somewhere
"Database migrations?" → Dave wrote a script, check his home dir
もしこの知識がAI実行可能な指示だったら? 単なるドキュメントではなく、AIが毎回ステップバイステップで従う指示だったらどうでしょうか? それを実現するのが、カスタムエージェントとスキルです。
仕組み
スキル vs エージェント
┌──────────────────────────────────────────────────────┐
│ Skills Agents (subagents) │
│ │
│ Invoked by you (/deploy) or Delegated by Claude │
│ loaded by Claude when the through the Agent │
│ description matches tool, or requested │
│ by you (@-mention) │
│ │
│ Runs in the main conversation Runs in its own │
│ (instructions injected) context window and │
│ returns a summary │
│ │
│ Location: Location: │
│ .claude/skills/<name>/SKILL.md .claude/agents/ │
│ <name>.md │
└──────────────────────────────────────────────────────┘
| 側面 | スキル | エージェント |
|---|---|---|
| 起動 | あなた(/skill-name)、または description が合致したときの Claude | Agent ツール経由の Claude。名前の指定や @-mention でも依頼可能 |
| コンテキスト | メイン会話に読み込まれる(context: fork ならフォークしたサブエージェントで実行) | 専用のコンテキストウィンドウ。結果だけが戻る |
| 目的 | ステップバイステップのワークフロー、テンプレート、参照知識 | 専門的な役割(レビュアー、探索者) |
| ツール制御 | allowed-tools が起動したターンのツールを事前承認 | tools の許可リスト、または disallowedTools の拒否リスト |
| 配置場所 | .claude/skills/<name>/SKILL.md | .claude/agents/<name>.md |
| 範囲 | 広い(ワークフロー全体) | 絞られている(単一の責務) |
どちらもユーザーレベル(~/.claude/skills/、~/.claude/agents/)やプラグイン内でも使えます。.claude/commands/ の従来のカスタムコマンドも引き続き動作し、同じ /name を作ります。スキルには、補助ファイルを置けるフォルダと、誰が起動できるかを制御する機能が加わっています。
スキルの構造
スキルは SKILL.md ファイルです。先頭に Claude Code での扱い方を示す YAML frontmatter があり、その後に Markdown の指示が続きます。次の内容を .claude/skills/deploy/SKILL.md として保存します --- フォルダ名が /deploy コマンドになります:
---
name: deploy
description: Deploy the application to production with safety checks
disable-model-invocation: true
---
# Deploy
1. Run `git status` to check for uncommitted changes
2. Run the full test suite with `npm test`
3. If tests fail, stop and report the failures
4. Run `npm run build` and verify no errors
5. Show the user: test results, build size, files changed
6. Ask: "Ready to deploy? [yes/no]"
7. Only if confirmed: run `git push origin main`
8. Report deployment status
## Rules
- NEVER push without user confirmation
- If any step fails, stop and report
先頭の --- はファイルの1行目でなければなりません。そうでないと Claude Code はファイル全体を指示として扱います。description は、Claude がスキルを使うべき場面を判断するために読む部分なので、主な用途を最初に書きましょう。disable-model-invocation: true は、副作用のあるワークフローを Claude が自分の判断で始めないようにします。/deploy で実行するのはあなたであり、コードが準備できたように見えるからといって Claude がデプロイを決めることはありません。
エージェントの構造
エージェントは、Claude がサブエージェントとして作業を委任できる専門的な役割を定義します。次の内容を .claude/agents/security-reviewer.md として保存します:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities. Use after changes to authentication, input handling, or data access.
tools: Read, Glob, Grep
model: sonnet
---
You are a security-focused code reviewer. Be thorough
but avoid false positives.
## Checklist
- SQL injection: raw queries with user input
- XSS: unescaped output in templates
- Auth bypass: missing middleware on protected routes
- Secrets: hardcoded API keys, passwords, tokens
- Path traversal: unsanitized file paths
- SSRF: user-controlled URLs in server requests
## Output Format
For each finding: file, line, severity, issue, fix.
If clean: "No security issues found." and list what you checked.
必須なのは name と description だけです。Markdown の本文はサブエージェントのシステムプロンプトになります。実際にツールを制限するのは tools フィールドで、このレビュアーが使えるのは Read、Glob、Grep だけです。model は実行モデル(sonnet、opus、haiku、fable、完全なモデル ID、または inherit)を選びます。
Claude が委任するとき、呼び出しは次のようになります:
{
"type": "tool_use",
"name": "Agent",
"input": {
"subagent_type": "security-reviewer",
"description": "Security review of src/api",
"prompt": "Review the changes in src/api/ for security issues"
}
}
エージェントを名前で依頼する(「この変更に security-reviewer エージェントを使って」)、@-mention で確実に実行させる、claude --agent security-reviewer でセッション全体をそのエージェントとして起動する、といった方法もあります。
スキルのライフサイクル
┌──────────────────────────────────────────────────────┐
│ 1. Session starts: Claude Code discovers skills │
│ (~/.claude/skills, .claude/skills, plugins) │
│ ▼ │
│ 2. Each skill's description is listed for Claude │
│ (unless disable-model-invocation: true) │
│ ▼ │
│ 3. You type /deploy, or Claude decides a skill fits │
│ ▼ │
│ 4. The rendered SKILL.md enters the conversation │
│ as one message and stays for later turns │
│ ▼ │
│ 5. Claude follows the steps with its normal tools │
│ (allowed-tools pre-approves some for that turn) │
│ ▼ │
│ 6. Workflow completes (or you interrupt) │
└──────────────────────────────────────────────────────┘
スキル設計パターン
パターン1:ステップバイステップワークフロー
最も一般的なパターンです。番号付きの一連のアクション:
## Steps
1. Check preconditions
2. Gather information
3. Present plan to user
4. Execute with safety checks
5. Verify results
6. Report summary
パターン2:デシジョンツリー
条件によって分岐するワークフロー向け:
## Process
Check the project type:
- If `package.json` exists → Node.js project
- If `tsconfig.json` exists → TypeScript
- Use `npx tsc --noEmit` for type checking
- Else → JavaScript
- Use `node --check` for syntax validation
- If `requirements.txt` exists → Python project
- Use `python -m py_compile` for syntax checking
- If `Cargo.toml` exists → Rust project
- Use `cargo check` for compilation checking
パターン3:テンプレートベース出力
構造化されたコンテンツを生成するスキル向け:
## Output Template
Generate a changelog entry using this format:
### [version] - YYYY-MM-DD
#### Added
- New features
#### Changed
- Changes to existing features
#### Fixed
- Bug fixes
Fill each section by analyzing git commits since the last tag.
エージェント設計パターン
パターン:専門レビュアー
---
name: performance-reviewer
description: Reviews code for performance problems. Use when a change touches database access, list endpoints, or hot loops.
tools: Read, Glob, Grep
---
Analyze code for performance issues.
## Check For
- N+1 queries in database access patterns
- Missing pagination on list endpoints
- Unbounded array operations (map/filter on large datasets)
- Missing indexes suggested by query patterns
- Synchronous operations that should be async
パターン:構造化出力付きの探索者
---
name: architecture-explorer
description: Maps the architecture of an unfamiliar codebase. Use before planning changes in code you haven't read.
tools: Read, Glob, Grep
model: haiku
---
Map the architecture of this codebase.
## Output Format
- Entry points (main files, index files)
- Dependency graph (what imports what)
- Data flow (where data enters, transforms, exits)
- External dependencies (APIs, databases, services)
コードベースのマッピングは深い推論ではなく大量の読み取りなので、探索者は Haiku で動かします(セッション21)。
ツール制限
エージェントのツールを制限することは、最小権限の原則に従うことです。レビュアーはコードを編集すべきではありません。探索者はコマンドを実行すべきではありません。制限は本文ではなく frontmatter に書きます --- 本文の「Tools: Read only」という一文は単なる指示ですが、tools フィールドはそれ以外のツールをすべて取り除きます:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities
tools: Read, Glob, Grep # allowlist: only these tools
# disallowedTools: Write, Edit # or: inherit everything except these
---
この許可リストがあれば、レビュアーはファイルの編集、コマンドの実行、さらに独自のサブエージェントの起動もできません(制限しない場合、サブエージェントはデフォルトで3階層までネストできます)。macOS、Linux、WSL では、Bash を含めずに Glob と Grep を列挙すると、このサブエージェントは専用の検索ツールも使えます。スキルの allowed-tools は、そのスキルを起動したターンのツールを事前承認するだけで、ツールを取り除くことはありません。ツールをブロックするには、パーミッションの deny ルールか、スキルが有効な間の disallowed-tools を使います。
重要なポイント
カスタムエージェントとスキルは、チームのワークフロー知識をエンコードします。 暗黙知を反復可能で一貫したAIの振る舞いに変換します。
平凡な Claude Code セットアップと強力なセットアップの違いは、モデルではなく、スキルとエージェントの質にあります。よく書かれたデプロイスキルは、すべてのデプロイが同じ安全チェックに従うことを意味します。よく書かれたセキュリティレビュアーは、すべてのPRが同じ厳密さで審査されることを意味します。
これは「Infrastructure as Code」をAIワークフローに適用したものです:Workflows as Markdown。 コンパイル不要、フレームワーク不要、デプロイメント不要。ファイルを配置するだけで、機能が追加されます。
チームが繰り返し行っていることを考えてみましょう:
- 特定のチェックリストに沿ったコードレビュー
- 安全ゲート付きのデプロイ手順
- データ移行のプロトコル
- 新しいメンバーへのコードベースのオンボーディング
どれもスキルやエージェントにでき、誰が起動しても毎回同じように実行されます。
ハンズオン
カスタムのレビュースキルとセキュリティレビュアーエージェントを構築します。
レビュースキル
.claude/skills/team-review/SKILL.md を作成します:
---
description: Review the current branch against main with the team checklist and a security pass. Use when asked to review a branch or prepare a PR.
---
# Team Review
Review the current branch's changes against the base branch.
## Steps
1. Run `git diff main...HEAD --stat` to see changed files
2. For each changed file:
- Read the full diff with `git diff main...HEAD -- <file>`
- Check for:
- Missing error handling
- Hardcoded values that should be config
- Functions longer than 50 lines
- Missing type annotations
- Console.log or debug statements left in
3. Use the security-reviewer agent on the changed files
4. Compile all findings into a report:
## Report Format
### Summary
[1-2 sentence overview]
### Findings
| File | Line | Severity | Issue |
|------|------|----------|-------|
### Security Review
[Output from security-reviewer agent]
### Recommendation
[Approve / Request Changes / Needs Discussion]
なぜ review という名前にしないのでしょうか? Claude Code はすでに /review を組み込みの /code-review スキルのエイリアスとして使っているため、チームのスキルには別の名前を付けましょう。このスキルは disable-model-invocation を設定していません。レビューはコードを読むだけなので、ブランチのレビューを頼んだときに Claude が自分で読み込んでも安全です。
セキュリティレビュアーエージェント
.claude/agents/security-reviewer.md を作成します:
---
name: security-reviewer
description: Reviews code changes for security vulnerabilities. Use after changes to authentication, input handling, or data access.
tools: Read, Glob, Grep
---
You review code for security vulnerabilities. Be thorough
but avoid false positives. Only flag issues you are
confident about.
## Checklist
- [ ] No SQL injection (parameterized queries)
- [ ] No XSS (output escaping)
- [ ] No hardcoded secrets
- [ ] Auth middleware on protected routes
- [ ] Input validation on user-facing endpoints
- [ ] No path traversal in file operations
## Output
List findings with file, line, severity, and fix.
If clean, state "No security issues found."
/team-review と入力すると、Claude はスキルに従い、セキュリティチェックを security-reviewer サブエージェントに委任して、構造化されたレポートを生成します。誰が実行しても、すべてのレビューが一貫しています。
変更点まとめ
| 拡張機能なし | カスタムエージェントとスキルあり |
|---|---|
| ワークフローがドキュメントにある | ワークフローがAI実行可能な Markdown |
| 一貫性が実行者に依存 | 毎回同じステップ |
| 新メンバーにトレーニングが必要 | スキルがトレーニングをエンコード |
| レビューがレビュアーによって異なる | エージェントが同じチェックリストを適用 |
| 暗黙知が人の頭の中にある | 知識がバージョン管理されたファイルにある |
| 機能追加にコードが必要 | 機能追加にMarkdownファイルが必要 |
次のセッション
自分のワークフローで Claude Code を拡張するツールが揃いました。セッション24ですべてをまとめます。本番パターン --- 個人の生産性ツールからチーム全体のプラットフォームへ移行するための可観測性、CI/CD統合、コスト管理、そして組織全体でAIアシスト開発をスケーリングするための成熟度モデルを学びます。