Plugins ermöglichen es Ihnen, Claude Code mit benutzerdefinierten Funktionen zu erweitern, die projektübergreifend gemeinsam genutzt werden können. Über das Agent SDK können Sie Plugins programmgesteuert aus lokalen Verzeichnissen laden, um benutzerdefinierte Schrägstrich-Befehle, Agenten, Skills, Hooks und MCP-Server zu Ihren Agent-Sitzungen hinzuzufügen.
Plugins sind Pakete von Claude Code-Erweiterungen, die Folgendes enthalten können:
Vollständige Informationen zur Plugin-Struktur und zum Erstellen von Plugins finden Sie unter Plugins.
Laden Sie Plugins, indem Sie ihre lokalen Dateisystempfade in Ihrer Optionskonfiguration angeben. Das SDK unterstützt das Laden mehrerer Plugins von verschiedenen Standorten.
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
}Plugin-Pfade können sein:
"./plugins/my-plugin")"/home/user/plugins/my-plugin")Der Pfad sollte auf das Root-Verzeichnis des Plugins verweisen (das Verzeichnis, das .claude-plugin/plugin.json enthält).
Wenn Plugins erfolgreich geladen werden, erscheinen sie in der Systeminitalisierungsmeldung. Sie können überprüfen, ob Ihre Plugins verfügbar sind:
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"]
}
}Befehle von Plugins werden automatisch mit dem Plugin-Namen versehen, um Konflikte zu vermeiden. Das Format ist plugin-name:command-name.
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);
}
}Wenn Sie ein Plugin über die CLI installiert haben (z. B. /plugin install my-plugin@marketplace), können Sie es im SDK weiterhin verwenden, indem Sie seinen Installationspfad angeben. Überprüfen Sie ~/.claude/plugins/ auf CLI-installierte Plugins.
Hier ist ein vollständiges Beispiel, das das Laden und die Verwendung von Plugins demonstriert:
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);Ein Plugin-Verzeichnis muss eine .claude-plugin/plugin.json Manifestdatei enthalten. Es kann optional Folgendes enthalten:
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 definitionsDetaillierte Informationen zum Erstellen von Plugins finden Sie unter:
Laden Sie Plugins während der Entwicklung, ohne sie global zu installieren:
plugins: [
{ type: "local", path: "./dev-plugins/my-plugin" }
]Beziehen Sie Plugins in Ihr Projekt-Repository ein, um teamweite Konsistenz zu gewährleisten:
plugins: [
{ type: "local", path: "./project-plugins/team-workflows" }
]Kombinieren Sie Plugins von verschiedenen Standorten:
plugins: [
{ type: "local", path: "./local-plugin" },
{ type: "local", path: "~/.claude/custom-plugins/shared-plugin" }
]Wenn Ihr Plugin nicht in der Init-Meldung angezeigt wird:
.claude-plugin/)Wenn Plugin-Befehle nicht funktionieren:
plugin-name:command-nameslash_commands mit dem korrekten Namespace angezeigt wirdcommands/ befindenWenn relative Pfade nicht funktionieren: