Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K

    第一步

    Claude 简介快速开始

    模型与定价

    模型概览选择模型Claude 4.5 的新功能迁移到 Claude 4.5模型弃用定价

    使用 Claude 构建

    功能概览使用 Messages API上下文窗口提示词最佳实践

    功能

    提示词缓存上下文编辑扩展思考流式消息批处理引用多语言支持Token 计数嵌入向量视觉PDF 支持Files API搜索结果Google Sheets 插件

    工具

    概述如何实现工具使用令牌高效的工具使用细粒度工具流式传输Bash 工具代码执行工具计算机使用工具文本编辑器工具Web fetch 工具网络搜索工具记忆工具

    代理技能

    概述在 API 中开始使用 Agent Skills技能创作最佳实践通过 API 使用 Agent Skills

    Agent SDK

    概览TypeScript SDKPython SDK

    指南

    流式输入处理权限会话管理托管 Agent SDK修改系统提示词SDK 中的 MCP自定义工具SDK 中的子代理SDK 中的斜杠命令SDK 中的代理技能跟踪成本和使用情况待办事项列表SDK 中的插件

    API 中的 MCP

    MCP 连接器远程 MCP 服务器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示词工程

    概述提示词生成器使用提示模板提示词改进器保持清晰和直接使用示例(多示例提示)让 Claude 思考(思维链)使用XML标签给Claude分配角色(系统提示)预填充 Claude 的响应链式复杂提示长文本技巧扩展思考技巧

    测试与评估

    定义成功标准开发测试用例使用评估工具减少延迟

    加强防护措施

    减少幻觉提高输出一致性缓解越狱handle-streaming-refusals减少提示词泄露保持Claude的角色特征

    管理和监控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    指南

    SDK 中的插件

    通过 Agent SDK 加载自定义插件,使用命令、代理、技能和钩子来扩展 Claude Code

    插件允许您使用可在项目间共享的自定义功能来扩展 Claude Code。通过 Agent SDK,您可以以编程方式从本地目录加载插件,以便向代理会话添加自定义斜杠命令、代理、技能、钩子和 MCP 服务器。

    什么是插件?

    插件是 Claude Code 扩展的包,可以包括:

    • 命令:自定义斜杠命令
    • 代理:用于特定任务的专门子代理
    • 技能:Claude 自主使用的模型调用功能
    • 钩子:响应工具使用和其他事件的事件处理程序
    • MCP 服务器:通过模型上下文协议的外部工具集成

    有关插件结构和如何创建插件的完整信息,请参阅插件。

    加载插件

    通过在选项配置中提供本地文件系统路径来加载插件。SDK 支持从不同位置加载多个插件。

    TypeScript
    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
    }
    Python
    import asyncio
    from claude_agent_sdk import query
    
    async def main():
        async for message in 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
            pass
    
    asyncio.run(main())

    路径规范

    插件路径可以是:

    • 相对路径:相对于您当前工作目录解析(例如,"./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 Skills
      © 2025 ANTHROPIC PBC

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

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

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      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

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

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

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

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

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      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

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

      • Privacy policy
      • Responsible disclosure policy
      • Terms of service: Commercial
      • Terms of service: Consumer
      • Usage policy
      © 2025 ANTHROPIC PBC