126 lines
3.6 KiB
Markdown
126 lines
3.6 KiB
Markdown
---
|
|
name: context7-docs
|
|
description: Fetch up-to-date library documentation and code examples using Context7
|
|
---
|
|
|
|
## When to use
|
|
Use when the user asks to:
|
|
- Look up library APIs (React, Next.js, Supabase, etc.)
|
|
- Get current code examples for a specific library version
|
|
- Find documentation for configuration or setup steps
|
|
- Resolve ambiguous API questions with authoritative sources
|
|
- Avoid outdated training data or hallucinated APIs
|
|
|
|
## Prerequisites
|
|
- `npx` must be available (Node.js)
|
|
- Internet connection (Context7 fetches latest docs)
|
|
- No API key needed for basic usage (public mode)
|
|
|
|
## Workflow
|
|
|
|
### 1. Find the library ID (if not known)
|
|
If the user mentions a library name (e.g., "React", "Supabase"), first resolve to Context7 library ID:
|
|
|
|
```bash
|
|
npx ctx7 library <library-name>
|
|
```
|
|
|
|
Example:
|
|
```bash
|
|
npx ctx7 library react
|
|
```
|
|
|
|
Output:
|
|
```
|
|
1. Title: React
|
|
Context7-compatible library ID: /reactjs/react.dev
|
|
...
|
|
```
|
|
|
|
Pick the best match (usually highest benchmark score or official source).
|
|
|
|
### 2. Retrieve documentation
|
|
Use the library ID to fetch relevant docs:
|
|
|
|
```bash
|
|
npx ctx7 docs <library-id> --query "<user question>"
|
|
```
|
|
|
|
Example:
|
|
```bash
|
|
npx ctx7 docs /reactjs/react.dev --query "how to use useEffect with empty dependency array"
|
|
```
|
|
|
|
### 3. Present results
|
|
Context7 returns relevant code snippets and explanations. Structure the answer:
|
|
- Brief summary of the solution
|
|
- Code example(s) directly from docs
|
|
- Link to full docs if needed
|
|
|
|
## Library ID Shortcuts
|
|
Once you know the library ID, use it directly in prompts:
|
|
|
|
User: "How do I set up Next.js middleware? use context7"
|
|
|
|
You can parse "use context7" as a signal to call Context7 with:
|
|
- library: inferred or ask user
|
|
- query: the full question
|
|
|
|
## Common Libraries (pre-resolved)
|
|
|
|
| Library | Context7 ID |
|
|
|---------|-------------|
|
|
| React | /reactjs/react.dev or /websites/react_dev |
|
|
| Next.js | /vercel/next.js |
|
|
| Supabase | /supabase/supabase |
|
|
| Vue | /vuejs/core |
|
|
| Angular | /angular/core |
|
|
| Node.js | /nodejs/node |
|
|
| Express | /expressjs/express |
|
|
| FastAPI | /tiangolo/fastapi |
|
|
| Django | /django/django |
|
|
| PostgreSQL | /postgres/postgres |
|
|
| MongoDB | /mongodb/docs |
|
|
|
|
You can cache these mappings for faster lookup.
|
|
|
|
## Examples
|
|
|
|
<Good>
|
|
User: "Implement basic authentication with Supabase"
|
|
Assistant:
|
|
1. Resolve library: `npx ctx7 library supabase` → /supabase/supabase
|
|
2. Fetch docs: `npx ctx7 docs /supabase/supabase --query "basic email/password authentication"`
|
|
3. Return code example from Context7 output
|
|
</Good>
|
|
|
|
<Bad>
|
|
Skipping library resolution and guessing API from memory (may be outdated).
|
|
</Bad>
|
|
|
|
## Tips
|
|
|
|
- Always verify the returned code matches the user's version if specified (Context7 auto-detects version from query)
|
|
- If results are empty, try rephrasing the query or different library ID
|
|
- For version-specific questions, include version in query: "Next.js 14 middleware"
|
|
- Cache library IDs after first lookup to reduce API calls
|
|
|
|
## Error Handling
|
|
|
|
- **Library not found**: Try alternative names or check spelling
|
|
- **No docs returned**: Query might be too vague; break into smaller questions
|
|
- **Network error**: Retry once, then fall back to general knowledge (with disclaimer)
|
|
|
|
## Verification
|
|
- [ ] Library ID is correct (from `ctx7 library` output)
|
|
- [ ] Query clearly describes the needed information
|
|
- [ ] Returned code is complete and runnable
|
|
- [ ] Version matches user's context if specified
|
|
|
|
## Advanced: MCP Mode (Future)
|
|
If OpenClaw adds MCP client support, you can register Context7 MCP server:
|
|
```
|
|
https://mcp.context7.com/mcp
|
|
```
|
|
With header `CONTEXT7_API_KEY` if you have one. Then use `ctx7` tools directly without shell calls.
|