
🚩 2026 年「术哥无界」系列实战文档 X 篇原创计划 第 40 篇,AI 编程最佳实战「2026」系列第 6 篇 大家好,欢迎来到 术哥无界 | ShugeX | 运维有术。
我是术哥,一名专注于 AI 编程、AI 智能体、Agent Skills、MCP、云原生、Milvus 向量数据库的技术实践者与开源布道者! Talk is cheap, let's explore。无界探索,有术而行。

Claude Code 六大配置维度全景图
你已经在用 Claude Code 了,能读代码、改文件、跑命令。但总觉得差点意思——每次开新会话都要重新解释项目背景,频繁的权限确认打断思路,重复的操作流程一遍遍手动执行。
问题不在工具,在配置。这篇文章帮你把 Claude Code 从"能用"配置成"好用"。
在开始之前,确保你已经:

Claude Code 配置文件层级关系:以 CLAUDE.md 为中心
一句话总结:CLAUDE.md 是 Claude 的"项目说明书",告诉它你的项目习惯、代码风格、工作流程。
Claude Code 启动时会自动读取项目中的 CLAUDE.md 文件。这个文件相当于给 Claude 写了一份"项目说明书",让它知道:
没有这份说明书,Claude 每次都要从代码中推断这些信息。有了它,Claude 就能"秒懂"你的项目。
CLAUDE.md 有四个作用域,优先级从高到低:
作用域 | 位置 | 用途 | 共享范围 |
|---|---|---|---|
Managed | 系统配置目录 | 组织级指令 | 所有用户 |
Project | ./CLAUDE.md | 项目团队共享 | 通过 git 共享 |
User | ~/.claude/CLAUDE.md | 个人偏好 | 仅自己 |
Local | ./CLAUDE.local.md | 项目特定个人配置 | 仅自己(不提交) |
推荐做法:
./CLAUDE.md(提交到 git)./CLAUDE.local.md(不提交)✅ 应该包含的内容:
# Build & Test Commands
- Build: `npm run build`
- Test: `npm test`
- Lint: `npm run lint`
# Code Style
- Use TypeScript strict mode
- Prefer functional components
- 2-space indentation
# Architecture
- See @docs/architecture.md for system design
- API handlers in src/api/handlers/
# Workflow
- Always run tests before committing
- Use conventional commit messages
# Common Gotchas
- Database migrations must be backwards compatible
- JWT tokens expire after 24 hours
❌ 不应该包含的内容:
当配置变长时,使用 @path 导入其他文件:
# Architecture
See @docs/architecture.md for system design
See @docs/api-conventions.md for API conventions
配置好 CLAUDE.md 后:
最佳实践:控制在 200 行以内,只包含 Claude 无法从代码推断的信息。
一句话总结:MCP(Model Context Protocol)让 Claude Code 能连接外部工具和数据源,如 GitHub、数据库、Slack 等。
Claude Code 默认只能访问本地文件和执行命令。但实际工作中,你需要:
MCP 协议让 Claude 能安全地连接这些外部工具,扩展其能力边界。
方式 | 适用场景 | 特点 |
|---|---|---|
HTTP | 远程服务 | 推荐,支持认证和加密 |
Stdio | 本地进程 | 通过命令启动本地服务 |
SSE | 已弃用 | 建议迁移到 HTTP |
# HTTP 服务器(推荐)
claude mcp add --transport http github https://api.githubcopilot.com/mcp/
# 带认证的 HTTP
claude mcp add --transport http secure-api https://api.example.com/mcp \
--header "Authorization: Bearer your-token"
# Stdio 本地服务器
claude mcp add --transport stdio --env DB_URL=xxx db \
-- npx -y @bytebase/dbhub --dsn "postgresql://localhost/mydb"
# 本地作用域(默认,仅自己)
claude mcp add --scope local server-name
# 项目作用域(通过 git 共享)
claude mcp add --scope project server-name
# 用户作用域(全局)
claude mcp add --scope user server-name
手动配置时,创建 .mcp.json 文件:
{
"mcpServers": {
"github": {
"type": "http",
"url": "https://api.githubcopilot.com/mcp/"
},
"database": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@bytebase/dbhub", "--dsn", "${DB_DSN}"]
},
"slack": {
"type": "http",
"url": "https://mcp.slack.com/anthropic",
"headers": {
"Authorization": "Bearer ${SLACK_TOKEN}"
}
}
}
}
环境变量扩展:使用 {VAR_NAME} 或 {VAR_NAME:-default} 语法。
配置好 MCP 后:
常见问题排查:
问题 | 解决方案 |
|---|---|
连接失败 | 检查 URL、网络、防火墙 |
认证失败 | 验证 token 是否有效 |
stdio 服务不工作 | 确认命令可执行,检查环境变量 |
一句话总结:Hooks 让你在工具执行前后插入自定义逻辑,实现自动化工作流。
想象这些场景:
这些都可以通过 Hooks 自动化,无需手动干预。
SessionStart → UserPromptSubmit → PreToolUse → [PermissionRequest] →
Tool Execution → PostToolUse/PostToolUseFailure → Stop → SessionEnd
事件 | 触发时机 | 可否阻止 | 典型用途 |
|---|---|---|---|
PreToolUse | 工具执行前 | 是 | 验证命令安全性 |
PostToolUse | 工具成功后 | 否 | 自动 lint、格式化 |
PostToolUseFailure | 工具失败后 | 否 | 发送通知 |
UserPromptSubmit | 提交提示词 | 是 | 过滤敏感信息 |
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": ".claude/hooks/validate-bash.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npm run lint",
"timeout": 30
}
]
}
]
}
}
#!/bin/bash
# .claude/hooks/quality-check.sh
# 读取工具输入
INPUT=$(cat)
FILE_PATH=$(echo"$INPUT" | jq -r '.tool_input.file_path')
# 只检查 TypeScript 文件
if [[ "$FILE_PATH" =~ \.(ts|tsx)$ ]]; then
# 运行 ESLint
if ! npx eslint "$FILE_PATH" --fix; then
jq -n '{
hookSpecificOutput: {
hookEventName: "PostToolUse",
decision: "block",
reason: "ESLint found issues that could not be auto-fixed"
}
}'
exit 0
fi
fi
exit 0
配置好 Hooks 后:
常见问题:
问题 | 解决方案 |
|---|---|
Hook 不生效 | 检查 matcher 正则、脚本权限、JSON 语法 |
Hook 超时 | 增加 timeout 参数(默认 60 秒) |
无法阻止操作 | 确认 Hook 事件支持阻止(如 PreToolUse) |

