跳至主要內容

Context7 MCP:在 Claude Code 中存取最新文檔

Context7 MCP 讓 Claude Code 即時查詢函式庫與框架的最新官方文檔,不再依賴過時的訓練資料。涵蓋 npx ctx7 setup 安裝、resolve-library-id 與 query-docs 工具、OAuth 設定、CLI + Skills 替代方案與疑難排解。

AI 編程助手最大的挑戰之一是知識截止日期——Claude 的訓練資料有特定的結束日期,這意味著它可能不知道最近的 API 變更、新功能或已棄用的方法。

Context7 MCP 透過讓 Claude 即時存取任何函式庫或框架的官方文檔來解決這個問題。

2026 年 10 月 21 日更新: 安裝方式已改為 npx ctx7 setup --claude 或 Context7 外掛,託管端點為 https://mcp.context7.com/mcp,MCP 工具為 resolve-library-id 與 query-docs。以下設定範例均已對照 Context7 官方文件與 Claude Code MCP 參考文件核實。完整的工具與價格說明請見姊妹篇 Context7 MCP:讓 AI 編碼助手永遠使用最新文檔。

問題:過時的知識

User: "How do I use the new Next.js App Router?"

Claude (without Context7):
→ May reference older Pages Router patterns
→ Missing new server components syntax
→ Outdated API examples
→ Deprecated patterns still suggested
User: "How do I use the new Next.js App Router?"

Claude (with Context7):
→ Fetches current Next.js documentation
→ Uses latest App Router patterns
→ Correct server components syntax
→ Up-to-date best practices

什麼是 Context7?

Context7 是一個 MCP(Model Context Protocol)伺服器,它可以:

  • 取得最新文檔,來源由 Context7 索引並持續更新
  • 把函式庫名稱解析成穩定的 ID,確保答案來自正確的專案與版本
  • 涵蓋持續更新的大量公開函式庫 — 可用 npx ctx7 library <name> "<你想做的事>" 搜尋
  • 支援私有 repo(Pro 與 Enterprise 方案)
┌─────────────────────────────────────────────────────────────┐
│                    Context7 Architecture                     │
│                                                             │
│  ┌───────────┐     ┌───────────┐     ┌───────────┐        │
│  │  Claude   │ ──► │ Context7  │ ──► │  Official │        │
│  │   Code    │     │    MCP    │     │   Docs    │        │
│  └───────────┘     └───────────┘     └───────────┘        │
│       │                 │                  │               │
│       │                 ▼                  │               │
│       │           ┌───────────┐            │               │
│       │           │   Index   │◄───────────┘               │
│       │           └───────────┘                            │
│       │                 │                                  │
│       │                 ▼                                  │
│       └──────────► Enriched Response                       │
│                                                             │
└─────────────────────────────────────────────────────────────┘

設定 Context7

步驟 1:新增 Context7

最快的方式是使用 Context7 自家的 CLI,需要 Node.js 22.13 以上版本:

npx ctx7 setup --claude

它會以 OAuth device flow 登入、產生 API Key,並安裝對應的 skill。它提供 MCP server 模式,以及完全不需要 MCP 服務器的 CLI + Skills 模式。

若要手動新增 MCP 服務器,Context7 官方的 Claude Code 說明提供以下兩種形式:

# 遠端服務器(託管,推薦)
claude mcp add --scope user \
  --header "Authorization: Bearer YOUR_API_KEY" \
  --transport http context7 https://mcp.context7.com/mcp

# 本機 stdio 服務器
claude mcp add --scope user context7 \
  -- npx -y @upstash/context7-mcp --api-key YOUR_API_KEY

把 --scope user 換成 --scope project,即可將服務器寫入 .mcp.json 並透過版控與團隊共享。(預設的 local 與 user 都存放在 ~/.claude.json;沒有 global 這個 scope。)

步驟 2:驗證安裝

claude mcp get context7
claude mcp list

claude mcp list 會在每個服務器旁顯示健康狀態 — ✔ Connected、! Needs authentication,或附帶錯誤細節的 ✘ Failed to connect。來自 .mcp.json 的專案層級服務器在互動式工作階段核准前,會停留在 ⏸ Pending approval。

步驟 3:測試整合

"How do I set up a Next.js 16 proxy? use context7"

Claude 會解析函式庫、取得當前文檔,並依據文檔作答。

使用 Context7

自動文檔獲取

ctx7 setup 會安裝一個 skill,遇到套件問題時自動觸發,不需要你特別說明。若要手動設定相同行為,在 CLAUDE.md 中加入規則:

Always use Context7 when I need library/API documentation, code generation,
setup or configuration steps without me having to explicitly ask.

接著:

User: "How do I set up Prisma with PostgreSQL?"

