想让 Claude Code 学会新技能?插件系统允许你创建自定义 Skills、Agents、Hooks 和 MCP Server,并在项目和团队之间共享。
插件 vs 独立配置
| 方式 | Skill 命名 | 适用场景 |
|---|---|---|
独立配置(.claude/ 目录) | /hello | 个人工作流、单项目定制、快速实验 |
插件(.claude-plugin/) | /plugin-name:hello | 团队共享、社区分发、跨项目复用 |
建议:先用独立配置快速迭代,准备好分享时再转为插件。
创建第一个插件
1. 创建目录结构
bash
mkdir my-first-plugin
mkdir my-first-plugin/.claude-plugin2. 编写 Manifest
json
// my-first-plugin/.claude-plugin/plugin.json
{
"name": "my-first-plugin",
"description": "A greeting plugin to learn the basics",
"version": "1.0.0",
"author": {
"name": "Your Name"
}
}3. 添加 Skill
bash
mkdir -p my-first-plugin/skills/hellomarkdown
<!-- my-first-plugin/skills/hello/SKILL.md -->
---
description: Greet the user with a friendly message
disable-model-invocation: true
---
Greet the user warmly and ask how you can help them today.4. 测试
bash
claude --plugin-dir ./my-first-plugin
# 在会话中输入:
/my-first-plugin:hello插件目录结构
text
my-plugin/
├── .claude-plugin/
│ └── plugin.json # Manifest(只有这个放在 .claude-plugin 里)
├── skills/ # 自定义 Skills
│ └── pdf-processor/
│ ├── SKILL.md
│ ├── reference.md
│ └── scripts/
├── agents/ # 自定义 Agent
│ └── code-reviewer.md
├── hooks/ # 事件钩子
│ └── hooks.json
├── commands/ # 简单命令
│ └── deploy.md
└── mcp/ # MCP Server 配置⚠️ 常见错误:不要把
skills/、agents/等目录放在.claude-plugin/里面。只有plugin.json放在.claude-plugin/中。
组件详解
Skills
带 $ARGUMENTS 占位符支持动态参数:
markdown
---
description: Greet the user with a personalized message
---
# Hello Skill
Greet the user named "$ARGUMENTS" warmly.使用:/my-plugin:hello Alex
Agents
自定义子 Agent,支持模型选择和工具限制:
markdown
---
name: code-reviewer
description: Specialized code review agent
model: sonnet
effort: medium
maxTurns: 20
disallowedTools: Write, Edit
---
You are a code review specialist...Hooks
响应 Claude Code 生命周期事件:
json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/format-code.sh"
}
]
}
]
}
}支持的事件:
| 事件 | 触发时机 |
|---|---|
| SessionStart | 会话开始 |
| PreToolUse | 工具调用前(可阻止) |
| PostToolUse | 工具调用成功后 |
| SubagentStart | 子 Agent 启动 |
| SubagentStop | 子 Agent 完成 |
| TaskCreated | 任务创建 |
分发插件
通过 Marketplace
- 创建 Marketplace 仓库(GitHub)
- 添加
marketplace.json索引 - 用户通过
/plugin marketplace add安装
本地安装
bash
# 从目录安装
/plugin install-dir ./path/to/plugin
# 从 Marketplace 安装
/plugin install my-plugin@marketplace-name版本管理
使用语义化版本号:
json
{
"version": "2.1.0"
}- 主版本号:不兼容变更
- 次版本号:新功能(向后兼容)
- 修订号:Bug 修复
开发技巧
- 使用
--plugin-dir本地开发,避免反复安装 /reload-plugins实时加载修改- 命名空间防止插件间冲突
- Skills 支持辅助文件(reference.md、scripts/)
- 安全考虑:插件 Agent 不支持 hooks、mcpServers 和 permissionMode
原文来源:Claude Code Plugins 文档 | 来源:Claude Code 官方文档