Context7 - Up-to-date Library Documentation
Context7 provides real-time, version-specific documentation and code examples for programming libraries. It solves the problem of LLMs generating outdated code by fetching current documentation directly from the source.
When to Use
- •When you need accurate, up-to-date documentation for a library
- •When implementing features using unfamiliar APIs
- •When the user mentions "use context7" in their prompt
- •When you suspect training data may have outdated API information
Prerequisites
- •Get your API key from https://context7.com
- •Set up mcporter config with your API key:
mkdir -p ./config
cat > ./config/mcporter.json << EOF
{
"mcpServers": {
"context7": {
"url": "https://mcp.context7.com/mcp",
"headers": {
"CONTEXT7_API_KEY": "YOUR_API_KEY_HERE"
}
}
}
}
EOF
Or if using opencode/cursor config, ensure the {env:CONTEXT7_API_KEY} is properly set in your environment.
Usage via mcporter (Recommended)
1. Resolve Library ID
First, find the Context7 library ID for the library you need:
npx mcporter --config ./config/mcporter.json call 'context7.resolve-library-id(query: "How to create routes", libraryName: "express")'
Or using key=value syntax:
npx mcporter --config ./config/mcporter.json call context7.resolve-library-id query="How to create routes" libraryName="express"
This returns matching libraries with their IDs (e.g., /expressjs/express).
2. Query Documentation
Once you have the library ID, query the documentation:
npx mcporter --config ./config/mcporter.json call 'context7.query-docs(libraryId: "/expressjs/express", query: "How to set up middleware")'
Or using key=value syntax:
npx mcporter --config ./config/mcporter.json call context7.query-docs libraryId="/expressjs/express" query="How to set up middleware"
Bundled CLI
This skill includes a pre-generated CLI at context7-cli.ts. Run it with bun:
# Show help bun run ./context7/context7-cli.ts # Resolve library ID bun run ./context7/context7-cli.ts resolve-library-id \ --query "How to create a REST API" \ --library-name "express" # Query documentation bun run ./context7/context7-cli.ts query-docs \ --library-id "/expressjs/express" \ --query "How to handle errors"
Note: The bundled CLI requires mcporter as a dependency. Install it first:
npm install -g mcporter
Regenerating the CLI
To regenerate the CLI with the latest schema:
cd /tmp && npx mcporter generate-cli --server context7 mv /tmp/context7.ts ./context7/context7-cli.ts
Available Tools
resolve-library-id
Resolves a library name to a Context7-compatible library ID.
Parameters:
- •
query(required): The user's question or task - used to rank results by relevance - •
libraryName(required): Library name to search for
Returns: List of matching libraries with IDs, descriptions, and quality scores.
query-docs
Fetches up-to-date documentation for a specific library.
Parameters:
- •
libraryId(required): Context7 library ID (e.g.,/vercel/next.js,/mongodb/docs) - •
query(required): Specific question about the library
Returns: Relevant documentation snippets and code examples.
Common Library IDs
| Library | ID |
|---|---|
| Next.js | /vercel/next.js |
| React | /facebook/react |
| Express | /expressjs/express |
| MongoDB | /mongodb/docs |
| Supabase | /supabase/supabase |
| Prisma | /prisma/prisma |
Best Practices
- •Always call
resolve-library-idfirst unless you know the exact library ID - •Be specific in your queries - "How to set up JWT authentication" is better than "auth"
- •Limit to 3 calls per question - use best available result after that
- •Do not include sensitive information (API keys, credentials) in queries
Output Formats
Control output format with -o flag:
npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o json # JSON output npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o markdown # Markdown output npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o text # Plain text (default)