首页 › tutorials › Claude Code插件生态全实战:发现到发布

Claude Code插件生态全实战:从发现到发布

概述

2026年5月,Anthropic正式推出官方插件目录(claude-plugins-official),收录55+高质量插件,标志着Claude Code从单兵工具进化为平台生态。本教程将带你从零掌握插件的全生命周期:发现、安装、构建、发布、私有机房搭建。无论你是个人开发者想提升效率,还是团队负责人需要统一工作流,都能在本文中找到落地方案。

适用人群:已使用Claude Code 1个月以上的开发者,希望扩展工具能力或为团队定制专属插件。

前置条件: - Claude Code >= 1.0.55(支持 /plugin 命令) - Git 基础操作能力 - 了解 Skill、Hook、MCP Server 基本概念


一、Claude Code插件体系全景

1.1 插件是什么

Claude Code插件是一个可分发的扩展包,将以下组件打包为独立单元:

组件 说明 是否必须
plugin.json 插件元数据清单 必须
Skills 模型自动触发或用户通过斜杠命令调用的能力 可选
Agents 预定义的子代理配置 可选
Hooks 事件驱动的自动化脚本 可选
MCP Servers 外部工具集成协议 可选
Commands 遗留格式的斜杠命令(已被Skills替代) 可选(遗留)

1.2 插件 vs Skills vs MCP Server 的区别

很多开发者混淆这三者,理解它们的边界至关重要:

┌─────────────────────────────────────────────────┐
│                  Plugin(插件)                    │
│  一个可分发的扩展包,是最大粒度的封装单元            │
│                                                   │
│  ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │
│  │  Skills   │ │  Agents  │ │   MCP Servers    │ │
│  │ (能力)    │ │ (子代理)  │ │   (工具集成)      │ │
│  └──────────┘ └──────────┘ └──────────────────┘ │
│                                                   │
│  ┌──────────┐ ┌──────────────────────────────┐  │
│  │  Hooks   │ │    Commands (遗留格式)        │  │
│  │ (事件)   │ │    斜杠命令                    │  │
│  └──────────┘ └──────────────────────────────┘  │
└─────────────────────────────────────────────────┘

关键洞察:插件是分发的载体,而非功能本身。你完全可以不用插件,直接把 Skill 放到本地目录。插件的价值在于共享和版本管理。

1.3 插件生态系统架构

graph TB
    subgraph "Anthropic Official"
        A[claude-plugins-official<br/>官方插件目录]
    end

    subgraph "Community"
        B[Third-party Marketplaces<br/>社区市场]
        C[Private Marketplaces<br/>私有机房]
    end

    subgraph "Claude Code Client"
        D[/plugin install]
        E[/plugin discover]
        F[/plugin marketplace]
        G[~/.claude/plugins/]
    end

    A --> D
    B --> D
    C --> D
    D --> G
    E --> A
    E --> B
    F --> A
    F --> B
    F --> C

    subgraph "Plugin Components"
        H[Skills]
        I[Agents]
        J[Hooks]
        K[MCP Servers]
    end

    G --> H
    G --> I
    G --> J
    G --> K

二、发现与安装插件

2.1 浏览官方插件目录

官方目录托管在 GitHub:anthropics/claude-plugins-official

目录结构:

claude-plugins-official/
├── plugins/              # Anthropic 官方维护的插件
│   └── example-plugin/
├── external_plugins/     # 社区提交的第三方插件
│   └── .../
├── README.md
└── LICENSE

2.2 通过 /plugin 命令管理插件

Claude Code 提供了完整的插件管理命令集:

# 浏览可用插件(交互式)
/plugin discover

# 安装指定插件
/plugin install {plugin-name}@claude-plugins-official

# 示例:安装官方示例插件
/plugin install example-plugin@claude-plugins-official

# 列出已安装插件
/plugin list

# 更新指定插件
/plugin update example-plugin

# 更新整个 marketplace 索引
/plugin marketplace update claude-plugins-official

# 卸载插件
/plugin uninstall example-plugin

2.3 安装后的验证

安装完成后,执行以下检查:

# 1. 查看插件是否出现在列表中
/plugin list

