本指南为 Claude Code 的常见工作流程提供了分步教程。每个教程都包含清晰的说明、示例命令和最佳实践,帮助您充分利用 Claude Code。

目录

理解新代码库

快速了解代码库概况

使用时机: 您刚加入一个新项目,需要快速了解其结构。

1

导航到项目根目录

cd /path/to/project
2

启动 Claude Code

claude
3

请求高层概述

> give me an overview of this codebase
4

深入了解具体组件

> explain the main architecture patterns used here
> what are the key data models?
> how is authentication handled?

提示:

  • 从宏观问题开始,然后逐步深入具体领域
  • 询问项目中使用的编码约定和模式
  • 请求项目特定术语的词汇表

查找相关代码

使用时机: 您需要定位与特定功能相关的代码。

1

让 Claude 查找相关文件

> find the files that handle user authentication
2

了解组件之间的交互

> how do these authentication files work together?
3

理解执行流程

> trace the login process from front-end to database

提示:

  • 明确说明您要查找的内容
  • 使用项目中的领域语言

高效修复错误

诊断错误消息

使用时机: 您遇到错误消息,需要找到并修复其源头。

1

与 Claude 分享错误

> I'm seeing an error when I run npm test
2

请求修复建议

> suggest a few ways to fix the @ts-ignore in user.ts
3

应用修复

> update user.ts to add the null check you suggested

提示:

  • 告诉 Claude 重现问题的命令并获取堆栈跟踪
  • 提及重现错误的步骤
  • 让 Claude 知道错误是间歇性的还是持续性的

重构代码

现代化遗留代码

使用时机: 您需要更新旧代码以使用现代模式和实践。

1

识别需要重构的遗留代码

> find deprecated API usage in our codebase
2

获取重构建议

> suggest how to refactor utils.js to use modern JavaScript features
3

安全地应用更改

> refactor utils.js to use ES2024 features while maintaining the same behavior
4

验证重构

> run tests for the refactored code

提示:

  • 请 Claude 解释现代方法的好处
  • 在需要时请求保持向后兼容性
  • 以小的、可测试的增量进行重构

处理测试

添加测试覆盖

使用时机: 您需要为未覆盖的代码添加测试。

1

识别未测试的代码

> find functions in NotificationsService.swift that are not covered by tests
2

生成测试框架

> add tests for the notification service
3

添加有意义的测试用例

> add test cases for edge conditions in the notification service
4

运行和验证测试

> run the new tests and fix any failures

提示:

  • 请求覆盖边缘情况和错误条件的测试
  • 在适当时请求单元测试和集成测试
  • 让 Claude 解释测试策略

创建拉取请求

生成全面的 PR

使用时机: 您需要为您的更改创建一个文档完善的拉取请求。

1

总结您的更改

> summarize the changes I've made to the authentication module
2

使用 Claude 生成 PR

> create a pr
3

审查和完善

> enhance the PR description with more context about the security improvements
4

添加测试详情

> add information about how these changes were tested

提示:

  • 直接要求 Claude 为您创建 PR
  • 在提交前审查 Claude 生成的 PR
  • 请 Claude 突出潜在风险或注意事项

处理文档

生成代码文档

使用时机: 您需要为代码添加或更新文档。

1

识别未文档化的代码

> find functions without proper JSDoc comments in the auth module
2

生成文档

> add JSDoc comments to the undocumented functions in auth.js
3

审查和增强

> improve the generated documentation with more context and examples
4

验证文档

> check if the documentation follows our project standards

提示:

  • 指定您想要的文档风格(JSDoc、docstrings 等)
  • 在文档中请求示例
  • 为公共 API、接口和复杂逻辑请求文档

处理图像

分析图像和截图

使用时机: 您需要处理代码库中的图像或获取 Claude 的帮助分析图像内容。

1

将图像添加到对话中

您可以使用以下任何方法:

# 1. 将图像拖放到 Claude Code 窗口中
# 2. 复制图像并使用 ctrl+v 将其粘贴到 CLI 中
# 3. 提供图像路径
claude "Analyze this image: /path/to/your/image.png"
2

请 Claude 分析图像

> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?
3

