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)
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.
import { resolveLibraryId, queryDocs } from '@upstash/context7-tools-ai-sdk';import { generateText, stepCountIs } from 'ai';import { openai } from '@ai-sdk/openai';const { text, steps } = await generateText({ model: openai('gpt-4o'), prompt: 'How do I implement server actions in Next.js?', tools: { resolveLibraryId: resolveLibraryId(), queryDocs: queryDocs(), }, stopWhen: stepCountIs(5),});// The model will:// Step 1: Call resolveLibraryId with libraryName: "Next.js"// Step 2: Call queryDocs with the returned library ID and query about server actions// Step 3: Synthesize answer with code examplesconsole.log(text);console.log('Steps taken:', steps.length);
Both tools handle errors gracefully and return informative messages:
// If library not found'No libraries found matching "invalid-lib". Try a different search term or check the library name.'// If API key is invalid'Error searching for libraries: Unauthorized. Check your API key and try again.'// If documentation not found'No documentation found for library "/invalid/id". This might have happened because you used an invalid Context7-compatible library ID. Use \'resolveLibraryId\' to get a valid ID.'