# 2. 查看新增的斜杠命令
/help

# 3. 检查 MCP 服务是否启动
# Claude Code 会自动加载插件中的 .mcp.json 配置

2.4 常见安装问题

问题 原因 解决方案
marketplace not found 未添加 marketplace 源 /plugin marketplace add anthropics/claude-plugins-official
plugin not found 名称拼写错误或索引过期 /plugin marketplace update claude-plugins-official
安装后命令不可用 需要重启 Claude Code 退出后重新启动 claude
MCP 连接失败 服务端地址不可达 检查网络和 .mcp.json 配置

三、插件结构深度解析

3.1 标准目录结构

每个插件遵循 Anthropic 规定的标准布局:

my-plugin/
├── .claude-plugin/
│   └── plugin.json          # 插件元数据(必须)
├── .mcp.json                # MCP 服务配置(可选)
├── skills/                  # 技能定义
│   ├── code-review/
│   │   └── SKILL.md
│   └── deploy/
│       └── SKILL.md
├── agents/                  # 代理定义
│   └── test-runner.md
├── hooks/                   # 事件钩子
│   └── post-commit.sh
├── commands/                # 遗留斜杠命令格式
│   └── legacy-cmd.md
└── README.md                # 文档

关键规则: - plugin.json 必须位于 .claude-plugin/ 目录下 - Skills 放在 skills/ 子目录中,每个 Skill 一个子目录,内含 SKILL.md - 组件位置没有强制要求,但遵循约定便于维护

3.2 plugin.json 清单详解

plugin.json 是插件的核心元数据文件:

{
  "name": "my-deploy-plugin",
  "description": "一键部署到多环境的CI/CD插件,支持蓝绿部署和金丝雀发布",
  "version": "1.2.0",
  "author": {
    "name": "张三",
    "email": "[email protected]"
  },
  "homepage": "https://github.com/zhangsan/claude-deploy-plugin",
  "license": "MIT",
  "keywords": ["deploy", "ci-cd", "devops"],
  "requires": {
    "claude-code": ">=1.0.55"
  },
  "permissions": [
    "bash:execute",
    "file:read",
    "file:write",
    "network:outbound"
  ]
}

字段说明:

字段 必须 说明
name 是 插件唯一标识,小写字母+连字符
description 是 一句话描述插件功能
version 是 语义化版本号
author 是 作者信息
homepage 否 项目主页URL
license 否 开源许可证
keywords 否 搜索关键词
requires 否 依赖的 Claude Code 最低版本
permissions 否 声明所需权限

3.3 Skills 的两种形态

Skills 是插件中最常见的组件,有两种触发方式:

模型触发型(根据上下文自动激活):

---
name: smart-test-gen
description: 当用户编写新函数或修改现有函数时,自动生成对应的单元测试
version: 1.0.0
---

# 智能测试生成

## 触发条件
当检测到以下场景时自动激活:
- 用户编写了新的函数或方法
- 用户修改了现有函数的签名或逻辑
- 用户明确要求生成测试

## 执行步骤
1. 分析目标函数的输入输出类型
2. 识别边界条件和异常路径
3. 生成覆盖以下场景的测试用例...

用户触发型(斜杠命令):

---
name: deploy
description: 部署当前项目到指定环境
argument-hint: <environment> [--canary] [--rollback]
allowed-tools: [Bash, Read, Glob, Grep]
---

# 部署命令

## 使用方式
/deploy staging          # 部署到预发布环境
/deploy production       # 部署到生产环境
/deploy production --canary  # 金丝雀发布

## 执行流程
1. 检查当前分支和 Git 状态...

3.4 MCP Server 集成

插件通过 .mcp.json 声明需要的外部工具服务:

{
  "my-database-tool": {
    "type": "stdio",
    "command": "npx",
    "args": ["-y", "mcp-server-postgres", "postgresql://localhost/mydb"]
  },
  "my-api-tool": {
    "type": "http",
    "url": "https://api.example.com/mcp"
  }
}

安装插件时,Claude Code 会自动将这些 MCP 配置合并到运行时环境。

3.5 Hooks 事件钩子

