本指南为 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.md 文件

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

1

为您的代码库引导创建 CLAUDE.md

> /init

提示:

  • 包含常用命令(构建、测试、lint)以避免重复搜索
  • 记录代码风格偏好和命名约定
  • 添加特定于您项目的重要架构模式
  • 您可以将 CLAUDE.md 文件添加到运行 Claude 的文件夹、父目录(Claude 自动读取这些文件)或子目录(Claude 按需拉取这些文件)中

将 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

通过管道传输数据

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

设置模型上下文协议 (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 标志与 project(默认)或 global 来指定配置存储位置
  • 使用 -e--env 标志设置环境变量(例如,-e KEY=value
  • MCP 遵循客户端-服务器架构,其中 Claude Code(客户端)可以连接到多个专门的服务器

连接到 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 会话中,您可以询问有关数据库的问题
$ 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 可以帮助您探索数据库结构并运行分析查询
  • 您可以使用此功能快速了解不熟悉项目中的数据库架构
  • 确保您的连接字符串使用具有最小所需权限的适当凭据

下一步

Claude Code 参考实现

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