Skip to main content
Glama

resolve-library-id

Convert package names to Context7-compatible library IDs for accessing current documentation and code examples from library sources.

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:

  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)

  • 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

TableJSON Schema
NameRequiredDescriptionDefault
libraryNameYesLibrary name to search for and retrieve a Context7-compatible library ID.

Implementation Reference

  • src/index.ts:64-130 (registration)
    Registration of the 'resolve-library-id' tool on the MCP server, including tool name, description, Zod input schema, and inline handler function that performs library search and formats results for user selection.
    "resolve-library-id", `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) - 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.`, { libraryName: z .string() .describe("Library name to search for and retrieve a Context7-compatible library ID."), }, async ({ libraryName }) => { const searchResponse: SearchResponse = await searchLibraries(libraryName); 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); return { content: [ { type: "text", text: `Available Libraries (top matches): 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 - Trust Score: Authority indicator - Versions: List of versions if available. Use one of those versions if and only if the user explicitly provides a version in their query. For best results, select libraries based on name match, trust score, snippet coverage, and relevance to your use case. ---------- ${resultsText}`, }, ], }; } );
  • Core helper function implementing the library search by querying the Context7 API search endpoint.
    export async function searchLibraries(query: string): Promise<SearchResponse> { try { const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/search`); url.searchParams.set("query", query); const response = await fetch(url); if (!response.ok) { const errorCode = response.status; if (errorCode === 429) { console.error(`Rate limited due to too many requests. Please try again later.`); return { results: [], error: `Rate limited due to too many requests. Please try again later.`, } as SearchResponse; } console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`); return { results: [], error: `Failed to search libraries. Please try again later. Error code: ${errorCode}`, } as SearchResponse; } return await response.json(); } catch (error) { console.error("Error searching libraries:", error); return { results: [], error: `Error searching libraries: ${error}` } as SearchResponse; } }
  • Helper function to format the search results into a readable list 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"); }
  • Supporting helper to format individual search results used by formatSearchResults.
    export function formatSearchResult(result: SearchResult): string { // Always include these basic details const formattedResult = [ `- Title: ${result.title}`, `- Context7-compatible library ID: ${result.id}`, `- Description: ${result.description}`, ]; // Only add code snippets count if it's a valid value if (result.totalSnippets !== -1 && result.totalSnippets !== undefined) { formattedResult.push(`- Code Snippets: ${result.totalSnippets}`); } // Only add trust score if it's a valid value if (result.trustScore !== -1 && result.trustScore !== undefined) { formattedResult.push(`- Trust Score: ${result.trustScore}`); } // Only add versions if it's a valid value if (result.versions !== undefined && result.versions.length > 0) { formattedResult.push(`- Versions: ${result.versions.join(", ")}`); } // Join all parts with newlines return formattedResult.join("\n"); }
  • Type definition for the SearchResponse used in the tool handler and helpers.
    export interface SearchResponse { error?: string; results: SearchResult[]; }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/2511319/context7'

If you have feedback or need assistance with the MCP directory API, please join our Discord server