plugin-structure
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlugin Structure for Claude Code
Claude Code 插件结构
Overview
概述
Claude Code plugins follow a standardized directory structure with automatic component discovery. Understanding this structure enables creating well-organized, maintainable plugins that integrate seamlessly with Claude Code.
Key concepts:
- Conventional directory layout for automatic discovery
- Manifest-driven configuration in
.claude-plugin/plugin.json - Component-based organization (commands, agents, skills, hooks)
- Portable path references using
${CLAUDE_PLUGIN_ROOT} - Explicit vs. auto-discovered component loading
Claude Code 插件遵循标准化的目录结构,支持组件自动发现。了解此结构有助于创建组织良好、易于维护且能与Claude Code无缝集成的插件。
核心概念:
- 用于自动发现的约定式目录布局
- 位于的清单驱动配置
.claude-plugin/plugin.json - 基于组件的组织方式(命令、Agent、Skill、钩子)
- 使用的可移植路径引用
${CLAUDE_PLUGIN_ROOT} - 显式加载与自动发现组件的对比
Directory Structure
目录结构
Every Claude Code plugin follows this organizational pattern:
plugin-name/
├── .claude-plugin/
│ └── plugin.json # Required: Plugin manifest
├── commands/ # Slash commands (.md files)
├── agents/ # Subagent definitions (.md files)
├── skills/ # Agent skills (subdirectories)
│ └── skill-name/
│ └── SKILL.md # Required for each skill
├── hooks/
│ └── hooks.json # Event handler configuration
├── .mcp.json # MCP server definitions
└── scripts/ # Helper scripts and utilitiesCritical rules:
- Manifest location: The manifest MUST be in
plugin.jsondirectory.claude-plugin/ - Component locations: All component directories (commands, agents, skills, hooks) MUST be at plugin root level, NOT nested inside
.claude-plugin/ - Optional components: Only create directories for components the plugin actually uses
- Naming convention: Use kebab-case for all directory and file names
每个Claude Code插件都遵循以下组织模式:
plugin-name/
├── .claude-plugin/
│ └── plugin.json # 必填:插件清单
├── commands/ # 斜杠命令(.md文件)
├── agents/ # 子Agent定义(.md文件)
├── skills/ # Agent Skill(子目录)
│ └── skill-name/
│ └── SKILL.md # 每个Skill必填
├── hooks/
│ └── hooks.json # 事件处理器配置
├── .mcp.json # MCP服务器定义
└── scripts/ # 辅助脚本和工具关键规则:
- 清单位置:清单必须位于
plugin.json目录中.claude-plugin/ - 组件位置:所有组件目录(commands、agents、skills、hooks)必须位于插件根目录,不能嵌套在内
.claude-plugin/ - 可选组件:仅为插件实际使用的组件创建目录
- 命名规范:所有目录和文件名使用kebab-case格式
Plugin Manifest (plugin.json)
插件清单(plugin.json)
The manifest defines plugin metadata and configuration. Located at :
.claude-plugin/plugin.json清单定义插件的元数据和配置,位于:
.claude-plugin/plugin.jsonRequired Fields
必填字段
json
{
"name": "plugin-name"
}Name requirements:
- Use kebab-case format (lowercase with hyphens)
- Must be unique across installed plugins
- No spaces or special characters
- Example: ,
code-review-assistant,test-runnerapi-docs
json
{
"name": "plugin-name"
}名称要求:
- 使用kebab-case格式(小写加连字符)
- 在已安装的插件中必须唯一
- 不能包含空格或特殊字符
- 示例:、
code-review-assistant、test-runnerapi-docs
Recommended Metadata
推荐元数据
json
{
"name": "plugin-name",
"version": "1.0.0",
"description": "Brief explanation of plugin purpose",
"author": {
"name": "Author Name",
"email": "author@example.com",
"url": "https://example.com"
},
"homepage": "https://docs.example.com",
"repository": "https://github.com/user/plugin-name",
"license": "MIT",
"keywords": ["testing", "automation", "ci-cd"]
}Version format: Follow semantic versioning (MAJOR.MINOR.PATCH)
Keywords: Use for plugin discovery and categorization
json
{
"name": "plugin-name",
"version": "1.0.0",
"description": "插件用途的简要说明",
"author": {
"name": "作者姓名",
"email": "author@example.com",
"url": "https://example.com"
},
"homepage": "https://docs.example.com",
"repository": "https://github.com/user/plugin-name",
"license": "MIT",
"keywords": ["testing", "automation", "ci-cd"]
}版本格式:遵循语义化版本(MAJOR.MINOR.PATCH)
关键词:用于插件发现和分类
Component Path Configuration
组件路径配置
Specify custom paths for components (supplements default directories):
json
{
"name": "plugin-name",
"commands": "./custom-commands",
"agents": ["./agents", "./specialized-agents"],
"hooks": "./config/hooks.json",
"mcpServers": "./.mcp.json"
}Important: Custom paths supplement defaults—they don't replace them. Components in both default directories and custom paths will load.
Path rules:
- Must be relative to plugin root
- Must start with
./ - Cannot use absolute paths
- Support arrays for multiple locations
指定组件的自定义路径(补充默认目录):
json
{
"name": "plugin-name",
"commands": "./custom-commands",
"agents": ["./agents", "./specialized-agents"],
"hooks": "./config/hooks.json",
"mcpServers": "./.mcp.json"
}重要提示:自定义路径是对默认目录的补充,而非替代。默认目录和自定义路径中的组件都会被加载。
路径规则:
- 必须相对于插件根目录
- 必须以开头
./ - 不能使用绝对路径
- 支持数组指定多个位置
Component Organization
组件组织
Commands
命令
Location: directory
Format: Markdown files with YAML frontmatter
Auto-discovery: All files in load automatically
commands/.mdcommands/Example structure:
commands/
├── review.md # /review command
├── test.md # /test command
└── deploy.md # /deploy commandFile format:
markdown
---
name: command-name
description: Command description
---
Command implementation instructions...Usage: Commands integrate as native slash commands in Claude Code
位置:目录
格式:带YAML前置元数据的Markdown文件
自动发现:中的所有文件会自动加载
commands/commands/.md示例结构:
commands/
├── review.md # /review命令
├── test.md # /test命令
└── deploy.md # /deploy命令文件格式:
markdown
---
name: command-name
description: 命令描述
---
命令实现说明...用途:命令作为原生斜杠命令集成到Claude Code中
Agents
Agent
Location: directory
Format: Markdown files with YAML frontmatter
Auto-discovery: All files in load automatically
agents/.mdagents/Example structure:
agents/
├── code-reviewer.md
├── test-generator.md
└── refactorer.mdFile format:
markdown
---
description: Agent role and expertise
capabilities:
- Specific task 1
- Specific task 2
---
Detailed agent instructions and knowledge...Usage: Users can invoke agents manually, or Claude Code selects them automatically based on task context
位置:目录
格式:带YAML前置元数据的Markdown文件
自动发现:中的所有文件会自动加载
agents/agents/.md示例结构:
agents/
├── code-reviewer.md
├── test-generator.md
└── refactorer.md文件格式:
markdown
---
description: Agent的角色和专长
capabilities:
- 特定任务1
- 特定任务2
---
详细的Agent说明和知识...用途:用户可以手动调用Agent,或由Claude Code根据任务上下文自动选择
Skills
Skill
Location: directory with subdirectories per skill
Format: Each skill in its own directory with file
Auto-discovery: All files in skill subdirectories load automatically
skills/SKILL.mdSKILL.mdExample structure:
skills/
├── api-testing/
│ ├── SKILL.md
│ ├── scripts/
│ │ └── test-runner.py
│ └── references/
│ └── api-spec.md
└── database-migrations/
├── SKILL.md
└── examples/
└── migration-template.sqlSKILL.md format:
markdown
---
name: Skill Name
description: When to use this skill
version: 1.0.0
---
Skill instructions and guidance...Supporting files: Skills can include scripts, references, examples, or assets in subdirectories
Usage: Claude Code autonomously activates skills based on task context matching the description
位置:目录,每个Skill对应一个子目录
格式:每个Skill在独立目录中包含文件
自动发现:Skill子目录中的所有文件会自动加载
skills/SKILL.mdSKILL.md示例结构:
skills/
├── api-testing/
│ ├── SKILL.md
│ ├── scripts/
│ │ └── test-runner.py
│ └── references/
│ └── api-spec.md
└── database-migrations/
├── SKILL.md
└── examples/
└── migration-template.sqlSKILL.md格式:
markdown
---
name: Skill名称
description: 何时使用此Skill
version: 1.0.0
---
Skill说明和指导...支持文件:Skill可以在子目录中包含脚本、参考资料、示例或资源
用途:Claude Code会根据与描述匹配的任务上下文自动激活Skill
Hooks
钩子
Location: or inline in
Format: JSON configuration defining event handlers
Registration: Hooks register automatically when plugin enables
hooks/hooks.jsonplugin.jsonExample structure:
hooks/
├── hooks.json # Hook configuration
└── scripts/
├── validate.sh # Hook script
└── check-style.sh # Hook scriptConfiguration format:
json
{
"PreToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "bash ${CLAUDE_PLUGIN_ROOT}/hooks/scripts/validate.sh",
"timeout": 30
}]
}]
}Available events: PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification
Usage: Hooks execute automatically in response to Claude Code events
位置:或内联在中
格式:定义事件处理器的JSON配置
注册:插件启用时钩子会自动注册
hooks/hooks.jsonplugin.json示例结构:
hooks/
├── hooks.json # 钩子配置
└── scripts/
├── validate.sh # 钩子脚本
└── check-style.sh # 钩子脚本配置格式:
json
{
"PreToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "bash ${CLAUDE_PLUGIN_ROOT}/hooks/scripts/validate.sh",
"timeout": 30
}]
}]
}可用事件:PreToolUse、PostToolUse、Stop、SubagentStop、SessionStart、SessionEnd、UserPromptSubmit、PreCompact、Notification
用途:钩子会响应Claude Code事件自动执行
MCP Servers
MCP服务器
Location: at plugin root or inline in
Format: JSON configuration for MCP server definitions
Auto-start: Servers start automatically when plugin enables
.mcp.jsonplugin.jsonExample format:
json
{
"mcpServers": {
"server-name": {
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/servers/server.js"],
"env": {
"API_KEY": "${API_KEY}"
}
}
}
}Usage: MCP servers integrate seamlessly with Claude Code's tool system
位置:插件根目录的或内联在中
格式:MCP服务器定义的JSON配置
自动启动:插件启用时服务器会自动启动
.mcp.jsonplugin.json示例格式:
json
{
"mcpServers": {
"server-name": {
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/servers/server.js"],
"env": {
"API_KEY": "${API_KEY}"
}
}
}
}用途:MCP服务器与Claude Code的工具系统无缝集成
Portable Path References
可移植路径引用
${CLAUDE_PLUGIN_ROOT}
${CLAUDE_PLUGIN_ROOT}
Use environment variable for all intra-plugin path references:
${CLAUDE_PLUGIN_ROOT}json
{
"command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/run.sh"
}Why it matters: Plugins install in different locations depending on:
- User installation method (marketplace, local, npm)
- Operating system conventions
- User preferences
Where to use it:
- Hook command paths
- MCP server command arguments
- Script execution references
- Resource file paths
Never use:
- Hardcoded absolute paths ()
/Users/name/plugins/... - Relative paths from working directory (in commands)
./scripts/... - Home directory shortcuts ()
~/plugins/...
所有插件内部路径引用使用环境变量:
${CLAUDE_PLUGIN_ROOT}json
{
"command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/run.sh"
}重要性:插件的安装位置因以下因素而异:
- 用户安装方式(市场、本地、npm)
- 操作系统约定
- 用户偏好
使用场景:
- 钩子命令路径
- MCP服务器命令参数
- 脚本执行引用
- 资源文件路径
禁止使用:
- 硬编码绝对路径()
/Users/name/plugins/... - 相对于工作目录的路径(命令中的)
./scripts/... - 主目录快捷方式()
~/plugins/...
Path Resolution Rules
路径解析规则
In manifest JSON fields (hooks, MCP servers):
json
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/tool.sh"In component files (commands, agents, skills):
markdown
Reference scripts at: ${CLAUDE_PLUGIN_ROOT}/scripts/helper.pyIn executed scripts:
bash
#!/bin/bash在清单JSON字段中(钩子、MCP服务器):
json
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/tool.sh"在组件文件中(命令、Agent、Skill):
markdown
参考脚本位置:${CLAUDE_PLUGIN_ROOT}/scripts/helper.py在执行的脚本中:
bash
#!/bin/bash${CLAUDE_PLUGIN_ROOT} available as environment variable
${CLAUDE_PLUGIN_ROOT}作为环境变量可用
source "${CLAUDE_PLUGIN_ROOT}/lib/common.sh"
undefinedsource "${CLAUDE_PLUGIN_ROOT}/lib/common.sh"
undefinedFile Naming Conventions
文件命名规范
Component Files
组件文件
Commands: Use kebab-case files
.md- →
code-review.md/code-review - →
run-tests.md/run-tests - →
api-docs.md/api-docs
Agents: Use kebab-case files describing role
.mdtest-generator.mdcode-reviewer.mdperformance-analyzer.md
Skills: Use kebab-case directory names
api-testing/database-migrations/error-handling/
命令:使用kebab-case格式的文件
.md- →
code-review.md命令/code-review - →
run-tests.md命令/run-tests - →
api-docs.md命令/api-docs
Agent:使用描述角色的kebab-case格式文件
.mdtest-generator.mdcode-reviewer.mdperformance-analyzer.md
Skill:使用kebab-case格式的目录名
api-testing/database-migrations/error-handling/
Supporting Files
支持文件
Scripts: Use descriptive kebab-case names with appropriate extensions
validate-input.shgenerate-report.pyprocess-data.js
Documentation: Use kebab-case markdown files
api-reference.mdmigration-guide.mdbest-practices.md
Configuration: Use standard names
hooks.json.mcp.jsonplugin.json
脚本:使用描述性kebab-case名称和适当的扩展名
validate-input.shgenerate-report.pyprocess-data.js
文档:使用kebab-case格式的Markdown文件
api-reference.mdmigration-guide.mdbest-practices.md
配置:使用标准名称
hooks.json.mcp.jsonplugin.json
Auto-Discovery Mechanism
自动发现机制
Claude Code automatically discovers and loads components:
- Plugin manifest: Reads when plugin enables
.claude-plugin/plugin.json - Commands: Scans directory for
commands/files.md - Agents: Scans directory for
agents/files.md - Skills: Scans for subdirectories containing
skills/SKILL.md - Hooks: Loads configuration from or manifest
hooks/hooks.json - MCP servers: Loads configuration from or manifest
.mcp.json
Discovery timing:
- Plugin installation: Components register with Claude Code
- Plugin enable: Components become available for use
- No restart required: Changes take effect on next Claude Code session
Override behavior: Custom paths in supplement (not replace) default directories
plugin.jsonClaude Code会自动发现并加载组件:
- 插件清单:插件启用时读取
.claude-plugin/plugin.json - 命令:扫描目录中的
commands/文件.md - Agent:扫描目录中的
agents/文件.md - Skill:扫描目录中包含
skills/的子目录SKILL.md - 钩子:从或清单加载配置
hooks/hooks.json - MCP服务器:从或清单加载配置
.mcp.json
发现时机:
- 插件安装:组件向Claude Code注册
- 插件启用:组件变为可用状态
- 无需重启:更改在下次Claude Code会话中生效
覆盖行为:中的自定义路径是对默认目录的补充(而非替代)
plugin.jsonBest Practices
最佳实践
Organization
组织
-
Logical grouping: Group related components together
- Put test-related commands, agents, and skills together
- Create subdirectories in for different purposes
scripts/
-
Minimal manifest: Keeplean
plugin.json- Only specify custom paths when necessary
- Rely on auto-discovery for standard layouts
- Use inline configuration only for simple cases
-
Documentation: Include README files
- Plugin root: Overall purpose and usage
- Component directories: Specific guidance
- Script directories: Usage and requirements
-
逻辑分组:将相关组件分组
- 把测试相关的命令、Agent和Skill放在一起
- 在中为不同用途创建子目录
scripts/
-
精简清单:保持简洁
plugin.json- 仅在必要时指定自定义路径
- 对于标准布局依赖自动发现
- 仅在简单情况下使用内联配置
-
文档:包含README文件
- 插件根目录:整体用途和使用方法
- 组件目录:特定指导
- 脚本目录:使用方法和要求
Naming
命名
-
Consistency: Use consistent naming across components
- If command is , name related agent
test-runnertest-runner-agent - Match skill directory names to their purpose
- If command is
-
Clarity: Use descriptive names that indicate purpose
- Good: ,
api-integration-testing/code-quality-checker.md - Avoid: ,
utils/,misc.mdtemp.sh
- Good:
-
Length: Balance brevity with clarity
- Commands: 2-3 words (,
review-pr)run-ci - Agents: Describe role clearly (,
code-reviewer)test-generator - Skills: Topic-focused (,
error-handling)api-design
- Commands: 2-3 words (
-
一致性:组件使用一致的命名
- 如果命令是,相关Agent命名为
test-runnertest-runner-agent - Skill目录名与其用途匹配
- 如果命令是
-
清晰性:使用能表明用途的描述性名称
- 推荐:、
api-integration-testing/code-quality-checker.md - 避免:、
utils/、misc.mdtemp.sh
- 推荐:
-
长度:在简洁性和清晰性之间取得平衡
- 命令:2-3个词(、
review-pr)run-ci - Agent:清晰描述角色(、
code-reviewer)test-generator - Skill:聚焦主题(、
error-handling)api-design
- 命令:2-3个词(
Portability
可移植性
- Always use ${CLAUDE_PLUGIN_ROOT}: Never hardcode paths
- Test on multiple systems: Verify on macOS, Linux, Windows
- Document dependencies: List required tools and versions
- Avoid system-specific features: Use portable bash/Python constructs
- 始终使用${CLAUDE_PLUGIN_ROOT}:绝不硬编码路径
- 多系统测试:在macOS、Linux、Windows上验证
- 文档依赖:列出所需工具和版本
- 避免系统特定功能:使用可移植的bash/Python结构
Maintenance
维护
- Version consistently: Update version in plugin.json for releases
- Deprecate gracefully: Mark old components clearly before removal
- Document breaking changes: Note changes affecting existing users
- Test thoroughly: Verify all components work after changes
- 版本一致性:发布时更新plugin.json中的版本
- 优雅弃用:在移除旧组件前清晰标记
- 文档破坏性变更:记录影响现有用户的变更
- 彻底测试:变更后验证所有组件正常工作
Common Patterns
常见模式
Minimal Plugin
最小化插件
Single command with no dependencies:
my-plugin/
├── .claude-plugin/
│ └── plugin.json # Just name field
└── commands/
└── hello.md # Single command无依赖的单个命令:
my-plugin/
├── .claude-plugin/
│ └── plugin.json # 仅包含名称字段
└── commands/
└── hello.md # 单个命令Full-Featured Plugin
全功能插件
Complete plugin with all component types:
my-plugin/
├── .claude-plugin/
│ └── plugin.json
├── commands/ # User-facing commands
├── agents/ # Specialized subagents
├── skills/ # Auto-activating skills
├── hooks/ # Event handlers
│ ├── hooks.json
│ └── scripts/
├── .mcp.json # External integrations
└── scripts/ # Shared utilities包含所有组件类型的完整插件:
my-plugin/
├── .claude-plugin/
│ └── plugin.json
├── commands/ # 用户面向的命令
├── agents/ # 专业化子Agent
├── skills/ # 自动激活的Skill
├── hooks/ # 事件处理器
│ ├── hooks.json
│ └── scripts/
├── .mcp.json # 外部集成
└── scripts/ # 共享工具Skill-Focused Plugin
聚焦Skill的插件
Plugin providing only skills:
my-plugin/
├── .claude-plugin/
│ └── plugin.json
└── skills/
├── skill-one/
│ └── SKILL.md
└── skill-two/
└── SKILL.md仅提供Skill的插件:
my-plugin/
├── .claude-plugin/
│ └── plugin.json
└── skills/
├── skill-one/
│ └── SKILL.md
└── skill-two/
└── SKILL.mdTroubleshooting
故障排除
Component not loading:
- Verify file is in correct directory with correct extension
- Check YAML frontmatter syntax (commands, agents, skills)
- Ensure skill has (not
SKILL.mdor other name)README.md - Confirm plugin is enabled in Claude Code settings
Path resolution errors:
- Replace all hardcoded paths with
${CLAUDE_PLUGIN_ROOT} - Verify paths are relative and start with in manifest
./ - Check that referenced files exist at specified paths
- Test with in hook scripts
echo $CLAUDE_PLUGIN_ROOT
Auto-discovery not working:
- Confirm directories are at plugin root (not in )
.claude-plugin/ - Check file naming follows conventions (kebab-case, correct extensions)
- Verify custom paths in manifest are correct
- Restart Claude Code to reload plugin configuration
Conflicts between plugins:
- Use unique, descriptive component names
- Namespace commands with plugin name if needed
- Document potential conflicts in plugin README
- Consider command prefixes for related functionality
For detailed examples and advanced patterns, see files in and directories.
references/examples/组件未加载:
- 验证文件位于正确目录且扩展名正确
- 检查YAML前置元数据语法(命令、Agent、Skill)
- 确保Skill包含(而非
SKILL.md或其他名称)README.md - 确认插件在Claude Code设置中已启用
路径解析错误:
- 用替换所有硬编码路径
${CLAUDE_PLUGIN_ROOT} - 验证清单中的路径是相对路径且以开头
./ - 检查引用的文件是否存在于指定路径
- 在钩子脚本中用测试
echo $CLAUDE_PLUGIN_ROOT
自动发现不工作:
- 确认目录位于插件根目录(而非内)
.claude-plugin/ - 检查文件命名是否遵循规范(kebab-case、正确扩展名)
- 验证清单中的自定义路径是否正确
- 重启Claude Code以重新加载插件配置
插件间冲突:
- 使用唯一、描述性的组件名称
- 必要时用插件名称作为命令命名空间
- 在插件README中记录潜在冲突
- 考虑为相关功能使用命令前缀
有关详细示例和高级模式,请参阅和目录中的文件。
references/examples/