使用图像作为上下文

> Here's a screenshot of the error. What's causing it?
> This is our current database schema. How should we modify it for the new feature?
4

从视觉内容获取代码建议

> Generate CSS to match this design mockup
> What HTML structure would recreate this component?

提示:

  • 当文字描述不清晰或繁琐时使用图像
  • 包含错误、UI 设计或图表的截图以提供更好的上下文
  • 您可以在一次对话中处理多个图像
  • 图像分析适用于图表、截图、模型等

使用延伸思考

利用 Claude 的延伸思考处理复杂任务

使用时机: 当处理复杂的架构决策、具有挑战性的错误或需要深入推理的多步骤实现计划时。

1

提供上下文并请求 Claude 思考

> I need to implement a new authentication system using OAuth2 for our API. Think deeply about the best approach for implementing this in our codebase.

Claude 将从您的代码库收集相关信息并使用延伸思考,这将在界面中可见。

2

通过后续提示完善思考

> think about potential security vulnerabilities in this approach
> think harder about edge cases we should handle

如何最大限度地利用延伸思考的提示:

延伸思考对以下复杂任务最有价值:

  • 规划复杂的架构更改
  • 调试复杂问题
  • 创建新功能的实现计划
  • 理解复杂的代码库
  • 评估不同方法之间的权衡

提示思考的方式会产生不同程度的思考深度:

  • “think” 触发基本的延伸思考
  • 加强短语如 “think more”、“think a lot”、“think harder” 或 “think longer” 触发更深入的思考

有关更多延伸思考提示技巧,请参见延伸思考技巧

Claude 将在响应上方以斜体灰色文本显示其思考过程。


设置项目记忆

创建有效的 CLAUDE.md 文件

使用时机: 您想要设置 CLAUDE.md 文件来存储重要的项目信息、约定和常用命令。

1

为您的代码库引导 CLAUDE.md

> /init

提示:

  • 包含常用命令(构建、测试、lint)以避免重复搜索
  • 记录代码风格偏好和命名约定
  • 添加特定于您项目的重要架构模式
  • 您可以将 CLAUDE.md 文件添加到以下任何位置:
    • 运行 Claude 的文件夹:自动添加到您在该文件夹中启动的对话
    • 子目录:Claude 按需拉取这些文件
    • ~/.claude/CLAUDE.md:您不想检入源代码控制的用户特定偏好

设置模型上下文协议 (MCP)

模型上下文协议 (MCP) 是一个开放协议,使 LLM 能够访问外部工具和数据源。有关更多详细信息,请参见 MCP 文档

使用第三方 MCP 服务器需要您自担风险。确保您信任 MCP 服务器,对于与互联网通信的 MCP 服务器要特别小心,因为这些服务器可能使您面临提示注入风险。

配置 MCP 服务器

使用时机: 您想要通过使用模型上下文协议将 Claude 连接到专门的工具和外部服务器来增强其功能。

1

添加 MCP Stdio 服务器

# 基本语法
claude mcp add <name> <command> [args...]

# 示例:添加本地服务器
claude mcp add my-server -e API_KEY=123 -- /path/to/server arg1 arg2
2

管理您的 MCP 服务器

# 列出所有配置的服务器
claude mcp list

# 获取特定服务器的详细信息
claude mcp get my-server

# 移除服务器
claude mcp remove my-server

