Skip to content

创建并分发插件市场

插件市场(plugin marketplace)是一份可供他人订阅的插件目录,提供集中发现、版本追踪和自动更新能力。本文涵盖从零搭建市场的完整流程:编写 marketplace.json(必填字段、插件来源类型、strict 模式)、托管到 GitHub 或私有 Git 服务、容器镜像预填充插件、企业管控 strictKnownMarketplaces(空数组锁定/来源白名单/正则匹配),以及 CLI 管理命令和故障排查清单。想安装已有市场的插件?见发现并安装插件

概览

创建和分发插件市场分四步:

  1. 创建插件:构建包含 skills、agents、hooks、MCP 服务器或 LSP 服务器的插件。本文假设你已有待分发的插件;创建插件参见创建插件
  2. 创建市场文件:定义 marketplace.json,列出插件及其来源(见创建市场文件)。
  3. 托管市场:推送到 GitHub、GitLab 或其他 Git 托管服务(见托管与分发)。
  4. 与用户共享:用户用 /plugin marketplace add 添加并安装插件(见发现并安装插件)。

市场上线后,推送变更即可更新。用户用 /plugin marketplace update 刷新本地副本。

演示:创建本地市场

本示例创建一个包含 /quality-review 技能的单插件市场,用于代码审查。依次完成目录结构、技能文件、插件清单、市场目录,再安装测试。

步骤 1:创建目录结构

bash
mkdir -p my-marketplace/.claude-plugin
mkdir -p my-marketplace/plugins/quality-review-plugin/.claude-plugin
mkdir -p my-marketplace/plugins/quality-review-plugin/skills/quality-review

步骤 2:创建技能

markdown
# my-marketplace/plugins/quality-review-plugin/skills/quality-review/SKILL.md
---
description: 检查代码中的 Bug、安全问题和性能问题
disable-model-invocation: true
---

检查我选择的代码或最近的变更,关注:
- 潜在 Bug 或边界情况
- 安全问题
- 性能问题
- 可读性改进

步骤 3:创建插件清单

json
{
  "name": "quality-review-plugin",
  "description": "添加 /quality-review 技能用于快速代码审查",
  "version": "1.0.0"
}

步骤 4:创建市场文件

json
{
  "name": "my-plugins",
  "owner": {
    "name": "你的名字"
  },
  "plugins": [
    {
      "name": "quality-review-plugin",
      "source": "./plugins/quality-review-plugin",
      "description": "添加 /quality-review 技能用于快速代码审查"
    }
  ]
}

步骤 5:添加并安装

/plugin marketplace add ./my-marketplace
/plugin install quality-review-plugin@my-plugins

了解插件还能做什么(hooks、agents、MCP 服务器、LSP 服务器),参见插件

创建市场文件

在仓库根目录创建 .claude-plugin/marketplace.json。该文件定义市场名称、维护者信息和插件列表。每个插件条目至少需要 namesource。完整 schema 见下节。

json
{
  "name": "company-tools",
  "owner": {
    "name": "DevTools Team",
    "email": "devtools@example.com"
  },
  "plugins": [
    {
      "name": "code-formatter",
      "source": "./plugins/formatter",
      "description": "保存时自动格式化代码",
      "version": "2.1.0",
      "author": {
        "name": "DevTools Team"
      }
    },
    {
      "name": "deployment-tools",
      "source": {
        "source": "github",
        "repo": "company/deploy-plugin"
      },
      "description": "部署自动化工具"
    }
  ]
}

市场 Schema

必填字段

