让 Claude 不再使用过时的库文档(Context7)

添加 Context7,让 Claude 在编写代码之前先查阅 Next.js 或 Supabase 等库的最新、对应版本的文档,而不是依赖它的记忆。

  • 难度 入门
  • 用时 10-15 分钟
  • 适用于
    • Claude Code
    • Claude Desktop
    • Claude.ai
本页内容7 个部分

完成这份指南后,当你让 Claude “用 Supabase 加上登录功能”或“设置一个 Next.js 代理”时,它会先获取该库的最新文档,再根据文档编写代码。这解决了氛围编程(vibe coding)中常见的头疼问题:Claude 信心十足地使用某个函数,而这个函数在它的训练数据收集之后已被改名或移除。

Context7 是 Upstash 的文档服务。它的 README 说,它为编程代理提供从源头获取的“up-to-date, version-specific documentation and code examples”。你可以把它连接为 MCP 服务器(给 Claude 提供新工具的插件),也可以连接为命令行工具加一个技能(Claude 在相关时加载的指令包)。

最终你会得到什么

  • 连接到 Claude Code(或 Claude Desktop / claude.ai)的 Context7。
  • 一个免费的 API 密钥,用于获得更高的速率限制。
  • 项目中的一条长期规则,让 Claude 无需你每次提醒就使用 Context7。

你需要准备什么

  • Claude Code、Claude Desktop或 claude.ai。
  • Node.js。本地 MCP 服务器通过 npx 运行。较新的 ctx7 设置命令需要 Node.js 22.13 或更新版本;用 node --version 检查。
  • 一个 Context7 API 密钥(可选,但推荐)。README 说可以在 context7.com/dashboard 免费获取一个,以获得“for higher rate limits”。

分步操作

方案 A:自动设置(Claude Code)

Context7 的 README 推荐这种方式:

npx ctx7 setup --claude

它会通过浏览器(OAuth)让你登录,创建一个 API 密钥,并为 Claude 安装一个 Context7 技能。--claude 标志专门针对 Claude Code。以后想撤销:npx ctx7 remove。

这种方式使用的是 ctx7 命令行工具和一个技能,而不是 MCP 服务器。两者都能让 Claude 获得最新文档;请选择其中一种方式,不要两种都用。

方案 B:Claude Code 中的 MCP 服务器

如果你更喜欢 MCP,或者方案 A 在你的机器上行不通,就用这个。把 YOUR_API_KEY 替换为你在控制台中获得的密钥。

远程服务器(无需安装任何东西):

claude mcp add --scope user --header "Authorization: Bearer YOUR_API_KEY" --transport http context7 https://mcp.context7.com/mcp

或者本地服务器(通过 npx 在你的电脑上运行):

claude mcp add --scope user context7 -- npx -y @upstash/context7-mcp --api-key YOUR_API_KEY

--scope user 让 Context7 在每个项目中可用。启动 claude 并输入 /mcp;context7 应该显示为已连接。

方案 C:Claude Desktop或 claude.ai

远程,作为自定义连接器。Context7 的客户端指南说:前往 Settings > Connectors > Add Custom Connector,命名为 Context7,并输入 https://mcp.context7.com/mcp。在当前的 Claude 应用中,它位于 Customize > Connectors > Add custom connector。Context7 的指南没有为这种方式提供 API 密钥步骤,所以请预期不带密钥时较低的速率限制。在 Free 的 Claude 方案上,你只能添加一个自定义连接器,所以这会占用你的名额。

本地,在 Claude Desktop中。从系统菜单栏的 Claude 菜单中选择 Settings…,然后选择 Developer > Edit Config,并添加:

{
  "mcpServers": {
    "context7": {
      "command": "npx",
      "args": ["-y", "@upstash/context7-mcp", "--api-key", "YOUR_API_KEY"]
    }
  }
}

保存,完全退出并重新打开 Claude Desktop。

用项目规则让它自动生效

README 建议添加一条规则,这样无需你说明,Context7 就会用于库相关的问题。在 Claude Code 中,项目指令存放在项目顶层名为 CLAUDE.md 的文件中(或放在 .claude/CLAUDE.md 中)。如果你还没有,运行 /init,Claude Code 会为你起草一个。然后添加这样一行:

When writing or changing code that uses a third-party library, framework or API, look up the current docs with Context7 first. Match the version in package.json.

最后一句话很重要。当你提到某个版本时,Context7 会尝试匹配该版本,而你的 package.json(或 requirements.txt)正是真实版本所在的位置。

Claude 在幕后做了什么

使用 MCP 方式时,Context7 给 Claude 两个工具:

工具输入作用
resolve-library-idlibraryName、query找到正确的 Context7 库 ID,例如 /vercel/next.js
query-docslibraryId、query(可选 language)返回该库的文档和代码示例

使用 CLI 方式时,对应的是 ctx7 library <name> <query> 和 ctx7 docs <libraryId> <query>。

让它真正有用:可以试试的提示词

How do I set up a Next.js 16 proxy for /api routes? use context7

README 的触发短语字面上就是 “use context7”。把它加在提示词末尾,是确保执行查阅的最简单办法。

Check package.json for the versions we use, then use library /supabase/supabase to look up the current way to add email magic-link login. Implement it in src/lib/auth.ts and tell me which doc pages you relied on.

以斜杠开头(/supabase/supabase)会指明确切的库,并跳过匹配步骤。问 Claude 它依据了哪些页面,能给你留下可以核对的依据。

This error appeared after I upgraded Tailwind: <paste error>. Use Context7 to find the migration notes for the version in package.json, explain what changed in plain English, and fix only the files that need it.

故障排查

  • Claude 忽略了 Context7。在提示词中加上 “use context7”,并添加上面的 CLAUDE.md 规则。在 Claude Desktop或 claude.ai 中,检查该聊天里 + > Connectors 下连接器的开关是否已打开。
  • npx ctx7 setup 失败。检查 node --version 是否为 22.13 或更高,或者改用方案 B。
  • 远程服务器报 “Unauthorized”。请求头必须严格写成 Authorization: Bearer YOUR_API_KEY,其中有 Bearer 这个词,密钥前有一个空格。
  • 库或版本不对。使用斜杠 ID(例如 /vercel/next.js),并在提示词中说明版本。
  • 被限速。添加 API 密钥。不带密钥的使用限制较低。

注意事项

  • Context7 的 README 说它的项目是社区贡献的,并且它“cannot guarantee the accuracy, completeness, or security of all library documentation”。请把返回的内容当作参考资料,而不是指令。发现可疑内容,请使用该库 Context7 页面上的 Report 按钮举报。
  • Claude Code 的文档警告说,抓取外部内容的服务器可能让你面临提示词注入。接受更改之前请继续审阅,尤其是 Claude 想要运行的命令。
  • 你的 API 密钥就是密码。不要把它粘贴到共享的 .mcp.json 文件中,也不要把它提交到仓库。

下一步

来源

已于 对照以下来源核对信息。 页面更新于 。 我们如何核对信息

  1. github.com/upstash/context7(在新标签页中打开)
  2. context7.com/docs/resources/all-clients(在新标签页中打开)
  3. code.claude.com/docs/en/mcp(在新标签页中打开)
  4. claude.com/docs/connectors/custom/add-unlisted(在新标签页中打开)
  5. modelcontextprotocol.io/docs/develop/connect-local-servers(在新标签页中打开)
  6. code.claude.com/docs/en/memory(在新标签页中打开)

相关指南

全部指南