插件可以在特定事件触发时执行自定义脚本:

#!/bin/bash
# hooks/post-commit.sh
# 每次 Git commit 后自动运行测试

echo "Running post-commit checks..."
npm test -- --coverage
if [ $? -ne 0 ]; then
  echo "⚠️ Tests failed! Consider fixing before pushing."
fi

四、实战案例:构建一个代码审查插件

我们将构建一个名为 code-review-pro 的完整插件,包含自动审查能力、斜杠命令和 Git Hook。

4.1 创建项目结构

# 创建插件目录
mkdir -p code-review-pro/.claude-plugin
mkdir -p code-review-pro/skills/auto-review
mkdir -p code-review-pro/skills/review-command
mkdir -p code-review-pro/hooks

cd code-review-pro
git init

4.2 编写 plugin.json

{
  "name": "code-review-pro",
  "description": "智能代码审查插件,支持自动审查、差异审查和PR审查",
  "version": "1.0.0",
  "author": {
    "name": "Your Name",
    "email": "[email protected]"
  },
  "homepage": "https://github.com/you/code-review-pro",
  "license": "MIT",
  "keywords": ["review", "code-quality", "pr-review"],
  "requires": {
    "claude-code": ">=1.0.55"
  }
}

4.3 编写自动审查 Skill(模型触发型)

创建 skills/auto-review/SKILL.md:

---
name: auto-review
description: 当用户完成代码编辑并准备提交时,自动触发代码审查建议
version: 1.0.0
---

# 自动代码审查

## 触发条件
当检测到用户执行以下操作时激活:
- 用户准备 git commit
- 用户明确说"帮我审查代码"
- 用户完成一段较长的代码编辑

## 审查维度

### 1. 安全性检查
- SQL 注入风险
- XSS 漏洞
- 敏感信息泄露(硬编码密钥、token)
- 不安全的反序列化

### 2. 代码质量
- 函数复杂度(圈复杂度 > 10 标记为警告)
- 重复代码检测
- 命名规范一致性
- 未使用的导入和变量

### 3. 性能
- N+1 查询模式
- 不必要的同步操作
- 大循环内的内存分配
- 缺少索引的数据库查询

### 4. 错误处理
- 未捕获的异常
- 空值检查缺失
- 错误信息过于笼统

## 输出格式
使用以下标记分级:
- 🔴 CRITICAL:必须修复,存在安全或正确性问题
- 🟡 WARNING:建议修复,影响可维护性或性能
- 🔵 INFO:优化建议,不影响功能正确性

4.4 编写斜杠命令 Skill(用户触发型)

创建 skills/review-command/SKILL.md:

---
name: review
description: 对指定文件或Git差异执行深度代码审查
argument-hint: <file-path | --diff | --staged> [--focus security|performance|all]
allowed-tools: [Read, Glob, Grep, Bash]
---

# 代码审查命令

## 使用方式
/review src/auth/login.ts                  # 审查指定文件
/review --diff                             # 审查当前未暂存的修改
/review --staged                           # 审查已暂存的修改
/review --diff --focus security            # 仅审查安全相关问题
/review --staged --focus performance       # 仅审查性能问题

## 执行流程

### 当参数为文件路径时
1. 读取目标文件完整内容
2. 识别文件类型和使用的框架
3. 按审查维度逐一分析
4. 生成结构化审查报告

### 当参数为 --diff 或 --staged 时
1. 运行 `git diff` 或 `git diff --staged` 获取变更
2. 逐个文件分析变更内容
3. 重点关注新增和修改的代码
4. 生成差异审查报告

### 当指定 --focus 时
仅输出对应维度的审查结果,跳过其他维度

## 输出格式

代码审查报告

📊 总览

🔴 严重问题

1. [文件:行号] 问题标题

问题描述: ... 修复建议: ... ```typescript // 修复后 ```

🟡 警告

...

🔵 建议

...


4.5 编写 Hook

创建 hooks/pre-commit.sh:

#!/bin/bash
# pre-commit hook: 在用户执行 git commit 前触发

echo "🔍 Running code review pre-commit check..."

# 获取暂存文件列表
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx|py|rs|go)$')

