本指南提供了 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 解釋測試策略

建立拉取請求

生成全面的拉取請求

使用時機: 您需要為您的更改建立一個文件完善的拉取請求。

1

總結您的更改

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

使用 Claude 生成拉取請求

> 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 為您建立拉取請求
  • 在提交前審查 Claude 生成的拉取請求
  • 請 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?