Hooks 完整生命周期:从会话开始到结束
一句话总结:Skills 是可复用的工作流模板,Subagents 是专门的子代理,两者都能在团队间共享。
有些工作流程是重复的:
把这些流程打包成 Skills,可以一键调用,还能在团队间共享。
---
name:code-reviewer
description:Reviewcodeforbestpracticesandpotentialissues
allowed-tools:Read,Grep,Glob
disable-model-invocation:false
user-invocable:true
---
Review the code following these guidelines:
1.Checkforcommonsecurityvulnerabilities
2.Verifyerrorhandlingiscomprehensive
3.Ensurecodefollowsprojectconventions
4.Identifypotentialperformanceissues
Providespecificlinereferencesandsuggestions.
.claude/
└── skills/
├── code-reviewer/
│ └── SKILL.md
└── deploy/
└── SKILL.md
使用 !command 语法插入实时数据:
---
name:pr-summary
description:Summarizepullrequestchanges
---
## PR Context
-Diff:!`ghprdiff`
-Comments:!`ghprview--comments`
-Files:!`ghprdiff--name-only`
SummarizethisPR...
配置好 Skills 后:
Subagents 与 Skills 的区别:
一句话总结:通过精细的权限控制,让 Claude 在安全边界内自主工作,减少确认打断。
默认情况下,Claude 执行敏感操作时需要你确认。这些确认是必要的,但频繁的确认会打断工作流。
通过权限配置,你可以:
npm test)rm -rf /).env){
"permissions": {
"allow": [
"Bash(npm run *)",
"Bash(git *)",
"Read(./src/**)"
],
"deny": [
"Bash(rm -rf *)",
"Read(./.env)",
"Read(./secrets/**)"
],
"additionalDirectories": [
"../shared-config/"
]
}
}
更安全的做法是启用沙箱模式:
{
"sandbox": {
"enabled": true,
"autoAllowBashIfSandboxed": true,
"excludedCommands": ["docker"],
"filesystem": {
"allowWrite": ["//tmp/build", "~/.kube"],
"denyRead": ["~/.aws/credentials"]
},
"network": {
"allowedDomains": ["github.com", "*.npmjs.org"],
"allowUnixSockets": ["/var/run/docker.sock"]
}
}
}
配置好权限后:
安全建议:
--dangerously-skip-permissions~/.claude/CLAUDE.md,写入你的个人偏好CLAUDE.md,说明项目特定配置.claude/settings.json,配置团队共享的权限和 hooks.claude/skills/ 目录,存放团队工作流.mcp.json,配置项目 MCP 服务器allowedMcpServers,限制可用的 MCP 服务器allowManagedHooksOnly,只允许管理配置的 hooks!command 语法插入实时数据.claude/rules/ 按文件类型加载规则场景 | 推荐配置 |
|---|---|
快速上手 | 只配置 CLAUDE.md |
提升效率 | 配置 MCP + Hooks |
团队协作 | 配置 Skills + 共享 settings |
企业部署 | 配置 Managed settings + 沙箱 |

根据你的场景选择合适的配置路径
配置不是一蹴而就的,建议从 CLAUDE.md 开始,逐步添加 MCP、Hooks、Skills,根据实际需求持续优化。
你在配置 Claude Code 时遇到过什么问题?欢迎在评论区分享你的经验和踩过的坑。
好啦,谢谢你观看我的文章,如果喜欢可以点赞转发给需要的朋友,我们下一期再见!敬请期待!