- ✅ 掌握 Slash Commands 的创建和使用
- ✅ 学会配置 Hooks 实现自动化工作流
- ✅ 理解 MCP (Model Context Protocol) 服务器的集成
- ✅ 创建自定义 Agents
Slash Commands 允许你创建可重用的提示词模板。
.claude/
commands/
review-pr.md
explain-code.md
write-tests.md
---
description: "Review pull request and provide feedback"
---
Please review the following pull request:
- Check code quality
- Identify potential bugs
- Suggest improvements
- Verify test coverage---
description: "Generate tests for a specific file"
---
Generate comprehensive unit tests for {{file}} including:
- Happy path scenarios
- Edge cases
- Error handling
- Integration scenarios- 使用描述性文件名(如
review-security.md) - 添加
description元数据 - 使用
{{variable}}作为参数占位符 - 保持命令专注于单一任务
Hooks 允许在特定事件发生时自动执行 shell 命令。
~/.config/claude/settings.json
用户提交消息后执行
{
"hooks": {
"user-prompt-submit-hook": {
"command": "echo 'Processing your request...'",
"blocking": false
}
}
}工具调用前执行
{
"hooks": {
"tool-call-hook": {
"command": "python validate_tool_call.py",
"blocking": true,
"timeout": 5000
}
}
}对话开始时执行
{
"hooks": {
"conversation-start-hook": {
"command": "git status",
"blocking": false
}
}
}对话结束时执行
{
"hooks": {
"conversation-end-hook": {
"command": "./cleanup.sh",
"blocking": false
}
}
}command: 要执行的 shell 命令blocking: 是否阻塞等待命令完成(默认 false)timeout: 超时时间(毫秒)
Hooks 可以访问以下环境变量:
CLAUDE_TOOL_NAME: 工具名称(仅 tool-call-hook)CLAUDE_TOOL_INPUT: 工具输入(仅 tool-call-hook)CLAUDE_WORKSPACE: 当前工作目录
MCP 允许 Claude Code 连接到外部数据源和工具。
在 ~/.config/claude/settings.json 中:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/files"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "your-token-here"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost/mydb"]
}
}
}-
Filesystem Server
- 安全访问文件系统
- 限制访问特定目录
-
GitHub Server
- 搜索仓库和代码
- 创建 issues 和 PR
- 管理文件内容
-
PostgreSQL Server
- 查询数据库
- 分析数据架构
-
Google Drive Server
- 访问文档和表格
- 搜索文件
-
Brave Search
- Web 搜索能力
Agents 是专门处理特定任务的自主助手。
.claude/
agents/
code-reviewer/
agent.json
instructions.md
test-generator/
agent.json
instructions.md
{
"name": "code-reviewer",
"description": "Reviews code for quality, security, and best practices",
"tools": ["Read", "Grep", "Glob"],
"model": "claude-sonnet-4-5",
"temperature": 0.3
}# Code Reviewer Agent
## Role
You are a senior code reviewer focusing on quality, security, and maintainability.
## Process
1. Read the specified files
2. Check for:
- Security vulnerabilities
- Code smells
- Performance issues
- Best practice violations
3. Provide actionable feedback
4. Suggest improvements with code examples
## Guidelines
- Be constructive and specific
- Prioritize security and correctness
- Consider the project context
- Provide examples for suggestions创建一套完整的 code review 工作流:
- Slash command:
.claude/commands/review.md - Hook: 在代码审查前自动运行 linter
- Custom agent: 专门的代码审查代理
创建测试生成系统:
- Slash command: 生成不同类型的测试
- Hook: 生成测试后自动运行
- Agent: 分析代码覆盖率并建议改进
使用 GitHub MCP 服务器:
- 配置 GitHub token
- 创建自动化 PR 创建命令
- 实现 issue 管理工作流
- 创建了至少 3 个自定义 slash commands
- 配置了至少 2 个不同的 hooks
- 成功集成至少 1 个 MCP 服务器
- 创建了至少 1 个自定义 agent
- 理解 blocking vs non-blocking hooks
- 能够使用 hooks 实现自动化工作流
- 理解 MCP 的作用和限制
- 能够调试 hooks 和 MCP 配置问题
A: 检查 .claude/commands/ 目录和文件扩展名(必须是 .md)
A: 检查 settings.json 语法,确保命令路径正确,查看 logs
A: 验证服务器安装,检查环境变量,确认网络连接
A: 检查 agent.json 配置,确认工具列表正确,验证 instructions.md 格式
完成本模块后,进入模块 4:高级代码操作,学习复杂的代码重构和项目结构优化。