diff --git a/README.md b/README.md index 396516a..090fe2f 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,19 @@ npm run lint npm run build ``` +### Local Configuration + +```json +{ + "mcpServers": { + "context7": { + "command": "node", + "args": ["/ABSOLUTE/PATH/TO/PARENT/FOLDER/context7-mcp/build/index.js"] + } + } +} +``` + ### Testing with MCP Inspector You can also use the MCP Inspector to test the tools by following the MCP documentation for setting up the inspector. diff --git a/src/index.ts b/src/index.ts index 9bf65fe..edf9a90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,7 +80,7 @@ server.tool( libraryName: z .string() .describe( - "Name of the library to retrieve documentation for (e.g., 'upstash-redis', 'nextjs'). Must match exactly a library name from 'list-available-docs'." + "Name of the library to retrieve documentation for (e.g., 'mongodb/docs', 'vercel/nextjs'). Must match exactly a library name from 'list-available-docs'." ), topic: z .string() @@ -93,7 +93,7 @@ server.tool( .min(5000) .optional() .describe( - "Maximum number of tokens of documentation to retrieve (default: 5000).Higher values provide more comprehensive documentation but use more context window." + "Maximum number of tokens of documentation to retrieve (default: 5000). Higher values provide more comprehensive documentation but use more context window." ), }, async ({ libraryName, tokens = 5000, topic = "" }) => { diff --git a/src/lib/api.ts b/src/lib/api.ts index f0a9942..f11d95c 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -33,33 +33,43 @@ export async function fetchLibraryDocumentation( topic: string = "" ): Promise { try { - let contextURL = `${CONTEXT7_BASE_URL}/${libraryName}/llms.txt`; - const params = []; + // if libraryName has a "/" as the first character, remove it + if (libraryName.startsWith("/")) { + libraryName = libraryName.slice(1); + } + // Handle folders parameter + let basePath = libraryName; + let folders = ""; + if (libraryName.includes("?folders=")) { + const [path, foldersParam] = libraryName.split("?folders="); + basePath = path; + folders = foldersParam; + } + let contextURL = `${CONTEXT7_BASE_URL}/${basePath}/llms.txt`; + const params = []; + if (folders) { + params.push(`folders=${encodeURIComponent(folders)}`); + } if (tokens) { params.push(`tokens=${tokens}`); } if (topic) { params.push(`topic=${encodeURIComponent(topic)}`); } - if (params.length > 0) { contextURL += `?${params.join("&")}`; } const response = await fetch(contextURL); - if (!response.ok) { console.error(`Failed to fetch documentation: ${response.status}`); return null; } - const text = await response.text(); - if (!text || text === "No content available" || text === "No context data available") { return null; } - return text; } catch (error) { console.error("Error fetching library documentation:", error);