插件系统
插件概述
Claude Code 的插件系统允许你扩展其功能,添加新的工具、命令和集成。
插件能做什么?
- 🔧 添加自定义工具
- 📦 集成第三方服务
- 🎨 自定义输出格式
- ⚡ 扩展命令集
安装插件
从 npm 安装
bash
# 安装插件
npm install -g @claude-code/plugin-name
# 或使用 claude CLI
claude plugin install plugin-name配置插件
编辑 ~/.config/claude-code/plugins.json:
json
{
"plugins": {
"plugin-name": {
"enabled": true,
"config": {
"option1": "value1",
"option2": "value2"
}
}
}
}查看已安装插件
bash
claude plugin list
Installed plugins:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✓ context7 (v1.0.0) - Enhanced documentation
✓ prettier (v1.2.0) - Code formatting
✓ eslint (v2.0.0) - Linting
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━常用插件
1. Context7 插件
功能: 增强的文档访问
安装:
bash
claude plugin install context7使用:
bash
You: React 18 的 useTransition 如何使用?
Claude: [Using Context7 plugin]
Fetching latest React documentation...
useTransition allows you to mark state updates as transitions...2. Prettier 插件
功能: 自动代码格式化
配置:
json
{
"plugins": {
"prettier": {
"enabled": true,
"config": {
"semi": true,
"singleQuote": true,
"tabWidth": 2
}
}
}
}使用:
bash
You: 格式化 src/app.js
Claude: [Using Prettier plugin]
✓ Formatted src/app.js
- Fixed 12 formatting issues3. ESLint 插件
功能: 代码质量检查
使用:
bash
You: 检查代码质量问题
Claude: [Using ESLint plugin]
Found 5 issues:
⚠ src/api.js:23 - Unused variable 'data'
⚠ src/utils.js:45 - Missing return statement
...开发自定义插件
插件结构
javascript
// my-plugin/index.js
module.exports = {
name: 'my-plugin',
version: '1.0.0',
description: 'My custom plugin',
// 插件初始化
async init(context) {
console.log('Plugin initialized');
},
// 注册工具
tools: {
'my-tool': {
description: 'My custom tool',
async execute(params) {
// 工具逻辑
return { result: 'success' };
}
}
},
// 注册命令
commands: {
'my-command': {
description: 'My custom command',
async handler(args) {
// 命令逻辑
}
}
}
};package.json
json
{
"name": "@claude-code/plugin-my-plugin",
"version": "1.0.0",
"description": "My custom Claude Code plugin",
"main": "index.js",
"keywords": ["claude-code", "plugin"],
"peerDependencies": {
"@claude-code/sdk": "^1.0.0"
}
}发布插件
bash
# 测试插件
npm link
# 发布到 npm
npm publish插件管理
启用/禁用插件
bash
# 禁用插件
claude plugin disable plugin-name
# 启用插件
claude plugin enable plugin-name更新插件
bash
# 更新单个插件
claude plugin update plugin-name
# 更新所有插件
claude plugin update-all卸载插件
bash
claude plugin uninstall plugin-name小结
在本章中,我们学习了:
- ✅ 插件系统概述
- ✅ 如何安装和配置插件
- ✅ 常用插件介绍
- ✅ 开发自定义插件
- ✅ 插件管理操作
关键要点:
- 插件扩展 Claude Code 功能
- 可以从 npm 安装或自行开发
- 灵活的配置选项
- 易于管理和更新