Skip to main content
Glama

get-library-docs

Fetch current library documentation from Context7 MCP to access up-to-date information and code examples, ensuring accurate API references and eliminating outdated training data.

Instructions

Fetches up-to-date documentation for a library. You must call 'resolve-library-id' 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.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
context7CompatibleLibraryIDYesExact Context7-compatible library ID (e.g., '/mongodb/docs', '/vercel/next.js', '/supabase/supabase', '/vercel/next.js/v14.3.0-canary.87') retrieved from 'resolve-library-id' or directly from user query in the format '/org/project' or '/org/project/version'.
topicNoTopic to focus documentation on (e.g., 'hooks', 'routing').
tokensNoMaximum number of tokens of documentation to retrieve (default: 10000). Higher values provide more context but consume more tokens.

Implementation Reference

  • MCP tool handler function that invokes the fetchLibraryDocumentation helper, handles errors, and returns formatted MCP response content.
    async ({ context7CompatibleLibraryID, tokens = DEFAULT_MINIMUM_TOKENS, topic = "" }) => { const fetchDocsResponse = await fetchLibraryDocumentation(context7CompatibleLibraryID, { tokens, topic, }); if (!fetchDocsResponse) { return { content: [ { type: "text", text: "Documentation not found or not finalized for this library. This might have happened because you used an invalid Context7-compatible library ID. To get a valid Context7-compatible library ID, use the 'resolve-library-id' with the package name you wish to retrieve documentation for.", }, ], }; } return { content: [ { type: "text", text: fetchDocsResponse, }, ], }; }
  • Zod schema for tool input parameters including required library ID, optional topic filter, and token limit with preprocessing and transformation.
    { context7CompatibleLibraryID: z .string() .describe( "Exact Context7-compatible library ID (e.g., '/mongodb/docs', '/vercel/next.js', '/supabase/supabase', '/vercel/next.js/v14.3.0-canary.87') retrieved from 'resolve-library-id' or directly from user query in the format '/org/project' or '/org/project/version'." ), topic: z .string() .optional() .describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."), tokens: z .preprocess((val) => (typeof val === "string" ? Number(val) : val), z.number()) .transform((val) => (val < DEFAULT_MINIMUM_TOKENS ? DEFAULT_MINIMUM_TOKENS : val)) .optional() .describe( `Maximum number of tokens of documentation to retrieve (default: ${DEFAULT_MINIMUM_TOKENS}). Higher values provide more context but consume more tokens.` ), },
  • src/index.ts:132-179 (registration)
    Registration of the 'get-library-docs' tool on the MCP server instance, specifying name, description, input schema, and handler.
    server.tool( "get-library-docs", "Fetches up-to-date documentation for a library. You must call 'resolve-library-id' 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.", { context7CompatibleLibraryID: z .string() .describe( "Exact Context7-compatible library ID (e.g., '/mongodb/docs', '/vercel/next.js', '/supabase/supabase', '/vercel/next.js/v14.3.0-canary.87') retrieved from 'resolve-library-id' or directly from user query in the format '/org/project' or '/org/project/version'." ), topic: z .string() .optional() .describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."), tokens: z .preprocess((val) => (typeof val === "string" ? Number(val) : val), z.number()) .transform((val) => (val < DEFAULT_MINIMUM_TOKENS ? DEFAULT_MINIMUM_TOKENS : val)) .optional() .describe( `Maximum number of tokens of documentation to retrieve (default: ${DEFAULT_MINIMUM_TOKENS}). Higher values provide more context but consume more tokens.` ), }, async ({ context7CompatibleLibraryID, tokens = DEFAULT_MINIMUM_TOKENS, topic = "" }) => { const fetchDocsResponse = await fetchLibraryDocumentation(context7CompatibleLibraryID, { tokens, topic, }); if (!fetchDocsResponse) { return { content: [ { type: "text", text: "Documentation not found or not finalized for this library. This might have happened because you used an invalid Context7-compatible library ID. To get a valid Context7-compatible library ID, use the 'resolve-library-id' with the package name you wish to retrieve documentation for.", }, ], }; } return { content: [ { type: "text", text: fetchDocsResponse, }, ], }; } );
  • Supporting utility function that performs the actual API request to Context7 to fetch library documentation based on library ID, with token and topic parameters, including error handling and normalization.
    export async function fetchLibraryDocumentation( libraryId: string, options: { tokens?: number; topic?: string; } = {} ): Promise<string | null> { try { if (libraryId.startsWith("/")) { libraryId = libraryId.slice(1); } const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/${libraryId}`); if (options.tokens) url.searchParams.set("tokens", options.tokens.toString()); if (options.topic) url.searchParams.set("topic", options.topic); url.searchParams.set("type", DEFAULT_TYPE); const response = await fetch(url, { headers: { "X-Context7-Source": "mcp-server", }, }); if (!response.ok) { const errorCode = response.status; if (errorCode === 429) { const errorMessage = `Rate limited due to too many requests. Please try again later.`; console.error(errorMessage); return errorMessage; } const errorMessage = `Failed to fetch documentation. Please try again later. Error code: ${errorCode}`; console.error(errorMessage); return errorMessage; } const text = await response.text(); if (!text || text === "No content available" || text === "No context data available") { return null; } return text; } catch (error) { const errorMessage = `Error fetching library documentation. Please try again later. ${error}`; console.error(errorMessage); return errorMessage; } }

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