get-library-docs
Fetch current documentation for libraries to access up-to-date API references and code examples, ensuring accurate technical information without relying on 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
| Name | Required | Description | Default |
|---|---|---|---|
| context7CompatibleLibraryID | Yes | 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 | No | Topic to focus documentation on (e.g., 'hooks', 'routing'). | |
| tokens | No | Maximum number of tokens of documentation to retrieve (default: 10000). Higher values provide more context but consume more tokens. |
Implementation Reference
- src/index.ts:153-178 (handler)The handler function for the 'get-library-docs' tool. It calls fetchLibraryDocumentation and formats the response as MCP 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, }, ], }; }
- src/index.ts:135-152 (schema)Zod input schema for the 'get-library-docs' tool defining parameters: context7CompatibleLibraryID (required), topic (optional), tokens (optional with defaults and validation).{ 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)Registers the 'get-library-docs' tool on the MCP server with name, description, schema, and handler function.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, }, ], }; } );
- src/lib/api.ts:44-85 (helper)Core helper function that performs the HTTP fetch to Context7 API to retrieve library documentation based on library ID, tokens, and topic.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; } }