Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
外掛程式可讓您使用可在專案間共享的自訂功能來擴展 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 的目錄)。
當外掛程式成功載入時,它們會出現在系統初始化訊息中。您可以驗證您的外掛程式是否可用:
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"]
}
}來自外掛程式的命令會自動以外掛程式名稱作為命名空間,以避免衝突。格式為 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);
}
}如果您透過 CLI 安裝了外掛程式(例如,/plugin install my-plugin@marketplace),您仍然可以透過提供其安裝路徑在 SDK 中使用它。檢查 ~/.claude/plugins/ 以查找 CLI 安裝的外掛程式。
以下是示範外掛程式載入和使用的完整範例:
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);外掛程式目錄必須包含 .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" }
]如果您的外掛程式未出現在初始化訊息中:
.claude-plugin/)如果外掛程式命令不起作用:
plugin-name:command-name 格式slash_commands 中commands/ 目錄中如果相對路徑不起作用:
Was this page helpful?