Overview
Context7 provides two AI SDK tools that work together to fetch documentation:resolveLibraryId- Searches for libraries and returns Context7-compatible library IDsqueryDocs- Fetches documentation for a specific library using its ID
resolveLibraryId
Resolves a library name to a Context7-compatible library ID and returns matching libraries.Function Signature
Configuration
Input Schema
Usage Example
With Custom API Key
Return Format
Returns an array of library objects with:- Library ID (e.g.,
/reactjs/react.dev,/vercel/next.js) - Description
- Source reputation (High/Medium/Low)
- Code snippet coverage
- Benchmark score (quality indicator, 100 is highest)
Tool Description
The tool uses this description for the AI model:Resolves a package/product name to a Context7-compatible library ID and returns matching libraries. You MUST call this function before ‘queryDocs’ to obtain a valid Context7-compatible library ID UNLESS the user explicitly provides a library ID in the format ‘/org/project’ or ‘/org/project/version’ in their query. Selection Process:
- Analyze the query to understand what library/package the user is looking for
- Return the most relevant match based on:
- Name similarity to the query (exact matches prioritized)
- Description relevance to the query’s intent
- Documentation coverage (prioritize libraries with higher Code Snippet counts)
- Source reputation (consider libraries with High or Medium reputation more authoritative)
- Benchmark Score: Quality indicator (100 is the highest score)
queryDocs
Fetches documentation for a library using its Context7 library ID.Function Signature
Configuration
Input Schema
Usage Example
With streamText
Direct Library ID Usage
If you already know the library ID, you can skipresolveLibraryId:
Tool Description
The tool uses this description for the AI model:Retrieves and queries up-to-date documentation and code examples from Context7 for any programming library or framework. You must call ‘resolveLibraryId’ first to obtain the exact Context7-compatible library ID required to use this tool, UNLESS the user explicitly provides a library ID in the format ‘/org/project’ or ‘/org/project/version’ in their query.
Multi-Step Workflow
Both tools work together in a typical workflow:Advanced Configuration
Custom API Keys per Tool
With Additional Tools
Error Handling
Both tools handle errors gracefully and return informative messages:Type Exports
The package re-exports useful types from the SDK:Best Practices
- Always use stepCountIs: Prevent infinite loops by setting a maximum step count
- Let AI decide workflow: The model will automatically call
resolveLibraryIdbeforequeryDocs - Use environment variables: Store API keys in
CONTEXT7_API_KEYfor security - Provide specific queries: More detailed queries yield better documentation results
- Handle tool results: Check the
stepsarray to see which tools were called
Next Steps
Context7Agent
Use the pre-configured agent that handles the multi-step workflow automatically