スラッシュコマンドは、/で始まる特別なコマンドでClaude Codeセッションを制御する方法を提供します。これらのコマンドは、会話履歴のクリア、メッセージの圧縮、ヘルプの取得などのアクションを実行するためにSDKを通じて送信できます。

利用可能なスラッシュコマンドの発見

Claude Code SDKは、システム初期化メッセージで利用可能なスラッシュコマンドに関する情報を提供します。セッションが開始されたときにこの情報にアクセスします:
import { query } from "@anthropic-ai/claude-code";

for await (const message of query({
  prompt: "Hello Claude",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log("Available slash commands:", message.slash_commands);
    // 出力例: ["/compact", "/clear", "/help"]
  }
}

スラッシュコマンドの送信

通常のテキストと同様に、プロンプト文字列にスラッシュコマンドを含めることで送信します:
import { query } from "@anthropic-ai/claude-code";

// スラッシュコマンドを送信
for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "result") {
    console.log("Command executed:", message.result);
  }
}

一般的なスラッシュコマンド

/compact - 会話履歴の圧縮

/compactコマンドは、重要なコンテキストを保持しながら古いメッセージを要約することで、会話履歴のサイズを削減します:
import { query } from "@anthropic-ai/claude-code";

for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "compact_boundary") {
    console.log("Compaction completed");
    console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
    console.log("Trigger:", message.compact_metadata.trigger);
  }
}

/clear - 会話のクリア

/clearコマンドは、すべての以前の履歴をクリアして新しい会話を開始します:
import { query } from "@anthropic-ai/claude-code";

// 会話をクリアして新しく開始
for await (const message of query({
  prompt: "/clear",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log("Conversation cleared, new session started");
    console.log("Session ID:", message.session_id);
  }
}

カスタムスラッシュコマンドの作成

組み込みのスラッシュコマンドを使用することに加えて、SDKを通じて利用可能な独自のカスタムコマンドを作成できます。カスタムコマンドは、サブエージェントの設定方法と同様に、特定のディレクトリにマークダウンファイルとして定義されます。

ファイルの場所

カスタムスラッシュコマンドは、そのスコープに基づいて指定されたディレクトリに保存されます:
  • プロジェクトコマンド: .claude/commands/ - 現在のプロジェクトでのみ利用可能
  • 個人コマンド: ~/.claude/commands/ - すべてのプロジェクトで利用可能

ファイル形式

各カスタムコマンドはマークダウンファイルで、以下のようになります:
  • ファイル名(.md拡張子なし)がコマンド名になります
  • ファイルの内容がコマンドの動作を定義します
  • オプションのYAMLフロントマターが設定を提供します

基本例

.claude/commands/refactor.mdを作成:
選択されたコードを読みやすさと保守性を向上させるためにリファクタリングします。
クリーンコードの原則とベストプラクティスに焦点を当てます。
これにより、SDKを通じて使用できる/refactorコマンドが作成されます。

フロントマター付き

.claude/commands/security-check.mdを作成:
---
allowed-tools: Read, Grep, Glob
description: セキュリティ脆弱性スキャンを実行
model: claude-3-5-sonnet-20241022
---

以下を含むセキュリティ脆弱性についてコードベースを分析します:
- SQLインジェクションリスク
- XSS脆弱性
- 露出した認証情報
- 安全でない設定

SDKでのカスタムコマンドの使用

ファイルシステムで定義されると、カスタムコマンドはSDKを通じて自動的に利用可能になります:
import { query } from "@anthropic-ai/claude-code";

// カスタムコマンドを使用
for await (const message of query({
  prompt: "/refactor src/auth/login.ts",
  options: { maxTurns: 3 }
})) {
  if (message.type === "assistant") {
    console.log("Refactoring suggestions:", message.message);
  }
}

// カスタムコマンドはslash_commandsリストに表示されます
for await (const message of query({
  prompt: "Hello",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    // 組み込みとカスタムの両方のコマンドが含まれます
    console.log("Available commands:", message.slash_commands);
    // 例: ["/compact", "/clear", "/help", "/refactor", "/security-check"]
  }
}

高度な機能

引数とプレースホルダー

カスタムコマンドは、プレースホルダーを使用した動的引数をサポートします: .claude/commands/fix-issue.mdを作成:
---
argument-hint: [issue-number] [priority]
description: GitHub issueを修正
---

優先度$2で問題#$1を修正します。
問題の説明を確認し、必要な変更を実装します。
SDKで使用:
import { query } from "@anthropic-ai/claude-code";

// カスタムコマンドに引数を渡す
for await (const message of query({
  prompt: "/fix-issue 123 high",
  options: { maxTurns: 5 }
})) {
  // コマンドは$1="123"と$2="high"で処理されます
  if (message.type === "result") {
    console.log("Issue fixed:", message.result);
  }
}

Bashコマンドの実行

カスタムコマンドはbashコマンドを実行し、その出力を含めることができます: .claude/commands/git-commit.mdを作成:
---
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*)
description: gitコミットを作成
---

## コンテキスト

- 現在のステータス: !`git status`
- 現在の差分: !`git diff HEAD`

## タスク

変更に基づいて適切なメッセージでgitコミットを作成します。

ファイル参照

@プレフィックスを使用してファイルの内容を含めます: .claude/commands/review-config.mdを作成:
---
description: 設定ファイルをレビュー
---

以下の設定ファイルの問題をレビューします:
- パッケージ設定: @package.json
- TypeScript設定: @tsconfig.json
- 環境設定: @.env

セキュリティ問題、古い依存関係、設定ミスをチェックします。

名前空間による整理

より良い構造のためにサブディレクトリでコマンドを整理します:
.claude/commands/
├── frontend/
   ├── component.md      # /component (project:frontend)を作成
   └── style-check.md     # /style-check (project:frontend)を作成
├── backend/
   ├── api-test.md        # /api-test (project:backend)を作成
   └── db-migrate.md      # /db-migrate (project:backend)を作成
└── review.md              # /review (project)を作成
サブディレクトリはコマンドの説明に表示されますが、コマンド名自体には影響しません。

実用的な例

コードレビューコマンド

.claude/commands/code-review.mdを作成:
---
allowed-tools: Read, Grep, Glob, Bash(git diff:*)
description: 包括的なコードレビュー
---

## 変更されたファイル
!`git diff --name-only HEAD~1`

## 詳細な変更
!`git diff HEAD~1`

## レビューチェックリスト

上記の変更について以下をレビューします:
1. コード品質と読みやすさ
2. セキュリティ脆弱性
3. パフォーマンスへの影響
4. テストカバレッジ
5. ドキュメントの完全性

優先度別に整理された具体的で実行可能なフィードバックを提供します。

テストランナーコマンド

.claude/commands/test.mdを作成:
---
allowed-tools: Bash, Read, Edit
argument-hint: [test-pattern]
description: オプションのパターンでテストを実行
---

パターンにマッチするテストを実行: $ARGUMENTS

1. テストフレームワーク(Jest、pytestなど)を検出
2. 提供されたパターンでテストを実行
3. テストが失敗した場合、分析して修正
4. 修正を確認するために再実行
SDKを通じてこれらのコマンドを使用:
import { query } from "@anthropic-ai/claude-code";

// コードレビューを実行
for await (const message of query({
  prompt: "/code-review",
  options: { maxTurns: 3 }
})) {
  // レビューフィードバックを処理
}

// 特定のテストを実行
for await (const message of query({
  prompt: "/test auth",
  options: { maxTurns: 5 }
})) {
  // テスト結果を処理
}

関連項目