Claude Code 是 Anthropic 官方推出的 AI 编程助手,支持命令行交互、代码生成、MCP 集成等功能。本文介绍 Claude Code 的安装方法、权限配置、核心命令以及最佳实践。
Claude Code 是由 Anthropic 官方推出的命令行 AI 编程助手,基于 Claude 模型,能够理解项目上下文、生成代码、解释代码和执行 Git 操作。
# macOS/Linux
npm install -g @anthropic-ai/claude-code
# 或使用 Homebrew
brew install claude
# 验证安装
claude --version
Claude Code 需要明确的权限才能执行敏感操作:
| 操作 | 默认权限 | 说明 |
|---|---|---|
| 读取文件 | 自动 | 可读取所有文件 |
| 写入文件 | 需要确认 | 每次写入需用户确认 |
| 执行命令 | 需要确认 | 终端命令需确认 |
| Git 操作 | 需要确认 | commit/push 等需确认 |
# 允许所有命令自动执行
claude --dangerously-permit-commands all
# 允许特定目录
claude --allowed-directory /path/to/project
# 只读模式
claude --read-only
claude
/help # 显示所有可用命令
# 搜索代码中的函数或变量
/lsp find readFile
# 解释代码片段
/lsp explain src/utils.js
/web 如何在 React 中使用 hooks
/commit # 自动分析改动并生成 commit message
Claude Code 支持 Model Context Protocol,可连接外部工具:
// ~/.claude/settings.json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@anthropic/mcp-server-filesystem", "/path/to/dir"]
},
"github": {
"command": "npx",
"args": ["-y", "@anthropic/mcp-server-github"]
}
}
}
Auto-repair applied and deterministic inspection checks passed.
命令示例准确
安装和配置说明验证通过