Claude:
→ resolve-library-id(libraryName: "Prisma", query: "set up with PostgreSQL")
→ query-docs(libraryId: "/prisma/prisma", query: "set up with PostgreSQL")
→ 依據剛取得的文檔作答

明確請求文檔

在提示詞中加上 use context7 即可明確請求文檔:

"use context7 for the React docs on server components,
then explain how to use them with suspense."

如果你已經知道 Library ID,直接指明就能跳過解析步驟:

"use context7 with /vercel/next.js for app router setup"

特定函式庫查詢

Context7 理解函式庫的上下文:

"Using the latest Tailwind CSS docs, how do I set up
the new dark mode with class-based switching?"

支援的函式庫

Context7 索引了持續更新的大量公開函式庫。每一筆都附有 Library ID、已索引的程式碼片段數量、來源信譽評級、基準分數,以及可用的版本 ID。可用 npx ctx7 library <name> "<你想做的事>" 搜尋即時索引,或在 context7.com 瀏覽。常見的項目包括:

前端

React、Next.js、Vue.js、Svelte、Angular、Tailwind CSS

後端

Node.js、Express、Fastify、NestJS、Django、FastAPI

資料庫

Prisma、Drizzle、TypeORM、Mongoose、PostgreSQL、MongoDB

DevOps

Docker、Kubernetes、GitHub Actions、Vercel、AWS

Library ID 採 /owner/project 形式,Context7 官方文件使用的範例包括 /vercel/next.js、/prisma/prisma、/supabase/supabase 與 /mongodb/docs。含版本的 ID 會再加上 release:/vercel/next.js/v16.2.9。請勿自行猜測 ID — 執行 resolve-library-id(或 npx ctx7 library)並使用它回傳的結果,名稱要用官方標點寫法(是 Next.js,不是 nextjs)。

各函式庫的覆蓋程度不同,而且會隨索引更新而變動,因此請參考解析結果中的片段數量與來源信譽,不要預設某個函式庫一定完整。

使用案例

1. 學習新框架

"I'm new to SvelteKit. Using current documentation,
explain the folder structure and routing conventions."

Claude 獲取 SvelteKit 文檔並以準確、最新的資訊進行解釋。

2. 遷移協助

"We're migrating from Create React App to Vite.
What are the current steps and configuration needed?"

Claude 參考當前的 Vite 遷移指南。

3. API 整合

"How do I implement Stripe Checkout with the latest
API version? Include webhook handling."

Claude 獲取當前的 Stripe 文檔,包含最新的 API 模式。

4. 疑難排解

"I'm getting 'hydration mismatch' in Next.js 16.
What are the current best practices to fix this?"

Claude 參考當前的 Next.js 疑難排解文檔。

Context7 vs 訓練資料

方面訓練資料Context7
時效性截止日期即時
準確性可能過時最新
完整性有限完整文檔
API 版本歷史版本最新版
棄用狀態可能建議舊的知道已棄用

Context7 最有幫助的情況

高價值:

  • 快速演進的框架(Next.js、React 等)
  • 經常有破壞性變更的函式庫
  • 訓練截止後發布的新功能
  • 識別已棄用的 API

較低價值:

  • 穩定的語言(C、SQL 基礎)
  • 基本的程式設計概念
  • 已建立的模式

配置選項

Context7 MCP 服務器並沒有客戶端快取、自訂來源或速率限制的設定區塊。你實際能設定的是 MCP 服務器項目、認證方式,以及要使用哪一種模式。

MCP 服務器項目

.mcp.json(專案層級)或 ~/.claude.json(local 與 user 層級)中的遠端項目長這樣:

{
  "mcpServers": {
    "context7": {
      "type": "http",
      "url": "https://mcp.context7.com/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_API_KEY"
      }
    }
  }
}

遠端項目必須明確指定 type。只有 url 卻沒有 type 的項目,Claude Code 會視為 stdio 服務器並跳過,同時回報錯誤。

用 OAuth 取代 API Key

對於實作 MCP OAuth 規範的客戶端,把端點換掉即可:

- "url": "https://mcp.context7.com/mcp"
+ "url": "https://mcp.context7.com/mcp/oauth"

OAuth 僅適用於遠端 HTTP 連線;本機 stdio 連線使用 --api-key 認證。在 Claude Code 中執行 claude mcp login context7 完成流程(SSH 環境請加上 --no-browser)。

私有文檔

私有 repo 是 Context7 的帳號功能,不是設定檔選項:在 Pro 或 Enterprise 方案的 dashboard 中加入,解析費用按 token 計價。若要完全自管,ctx7 setup --base-url https://context7.internal.example 可指向地端部署。

用 CLI + Skills 取代 MCP

