Context7 在 Claude Code 的設定、重跑測試及 MCP 與 CLI 選擇指南
Context7 in Claude Code: Setup, a Rerun, and MCP vs CLI
Vadim Nicolai 提供 Context7 在 Claude Code 的實作指南,涵蓋設定、重跑 A/B 測試,以及 MCP server 與 CLI skill 的選擇。
This is a practical guide to Context7 in Claude Code: how to install it, how to rerun the test I used to measure it, and whether to pick the MCP server or the CLI skill. The test is small and easy to copy. On upstash-redis 1.9.0, an SDK release newer than the model and not installed where the agent worked, Claude Code passed 0 of 9 runs on its own and 7 of 9 with Context7.
Step 1: How do you install Context7 in Claude Code?
Context7's Claude Code page recommends one command:
npx ctx7 setup --claude
It asks whether to install the MCP server or a CLI skill. Add --cli to go straight to the skill, which calls the ctx7 CLI and needs no MCP at all. There is also a plugin with browser sign-in.
ctx7 setup asks you to sign in with a one-time device code, then creates an API key on your Context7 account. One tip: it writes into $CLAUDE_CONFIG_DIR when that is set, not into $HOME/.claude, so check that variable first.
Step 2: Can you try it without a key?
You can try Context7 before signing in. The local MCP server and the ctx7 library / ctx7 docs commands needed no key and no login in my runs:
npx -y ctx7 library langgraph "interrupt and resume with Command"
npx -y ctx7 docs /langchain-ai/langgraph "interrupt resume Command SqliteSaver"
The first command lists matching library ids. The second returns current docs for one of them. In my 23 November run, a ctx7 docs query took about 1.6 s and returned about 800–900 words, small enough to drop into an agent's context. The MCP server runs locally with npx -y @upstash/context7-mcp.
Step 3: How do you rerun the A/B test?
Each run is headless, in a fresh scratch directory outside any repo, so neither arm sees my project notes or the reference answers (user-level settings may still load, the same in both arms):
claude -p "$PROMPT" --setting-sources project \
--strict-mcp-config --mcp-config mcp.json \
--permission-mode acceptEdits \
--allowedTools "Write" "Edit" "Read" "Bash(.venv/bin/python:*)" \
--output-format stream-json --verbose --max-budget-usd 2
The two arms differ only in mcp.json:
- Baseline: an empty
mcp.json. - Context7: one local server,
npx -y @upstash/context7-mcp. Add its two tools to--allowedToolsand end the prompt withuse context7.
--strict-mcp-config loads only that file, so no other MCP server leaks in. Web tools are not in --allowedTools, so web lookups are denied in both arms. Cost and turns come from the run's final result object. I used 3 runs per task, 9 per cell, on claude-opus-5-5.
The three upstash-redis 1.9.0 tasks (PyPI, released 2 November 2026):
- Build a 3-dimensional cosine vector index, add three vectors, and return the two nearest.
- Keep the last 5 events per user in a fixed-size ring.
- Report a vector index's count and info, then drop it.
Tasks 1 and 2 named the API family ("the SDK's own vector API", "the SDK's array commands"). All three ended with "Use only APIs that exist in that version." I confirmed redis.vector and the array commands (arring, arlastitems, …) are new in 1.9.0 by diffing the 1.8.0 and 1.9.0 packages.
Step 4: How do you score runs without a server?
The pass check runs afterwards, outside the agent. A small recorder replaces the SDK's HTTP call and uses the SDK's own response parser. So the check sees exactly which Redis commands a solution would send, for example VECTOR.CREATE notes DIM 3 METRIC COSINE, with no server and no credentials. Then it compares them with the expected ones.
Rules that made the check fair:
- Every check was first shown to fail on a planted wrong answer.
-
exists_okis optional; a try/except also counts. - Any
LPUSH,RPUSH,LTRIM,LRANGEorZADDfails the ring task, including inside a transaction. - A missing solution file counts as a fail.
What numbers should you get?
| Without Context7 | With Context7 | |
|---|---|---|
| SDK not installed where the agent works | 0 / 9 | 7 / 9 |
SDK installed in .venv
|
9 / 9 | 9 / 9 |
| Mean cost per run, not installed | $0.136 | $0.228 |
| Mean cost per run, installed | $0.202 | $0.262 |
Without Context7, the agent tried the web in 8 of 9 runs and was denied (the Context7 arm tried it in 5 of 9, also denied). On the two vector tasks it then declined to write code, and for the ring it wrote the old answer, a list with LPUSH and LTRIM. With Context7 it made two or three docs queries per run and used the new API. Context7 served the current upstash-redis docs, which already include it. The other two runs also worked from the current docs: one added an EXISTS check before creating the index, which my exact-sequence check rejects, and one sent the right VECTOR.* commands through raw execute() because it could not confirm the SDK method existed.
A control on an API the model already knew, LangGraph 1.2.17's interrupt with Command(resume=...) and SqliteSaver, passed 2 of 2 in both arms. With the SDK installed, the agent read its source and passed 9 of 9 either way, and Context7 added about 6 cents per run. The payoff is where the code you need is not on disk yet: a version you are about to upgrade to, or a library you have not installed.
MCP server or CLI skill: which should you pick?
Both lanes are cheap in context:
- MCP server: the idle server added 332 tokens to the first turn (19,083 → 19,415), because Claude Code defers MCP tool schemas until they're needed.
- CLI skill: the always-on rule plus the skill description come to roughly 800 tokens, estimated from their length rather than measured. The skill body loads only when it is used.
So pick by client. Use the MCP server where MCP is supported, and the CLI skill anywhere with a shell. I used the MCP server for the A/B test and the ctx7 CLI for real work: it gave me LangGraph's RetryPolicy and runtime.execution_info.node_attempt, which I checked against the installed LangGraph 1.2.12, for a retry fix in my own event-harvesting pipeline.
FAQ
- Is it free? These tests needed no key and hit no rate-limit error. Context7's README recommends a free API key for higher rate limits, and the Free plan on Context7's plans page includes 1,000 API calls a month.
- Does it slow the agent down? Only slightly. With the SDK installed, Context7 runs took about 5.5 s and 2.6 turns more on average; without the SDK the gap was about 12 s, partly because the baseline often gave up early.
- Do I need to type "use context7"? My Context7-arm runs always included it. The CLI setup adds a rule meant to make the agent look up library docs without it.
- Who else uses it? Homebrew and Nixpkgs package it, JetBrains' ThinkRail lists it as its first MCP preset, and Docker's MCP registry lists it. The MCP server package was downloaded about 1.2 million times from npm in the week to 22 November 2026.
Related reading
-
LangGraph Deep Agents Human-in-the-Loop Tool Approvals: the
interruptpattern used in the control test. - Durable Execution in LangGraph: Agents That Survive Failure and Resume Where They Left Off: checkpoints and resume.
- Semantic caching for LLMs: cutting the cost side of agent runs.
This post was sponsored by Upstash; the tests and opinions are mine.
來源:DEV Community · dev.to