字段类型说明示例
namestring市场标识符(kebab-case,无空格)。面向用户:安装时会显示(如 /plugin install my-tool@your-marketplace"acme-tools"
ownerobject市场维护者信息(见下表)
pluginsarray可用插件列表见下文

owner 字段

字段类型必填说明
namestring维护者或团队名称
emailstring联系邮箱

可选元数据

字段类型说明
metadata.descriptionstring简短的市场描述
metadata.versionstring市场版本号
metadata.pluginRootstring相对插件源路径的基础目录(如设为 "./plugins",插件 source 可写 "formatter" 而非 "./plugins/formatter"

插件条目

plugins 数组中的每个条目描述一个插件及其来源。可包含插件清单 schema 中的任意字段(如 descriptionversionauthorcommandshooks 等),以及市场特有字段:sourcecategorytagsstrict

必填字段

字段类型说明
namestring插件标识符(kebab-case,无空格)。面向用户:安装时会显示(如 /plugin install my-plugin@marketplace
sourcestring | object插件来源(见插件来源

可选字段

标准元数据:

字段类型说明
descriptionstring简短描述
versionstring插件版本
authorobject作者信息(name 必填,email 可选)
homepagestring主页或文档 URL
repositorystring源码仓库 URL
licensestringSPDX 许可证标识(如 MIT、Apache-2.0)
keywordsarray搜索关键词
categorystring插件分类
tagsarray搜索标签
strictboolean控制 plugin.json 是否为组件定义的权威来源(默认 true)。见 Strict 模式

组件配置字段:

字段类型说明
skillsstring | array技能目录的自定义路径(包含 <name>/SKILL.md
commandsstring | array扁平 .md 技能文件或目录的自定义路径
agentsstring | arrayagent 文件的自定义路径
hooksstring | object自定义 hooks 配置或指向 hooks 文件的路径
mcpServersstring | objectMCP 服务器配置或 MCP 配置文件路径
lspServersstring | objectLSP 服务器配置或 LSP 配置文件路径

插件来源

插件来源告诉 Claude Code 去哪里获取每个插件。插件克隆或复制到本地后,会缓存到 ~/.claude/plugins/cache

来源类型字段说明
相对路径string(如 "./my-plugin"市场仓库内的本地目录,必须以 ./ 开头,相对于市场根目录(非 .claude-plugin/ 目录)
githubobjectreporef?sha?GitHub 仓库
urlobjecturlref?sha?Git URL 来源
git-subdirobjecturlpathref?sha?Git 仓库中的子目录,稀疏克隆以节省带宽,适合 monorepo
npmobjectpackageversion?registry?通过 npm install 安装

相对路径

同一仓库内的插件,使用以 ./ 开头的路径:

json
{
  "name": "my-plugin",
  "source": "./plugins/my-plugin"
}

路径相对于市场根目录(即包含 .claude-plugin/ 的目录),不是 marketplace.json 所在目录。禁止使用 ../ 引用市场根目录外的路径。

GitHub 仓库

json
{
  "name": "github-plugin",
  "source": {
    "source": "github",
    "repo": "owner/plugin-repo"
  }
}

锁定到特定分支、标签或提交:

json
{
  "name": "github-plugin",
  "source": {
    "source": "github",
    "repo": "owner/plugin-repo",
    "ref": "v2.0.0",
    "sha": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0"
  }
}
字段类型说明
repostring必填。owner/repo 格式
refstring可选。分支或标签(默认仓库默认分支)
shastring可选。完整 40 字符 git commit SHA,锁定到精确版本

Git 仓库

json
{
  "name": "git-plugin",
  "source": {
    "source": "url",
    "url": "https://gitlab.com/team/plugin.git"
  }
}

锁定分支或提交:

json
{
  "name": "git-plugin",
  "source": {
    "source": "url",
    "url": "https://gitlab.com/team/plugin.git",
    "ref": "main",
    "sha": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0"
  }
}
字段类型说明
urlstring必填。完整 git URL(https://git@),.git 后缀可省略,Azure DevOps 和 AWS CodeCommit 无后缀的 URL 也支持
refstring可选。分支或标签
shastring可选。完整 40 字符 git commit SHA

Git 子目录

git-subdir 适用于插件位于 Git 仓库子目录的场景。Claude Code 使用稀疏克隆(sparse checkout)只下载该子目录,大幅节省 monorepo 带宽。

json
{
  "name": "my-plugin",
  "source": {
    "source": "git-subdir",
    "url": "https://github.com/acme-corp/monorepo.git",
    "path": "tools/claude-plugin"
  }
}

锁定到特定版本:

json
{
  "name": "my-plugin",
  "source": {
    "source": "git-subdir",
    "url": "https://github.com/acme-corp/monorepo.git",
    "path": "tools/claude-plugin",
    "ref": "v2.0.0",
    "sha": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0"
  }
}

url 也接受 GitHub 简写(owner/repo)或 SSH URL(git@github.com:owner/repo.git)。

字段类型说明
urlstring必填。Git 仓库 URL、GitHub owner/repo 简写或 SSH URL
pathstring必填。仓库内子目录路径(如 "tools/claude-plugin"
refstring可选。分支或标签
shastring可选。完整 40 字符 git commit SHA

npm 包

通过 npm install 安装,适用于公共 npm registry 或团队私有 registry:

json
{
  "name": "my-npm-plugin",
  "source": {
    "source": "npm",
    "package": "@acme/claude-plugin"
  }
}

锁定版本:

json
{
  "name": "my-npm-plugin",
  "source": {
    "source": "npm",
    "package": "@acme/claude-plugin",
    "version": "2.1.0"
  }
}

使用私有 registry:

json
{
  "name": "my-npm-plugin",
  "source": {
    "source": "npm",
    "package": "@acme/claude-plugin",
    "version": "^2.0.0",
    "registry": "https://npm.example.com"
  }
}
字段类型说明
packagestring必填。包名或 scoped 包(如 @org/plugin
versionstring可选。版本或版本范围(如 2.1.0^2.0.0~1.5.0
registrystring可选。自定义 npm registry URL,默认使用系统 npm registry

高级插件条目示例

展示多个可选字段,包括 commands、agents、hooks 和 MCP 服务器的自定义路径:

json
{
  "name": "enterprise-tools",
  "source": {
    "source": "github",
    "repo": "company/enterprise-plugin"
  },
  "description": "企业工作流自动化工具",
  "version": "2.1.0",
  "author": {
    "name": "Enterprise Team",
    "email": "enterprise@example.com"
  },
  "homepage": "https://docs.example.com/plugins/enterprise-tools",
  "repository": "https://github.com/company/enterprise-plugin",
  "license": "MIT",
  "keywords": ["enterprise", "workflow", "automation"],
  "category": "productivity",
  "commands": [
    "./commands/core/",
    "./commands/enterprise/",
    "./commands/experimental/preview.md"
  ],
  "agents": ["./agents/security-reviewer.md", "./agents/compliance-checker.md"],
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PLUGIN_ROOT}/scripts/validate.sh"
          }
        ]
      }
    ]
  },
  "mcpServers": {
    "enterprise-db": {
      "command": "${CLAUDE_PLUGIN_ROOT}/servers/db-server",
      "args": ["--config", "${CLAUDE_PLUGIN_ROOT}/config.json"]
    }
  },
  "strict": false
}

注意要点:

  • ${CLAUDE_PLUGIN_ROOT}:在 hooks 和 MCP 服务器配置中引用插件安装目录内的文件。插件安装时会复制到缓存位置,因此必须使用此变量。需要跨插件更新持久化的依赖或状态,改用 ${CLAUDE_PLUGIN_DATA}
  • strict: false:不需要插件自带 plugin.json,市场条目即为完整定义。详见下节。

Strict 模式

strict 字段控制 plugin.json 是否为组件定义(skills、agents、hooks、MCP 服务器、output styles)的权威来源。

行为
true(默认)plugin.json 是权威。市场条目可在其基础上补充组件,两者合并加载
false市场条目是完整定义。若插件的 plugin.json 也声明了组件,会产生冲突导致插件加载失败

使用场景:

  • strict: true:插件有自己的 plugin.json 并管理自身组件。市场条目可追加额外 skills 或 hooks。这是默认值,适合大多数插件。
  • strict: false:市场运营者需要完全控制。插件仓库只提供原始文件,由市场条目定义哪些文件作为 skills、agents、hooks 等暴露出来。适合市场需要重新组织或策展插件组件的场景。

托管与分发

托管在 GitHub(推荐)

  1. 创建仓库,添加 .claude-plugin/marketplace.json
  2. 用户通过 /plugin marketplace add owner/repo 添加

优势:内置版本控制、issue 追踪和团队协作。

托管在其他 Git 服务

任意 Git 托管服务均可(GitLab、Bitbucket、自托管服务器):

/plugin marketplace add https://gitlab.com/company/plugins.git

私有仓库

Claude Code 支持从私有仓库安装插件。

手动安装与更新:Claude Code 使用已有的 git 凭据助手。终端里能 git clone 的私有仓库,在 Claude Code 里同样可用。常见凭据助手:gh auth login(GitHub)、macOS Keychain、git-credential-store

后台自动更新:启动时在后台运行,不能弹出凭据提示。需在环境变量中设置认证令牌:

提供商环境变量说明
GitHubGITHUB_TOKENGH_TOKENPersonal access token 或 GitHub App token
GitLabGITLAB_TOKENGL_TOKENPersonal access token 或 project token
BitbucketBITBUCKET_TOKENApp password 或 repository access token

在 shell 配置文件(如 .bashrc.zshrc)中设置:

bash
export GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx

为团队配置必装市场

在项目 .claude/settings.json 中添加市场,团队成员信任项目目录时会自动提示安装:

json
{
  "extraKnownMarketplaces": {
    "company-tools": {
      "source": {
        "source": "github",
        "repo": "your-org/claude-plugins"
      }
    }
  }
}

同时指定默认启用的插件:

json
{
  "enabledPlugins": {
    "code-formatter@company-tools": true,
    "deployment-tools@company-tools": true
  }
}

完整配置选项见插件设置

为容器预填充插件

容器镜像和 CI 环境可在构建时预填充插件目录,让 Claude Code 启动时无需克隆即可使用市场和插件。

设置 CLAUDE_CODE_PLUGIN_SEED_DIR 指向预填充目录。多个目录用 : 分隔(Unix)或 ; 分隔(Windows)。Claude Code 按顺序查找,第一个匹配的 marketplace 或 plugin cache 胜出。种子目录结构与 ~/.claude/plugins 相同:

$CLAUDE_CODE_PLUGIN_SEED_DIR/
  known_marketplaces.json
  marketplaces/<name>/...
  cache/<marketplace>/<plugin>/<version>/...

构建方法:构建镜像时先运行一次 Claude Code 安装所需插件,然后将 ~/.claude/plugins 目录复制到镜像中,再设置 CLAUDE_CODE_PLUGIN_SEED_DIR。或者在构建时直接设置 CLAUDE_CODE_PLUGIN_CACHE_DIR 为目标路径:

bash
CLAUDE_CODE_PLUGIN_CACHE_DIR=/opt/claude-seed claude plugin marketplace add your-org/plugins
CLAUDE_CODE_PLUGIN_CACHE_DIR=/opt/claude-seed claude plugin install my-tool@your-plugins

然后在容器运行时环境中设置 CLAUDE_CODE_PLUGIN_SEED_DIR=/opt/claude-seed

行为说明:

  • 只读:种子目录不会被写入。种子市场禁用自动更新(git pull 会失败于只读文件系统)
  • 种子条目优先:种子中声明的市场在每次启动时会覆盖用户配置中的同名条目。要禁用种子插件,用 /plugin disable 而非删除市场
  • 路径解析:Claude Code 在运行时通过探测 $CLAUDE_CODE_PLUGIN_SEED_DIR/marketplaces/<name>/ 定位内容,不依赖种子 JSON 内存储的路径,因此种子挂载到不同路径时也能正确工作
  • 禁止修改:对种子管理的市场运行 /plugin marketplace remove/plugin marketplace update 会失败,提示联系管理员更新种子镜像
  • 与 settings 组合:若 extraKnownMarketplacesenabledPlugins 声明的市场已存在于种子中,Claude Code 使用种子副本,不再克隆

企业管控:管理市场限制

管理员可通过托管设置中的 strictKnownMarketplaces 限制用户可添加的市场来源。

行为
未定义(默认)无限制,用户可添加任何市场
空数组 []完全锁定,用户无法添加任何新市场
来源列表用户只能添加与允许列表完全匹配的市场

禁止所有市场添加:

json
{
  "strictKnownMarketplaces": []
}

仅允许特定市场:

json
{
  "strictKnownMarketplaces": [
    {
      "source": "github",
      "repo": "acme-corp/approved-plugins"
    },
    {
      "source": "github",
      "repo": "acme-corp/security-tools",
      "ref": "v2.0"
    },
    {
      "source": "url",
      "url": "https://plugins.example.com/marketplace.json"
    }
  ]
}

通过正则匹配内部 Git 服务器(适合 GitHub Enterprise Server 或自托管 GitLab):

json
{
  "strictKnownMarketplaces": [
    {
      "source": "hostPattern",
      "hostPattern": "^github\\.example\\.com$"
    }
  ]
}

通过正则匹配文件系统路径

json
{
  "strictKnownMarketplaces": [
    {
      "source": "pathPattern",
      "pathPattern": "^/opt/approved/"
    }
  ]
}

".*" 作为 pathPattern 可允许任意文件系统路径,同时通过 hostPattern 管控网络来源。

限制的工作机制:验证在插件安装过程的早期阶段进行,早于任何网络请求或文件操作,阻止未授权的市场访问。允许列表对大多数来源类型使用精确匹配:GitHub 来源要求 repo 匹配,refpath 若在允许列表中指定则也必须匹配;URL 来源要求完整 URL 精确匹配。

strictKnownMarketplaces 在托管设置中配置,用户和项目配置无法覆盖。


版本管理与发布频道

插件版本决定缓存路径和更新检测。可在 plugin.jsonmarketplace.json 的插件条目中指定版本。

设置发布频道

创建指向同一仓库不同 ref 或 SHA 的两个市场,分别对应"稳定版"和"最新版"频道,再通过托管设置分配给不同用户组:

稳定版市场:

json
{
  "name": "stable-tools",
  "plugins": [
    {
      "name": "code-formatter",
      "source": {
        "source": "github",
        "repo": "acme-corp/code-formatter",
        "ref": "stable"
      }
    }
  ]
}

最新版市场:

json
{
  "name": "latest-tools",
  "plugins": [
    {
      "name": "code-formatter",
      "source": {
        "source": "github",
        "repo": "acme-corp/code-formatter",
        "ref": "latest"
      }
    }
  ]
}

通过托管设置为不同用户组分配市场,稳定组收到 stable-tools,早期体验组收到 latest-tools

json
{
  "extraKnownMarketplaces": {
    "stable-tools": {
      "source": {
        "source": "github",
        "repo": "acme-corp/stable-tools"
      }
    }
  }
}

验证与测试

bash
# 验证市场 JSON 语法
claude plugin validate .

# 在 Claude Code 中验证
/plugin validate .

# 添加并测试
/plugin marketplace add ./path/to/marketplace
/plugin install test-plugin@marketplace-name

完整插件测试工作流见本地测试插件,技术故障排查见插件参考


CLI 管理命令

claude plugin marketplace 子命令适用于脚本和自动化,与交互式会话中的 /plugin marketplace 等效。

添加市场

bash
claude plugin marketplace add <source> [options]

<source> 支持:GitHub owner/repo 简写、git URL、指向 marketplace.json 的远程 URL、本地目录路径。用 @ref 锁定分支或标签(GitHub 简写),用 #ref 锁定(git URL)。

选项说明默认
--scope <scope>声明范围:userprojectlocal。见插件安装范围user
--sparse <paths...>通过 git sparse-checkout 限制检出目录,适合 monorepo
bash
# 从 GitHub 添加
claude plugin marketplace add acme-corp/claude-plugins

# 锁定到特定分支
claude plugin marketplace add acme-corp/claude-plugins@v2.0

# 从 GitLab 添加
claude plugin marketplace add https://gitlab.example.com/team/plugins.git

# 从远程 URL 添加(直接提供 marketplace.json 的 URL)
claude plugin marketplace add https://example.com/marketplace.json

# 本地目录测试
claude plugin marketplace add ./my-marketplace

# 项目范围(写入 .claude/settings.json,团队共享)
claude plugin marketplace add acme-corp/claude-plugins --scope project

# monorepo 稀疏检出
claude plugin marketplace add acme-corp/monorepo --sparse .claude-plugin plugins

列出市场

bash
claude plugin marketplace list [--json]

移除市场

bash
claude plugin marketplace remove <name>

<name>claude plugin marketplace list 显示的市场名,不是添加时的来源。别名 rm 也接受。

更新市场

bash
claude plugin marketplace update [name]

省略 name 则更新全部市场。对种子管理的市场运行 removeupdate 会失败;批量更新时种子市场自动跳过,其他市场仍正常更新。


故障排查

市场加载验证错误

运行 claude plugin validate ./plugin validate . 检查。验证器检查 plugin.json、skill/agent/command frontmatter 和 hooks/hooks.json

错误原因解决方案
File not found: .claude-plugin/marketplace.json清单缺失创建包含必填字段的 .claude-plugin/marketplace.json
Invalid JSON syntax: Unexpected token...marketplace.json JSON 语法错误检查缺少逗号、多余逗号或未引号字符串
Duplicate plugin name "x" found in marketplace两个插件同名每个插件使用唯一 name
plugins[0].source: Path contains ".."来源路径含 ..使用不含 .. 的相对路径
YAML frontmatter failed to parse:...skill/agent/command 文件中 YAML 无效修复 frontmatter 的 YAML 语法
Invalid JSON syntax:...(hooks.json)hooks/hooks.json 格式错误修复 JSON 语法,格式错误会导致整个插件无法加载

警告(不阻塞加载):

  • Marketplace has no plugins defined:在 plugins 数组中至少添加一个插件
  • No marketplace description provided:添加 metadata.description
  • Plugin name "x" is not kebab-case:插件名含大写字母、空格或特殊字符,重命名为小写字母、数字和连字符(claude.ai 市场同步会拒绝非 kebab-case 名称)

故障排查题速查

症状解决方案
市场无法加载验证 URL 可访问;确认 .claude-plugin/marketplace.json 存在;用 claude plugin validate 检查语法
相对路径插件在 URL 市场中失败URL 市场只下载 marketplace.json 本身,改用 GitHub/npm/git URL 来源
安装后文件找不到插件复制到缓存,不能引用目录外文件,参见插件缓存与文件解析
Git 操作超时默认 120 秒,设置 CLAUDE_CODE_PLUGIN_GIT_TIMEOUT_MS=300000
私有仓库认证失败(手动)确认 git 凭据助手已配置,手动测试 git clone 是否可用
私有仓库认证失败(自动更新)设置 GITHUB_TOKEN/GITLAB_TOKEN/BITBUCKET_TOKEN 并验证权限未过期
离线环境更新失败后插件消失设置 CLAUDE_CODE_PLUGIN_KEEP_MARKETPLACE_ON_FAILURE=1 保留失败时的缓存

相关资源


常见问题

Q: strict: falsestrict: true 到底有什么区别,什么时候用哪个?

strict: true(默认)时,plugin.json 是权威,市场条目只是补充——两者会合并加载。适合插件作者自己管理的常规插件。strict: false 时,市场条目是完整定义,插件仓库不需要 plugin.json(但若有且声明了组件,会冲突导致加载失败)。适合市场运营者需要完全控制插件暴露哪些能力的场景,比如对第三方插件进行策展和重组。

Q: 如何防止用户自行添加未经审核的市场?

在托管设置中配置 strictKnownMarketplaces:设为 [] 完全禁止,或填写允许的来源列表。该字段在托管(managed/policy)层设置,用户和项目配置无法覆盖。内部 Git 服务器可用 hostPattern 正则匹配,无需逐一列出仓库。

Q: 容器里如何让 Claude Code 启动时不需要克隆插件?

使用 CLAUDE_CODE_PLUGIN_SEED_DIR 预填充插件。构建镜像时先在本地安装插件(设置 CLAUDE_CODE_PLUGIN_CACHE_DIR 指向目标路径),再在容器运行时环境设置 CLAUDE_CODE_PLUGIN_SEED_DIR。种子目录只读,不被写入,插件开箱即用,无需网络克隆。