How to Install Context7 MCP Server
Large Language Models (LLMs) have revolutionized coding assistance, offering remarkable capabilities in code generation, explanation, and debugging. However, they often operate with a significant limitation: their knowledge is frozen at the time of their last training. This means they frequently rely on outdated documentation, suggest deprecated functions, or even hallucinate APIs that never existed for the specific library versions you're using. This leads to frustration, wasted time, and broken code.
Enter Context7, an innovative MCP (Model Context Protocol) server developed by Upstash. Context7 tackles the problem of stale information head-on. It acts as a dynamic bridge between your coding prompt and the real-time world of software documentation. When invoked, Context7 fetches up-to-date, version-specific documentation and relevant code examples directly from the source and injects them into your LLM's context before it generates an answer.
The result?
- โ Accurate, Up-to-Date Code: Examples and suggestions based on the latest library versions.
- โ No More Hallucinated APIs: Rely on documented, existing functions.
- โ Version-Specific Guidance: Get answers tailored to the library versions you are actually using.
- โ Seamless Workflow: Integrate directly into your existing AI coding assistant (like Cursor, VS Code with MCP extensions, etc.) without constant tab-switching to documentation sites.
This tutorial provides a comprehensive guide on how to install, configure, and effectively use the Context7 MCP server to significantly enhance the accuracy and reliability of your AI coding assistant.
Tired of Postman? Want a decent postman alternative that doesn't suck?
Apidog is a powerful all-in-one API development platform that's revolutionizing how developers design, test, and document their APIs.
Unlike traditional tools like Postman, Apidog seamlessly integrates API design, automated testing, mock servers, and documentation into a single cohesive workflow. With its intuitive interface, collaborative features, and comprehensive toolset, Apidog eliminates the need to juggle multiple applications during your API development process.
Whether you're a solo developer or part of a large team, Apidog streamlines your workflow, increases productivity, and ensures consistent API quality across your projects.
The Core Idea: use context7
The magic of Context7 lies in its simplicity. You interact with it by adding a simple instruction to your natural language prompts: use context7.
For example:
Create a basic Next.js project with app router. use context7Write a Python script using requests to fetch data from an API, include error handling. use context7How do I set up a Redis client using the 'redis-py' library? use context7
When your MCP-enabled editor sees use context7, it routes the relevant parts of your request to the Context7 server. The server intelligently identifies the libraries mentioned, fetches the most current and relevant documentation snippets or code examples for the task described, and bundles this fresh context with your original prompt before sending it to the LLM. The LLM then generates its response based on this enriched, accurate information.
Prerequisites
Before installing Context7, ensure you have the following:
- Node.js: Version 18.0.0 or higher. Context7 relies on the Node.js ecosystem, primarily using
npx(or alternatives likebunx,deno) to execute. You can check your Node.js version by runningnode -vin your terminal. Download Node.js from nodejs.org if needed. - MCP-Compatible Client: Context7 is an MCP server. It needs a client application to interact with. Popular choices include:
- Cursor IDE
- VS Code (with an MCP extension like "MCP Support")
- Windsurf
- Claude Code (CLI tool)
- Claude Desktop
Installation and Configuration
Context7 needs to be registered as an MCP server within your chosen client application. The setup involves telling the client how to run the Context7 server, typically using npx, bunx, or deno to fetch and execute the latest version of the @upstash/context7-mcp package.
Below are detailed instructions for various popular clients. The recommended approach generally uses npx as it comes bundled with Node.js.
Important Note: The command npx -y @upstash/context7-mcp@latest tells npx to execute (x) the @upstash/context7-mcp package, automatically downloading it if necessary (-y confirms installation), and ensures it uses the most recent version (@latest).
1. Cursor
Cursor has built-in support for MCP servers.
- Open Settings: Go to
File->Settings(orCode->Settingson macOS), then navigate toExtensions->Cursor. - Find MCP Settings: Scroll down to the "MCP" section.
- Add Server Manually (UI): Click on
Add new global MCP server.- Name:
context7(or any name you prefer) - Command:
npx - Args: Add
-yand@upstash/context7-mcp@latestas separate arguments.
- Name:
- Add Server via
mcp.json(Recommended):Locate your Cursor configuration directory (usually
~/.cursor/on Linux/macOS or%USERPROFILE%\.cursor\on Windows).Open or create the
mcp.jsonfile.Add the following configuration:
{ "mcpServers": { "context7": { "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }Save the file and restart Cursor for the changes to take effect.
Alternatives for Cursor (using Bun or Deno):
If you prefer or encounter issues with npx, you can use bunx or deno.
Using Bun:
{ "mcpServers": { "context7": { "command": "bunx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }Using Deno:
{ "mcpServers": { "context7": { "command": "deno", "args": ["run", "--allow-net", "npm:@upstash/context7-mcp"] } } }(Note: Deno requires explicit network permissions (
--allow-net)).
2. VS Code (with MCP Extension)
You'll need an extension that adds MCP support to VS Code, such as the generically named "MCP Support" or others that might emerge. Configuration is typically done in your VS Code settings.json file.
Open Settings:
File->Preferences->Settings(orCode->Preferences->Settingson macOS).Open
settings.json: Click the "Open Settings (JSON)" icon in the top-right corner.Add MCP Server Configuration: Add the following snippet within the main JSON object. The exact structure (
serversvs.mcpServers,typefield) might vary slightly depending on the specific MCP extension you are using. Refer to your extension's documentation, but the core command and args remain the same. This example assumes a common structure:{ // ... your other settings ... "mcp.servers": { // Or the key specified by your MCP extension "Context7": { // Or any name you prefer "type": "stdio", // Common transport type for MCP "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } // ... rest of your settings ... }Save
settings.jsonand restart VS Code.
3. Windsurf
Windsurf uses a configuration file similar to Cursor's mcp.json.
Locate your Windsurf MCP configuration file (refer to Windsurf documentation if unsure of the location).
Add the Context7 server definition:
{ "mcpServers": { "context7": { "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } }Save the file and restart Windsurf.
4. Claude Code (CLI)
Claude Code provides a command-line interface for managing MCP servers.
Open your terminal.
Run the following command:
claude mcp add context7 -- npx -y @upstash/context7-mcp@latestThis registers
context7as an MCP server, instructing Claude Code to run thenpxcommand wheneveruse context7is encountered.
5. Claude Desktop
Claude Desktop uses a configuration file, typically claude_desktop_config.json.
Locate your
claude_desktop_config.jsonfile (refer to Claude Desktop documentation for its location).Add the Context7 server configuration:
{ // ... other configurations ... "mcpServers": { "Context7": { // Or any name you prefer "command": "npx", "args": ["-y", "@upstash/context7-mcp@latest"] } } // ... rest of configurations ... }Save the file and restart Claude Desktop.
6. Smithery (Automated Installation)
Smithery is a tool that can simplify MCP server installations, particularly for Claude Desktop.
Open your terminal.
Run the installation command:
npx -y @smithery/cli install @upstash/context7-mcp --client claudeThis command uses Smithery to automatically configure Context7 for Claude Desktop.
7. Using Docker
If you prefer containerization or encounter environment issues with Node.js/npx locally, you can run Context7 in a Docker container.
Create a
Dockerfile: Create a file namedDockerfile(no extension) in a convenient location with the following content:FROM node:18-alpine WORKDIR /app # Install the latest version globally within the container RUN npm install -g @upstash/context7-mcp@latest # Default command to run when the container starts CMD ["context7-mcp"]Build the Docker Image: Open a terminal in the same directory as your
Dockerfileand run (ensure Docker Desktop or the Docker daemon is running):docker build -t context7-mcp .This builds an image named
context7-mcp.Configure Your MCP Client: Update your client's MCP configuration to execute the Docker command instead of
npxdirectly. The key is to run the container interactively (-i) and remove it automatically when it exits (--rm).Example for a client using a
mcp.jsonor similar structure:{ "mcpServers": { "Context7": { // Or your preferred name "command": "docker", "args": ["run", "-i", "--rm", "context7-mcp"], // Command + Args to run the container "transportType": "stdio" // Often required for Docker interaction // Add other client-specific settings like timeout, disabled, etc. if needed } } }Important: Adapt this JSON structure (
mcpServersvsservers,transportType, etc.) based on the specific configuration requirements of your MCP client (Cursor, VS Code extension, etc.). The crucial part is replacing thenpxcommand/args with thedocker run ...command/args. Ensure the image name in theargs(context7-mcp) matches the tag used during thedocker buildcommand. Restart your MCP client after configuration.
Using Context7
Once installed and configured, using Context7 is straightforward:
- Write Your Prompt: Formulate your coding request or question naturally in your MCP client.
- Add the Trigger: Append
use context7to your prompt. - Get Enhanced Results: The client will invoke the Context7 server, which fetches relevant, up-to-date context. This context is combined with your prompt and sent to the LLM, leading to more accurate and reliable responses.
Available Context7 Tools
Context7 exposes specific tools that the LLM (or you, indirectly) can utilize:
resolve-library-id:- Purpose: Translates a common library name (e.g., "react", "nextjs", "requests") into an internal ID that Context7 uses to find documentation. This is mostly an internal step but useful to understand.
- Parameters:
libraryName(string, required): The general name of the library.
get-library-docs:- Purpose: Fetches the actual documentation snippets or code examples for a specific library.
- Parameters:
context7CompatibleLibraryID(string, required): The internal ID obtained fromresolve-library-id.topic(string, optional): Narrows down the documentation search to a specific topic (e.g., "routing", "hooks", "authentication", "error handling").tokens(integer, optional, default: 5000): Specifies the maximum number of tokens (roughly, words or parts of words) to return. Values below 5000 are automatically increased to 5000. This prevents overwhelming the LLM's context window.
You generally don't call these tools directly. Simply using use context7 allows the system (client, Context7 server, LLM working together) to potentially use these tools behind the scenes to fulfill your request. Mentioning specific libraries and tasks in your prompt helps Context7 determine what to fetch.
Practical Examples
Request:
How do I make a POST request with JSON data using the Python 'requests' library? use context7- Context7 Action: Identifies "requests" library, fetches relevant docs/examples for POST requests with JSON.
- LLM Result: Provides accurate, up-to-date Python code using
requests.post()with thejson=parameter.
Request:
Explain how to use the useState hook in React. use context7- Context7 Action: Identifies "React", focuses on the "useState" topic, fetches current documentation for the hook.
- LLM Result: Gives a clear explanation and code example based on the latest React documentation for
useState.
Request:
Configure a basic Express.js server to listen on port 3000. use context7- Context7 Action: Identifies "Express.js", fetches basic server setup examples.
- LLM Result: Generates a correct and minimal Express.js server code snippet.
Troubleshooting
If Context7 doesn't seem to be working:
- Check Prerequisites: Ensure Node.js v18+ is installed and accessible in the environment where your MCP client is launched.
- Verify Configuration: Double-check the MCP server configuration in your client (
mcp.json,settings.json, etc.). Ensure thecommandandargsare correct. Typos are common! - Restart Client: Always restart your MCP client (Cursor, VS Code, etc.) after modifying MCP configurations.
ERR_MODULE_NOT_FOUND: This error sometimes occurs withnpx.- Solution 1: Try
bunx: If you have Bun installed (npm install -g bun), change thecommandin your config fromnpxtobunx. - Solution 2: Try
deno: If you have Deno installed, change thecommandtodenoandargsto["run", "--allow-net", "npm:@upstash/context7-mcp"].
- Solution 1: Try
- General MCP Client Errors:
- Try removing
@latest: Change@upstash/context7-mcp@latestto@upstash/context7-mcpin theargs. This might help if there's a temporary issue with the absolute latest version. - Try
bunxordeno: As mentioned above, these can sometimes resolve package resolution issues.
- Try removing
- Check for Output/Errors: Some MCP clients might show output or errors from MCP servers in a specific panel or log file. Check your client's documentation. You can also try running the command directly in your terminal (e.g.,
npx -y @upstash/context7-mcp@latest) to see if it executes without errors. - Docker Issues: If using Docker, ensure the Docker daemon is running. Check that the image name in the client configuration matches the name used during
docker build. Try running thedocker run -i --rm context7-mcpcommand directly in the terminal to test the container.
Development (For Contributors)
If you wish to contribute to Context7 or run it locally from source:
Clone:
git clone https://github.com/upstash/context7.gitNavigate:
cd context7Install Dependencies:
bun install(Requires Bun)Build:
bun run buildLocal Configuration: Configure your MCP client to run the local script directly using
tsx(part of the development dependencies, often installed viabun install).Example
mcp.jsonfor local development:{ "mcpServers": { "context7-local": { // Use a different name to avoid conflicts "command": "npx", // Or bunx // Replace with the ACTUAL absolute or relative path to index.ts "args": ["tsx", "/full/path/to/your/cloned/context7/src/index.ts"] } } }
Conclusion
Context7 MCP Server is a powerful tool for overcoming one of the primary limitations of current LLM-based coding assistants: their reliance on potentially outdated training data. By dynamically fetching and injecting up-to-date documentation and code examples directly into the LLM's context, Context7 ensures you receive more accurate, reliable, and version-aware coding assistance.
Following the installation steps for your preferred MCP client and remembering to add use context7 to your prompts will significantly enhance your AI pair programming experience, saving you time and reducing frustration caused by inaccurate or hallucinated code.
