本参考提供了 Claude Code 插件系统的完整技术规范,包括组件架构、CLI 命令和开发工具。
plugin 是一个自包含的组件目录,用于扩展 Claude Code 的自定义功能。插件组件包括 skills、agents、hooks、MCP servers 和 LSP servers。
Plugin 组件参考
Skills
Plugins 向 Claude Code 添加 skills,创建可由您或 Claude 调用的 /name 快捷方式。
位置:插件根目录中的 skills/ 或 commands/ 目录
文件格式:Skills 是包含 SKILL.md 的目录;commands 是简单的 markdown 文件
Skill 结构:
skills/
├── pdf-processor/
│ ├── SKILL.md
│ ├── reference.md (可选)
│ └── scripts/ (可选)
└── code-reviewer/
└── SKILL.md
集成行为:
- 安装插件时会自动发现 Skills 和 commands
- Claude 可以根据任务上下文自动调用它们
- Skills 可以在 SKILL.md 旁边包含支持文件
有关完整详情,请参阅Skills。
Agents
Plugins 可以为特定任务提供专门的 subagents,Claude 可以在适当时自动调用。
位置:插件根目录中的 agents/ 目录
文件格式:描述 agent 功能的 Markdown 文件
Agent 结构:
---
name: agent-name
description: 该 agent 的专长以及 Claude 应何时调用它
model: sonnet
effort: medium
maxTurns: 20
disallowedTools: Write, Edit
---
详细的系统提示,描述 agent 的角色、专业知识和行为。
Plugin agents 支持 name、description、model、effort、maxTurns、tools、disallowedTools、skills、memory、background 和 isolation frontmatter 字段。唯一有效的 isolation 值是 "worktree"。出于安全原因,plugin 提供的 agents 不支持 hooks、mcpServers 和 permissionMode。
集成点:
- Agents 出现在
/agents 界面中
- Claude 可以根据任务上下文自动调用 agents
- Agents 可以由用户手动调用
- Plugin agents 与内置 Claude agents 一起工作
有关完整详情,请参阅Subagents。
Hooks
Plugins 可以提供事件处理程序,自动响应 Claude Code 事件。
位置:插件根目录中的 hooks/hooks.json,或在 plugin.json 中内联
格式:具有事件匹配器和操作的 JSON 配置
Hook 配置:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/format-code.sh"
}
]
}
]
}
}
Plugin hooks 响应与用户定义的 hooks相同的生命周期事件:
| Event | When it fires |
|---|
SessionStart | When a session begins or resumes |
UserPromptSubmit | When you submit a prompt, before Claude processes it |
PreToolUse | Before a tool call executes. Can block it |
PermissionRequest | When a permission dialog appears |
PermissionDenied | When a tool call is denied by the auto mode classifier. Return {retry: true} to tell the model it may retry the denied tool call |
PostToolUse | After a tool call succeeds |
PostToolUseFailure | After a tool call fails |
Notification | When Claude Code sends a notification |
SubagentStart | When a subagent is spawned |
SubagentStop | When a subagent finishes |
TaskCreated | When a task is being created via TaskCreate |
TaskCompleted | When a task is being marked as completed |
Stop | When Claude finishes responding |
StopFailure | When the turn ends due to an API error. Output and exit code are ignored |
TeammateIdle | When an agent team teammate is about to go idle |
InstructionsLoaded | When a CLAUDE.md or .claude/rules/*.md file is loaded into context. Fires at session start and when files are lazily loaded during a session |
ConfigChange | When a configuration file changes during a session |
CwdChanged | When the working directory changes, for example when Claude executes a cd command. Useful for reactive environment management with tools like direnv |
FileChanged | When a watched file changes on disk. The matcher field specifies which filenames to watch |
WorktreeCreate | When a worktree is being created via --worktree or isolation: "worktree". Replaces default git behavior |
WorktreeRemove | When a worktree is being removed, either at session exit or when a subagent finishes |
PreCompact | Before context compaction |
PostCompact | After context compaction completes |
Elicitation | When an MCP server requests user input during a tool call |
ElicitationResult | After a user responds to an MCP elicitation, before the response is sent back to the server |
SessionEnd | When a session terminates |
Hook 类型:
command:执行 shell 命令或脚本
http:将事件 JSON 作为 POST 请求发送到 URL
prompt:使用 LLM 评估提示(使用 $ARGUMENTS 占位符表示上下文)
agent:运行具有工具的 agentic 验证器以完成复杂验证任务
MCP servers
Plugins 可以捆绑 Model Context Protocol (MCP) servers 以将 Claude Code 与外部工具和服务连接。
位置:插件根目录中的 .mcp.json,或在 plugin.json 中内联
格式:标准 MCP server 配置
MCP server 配置:
{
"mcpServers": {
"plugin-database": {
"command": "${CLAUDE_PLUGIN_ROOT}/servers/db-server",
"args": ["--config", "${CLAUDE_PLUGIN_ROOT}/config.json"],
"env": {
"DB_PATH": "${CLAUDE_PLUGIN_ROOT}/data"
}
},
"plugin-api-client": {
"command": "npx",
"args": ["@company/mcp-server", "--plugin-mode"],
"cwd": "${CLAUDE_PLUGIN_ROOT}"
}
}
}
集成行为:
- 启用插件时,Plugin MCP servers 会自动启动
- Servers 在 Claude 的工具包中显示为标准 MCP 工具
- Server 功能与 Claude 的现有工具无缝集成
- Plugin servers 可以独立于用户 MCP servers 进行配置
LSP servers
想要使用 LSP plugins?从官方市场安装它们:在 /plugin Discover 选项卡中搜索”lsp”。本部分记录了如何为官方市场未涵盖的语言创建 LSP plugins。
Plugins 可以提供Language Server Protocol (LSP) servers,在处理代码库时为 Claude 提供实时代码智能。
LSP 集成提供:
- 即时诊断:Claude 在每次编辑后立即看到错误和警告
- 代码导航:转到定义、查找引用和悬停信息
- 语言感知:代码符号的类型信息和文档
位置:插件根目录中的 .lsp.json,或在 plugin.json 中内联
格式:将语言服务器名称映射到其配置的 JSON 配置
.lsp.json 文件格式:
{
"go": {
"command": "gopls",
"args": ["serve"],
"extensionToLanguage": {
".go": "go"
}
}
}
在 plugin.json 中内联:
{
"name": "my-plugin",
"lspServers": {
"go": {
"command": "gopls",
"args": ["serve"],
"extensionToLanguage": {
".go": "go"
}
}
}
}
必需字段:
| 字段 | 描述 |
|---|
command | 要执行的 LSP 二进制文件(必须在 PATH 中) |
extensionToLanguage | 将文件扩展名映射到语言标识符 |
可选字段:
| 字段 | 描述 |
|---|
args | LSP server 的命令行参数 |
transport | 通信传输:stdio(默认)或 socket |
env | 启动 server 时要设置的环境变量 |
initializationOptions | 在初始化期间传递给 server 的选项 |
settings | 通过 workspace/didChangeConfiguration 传递的设置 |
workspaceFolder | server 的工作区文件夹路径 |
startupTimeout | 等待 server 启动的最长时间(毫秒) |
shutdownTimeout | 等待正常关闭的最长时间(毫秒) |
restartOnCrash | server 崩溃时是否自动重启 |
maxRestarts | 放弃前的最大重启尝试次数 |
您必须单独安装语言服务器二进制文件。 LSP plugins 配置 Claude Code 如何连接到语言服务器,但它们不包括服务器本身。如果在 /plugin Errors 选项卡中看到 Executable not found in $PATH,请为您的语言安装所需的二进制文件。
可用的 LSP plugins:
| Plugin | 语言服务器 | 安装命令 |
|---|
pyright-lsp | Pyright (Python) | pip install pyright 或 npm install -g pyright |
typescript-lsp | TypeScript Language Server | npm install -g typescript-language-server typescript |
rust-lsp | rust-analyzer | 参阅 rust-analyzer 安装 |
首先安装语言服务器,然后从市场安装 plugin。
Plugin 安装范围
安装 plugin 时,您选择一个范围,确定 plugin 的可用位置以及谁可以使用它:
| 范围 | 设置文件 | 用例 |
|---|
user | ~/.claude/settings.json | 在所有项目中可用的个人 plugins(默认) |
project | .claude/settings.json | 通过版本控制共享的团队 plugins |
local | .claude/settings.local.json | 项目特定的 plugins,gitignored |
managed | Managed settings | 托管 plugins(只读,仅更新) |
Plugins 使用与其他 Claude Code 配置相同的范围系统。有关安装说明和范围标志,请参阅安装 plugins。有关范围的完整说明,请参阅Configuration scopes。
Plugin 清单架构
.claude-plugin/plugin.json 文件定义了您的 plugin 的元数据和配置。本部分记录了所有支持的字段和选项。
清单是可选的。如果省略,Claude Code 会自动发现默认位置中的组件,并从目录名称派生 plugin 名称。当您需要提供元数据或自定义组件路径时,使用清单。
完整架构
{
"name": "plugin-name",
"version": "1.2.0",
"description": "Brief plugin description",
"author": {
"name": "Author Name",
"email": "[email protected]",
"url": "https://github.com/author"
},
"homepage": "https://docs.example.com/plugin",
"repository": "https://github.com/author/plugin",
"license": "MIT",
"keywords": ["keyword1", "keyword2"],
"commands": ["./custom/commands/special.md"],
"agents": "./custom/agents/",
"skills": "./custom/skills/",
"hooks": "./config/hooks.json",
"mcpServers": "./mcp-config.json",
"outputStyles": "./styles/",
"lspServers": "./.lsp.json"
}
必需字段
如果包含清单,name 是唯一必需的字段。
| 字段 | 类型 | 描述 | 示例 |
|---|
name | string | 唯一标识符(kebab-case,无空格) | "deployment-tools" |
此名称用于命名空间组件。例如,在 UI 中,名为 plugin-dev 的 plugin 的 agent agent-creator 将显示为 plugin-dev:agent-creator。
元数据字段
| 字段 | 类型 | 描述 | 示例 |
|---|
version | string | 语义版本。如果也在市场条目中设置,plugin.json 优先。您只需在一个地方设置它。 | "2.1.0" |
description | string | plugin 目的的简要说明 | "Deployment automation tools" |
author | object | 作者信息 | {"name": "Dev Team", "email": "[email protected]"} |
homepage | string | 文档 URL | "https://docs.example.com" |
repository | string | 源代码 URL | "https://github.com/user/plugin" |
license | string | 许可证标识符 | "MIT"、"Apache-2.0" |
keywords | array | 发现标签 | ["deployment", "ci-cd"] |
组件路径字段
| 字段 | 类型 | 描述 | 示例 |
|---|
commands | string|array | 其他命令文件/目录 | "./custom/cmd.md" 或 ["./cmd1.md"] |
agents | string|array | 其他 agent 文件 | "./custom/agents/reviewer.md" |
skills | string|array | 其他 skill 目录 | "./custom/skills/" |
hooks | string|array|object | Hook 配置路径或内联配置 | "./my-extra-hooks.json" |
mcpServers | string|array|object | MCP 配置路径或内联配置 | "./my-extra-mcp-config.json" |
outputStyles | string|array | 其他输出样式文件/目录 | "./styles/" |
lspServers | string|array|object | Language Server Protocol 配置用于代码智能(转到定义、查找引用等) | "./.lsp.json" |
userConfig | object | 用户可配置的值,在启用时提示。请参阅用户配置 | 见下文 |
channels | array | 消息注入的频道声明(Telegram、Slack、Discord 风格)。请参阅Channels | 见下文 |
用户配置
userConfig 字段声明了 Claude Code 在启用插件时提示用户的值。使用此字段而不是要求用户手动编辑 settings.json。
{
"userConfig": {
"api_endpoint": {
"description": "您团队的 API 端点",
"sensitive": false
},
"api_token": {
"description": "API 身份验证令牌",
"sensitive": true
}
}
}
键必须是有效的标识符。每个值都可用于在 MCP 和 LSP server 配置、hook 命令以及(仅对非敏感值)skill 和 agent 内容中作为 ${user_config.KEY} 进行替换。值也会作为 CLAUDE_PLUGIN_OPTION_<KEY> 环境变量导出到插件子进程。
非敏感值存储在 settings.json 中的 pluginConfigs[<plugin-id>].options 下。敏感值进入系统钥匙链(或在钥匙链不可用的地方进入 ~/.claude/.credentials.json)。钥匙链存储与 OAuth 令牌共享,总限制约为 2 KB,因此请保持敏感值较小。
Channels
channels 字段允许插件声明一个或多个消息频道,将内容注入到对话中。每个频道绑定到插件提供的 MCP server。
{
"channels": [
{
"server": "telegram",
"userConfig": {
"bot_token": { "description": "Telegram 机器人令牌", "sensitive": true },
"owner_id": { "description": "您的 Telegram 用户 ID", "sensitive": false }
}
}
]
}
server 字段是必需的,必须与插件的 mcpServers 中的键匹配。可选的每个频道 userConfig 使用与顶级字段相同的架构,允许插件在启用插件时提示输入机器人令牌或所有者 ID。
路径行为规则
重要:自定义路径补充默认目录 - 它们不替换默认目录。
- 如果
commands/ 存在,除了自定义命令路径外,它也会被加载
- 所有路径必须相对于 plugin 根目录,并以
./ 开头
- 来自自定义路径的命令使用相同的命名和命名空间规则
- 可以将多个路径指定为数组以获得灵活性
路径示例:
{
"commands": [
"./specialized/deploy.md",
"./utilities/batch-process.md"
],
"agents": [
"./custom-agents/reviewer.md",
"./custom-agents/tester.md"
]
}
环境变量
Claude Code 提供两个变量用于引用插件路径。两者都在 skill 内容、agent 内容、hook 命令以及 MCP 或 LSP server 配置中出现的任何地方进行内联替换。两者也都作为环境变量导出到 hook 进程和 MCP 或 LSP server 子进程。
${CLAUDE_PLUGIN_ROOT}:插件安装目录的绝对路径。使用此路径引用与插件捆绑的脚本、二进制文件和配置文件。当插件更新时,此路径会更改,因此您在此处写入的文件不会在更新后保留。
${CLAUDE_PLUGIN_DATA}:用于插件状态的持久目录,在更新后保留。使用此目录用于已安装的依赖项,如 node_modules 或 Python 虚拟环境、生成的代码、缓存以及任何应在插件版本之间保留的其他文件。首次引用此变量时,目录会自动创建。
{
"hooks": {
"PostToolUse": [
{
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/process.sh"
}
]
}
]
}
}
持久数据目录
${CLAUDE_PLUGIN_DATA} 目录解析为 ~/.claude/plugins/data/{id}/,其中 {id} 是插件标识符,其中 a-z、A-Z、0-9、_ 和 - 之外的字符被替换为 -。对于安装为 formatter@my-marketplace 的插件,目录是 ~/.claude/plugins/data/formatter-my-marketplace/。
常见用途是一次安装语言依赖项并在会话和插件更新中重复使用它们。由于数据目录的生命周期长于任何单个插件版本,仅检查目录存在性无法检测到更新何时更改了插件的依赖项清单。推荐的模式是将捆绑的清单与数据目录中的副本进行比较,并在它们不同时重新安装。
此 SessionStart hook 在第一次运行时安装 node_modules,并在插件更新包含更改的 package.json 时再次安装:
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "diff -q \"${CLAUDE_PLUGIN_ROOT}/package.json\" \"${CLAUDE_PLUGIN_DATA}/package.json\" >/dev/null 2>&1 || (cd \"${CLAUDE_PLUGIN_DATA}\" && cp \"${CLAUDE_PLUGIN_ROOT}/package.json\" . && npm install) || rm -f \"${CLAUDE_PLUGIN_DATA}/package.json\""
}
]
}
]
}
}
当存储的副本缺失或与捆绑的副本不同时,diff 退出非零,涵盖第一次运行和依赖项更改的更新。如果 npm install 失败,尾部的 rm 会删除复制的清单,以便下一个会话重试。
捆绑在 ${CLAUDE_PLUGIN_ROOT} 中的脚本可以针对持久的 node_modules 运行:
{
"mcpServers": {
"routines": {
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/server.js"],
"env": {
"NODE_PATH": "${CLAUDE_PLUGIN_DATA}/node_modules"
}
}
}
}
当您从最后一个安装了插件的范围卸载插件时,数据目录会自动删除。/plugin 界面显示目录大小并在删除前提示。CLI 默认删除;传递 --keep-data 以保留它。
Plugin 缓存和文件解析
Plugins 通过以下两种方式之一指定:
- 通过
claude --plugin-dir,用于会话期间。
- 通过市场,为将来的会话安装。
出于安全和验证目的,Claude Code 将_市场_ plugins 复制到用户的本地 plugin 缓存(~/.claude/plugins/cache),而不是就地使用它们。在开发引用外部文件的 plugins 时,理解此行为很重要。
路径遍历限制
已安装的 plugins 无法引用其目录外的文件。遍历 plugin 根目录外的路径(例如 ../shared-utils)在安装后将不起作用,因为这些外部文件不会被复制到缓存中。
使用外部依赖
如果您的 plugin 需要访问其目录外的文件,您可以在 plugin 目录中创建指向外部文件的符号链接。在复制过程中会遵守符号链接:
# 在您的 plugin 目录内
ln -s /path/to/shared-utils ./shared-utils
符号链接的内容将被复制到 plugin 缓存中。这在保持缓存系统安全优势的同时提供了灵活性。
Plugin 目录结构
标准 plugin 布局
完整的 plugin 遵循此结构:
enterprise-plugin/
├── .claude-plugin/ # 元数据目录(可选)
│ └── plugin.json # plugin 清单
├── commands/ # 默认命令位置
│ ├── status.md
│ └── logs.md
├── agents/ # 默认 agent 位置
│ ├── security-reviewer.md
│ ├── performance-tester.md
│ └── compliance-checker.md
├── skills/ # Agent Skills
│ ├── code-reviewer/
│ │ └── SKILL.md
│ └── pdf-processor/
│ ├── SKILL.md
│ └── scripts/
├── hooks/ # Hook 配置
│ ├── hooks.json # 主 hook 配置
│ └── security-hooks.json # 其他 hooks
├── settings.json # plugin 的默认设置
├── .mcp.json # MCP server 定义
├── .lsp.json # LSP server 配置
├── scripts/ # Hook 和实用脚本
│ ├── security-scan.sh
│ ├── format-code.py
│ └── deploy.js
├── LICENSE # 许可证文件
└── CHANGELOG.md # 版本历史
.claude-plugin/ 目录包含 plugin.json 文件。所有其他目录(commands/、agents/、skills/、hooks/)必须在 plugin 根目录,而不是在 .claude-plugin/ 内。
文件位置参考
| 组件 | 默认位置 | 目的 |
|---|
| 清单 | .claude-plugin/plugin.json | Plugin 元数据和配置(可选) |
| 命令 | commands/ | Skill Markdown 文件(遗留;新 skills 使用 skills/) |
| Agents | agents/ | Subagent Markdown 文件 |
| Skills | skills/ | 具有 <name>/SKILL.md 结构的 Skills |
| Hooks | hooks/hooks.json | Hook 配置 |
| MCP servers | .mcp.json | MCP server 定义 |
| LSP servers | .lsp.json | 语言服务器配置 |
| 设置 | settings.json | 启用 plugin 时应用的默认配置。目前仅支持agent设置 |
CLI 命令参考
Claude Code 提供了用于非交互式 plugin 管理的 CLI 命令,对脚本和自动化很有用。
plugin install
从可用市场安装 plugin。
claude plugin install <plugin> [options]
参数:
<plugin>:Plugin 名称或 plugin-name@marketplace-name 用于特定市场
选项:
| 选项 | 描述 | 默认值 |
|---|
-s, --scope <scope> | 安装范围:user、project 或 local | user |
-h, --help | 显示命令帮助 | |
范围确定将已安装的 plugin 添加到哪个设置文件。例如,—scope project 写入 .claude/settings.json 中的 enabledPlugins,使 plugin 对克隆项目存储库的每个人都可用。
示例:
# 安装到用户范围(默认)
claude plugin install formatter@my-marketplace
# 安装到项目范围(与团队共享)
claude plugin install formatter@my-marketplace --scope project
# 安装到本地范围(gitignored)
claude plugin install formatter@my-marketplace --scope local
plugin uninstall
删除已安装的 plugin。
claude plugin uninstall <plugin> [options]
参数:
<plugin>:Plugin 名称或 plugin-name@marketplace-name
选项:
| 选项 | 描述 | 默认值 |
|---|
-s, --scope <scope> | 从范围卸载:user、project 或 local | user |
--keep-data | 保留插件的持久数据目录 | |
-h, --help | 显示命令帮助 | |
别名: remove、rm
默认情况下,从最后一个剩余范围卸载也会删除插件的 ${CLAUDE_PLUGIN_DATA} 目录。使用 --keep-data 保留它,例如在测试新版本后重新安装时。
plugin enable
启用已禁用的 plugin。
claude plugin enable <plugin> [options]
参数:
<plugin>:Plugin 名称或 plugin-name@marketplace-name
选项:
| 选项 | 描述 | 默认值 |
|---|
-s, --scope <scope> | 要启用的范围:user、project 或 local | user |
-h, --help | 显示命令帮助 | |
plugin disable
禁用 plugin 而不卸载它。
claude plugin disable <plugin> [options]
参数:
<plugin>:Plugin 名称或 plugin-name@marketplace-name
选项:
| 选项 | 描述 | 默认值 |
|---|
-s, --scope <scope> | 要禁用的范围:user、project 或 local | user |
-h, --help | 显示命令帮助 | |
plugin update
将 plugin 更新到最新版本。
claude plugin update <plugin> [options]
参数:
<plugin>:Plugin 名称或 plugin-name@marketplace-name
选项:
| 选项 | 描述 | 默认值 |
|---|
-s, --scope <scope> | 要更新的范围:user、project、local 或 managed | user |
-h, --help | 显示命令帮助 | |
调试和开发工具
调试命令
使用 claude --debug 查看 plugin 加载详情:
这显示:
- 正在加载哪些 plugins
- plugin 清单中的任何错误
- 命令、agent 和 hook 注册
- MCP server 初始化
常见问题
| 问题 | 原因 | 解决方案 |
|---|
| Plugin 未加载 | 无效的 plugin.json | 运行 claude plugin validate 或 /plugin validate 检查 plugin.json、skill/agent/command frontmatter 和 hooks/hooks.json 的语法和架构错误 |
| 命令未出现 | 目录结构错误 | 确保 commands/ 在根目录,而不是在 .claude-plugin/ 中 |
| Hooks 未触发 | 脚本不可执行 | 运行 chmod +x script.sh |
| MCP server 失败 | 缺少 ${CLAUDE_PLUGIN_ROOT} | 对所有 plugin 路径使用变量 |
| 路径错误 | 使用了绝对路径 | 所有路径必须是相对的,并以 ./ 开头 |
LSP Executable not found in $PATH | 语言服务器未安装 | 安装二进制文件(例如,npm install -g typescript-language-server typescript) |
示例错误消息
清单验证错误:
Invalid JSON syntax: Unexpected token } in JSON at position 142:检查缺少的逗号、多余的逗号或未引用的字符串
Plugin has an invalid manifest file at .claude-plugin/plugin.json. Validation errors: name: Required:缺少必需字段
Plugin has a corrupt manifest file at .claude-plugin/plugin.json. JSON parse error: ...:JSON 语法错误
Plugin 加载错误:
Warning: No commands found in plugin my-plugin custom directory: ./cmds. Expected .md files or SKILL.md in subdirectories.:命令路径存在但不包含有效的命令文件
Plugin directory not found at path: ./plugins/my-plugin. Check that the marketplace entry has the correct path.:marketplace.json 中的 source 路径指向不存在的目录
Plugin my-plugin has conflicting manifests: both plugin.json and marketplace entry specify components.:删除重复的组件定义或删除 marketplace 条目中的 strict: false
Hook 故障排除
Hook 脚本未执行:
- 检查脚本是否可执行:
chmod +x ./scripts/your-script.sh
- 验证 shebang 行:第一行应该是
#!/bin/bash 或 #!/usr/bin/env bash
- 检查路径是否使用
${CLAUDE_PLUGIN_ROOT}:"command": "${CLAUDE_PLUGIN_ROOT}/scripts/your-script.sh"
- 手动测试脚本:
./scripts/your-script.sh
Hook 未在预期事件上触发:
- 验证事件名称是否正确(区分大小写):
PostToolUse,而不是 postToolUse
- 检查匹配器模式是否与您的工具匹配:
"matcher": "Write|Edit" 用于文件操作
- 确认 hook 类型有效:
command、http、prompt 或 agent
MCP server 故障排除
Server 未启动:
- 检查命令是否存在且可执行
- 验证所有路径是否使用
${CLAUDE_PLUGIN_ROOT} 变量
- 检查 MCP server 日志:
claude --debug 显示初始化错误
- 在 Claude Code 外手动测试 server
Server 工具未出现:
- 确保 server 在
.mcp.json 或 plugin.json 中正确配置
- 验证 server 是否正确实现 MCP 协议
- 检查调试输出中的连接超时
目录结构错误
症状:Plugin 加载但组件(命令、agents、hooks)缺失。
正确结构:组件必须在 plugin 根目录,而不是在 .claude-plugin/ 内。只有 plugin.json 属于 .claude-plugin/。
my-plugin/
├── .claude-plugin/
│ └── plugin.json ← 仅清单在此处
├── commands/ ← 在根级别
├── agents/ ← 在根级别
└── hooks/ ← 在根级别
如果您的组件在 .claude-plugin/ 内,请将它们移到 plugin 根目录。
调试清单:
- 运行
claude --debug 并查找”loading plugin”消息
- 检查每个组件目录是否在调试输出中列出
- 验证文件权限允许读取 plugin 文件
分发和版本管理参考
版本管理
遵循语义版本控制进行 plugin 发布:
{
"name": "my-plugin",
"version": "2.1.0"
}
版本格式:MAJOR.MINOR.PATCH
- MAJOR:破坏性更改(不兼容的 API 更改)
- MINOR:新功能(向后兼容的添加)
- PATCH:错误修复(向后兼容的修复)
最佳实践:
- 从
1.0.0 开始进行第一个稳定版本
- 在分发更改之前更新
plugin.json 中的版本
- 在
CHANGELOG.md 文件中记录更改
- 使用预发布版本,如
2.0.0-beta.1 进行测试
Claude Code 使用版本来确定是否更新您的 plugin。如果您更改了 plugin 的代码但没有在 plugin.json 中提升版本,您的 plugin 的现有用户由于缓存而看不到您的更改。如果您的 plugin 在市场目录中,您可以通过 marketplace.json 管理版本,而不是从 plugin.json 中省略 version 字段。
另请参阅