Context7 平台:为任意提示词提供最新代码文档
英文标题:📦 upstash/context7
Context7 是一个为 LLM 和 AI 代码编辑器提供实时、版本特定代码文档的平台。它直接从源头拉取最新文档,解决了训练数据过时、API 幻觉等问题。用户可通过 CLI+Skills 或 MCP 两种模式集成,只需在提示词中提及库名或版本即可准确获取代码示例,大幅提升开发者效率并减少错误。
672.2 KB
中文
Context7 平台 - 让任何提示词获取最新代码文档
LLM 依赖的是你所使用的库的过时或通用信息。你将面临:
- ❌ 代码示例过时,基于一年前的训练数据
- ❌ 幻觉出的 API,根本不存在
- ❌ 针对旧版软件包的通用回答
Context7 直接从源头拉取最新、版本特定的文档和代码示例,并将其直接放入你的提示词中。
创建一个 Next.js 中间件,检查 Cookie 中的有效 JWT,
并将未认证用户重定向到 `/login`。 use context7
配置 Cloudflare Worker 脚本,将
JSON API 响应缓存五分钟。 use context7
给我展示一下 Supabase 的邮箱/密码注册 Auth API。
Context7 会将最新的代码示例和文档直接拉取到 LLM 的上下文中。无需切换标签页,没有不存在的幻觉 API,也没有过时的代码生成。
以两种模式工作:
- CLI + Skills — 安装一个技能,引导你的 Agent 使用
ctx7CLI 命令获取文档(无需 MCP) - MCP — 注册一个 Context7 MCP 服务器,使你的 Agent 可以原生调用文档工具
使用一条命令即可为你的编程 Agent 设置 Context7。ctx7 CLI 需要 Node.js 18 或更新版本。
通过 OAuth 认证,生成 API 密钥,并安装相应的技能。你可以在 CLI + Skills 和 MCP 模式之间选择。使用 --cursor、--claude 或 --opencode 来指定目标 Agent。
若要移除生成的设置,运行 npx ctx7 remove。如果你通过 npm install -g ctx7 全局安装了 CLI,请单独通过 npm uninstall -g ctx7 移除该软件包。
如需手动配置,请将 Context7 服务器 URL https://mcp.context7.com/mcp 与你的 MCP 客户端一起使用,并通过 CONTEXT7_API_KEY 头传递 API 密钥。有关客户端专属的设置说明,请参见下方链接。
如果你已经确切地知道要使用哪个库,可以将其 Context7 ID 添加到提示词中。这样 Context7 就可以跳过库匹配步骤,直接检索文档。
使用 Supabase 实现基础认证。 use library /supabase/supabase for API and docs.
斜杠语法可以精确告诉 Context7 要为哪个库加载文档。
要获取特定库版本的文档,只需在提示词中提及版本号:
我应该如何设置 Next.js 14 中间件? use context7
Context7 将自动匹配相应的版本。
如果你通过 ctx7 setup 安装,系统会自动配置一个技能,在涉及库相关问题时触发 Context7。若要改为手动设置规则,请向你的编程 Agent 添加一条规则:
- Cursor :
Cursor Settings > Rules - Claude Code :
CLAUDE.md - 或你的编程 Agent 中的等效位置
示例规则:
在我需要库/API 文档、代码生成、设置或配置步骤时,始终使用 Context7,无需我明确要求。
-
ctx7 library <name> <query>:按库名搜索 Context7 索引,并返回匹配的库及其 ID。 -
ctx7 docs <libraryId> <query>:使用与 Context7 兼容的库 ID 检索库文档(例如,/mongodb/docs、/vercel/next.js)。 -
resolve-library-id:将通用库名解析为与 Context7 兼容的库 ID。query(必需):用户的问题或任务(用于按相关性对结果排序)libraryName(必需):要搜索的库的名称
-
query-docs:使用与 Context7 兼容的库 ID 检索库文档。libraryId(必需):精确的 Context7 兼容库 ID(例如,/mongodb/docs、/vercel/next.js)query(必需):要获取相关文档的问题或任务
1- Context7 项目由社区贡献,尽管我们努力保持高质量,但我们无法保证所有库文档的准确性、完整性或安全性。Context7 中列出的项目由其各自的所有者开发和维护,而非 Context7。如果你遇到任何可疑、不当或可能有害的内容,请使用项目页面上的“举报”按钮立即通知我们。我们认真对待所有举报,并将及时审查被标记的内容,以维护我们平台的完整性和安全性。使用 Context7 即表示你确认自行决定并承担风险。
2- 此仓库托管 MCP 服务器的源代码。支持组件——API 后端、解析引擎和爬虫引擎——是私有的,不属于本仓库。
保持更新并加入我们的社区:
MIT
English / 原文
Context7 Platform - Up-to-date Code Docs For Any Prompt
LLMs rely on outdated or generic information about the libraries you use. You get:
- ❌ Code examples are outdated and based on year-old training data
- ❌ Hallucinated APIs that don't even exist
- ❌ Generic answers for old package versions
Context7 pulls up-to-date, version-specific documentation and code examples straight from the source — and places them directly into your prompt.
Create a Next.js middleware that checks for a valid JWT in cookies
and redirects unauthenticated users to `/login`. use context7
Configure a Cloudflare Worker script to cache
JSON API responses for five minutes. use context7
Show me the Supabase auth API for email/password sign-up.
Context7 fetches up-to-date code examples and documentation right into your LLM's context. No tab-switching, no hallucinated APIs that don't exist, no outdated code generation.
Works in two modes:
- CLI + Skills — installs a skill that guides your agent to fetch docs using
ctx7CLI commands (no MCP required) - MCP — registers a Context7 MCP server so your agent can call documentation tools natively
Set up Context7 for your coding agents with a single command. The ctx7 CLI requires Node.js 18 or newer.
Authenticates via OAuth, generates an API key, and installs the appropriate skill. You can choose between CLI + Skills or MCP mode. Use --cursor, --claude, or --opencode to target a specific agent.
To remove the generated setup later, run npx ctx7 remove. If you globally installed the CLI with npm install -g ctx7, remove that package separately with npm uninstall -g ctx7.
To configure manually, use the Context7 server URL https://mcp.context7.com/mcp with your MCP client and pass your API key via the CONTEXT7_API_KEY header. See the link below for client-specific setup instructions.
Manual Installation / Other Clients →
If you already know exactly which library you want to use, add its Context7 ID to your prompt. That way, Context7 can skip the library-matching step and directly retrieve docs.
Implement basic authentication with Supabase. use library /supabase/supabase for API and docs.
The slash syntax tells Context7 exactly which library to load docs for.
To get documentation for a specific library version, just mention the version in your prompt:
How do I set up Next.js 14 middleware? use context7
Context7 will automatically match the appropriate version.
If you installed via ctx7 setup, a skill is configured automatically that triggers Context7 for library-related questions. To set up a rule manually instead, add one to your coding agent:
- Cursor :
Cursor Settings > Rules - Claude Code :
CLAUDE.md - Or the equivalent in your coding agent
Example rule:
Always use Context7 when I need library/API documentation, code generation, setup or configuration steps without me having to explicitly ask.
-
ctx7 library <name> <query>: Searches the Context7 index by library name and returns matching libraries with their IDs. -
ctx7 docs <libraryId> <query>: Retrieves documentation for a library using a Context7-compatible library ID (e.g.,/mongodb/docs,/vercel/next.js). -
resolve-library-id: Resolves a general library name into a Context7-compatible library ID.query(required): The user's question or task (used to rank results by relevance)libraryName(required): The name of the library to search for
-
query-docs: Retrieves documentation for a library using a Context7-compatible library ID.libraryId(required): Exact Context7-compatible library ID (e.g.,/mongodb/docs,/vercel/next.js)query(required): The question or task to get relevant documentation for
1- Context7 projects are community-contributed and while we strive to maintain high quality, we cannot guarantee the accuracy, completeness, or security of all library documentation. Projects listed in Context7 are developed and maintained by their respective owners, not by Context7. If you encounter any suspicious, inappropriate, or potentially harmful content, please use the "Report" button on the project page to notify us immediately. We take all reports seriously and will review flagged content promptly to maintain the integrity and safety of our platform. By using Context7, you acknowledge that you do so at your own discretion and risk.
2- This repository hosts the MCP server’s source code. The supporting components — API backend, parsing engine, and crawling engine — are private and not part of this repository.
Stay updated and join our community:
MIT
