Skip to main content

Overview

Context7 provides two AI SDK tools that work together to fetch documentation:
  1. resolveLibraryId - Searches for libraries and returns Context7-compatible library IDs
  2. queryDocs - 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:
  1. Analyze the query to understand what library/package the user is looking for
  2. 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 skip resolveLibraryId:

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

  1. Always use stepCountIs: Prevent infinite loops by setting a maximum step count
  2. Let AI decide workflow: The model will automatically call resolveLibraryId before queryDocs
  3. Use environment variables: Store API keys in CONTEXT7_API_KEY for security
  4. Provide specific queries: More detailed queries yield better documentation results
  5. Handle tool results: Check the steps array to see which tools were called

Next Steps

Context7Agent

Use the pre-configured agent that handles the multi-step workflow automatically