if [ -z "$STAGED_FILES" ]; then
  echo "No source files to review."
  exit 0
fi

# 检查明显的安全问题
for FILE in $STAGED_FILES; do
  # 检查硬编码密钥
  if grep -nE '(password|secret|api_key|token)\s*=\s*["\x27][^"\x27]{8,}' "$FILE" 2>/dev/null; then
    echo "🔴 CRITICAL: Potential hardcoded secret in $FILE"
    exit 1
  fi

  # 检查调试代码
  if grep -nE '(console\.log|print\(|dbg!)' "$FILE" 2>/dev/null; then
    echo "🟡 WARNING: Debug statement found in $FILE"
  fi
done

echo "✅ Pre-commit checks passed."

4.6 编写 README.md

# Code Review Pro

智能代码审查插件 for Claude Code。

## 功能
- 🤖 自动代码审查:编辑完成后自动触发
- 📋 差异审查:审查 Git 暂存或未暂存的修改
- 🔒 安全聚焦:专门检查安全漏洞
- ⚡ 性能聚焦:识别性能瓶颈
- 🪝 Pre-commit Hook:提交前自动检查

## 安装
/plugin install code-review-pro@your-marketplace

## 使用
- 编辑代码后,审查建议自动出现
- `/review <file>` - 审查指定文件
- `/review --staged` - 审查暂存修改
- `/review --diff --focus security` - 安全审查

## 配置
无需额外配置,安装即用。

4.7 完整项目结构确认

code-review-pro/
├── .claude-plugin/
│   └── plugin.json              # 插件元数据
├── skills/
│   ├── auto-review/
│   │   └── SKILL.md             # 自动审查能力
│   └── review-command/
│       └── SKILL.md             # /review 斜杠命令
├── hooks/
│   └── pre-commit.sh            # 提交前钩子
└── README.md                    # 文档

五、发布插件到 Marketplace

5.1 发布到官方目录

Anthropic 官方目录接受两类插件:

内部插件(由 Anthropic 团队维护): - 直接在 plugins/ 目录下开发 - 需要内部审核流程

外部插件(社区提交): - 通过 plugin directory submission form 提交 - 需要满足质量和安全标准 - 审核通过后出现在 external_plugins/ 目录

提交流程:

flowchart LR
    A[准备插件代码] --> B[自测验证]
    B --> C[提交表单]
    C --> D{Anthropic 审核}
    D -->|通过| E[合并到官方目录]
    D -->|驳回| F[根据反馈修改]
    F --> C
    E --> G[用户可搜索安装]

5.2 创建私有 Marketplace

企业团队更常需要私有 Marketplace,在内部共享定制插件。

步骤一:创建 Marketplace 仓库

# 创建 marketplace 仓库
mkdir my-team-marketplace
cd my-team-marketplace
git init

# 创建索引文件
cat > marketplace.json << 'INDEXEOF'
{
  "name": "my-team-marketplace",
  "description": "团队内部插件市场",
  "plugins": [
    {
      "name": "code-review-pro",
      "version": "1.0.0",
      "description": "智能代码审查插件",
      "path": "plugins/code-review-pro"
    }
  ]
}
INDEXEOF

# 复制插件到 marketplace
mkdir -p plugins
cp -r ../code-review-pro plugins/

# 提交并推送到内部 Git
git add .
git commit -m "Initial marketplace with code-review-pro"
git remote add origin [email protected]:your-org/my-team-marketplace.git
git push -u origin main

步骤二:在 Claude Code 中注册 Marketplace

# 在 Claude Code 中添加私有 marketplace
/plugin marketplace add your-org/my-team-marketplace

# 浏览可用插件
/plugin discover

# 安装
/plugin install code-review-pro@my-team-marketplace

步骤三:版本更新管理

当插件更新后:

# 1. 更新 plugin.json 中的 version
# 2. 更新 marketplace.json 中的版本号
# 3. 推送更新
git add .
git commit -m "Update code-review-pro to v1.1.0"
git push

# 用户侧更新
/plugin marketplace update my-team-marketplace
/plugin update code-review-pro

5.3 marketplace.json 规范

