Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    入門
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.6 新功能遷移指南模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API處理停止原因提示詞最佳實踐
    上下文管理
    上下文視窗壓縮上下文編輯
    功能
    提示詞快取延伸思考自適應思考思考力度串流訊息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實作工具使用細粒度工具串流Bash 工具程式碼執行工具程式化工具呼叫電腦使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐企業級 Skills透過 API 使用 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2(預覽版)Python SDK遷移指南
    串流輸入即時串流回應處理停止原因處理權限使用者核准與輸入使用 hooks 控制執行工作階段管理檔案檢查點SDK 中的結構化輸出託管 Agent SDK安全部署 AI 代理修改系統提示詞SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的 Agent Skills追蹤成本與用量待辦清單SDK 中的外掛
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞產生器使用提示詞範本提示詞改進器清晰直接使用範例(多範例提示)讓 Claude 思考(CoT)使用 XML 標籤賦予 Claude 角色(系統提示詞)串聯複雜提示詞長上下文技巧延伸思考技巧
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    強化防護機制
    減少幻覺提高輸出一致性防範越獄攻擊串流拒絕減少提示詞洩漏讓 Claude 保持角色
    管理與監控
    Admin API 概覽資料駐留工作區用量與成本 APIClaude Code Analytics API零資料保留
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    指南

    SDK 中的外掛程式

    透過 Agent SDK 載入自訂外掛程式,以使用命令、代理、技能和掛鉤來擴展 Claude Code

    外掛程式可讓您使用可在專案間共享的自訂功能來擴展 Claude Code。透過 Agent SDK,您可以以程式設計方式從本機目錄載入外掛程式,以將自訂斜線命令、代理、技能、掛鉤和 MCP 伺服器新增到您的代理工作階段。

    什麼是外掛程式?

    外掛程式是 Claude Code 擴充功能的套件,可以包括:

    • 命令:自訂斜線命令
    • 代理:用於特定任務的專門子代理
    • 技能:Claude 自主使用的模型叫用功能
    • 掛鉤:回應工具使用和其他事件的事件處理程式
    • MCP 伺服器:透過模型上下文協議的外部工具整合

    如需有關外掛程式結構以及如何建立外掛程式的完整資訊,請參閱外掛程式。

    載入外掛程式

    透過在選項設定中提供外掛程式的本機檔案系統路徑來載入外掛程式。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" }
    ]

    疑難排解

    外掛程式未載入

    如果您的外掛程式未出現在初始化訊息中:

    1. 檢查路徑:確保路徑指向外掛程式根目錄(包含 .claude-plugin/)
    2. 驗證 plugin.json:確保您的資訊清單檔案具有有效的 JSON 語法
    3. 檢查檔案權限:確保外掛程式目錄可讀

    命令不可用

    如果外掛程式命令不起作用:

    1. 使用命名空間:外掛程式命令需要 plugin-name:command-name 格式
    2. 檢查初始化訊息:驗證命令是否以正確的命名空間出現在 slash_commands 中
    3. 驗證命令檔案:確保命令 markdown 檔案位於 commands/ 目錄中

    路徑解析問題

    如果相對路徑不起作用:

    1. 檢查工作目錄:相對路徑從您目前的工作目錄解析
    2. 使用絕對路徑:為了可靠性,請考慮使用絕對路徑
    3. 正規化路徑:使用路徑公用程式正確建構路徑

    另請參閱

    • 外掛程式 - 完整外掛程式開發指南
    • 外掛程式參考 - 技術規格
    • 斜線命令 - 在 SDK 中使用斜線命令
    • 子代理 - 使用專門代理
    • 技能 - 使用 Agent 技能

    Was this page helpful?