ctx7 setup --cli --claude 會在 ~/.claude/skills 安裝一個 docs skill,直接驅動 ctx7 library 與 ctx7 docs 指令,完全不註冊 MCP 服務器。兩種模式都可加上 --project 限定在目前專案。

與工作流程整合

開發工作流程

"Implement user authentication using the latest
Auth.js (NextAuth) patterns for Next.js 16."

Claude:
→ Fetches current Auth.js documentation
→ Identifies App Router patterns
→ Implements with current best practices
→ Includes proper TypeScript types

程式碼審查

"Review this Prisma schema. Check against current
best practices from the official documentation."

Claude:
→ Fetches Prisma schema documentation
→ Compares against current recommendations
→ Identifies improvements
→ Suggests updates if patterns changed

除錯

"I'm getting a React 19 error about refs in
function components. What's the current approach?"

Claude:
→ Fetches React 19 refs documentation
→ Explains current ref patterns
→ Provides migration steps if needed

與其他 MCP 結合

Context7 + Memory MCP

"Using current React documentation, implement our
component following patterns we saved in memory."

Claude:
→ Fetches current React docs
→ Retrieves saved patterns from memory
→ Combines both for implementation

Context7 + GitHub MCP

"Create a PR implementing the new Prisma relations
feature following current documentation."

Claude:
→ Fetches current Prisma docs
→ Implements feature
→ Creates PR with proper description

Context 注意事項

Token 使用量

Context7 的回應會增加 context:

Without Context7:
└── Claude's knowledge only

With Context7:
├── Claude's knowledge
└── + Fetched documentation (varies by query)

建議: 針對特定查詢使用 Context7,而非每個提示都使用。

配額意識

每次 resolve-library-id 與 query-docs 都會從方案的每月額度扣掉一次 API 呼叫。Free 方案每月 1,000 次;Pro 每席內含 2,000 次,超出後每 1,000 次 $5。因此「先解析再查詢」會用掉兩次:

"use context7 for Prisma relations"
→ resolve-library-id  (1 次)
→ query-docs          (1 次)

"use context7 with /prisma/prisma for relations"
→ query-docs          (1 次)← 直接指定 ID 可省下解析步驟

用量可在 Context7 dashboard 查看。

疑難排解

文檔未獲取

  1. 確認服務器已連線:claude mcp get context7 或 claude mcp list
  2. 若狀態是 ! Needs authentication,執行 claude mcp login context7
  3. 若狀態是 ⏸ Pending approval,在互動式工作階段中核准該 .mcp.json 服務器
  4. 到 dashboard 確認是否已達每月 API 呼叫上限
  5. 嘗試明確請求:「use context7 for X」

返回過時資訊

  1. 改用含版本的 Library ID(/vercel/next.js/v16.2.9)而非裸 ID
  2. 確認解析到正確的專案 — 多個套件可能同名
  3. 透過 Context7 疑難排解指南 回報過時或解析錯誤的函式庫

缺少函式庫

  1. 自己搜尋索引:npx ctx7 library <name> "<你想做的事>"
  2. 使用套件的官方標點寫法 — 是 Next.js,不是 nextjs
  3. 公開套件可透過 Adding Libraries 提交;內部文檔則在 Pro 或 Enterprise 方案中加入私有 repo

最佳實踐

1. 明確指定版本

Good:
"Using Next.js 16 App Router documentation..."

Avoid:
"Using Next.js documentation..."

2. 指定你需要的內容

Good:
"From the Prisma docs, explain the new relation mode options"

Avoid:
"Tell me about Prisma"

3. 結合上下文

"Given our PostgreSQL setup, use current Prisma docs
to implement the user-posts relationship."

4. 驗證時效性

"Confirm this pattern is current according to React docs,
then implement it."

開始使用

今天:

  1. 執行 npx ctx7 setup --claude,或用外掛安裝:/plugin marketplace add upstash/context7 接著 /plugin install context7@context7-marketplace
  2. 用一個函式庫查詢測試
  3. 比較有無 Context7 的輸出差異

本週:

  1. 識別經常使用的函式庫,並記下它們的 Library ID
  2. 把 Context7 規則加進 CLAUDE.md,讓它自動觸發
  3. 整合到開發工作流程

本月:

  1. 建立團隊的 Context7 使用模式
  2. 若整個團隊都需要,透過專案層級的 .mcp.json 共享服務器
  3. 在 dashboard 觀察 API 呼叫用量,據此選擇適合的方案

Context7 消除了 AI 訓練與當前文檔之間的知識差距。停止猜測 API 變更,自信地工作,因為 Claude 擁有最新資訊。

資料來源:Context7 的 Claude Code 文件、MCP Clients、Context7 on GitHub、Claude Code MCP 文件、MCP Protocol