{
  "name": "marketplace-name",
  "description": "市场描述",
  "homepage": "https://github.com/org/marketplace",
  "plugins": [
    {
      "name": "plugin-name",
      "version": "1.0.0",
      "description": "插件描述",
      "path": "plugins/plugin-name",
      "author": "Author Name",
      "tags": ["tag1", "tag2"]
    }
  ]
}

六、实战进阶:多组件协同插件

上面的 code-review-pro 是一个基础案例。真实场景中,插件往往需要多个组件协同工作。下面展示一个更复杂的例子——一个全栈开发辅助插件。

6.1 架构设计

graph TB
    subgraph "fullstack-dev-kit Plugin"
        A[plugin.json]

        subgraph "Skills"
            B[component-gen<br/>组件生成器]
            C[api-gen<br/>API生成器]
            D[db-migrate<br/>数据库迁移]
        end

        subgraph "MCP Servers"
            E[db-tools<br/>PostgreSQL工具]
            F[api-docs<br/>OpenAPI文档服务]
        end

        subgraph "Hooks"
            G[pre-push<br/>推送前检查]
        end
    end

    B --> E
    C --> F
    D --> E

    style A fill:#e1f5fe
    style B fill:#c8e6c9
    style C fill:#c8e6c9
    style D fill:#c8e6c9
    style E fill:#fff9c4
    style F fill:#fff9c4
    style G fill:#ffccbc

6.2 关键设计原则

原则一:单一职责 每个 Skill 只负责一个明确的功能。不要把"组件生成"和"API 生成"塞进同一个 Skill。

原则二:声明式依赖 通过 .mcp.json 声明 Skill 需要的外部工具,而不是在 Skill 中硬编码命令。

原则三:权限最小化 allowed-tools 只列出 Skill 实际需要的工具,避免过度授权:

# 只需要读文件的审查命令
allowed-tools: [Read, Glob, Grep]

# 需要执行命令的部署命令
allowed-tools: [Bash, Read, Glob, Grep]

原则四:版本语义化 插件的版本号遵循 semver,plugin.json 中的 requires 字段明确声明最低兼容版本:

{
  "version": "2.1.0",
  "requires": {
    "claude-code": ">=1.0.55"
  }
}

七、插件调试与排错

7.1 查看插件加载日志

# 启动 Claude Code 时查看详细日志
claude --debug

# 查看已安装插件列表和状态
/plugin list --verbose

7.2 常见问题排查

问题1:Skill 没有被触发

检查项: 1. SKILL.md 的 frontmatter 中 description 字段是否清晰描述了触发条件 2. 文件路径是否正确(skills/<name>/SKILL.md) 3. 是否在 allowed-tools 中声明了所需的工具

问题2:MCP Server 启动失败

检查项: 1. .mcp.json 中的 command 或 URL 是否正确 2. 本地是否安装了对应的依赖(如 npx 命令需要 npm) 3. 网络连接是否正常(HTTP 类型)

问题3:Hook 执行权限不足

# 确保 hook 脚本有执行权限
chmod +x hooks/pre-commit.sh

问题4:插件安装后不生效

# 重启 Claude Code
# 退出当前会话,重新启动
claude

# 强制刷新 marketplace 索引
/plugin marketplace update <marketplace-name>

7.3 本地测试插件

在发布前,可以直接将插件目录链接到本地测试:

# 方法一:直接引用本地路径(适用于 Claude Code 支持的场景)
/plugin install /path/to/my-plugin

# 方法二:通过本地 marketplace 测试
# 1. 创建本地 marketplace 仓库
# 2. 将插件放入 plugins/ 目录
# 3. 注册本地 marketplace
/plugin marketplace add /path/to/my-team-marketplace

八、插件生态现状与趋势

8.1 官方目录概览

截至2026年5月,claude-plugins-official 收录了55+插件,涵盖以下类别:

类别 代表插件 功能
代码质量 eslint-pro, typecheck-guard 自动化代码检查
部署运维 deploy-manager, k8s-helper CI/CD 和容器编排
数据库 db-migrate, schema-validator 数据库操作和迁移
文档 doc-generator, api-docs 自动文档生成
测试 test-runner, coverage-check 测试执行和覆盖率
安全 security-scan, dependency-audit 安全扫描和依赖审计

