Cursor 和 Claude Code 如何透過 Context7 避免生成過時 API 程式碼
How to stop Cursor and Claude Code from writing outdated API code with Context7
Context7 可將最新、版本相符的文件和程式碼範例載入 Cursor、Claude Code 等 AI 智能體的上下文,協助避免生成過時 API 程式碼。文章列出 `npx ctx7 setup` 的 CLI + Skills 與 MCP 兩種設定模式,也提供 Cursor、Claude Code 的手動設定方式及以 `use context7` 觸發文件查詢的方法。
You ask your coding agent for a Next.js route, a Supabase sign-up form or a Cloudflare Worker, and it confidently writes code against an API that changed a year ago. Sometimes it invents a function that never existed. The model is not broken. Its training data is simply older than the library you installed last week.
Context7 from Upstash fixes this by pulling current, version-specific documentation and code examples straight from the source and putting them into your agent's context at the moment it needs them. It works in Cursor, Claude Code, VS Code and Codex, and its docs carry manual setup for 30+ MCP clients.
This guide covers the one-command setup, manual setup for Cursor and Claude Code, the prompts that make it work well, and a rule so you never have to remember to ask.
What you need
- Node.js 22.13 or newer (the
ctx7CLI requires it) - Cursor, Claude Code, or another MCP client
- Optional but recommended: a free API key from the Context7 dashboard at context7.com/dashboard, which gives you higher rate limits
The fast way: one command
From any terminal:
npx ctx7 setup
This authenticates you with OAuth, generates an API key and installs the right pieces for your agent. You choose between two modes:
-
CLI + Skills: installs a skill that tells your agent to fetch docs with
ctx7CLI commands. No MCP server needed. - MCP: registers a Context7 MCP server so your agent can call the documentation tools natively.
To target one agent directly, add a flag:
npx ctx7 setup --cursor
npx ctx7 setup --claude
Changed your mind? npx ctx7 remove undoes the generated setup.
Manual setup in Claude Code
If you prefer to wire it yourself, Claude Code takes one command. The remote server version (nothing runs locally):
claude mcp add --scope user --header "Authorization: Bearer YOUR_API_KEY" --transport http context7 https://mcp.context7.com/mcp
Or run the server locally through npx:
claude mcp add --scope user context7 -- npx -y @upstash/context7-mcp --api-key YOUR_API_KEY
The --scope user flag makes Context7 available in every project, not just the current folder.
Manual setup in Cursor
Open Cursor Settings, then Tools & MCP, then New MCP Server. Context7 recommends putting the config in ~/.cursor/mcp.json (global) or .cursor/mcp.json inside a project if you only want it there:
{
"mcpServers": {
"context7": {
"url": "https://mcp.context7.com/mcp",
"headers": {
"Authorization": "Bearer YOUR_API_KEY"
}
}
}
}
Since Cursor 1.0 there is also a one-click install button on the Context7 docs page. If you want the local server instead, swap the url and headers for "command": "npx" and "args": ["-y", "@upstash/context7-mcp", "--api-key", "YOUR_API_KEY"].
On Windows, the docs note one difference for local servers: run npx through cmd, with "command": "cmd" and "args": ["/c", "npx", "-y", "@upstash/context7-mcp", "--api-key", "YOUR_API_KEY"].
Using it: add "use context7"
Once installed, the simplest trigger is to end your prompt with use context7. Two examples straight from the README:
Create a Next.js proxy 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
Behind the scenes the MCP server exposes two tools. resolve-library-id turns a library name like Next.js into a Context7 ID such as /vercel/next.js. query-docs then fetches the relevant documentation and code examples for your question, and it accepts an optional language parameter so examples in your language rank first.
Two tips that make answers sharper
Name the library ID. If you already know the library, put its ID in the prompt and Context7 skips the matching step:
Implement basic authentication with Supabase. use library /supabase/supabase for API and docs.
Name the version. Mention the version you are on and Context7 matches docs for that version:
How do I set up a Next.js 16 proxy? use context7
This is the part people skip, and it matters. A generic answer about "Next.js middleware" is exactly the stale code you were trying to avoid.
Make it automatic with a rule
Typing use context7 every time gets old. If you installed with ctx7 setup, a skill already triggers Context7 for library questions. If you set it up manually, add a rule:
- In Cursor: Cursor Settings, then Rules
- In Claude Code: your
CLAUDE.md
The rule the README suggests:
Always use Context7 when I need library/API documentation, code generation, setup or configuration steps without me having to explicitly ask.
Use the CLI without MCP
Context7 also works as a plain CLI, handy for checking what the agent will see:
npx ctx7 library "Next.js" "set up middleware"
npx ctx7 docs /vercel/next.js "set up middleware"
The first searches the index and returns matching library IDs. The second pulls docs for a given ID and question.
Things to know before you rely on it
- The libraries in Context7 are community contributed. The README says plainly that Upstash cannot guarantee the accuracy, completeness or security of every library's docs, and there is a Report button for anything suspicious.
- The GitHub repo holds the MCP server. The API backend, parsing engine and crawler behind it are private.
- If your library is missing, you can submit it from the Context7 site.
Where to go next
We keep a short, plain-English write-up of this exact workflow, with who it is for and what can go wrong: How to set up Context7 MCP so your agent uses current docs.
If you live in Cursor, our list of AI use cases for Cursor collects other repos that plug into it. And if Context7 made you curious about what else MCP can add to your editor, see our ranked list of the best open-source MCP servers.
Deduped tracks the AI repos people are actually building with and writes up how to use them. Repo: github.com/upstash/context7.
Get one email every Monday with the week's hottest AI repos and exactly how to use them: subscribe free at dedupedai.com
來源:DEV Community · dev.to