resolve-library-id
Convert a package or product name into a Context7-compatible library ID to retrieve relevant libraries. Enables accurate documentation access by prioritizing name similarity, intent, and trust score for precise results.
Instructions
Resolves a package/product name to a Context7-compatible library ID and returns a list of matching libraries.
You MUST call this function before 'get-library-docs' 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)
Trust score (consider libraries with scores of 7-10 more authoritative)
Response Format:
Return the selected library ID in a clearly marked section
Provide a brief explanation for why this library was chosen
If multiple good matches exist, acknowledge this but proceed with the most relevant one
If no good matches exist, clearly state this and suggest query refinements
For ambiguous queries, request clarification before proceeding with a best-guess match.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| libraryName | Yes | Library name to search for and retrieve a Context7-compatible library ID. |
Implementation Reference
- packages/mcp/src/index.ts:142-190 (handler)The handler function that executes the 'resolve-library-id' tool logic: searches for libraries using searchLibraries, formats results with formatSearchResults, and returns formatted text response.async ({ libraryName }) => { const ctx = requestContext.getStore(); const searchResponse: SearchResponse = await searchLibraries( libraryName, ctx?.clientIp, ctx?.apiKey ); if (!searchResponse.results || searchResponse.results.length === 0) { return { content: [ { type: "text", text: searchResponse.error ? searchResponse.error : "Failed to retrieve library documentation data from Context7", }, ], }; } const resultsText = formatSearchResults(searchResponse); const responseText = `Available Libraries: Each result includes: - Library ID: Context7-compatible identifier (format: /org/project) - Name: Library or package name - Description: Short summary - Code Snippets: Number of available code examples - Source Reputation: Authority indicator (High, Medium, Low, or Unknown) - Benchmark Score: Quality indicator (100 is the highest score) - Versions: List of versions if available. Use one of those versions if the user provides a version in their query. The format of the version is /org/project/version. For best results, select libraries based on name match, source reputation, snippet coverage, benchmark score, and relevance to your use case. ---------- ${resultsText}`; return { content: [ { type: "text", text: responseText, }, ], }; }
- packages/mcp/src/index.ts:112-191 (registration)Registration of the 'resolve-library-id' tool with server.registerTool, including title, description, and input schema.server.registerTool( "resolve-library-id", { title: "Resolve Context7 Library ID", description: `Resolves a package/product name to a Context7-compatible library ID and returns a list of matching libraries. You MUST call this function before 'get-library-docs' 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) Response Format: - Return the selected library ID in a clearly marked section - Provide a brief explanation for why this library was chosen - If multiple good matches exist, acknowledge this but proceed with the most relevant one - If no good matches exist, clearly state this and suggest query refinements For ambiguous queries, request clarification before proceeding with a best-guess match.`, inputSchema: { libraryName: z .string() .describe("Library name to search for and retrieve a Context7-compatible library ID."), }, }, async ({ libraryName }) => { const ctx = requestContext.getStore(); const searchResponse: SearchResponse = await searchLibraries( libraryName, ctx?.clientIp, ctx?.apiKey ); if (!searchResponse.results || searchResponse.results.length === 0) { return { content: [ { type: "text", text: searchResponse.error ? searchResponse.error : "Failed to retrieve library documentation data from Context7", }, ], }; } const resultsText = formatSearchResults(searchResponse); const responseText = `Available Libraries: Each result includes: - Library ID: Context7-compatible identifier (format: /org/project) - Name: Library or package name - Description: Short summary - Code Snippets: Number of available code examples - Source Reputation: Authority indicator (High, Medium, Low, or Unknown) - Benchmark Score: Quality indicator (100 is the highest score) - Versions: List of versions if available. Use one of those versions if the user provides a version in their query. The format of the version is /org/project/version. For best results, select libraries based on name match, source reputation, snippet coverage, benchmark score, and relevance to your use case. ---------- ${resultsText}`; return { content: [ { type: "text", text: responseText, }, ], }; } );
- packages/mcp/src/index.ts:136-140 (schema)Zod input schema definition for the tool: requires 'libraryName' string.inputSchema: { libraryName: z .string() .describe("Library name to search for and retrieve a Context7-compatible library ID."), },
- packages/mcp/src/lib/api.ts:91-119 (helper)Helper function that performs the actual API search for libraries matching the query and handles errors.export async function searchLibraries( query: string, clientIp?: string, apiKey?: string ): Promise<SearchResponse> { try { const url = new URL(`${CONTEXT7_API_BASE_URL}/v2/search`); url.searchParams.set("query", query); const headers = generateHeaders(clientIp, apiKey); const response = await fetch(url, { headers }); if (!response.ok) { const errorCode = response.status; const errorMessage = createErrorMessage(errorCode, apiKey); console.error(errorMessage); return { results: [], error: errorMessage, } as SearchResponse; } const searchData = await response.json(); return searchData as SearchResponse; } catch (error) { const errorMessage = `Error searching libraries: ${error}`; console.error(errorMessage); return { results: [], error: errorMessage } as SearchResponse; } }
- packages/mcp/src/lib/utils.ts:62-69 (helper)Utility function to format the search results into readable text for the tool response.export function formatSearchResults(searchResponse: SearchResponse): string { if (!searchResponse.results || searchResponse.results.length === 0) { return "No documentation libraries found matching your query."; } const formattedResults = searchResponse.results.map(formatSearchResult); return formattedResults.join("\n----------\n"); }