Skip to main content
Glama

get-library-docs

Fetch up-to-date, version-specific documentation for libraries using a Context7-compatible ID. Specify topics or token limits to focus and tailor the retrieved information for precise development needs.

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'.
tokensNoMaximum number of tokens of documentation to retrieve (default: 10000). Higher values provide more context but consume more tokens.
topicNoTopic to focus documentation on (e.g., 'hooks', 'routing').

Implementation Reference

  • Registers the 'get-library-docs' MCP tool with title, description, input schema, and handler function that delegates to fetchLibraryDocumentation.
    server.registerTool( "get-library-docs", { title: "Get Library Docs", description: "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. Use mode='code' (default) for API references and code examples, or mode='info' for conceptual guides, narrative information, and architectural questions.", inputSchema: { 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'." ), mode: z .enum(["code", "info"]) .optional() .default("code") .describe( "Documentation mode: 'code' for API references and code examples (default), 'info' for conceptual guides, narrative information, and architectural questions." ), topic: z .string() .optional() .describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."), page: z .number() .int() .min(1) .max(10) .optional() .describe( "Page number for pagination (start: 1, default: 1). If the context is not sufficient, try page=2, page=3, page=4, etc. with the same topic." ), }, }, async ({ context7CompatibleLibraryID, mode = DOCUMENTATION_MODES.CODE, page = 1, topic }) => { const ctx = requestContext.getStore(); const apiKey = ctx?.apiKey || globalApiKey; const fetchDocsResponse = await fetchLibraryDocumentation( context7CompatibleLibraryID, mode, { page, limit: DEFAULT_RESULTS_LIMIT, topic, }, ctx?.clientIp, apiKey ); 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, }, ], }; } );
  • MCP tool handler function that retrieves API key from context, calls fetchLibraryDocumentation, and formats the response as MCP content.
    async ({ context7CompatibleLibraryID, mode = DOCUMENTATION_MODES.CODE, page = 1, topic }) => { const ctx = requestContext.getStore(); const apiKey = ctx?.apiKey || globalApiKey; const fetchDocsResponse = await fetchLibraryDocumentation( context7CompatibleLibraryID, mode, { page, limit: DEFAULT_RESULTS_LIMIT, topic, }, ctx?.clientIp, apiKey ); 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-based input schema defining the tool parameters: context7CompatibleLibraryID (required), mode, topic, page.
    inputSchema: { 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'." ), mode: z .enum(["code", "info"]) .optional() .default("code") .describe( "Documentation mode: 'code' for API references and code examples (default), 'info' for conceptual guides, narrative information, and architectural questions." ), topic: z .string() .optional() .describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."), page: z .number() .int() .min(1) .max(10) .optional() .describe( "Page number for pagination (start: 1, default: 1). If the context is not sufficient, try page=2, page=3, page=4, etc. with the same topic." ), },
  • Helper function that parses library ID, constructs API URL, makes HTTP fetch request to Context7, handles errors and empty responses.
    export async function fetchLibraryDocumentation( libraryId: string, docMode: DocumentationMode, options: { page?: number; limit?: number; topic?: string; } = {}, clientIp?: string, apiKey?: string ): Promise<string | null> { try { const { username, library, tag } = parseLibraryId(libraryId); // Build URL path let urlPath = `${CONTEXT7_API_BASE_URL}/v2/docs/${docMode}/${username}/${library}`; if (tag) { urlPath += `/${tag}`; } const url = new URL(urlPath); url.searchParams.set("type", DEFAULT_TYPE); if (options.topic) url.searchParams.set("topic", options.topic); if (options.page) url.searchParams.set("page", options.page.toString()); if (options.limit) url.searchParams.set("limit", options.limit.toString()); const headers = generateHeaders(clientIp, apiKey, { "X-Context7-Source": "mcp-server" }); const response = await fetch(url, { headers }); if (!response.ok) { const errorCode = response.status; const errorMessage = createErrorMessage(errorCode, apiKey); console.error(errorMessage); return errorMessage; } const text = await response.text(); if (!text || text === "No content available" || text === "No context data available") { const suggestion = docMode === DOCUMENTATION_MODES.CODE ? " Try mode='info' for guides and tutorials." : " Try mode='code' for API references and code examples."; return `No ${docMode} documentation available for this library.${suggestion}`; } return text; } catch (error) { const errorMessage = `Error fetching library documentation. Please try again later. ${error}`; console.error(errorMessage); return errorMessage; } }

Other Tools

Related 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/upstash/context7-mcp'

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