提示:

  • 使用 -s--scope 标志指定配置存储位置:
    • local(默认):仅在当前项目中对您可用(在旧版本中称为 project
    • project:通过 .mcp.json 文件与项目中的所有人共享
    • user:在所有项目中对您可用(在旧版本中称为 global
  • 使用 -e--env 标志设置环境变量(例如,-e KEY=value
  • 使用 MCP_TIMEOUT 环境变量配置 MCP 服务器启动超时(例如,MCP_TIMEOUT=10000 claude 设置 10 秒超时)
  • 随时使用 Claude Code 中的 /mcp 命令检查 MCP 服务器状态
  • MCP 遵循客户端-服务器架构,其中 Claude Code(客户端)可以连接到多个专门的服务器

理解 MCP 服务器作用域

使用时机: 您想要了解不同的 MCP 作用域如何工作以及如何与团队共享服务器。

1

本地作用域 MCP 服务器

默认作用域(local)将 MCP 服务器配置存储在您的项目特定用户设置中。这些服务器仅在您在当前项目工作时对您可用。

# 添加本地作用域服务器(默认)
claude mcp add my-private-server /path/to/server

# 显式指定本地作用域
claude mcp add my-private-server -s local /path/to/server
2

项目作用域 MCP 服务器(.mcp.json)

项目作用域服务器存储在项目根目录的 .mcp.json 文件中。此文件应检入版本控制以与团队共享服务器。

# 添加项目作用域服务器
claude mcp add shared-server -s project /path/to/server

这将创建或更新具有以下结构的 .mcp.json 文件:

{
  "mcpServers": {
    "shared-server": {
      "command": "/path/to/server",
      "args": [],
      "env": {}
    }
  }
}
3

用户作用域 MCP 服务器

用户作用域服务器在您机器上的所有项目中对您可用,并且对您是私有的。

# 添加用户服务器
claude mcp add my-user-server -s user /path/to/server

提示:

  • 本地作用域服务器优先于同名的项目作用域和用户作用域服务器
  • 项目作用域服务器(在 .mcp.json 中)优先于同名的用户作用域服务器
  • 在使用 .mcp.json 中的项目作用域服务器之前,Claude Code 会提示您出于安全考虑进行批准
  • .mcp.json 文件旨在检入版本控制以与团队共享 MCP 服务器
  • 项目作用域服务器使团队中的每个人都能轻松访问相同的 MCP 工具
  • 如果您需要重置对启用或禁用哪些项目作用域服务器的选择,请使用 claude mcp reset-project-choices 命令

连接到 Postgres MCP 服务器

使用时机: 您想要给 Claude 只读访问 PostgreSQL 数据库的权限,用于查询和架构检查。

1

添加 Postgres MCP 服务器

claude mcp add postgres-server /path/to/postgres-mcp-server --connection-string "postgresql://user:pass@localhost:5432/mydb"
2

使用 Claude 查询您的数据库

# 在您的 Claude 会话中,您可以询问有关数据库的问题

> describe the schema of our users table
> what are the most recent orders in the system?
> show me the relationship between customers and invoices

提示:

  • Postgres MCP 服务器出于安全考虑提供只读访问
  • Claude 可以帮助您探索数据库结构并运行分析查询
  • 您可以使用此功能快速了解不熟悉项目中的数据库架构
  • 确保您的连接字符串使用具有最低所需权限的适当凭据

从 JSON 配置添加 MCP 服务器

使用时机: 您有一个单个 MCP 服务器的 JSON 配置,想要将其添加到 Claude Code。

1

从 JSON 添加 MCP 服务器

# 基本语法
claude mcp add-json <name> '<json>'

# 示例:使用 JSON 配置添加 stdio 服务器
claude mcp add-json weather-api '{"type":"stdio","command":"/path/to/weather-cli","args":["--api-key","abc123"],"env":{"CACHE_DIR":"/tmp"}}'
2

验证服务器已添加

claude mcp get weather-api

提示:

  • 确保 JSON 在您的 shell 中正确转义
  • JSON 必须符合 MCP 服务器配置架构
  • 您可以使用 -s global 将服务器添加到您的全局配置而不是项目特定配置中

从 Claude Desktop 导入 MCP 服务器

使用时机: 您已经在 Claude Desktop 中配置了 MCP 服务器,想要在 Claude Code 中使用相同的服务器而无需手动重新配置。

1

从 Claude Desktop 导入服务器

# 基本语法
claude mcp add-from-claude-desktop
2

选择要导入的服务器

运行命令后,您将看到一个交互式对话框,允许您选择要导入的服务器。

3

验证服务器已导入

claude mcp list

提示:

  • 此功能仅适用于 macOS 和 Windows Subsystem for Linux (WSL)
  • 它从这些平台上的标准位置读取 Claude Desktop 配置文件
  • 使用 -s global 标志将服务器添加到您的全局配置
  • 导入的服务器将与 Claude Desktop 中的名称相同
  • 如果已存在同名服务器,它们将获得数字后缀(例如,server_1

将 Claude Code 用作 MCP 服务器

使用时机: 您想要将 Claude Code 本身用作 MCP 服务器,使其他应用程序可以连接到它,提供 Claude 的工具和功能。

1

将 Claude 作为 MCP 服务器启动

# 基本语法
claude mcp serve
2

从其他应用程序连接

您可以从任何 MCP 客户端(如 Claude Desktop)连接到 Claude Code MCP 服务器。如果您使用的是 Claude Desktop,可以使用此配置添加 Claude Code MCP 服务器:

{
  "command": "claude",
  "args": ["mcp", "serve"],
  "env": {}
}

提示:

  • 服务器提供对 Claude 的工具(如 View、Edit、LS 等)的访问
  • 在 Claude Desktop 中,尝试让 Claude 读取目录中的文件、进行编辑等
  • 请注意,此 MCP 服务器仅是将 Claude Code 的工具暴露给您的 MCP 客户端,因此您自己的客户端负责实现单个工具调用的用户确认

将 Claude 用作类 Unix 工具

将 Claude 添加到您的验证过程

使用时机: 您想要将 Claude Code 用作 linter 或代码审查器。

步骤:

1

将 Claude 添加到您的构建脚本

// package.json
{
    ...
    "scripts": {
        ...
        "lint:claude": "claude -p 'you are a linter. please look at the changes vs. main and report any issues related to typos. report the filename and line number on one line, and a description of the issue on the second line. do not return any other text.'"
    }
}

管道输入输出

使用时机: 您想要将数据通过管道输入到 Claude,并以结构化格式获取返回数据。

1

通过管道传输数据到 Claude

cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

创建自定义斜杠命令

Claude Code 支持您可以创建的自定义斜杠命令,以快速执行特定提示或任务。

创建项目特定命令

使用时机: 您想要为您的项目创建所有团队成员都可以使用的可重用斜杠命令。

1

在项目中创建命令目录

mkdir -p .claude/commands
2

为每个命令创建 Markdown 文件

echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md
3

在 Claude Code 中使用您的自定义命令

claude
> /project:optimize

提示:

  • 命令名称源自文件名(例如,optimize.md 变成 /project:optimize
  • 您可以在子目录中组织命令(例如,.claude/commands/frontend/component.md 变成 /project:frontend:component
  • 项目命令对克隆仓库的每个人都可用
  • Markdown 文件内容成为调用命令时发送给 Claude 的提示

使用 $ARGUMENTS 添加命令参数

使用时机: 您想要创建可以接受用户额外输入的灵活斜杠命令。

1

创建带有 $ARGUMENTS 占位符的命令文件

echo "Find and fix issue #$ARGUMENTS. Follow these steps:
1. Understand the issue described in the ticket
2. Locate the relevant code in our codebase
3. Implement a solution that addresses the root cause
4. Add appropriate tests
5. Prepare a concise PR description" > .claude/commands/fix-issue.md
2

使用带有问题编号的命令

claude
> /project:fix-issue 123

这将在提示中将 $ARGUMENTS 替换为 “123”。

提示:

  • $ARGUMENTS 占位符会被命令后面的任何文本替换
  • 您可以在命令模板中的任何位置放置 $ARGUMENTS
  • 其他有用的应用:为特定函数生成测试用例、为组件创建文档、审查特定文件中的代码或将内容翻译成指定语言

创建个人斜杠命令

使用时机: 您想要创建在所有项目中都可以使用的个人斜杠命令。

1

在您的主文件夹中创建命令目录

mkdir -p ~/.claude/commands
2

为每个命令创建 Markdown 文件

echo "Review this code for security vulnerabilities, focusing on:" > ~/.claude/commands/security-review.md
3

使用您的个人自定义命令

claude
> /user:security-review

提示:

  • 个人命令以 /user: 而不是 /project: 为前缀
  • 个人命令仅对您可用,不与团队共享
  • 个人命令在您的所有项目中都可以使用
  • 您可以将这些用于跨不同代码库的一致工作流程

下一步

Claude Code 参考实现

克隆我们的开发容器参考实现。

Was this page helpful?