プラグインを使用すると、プロジェクト全体で共有できるカスタム機能を使用して Claude Code を拡張できます。Agent SDK を通じて、ローカルディレクトリからプログラムでプラグインを読み込んで、カスタムスラッシュコマンド、エージェント、スキル、フック、MCP サーバーをエージェントセッションに追加できます。
プラグインは Claude Code 拡張機能のパッケージであり、以下を含むことができます:
プラグイン構造とプラグインの作成方法に関する完全な情報については、プラグインを参照してください。
オプション設定でローカルファイルシステムパスを指定してプラグインを読み込みます。SDK は複数の場所から複数のプラグインを読み込むことをサポートしています。
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Hello",
options: {
plugins: [
{ type: "local", path: "./my-plugin" },
{ type: "local", path: "/absolute/path/to/another-plugin" }
]
}
})) {
// Plugin commands, agents, and other features are now available
}プラグインパスは以下のいずれかです:
"./plugins/my-plugin")"/home/user/plugins/my-plugin")パスはプラグインのルートディレクトリ(.claude-plugin/plugin.json を含むディレクトリ)を指す必要があります。
プラグインが正常に読み込まれると、システム初期化メッセージに表示されます。プラグインが利用可能であることを確認できます:
プラグインからのコマンドは、競合を避けるためにプラグイン名で自動的に名前空間化されます。形式は plugin-name:command-name です。
CLI 経由でプラグインをインストールした場合(例:/plugin install my-plugin@marketplace)、SDK でもそのインストールパスを指定することで使用できます。CLI でインストールされたプラグインについては ~/.claude/plugins/ を確認してください。
プラグインの読み込みと使用を示す完全な例を以下に示します:
プラグインディレクトリには .claude-plugin/plugin.json マニフェストファイルが含まれている必要があります。オプションで以下を含めることができます:
my-plugin/
├── .claude-plugin/
│ └── plugin.json # Required: plugin manifest
├── commands/ # Custom slash commands
│ └── custom-cmd.md
├── agents/ # Custom agents
│ └── specialist.md
├── skills/ # Agent Skills
│ └── my-skill/
│ └── SKILL.md
├── hooks/ # Event handlers
│ └── hooks.json
└── .mcp.json # MCP server definitionsプラグイン作成の詳細については、以下を参照してください:
グローバルにインストールせずに開発中にプラグインを読み込みます:
plugins: [
{ type: "local", path: "./dev-plugins/my-plugin" }
]チーム全体の一貫性のためにプロジェクトリポジトリにプラグインを含めます:
plugins: [
{ type: "local", path: "./project-plugins/team-workflows" }
]異なる場所からプラグインを組み合わせます:
plugins: [
{ type: "local", path: "./local-plugin" },
{ type: "local", path: "~/.claude/custom-plugins/shared-plugin" }
]プラグインが init メッセージに表示されない場合:
.claude-plugin/ を含む)を指していることを確認してくださいプラグインコマンドが機能しない場合:
plugin-name:command-name 形式が必要ですslash_commands に表示されていることを確認してくださいcommands/ ディレクトリにあることを確認してください相対パスが機能しない場合:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Hello",
options: {
plugins: [{ type: "local", path: "./my-plugin" }]
}
})) {
if (message.type === "system" && message.subtype === "init") {
// Check loaded plugins
console.log("Plugins:", message.plugins);
// Example: [{ name: "my-plugin", path: "./my-plugin" }]
// Check available commands from plugins
console.log("Commands:", message.slash_commands);
// Example: ["/help", "/compact", "my-plugin:custom-command"]
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// Load a plugin with a custom /greet command
for await (const message of query({
prompt: "/my-plugin:greet", // Use plugin command with namespace
options: {
plugins: [{ type: "local", path: "./my-plugin" }]
}
})) {
// Claude executes the custom greeting command from the plugin
if (message.type === "assistant") {
console.log(message.content);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
import * as path from "path";
async function runWithPlugin() {
const pluginPath = path.join(__dirname, "plugins", "my-plugin");
console.log("Loading plugin from:", pluginPath);
for await (const message of query({
prompt: "What custom commands do you have available?",
options: {
plugins: [
{ type: "local", path: pluginPath }
],
maxTurns: 3
}
})) {
if (message.type === "system" && message.subtype === "init") {
console.log("Loaded plugins:", message.plugins);
console.log("Available commands:", message.slash_commands);
}
if (message.type === "assistant") {
console.log("Assistant:", message.content);
}
}
}
runWithPlugin().catch(console.error);