8.2 生态趋势

根据2026年5月 Code with Claude 开发者大会公布的信息:

  1. 插件数量爆发:从年初的20+增长到55+,社区提交占比超过60%
  2. 企业采纳加速:私有 Marketplace 成为团队标准实践
  3. 质量门槛提升:Anthropic 加强了外部插件的审核标准,要求通过安全审计
  4. 跨平台兼容:插件系统正在向 IDE 集成场景扩展

九、完整工作流总结

flowchart TD
    A[确定需求] --> B{功能类型?}

    B -->|简单能力| C[创建 SKILL.md]
    B -->|多组件集成| D[创建 Plugin]

    C --> E[放入 ~/.claude/skills/]
    E --> F[直接使用]

    D --> G[编写 plugin.json]
    G --> H[编写 Skills/Agents/Hooks]
    H --> I[本地测试]

    I --> J{发布目标?}
    J -->|官方目录| K[提交 Anthropic 审核]
    J -->|团队共享| L[创建私有 Marketplace]
    J -->|开源社区| M[发布到 GitHub Marketplace]

    K --> N[审核通过后上线]
    L --> O[团队 /plugin install]
    M --> P[社区 /plugin install]

    style A fill:#e3f2fd
    style F fill:#c8e6c9
    style N fill:#c8e6c9
    style O fill:#c8e6c9
    style P fill:#c8e6c9

十、核心要点速查

操作 命令/方法
浏览插件 /plugin discover
安装插件 /plugin install name@marketplace
查看已装 /plugin list
更新插件 /plugin update name
卸载插件 /plugin uninstall name
添加市场 /plugin marketplace add org/repo
更新市场 /plugin marketplace update name
本地测试 /plugin install /local/path

何时用 Skill vs Plugin: - 个人使用、快速迭代 → 直接用 Skill(~/.claude/skills/) - 团队共享、版本管理 → 打包为 Plugin + Marketplace - 企业标准、强制执行 → Plugin + Hooks + 私有 Marketplace


延伸阅读

训练营适用性评估

维度 说明
适合训练营类型 企业内训、开发者社区 Workshop、在线课程
预计教学时长 3-4小时(含实操)
学员基础要求 Claude Code 使用1个月+,Git 操作熟练
实操环境要求 每人一台开发机,已安装 Claude Code >= 1.0.55
推荐教学节奏 理论30分钟 → 安装体验30分钟 → 构建插件90分钟 → 发布Marketplace60分钟

参考来源

  1. Discover and install prebuilt plugins through marketplaces - Claude Code Docs — Anthropic 官方插件发现与安装文档
  2. Create and distribute a plugin marketplace - Claude Code Docs — Marketplace 创建与分发官方指南
  3. anthropics/claude-plugins-official - GitHub — Anthropic 官方插件目录仓库
  4. How to Build Claude Code Plugins - DataCamp — DataCamp 插件构建分步教程
  5. Claude Code May 2026 Release Notes — 2026年5月版本更新,含 Plugin Marketplace 正式发布

[!tip] 51学通信知识星球 星主:爱卫生(微信:gprshome201101)——公众号「51学通信」创办者,19 年通信行业运维与讲师经验,著有《GPRS网络信令实例详解》《EPC原理与实践》《5G核心网原理与实践》三本纸质书(各大电商平台有售)。 星球定位:深耕 5G 核心网、IMS、NFV/AI、云原生等 ICT 领域,主打运维岗位与信令分析。 当前规模:5200 篇主题 / 990 个回答,198 元/年。 加入福利:加入星球后请加爱卫生微信,拉您进知识星球年度陪伴与答疑群——不限量回复您的各种问题,另有群内分享。 AI 加持:51学通信知识星球已作为 skill 接入 WorkBuddy,使用 deepseek-4-flash 在海量星球帖子中精准找贴、找视频、找 FAQ 和案例,做总结、找文档和 PPT 等场景全覆盖。

51学通信知识星球海报-扫码加入