fetch-library-docs
Fetch library documentation from multiple programming languages to extract READMEs, API docs, and code examples for analysis.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| library | Yes | Name of the package or URL of the library documentation to fetch | |
| language | No | Programming language or repository type if providing a package name (e.g., javascript, python, java, dotnet) |
Implementation Reference
- src/index.ts:228-284 (registration)Registration of the 'fetch-library-docs' MCP tool, including input schema (Zod) and inline async handler function that resolves URL and delegates to scraperService.server.tool( "fetch-library-docs", { library: z .string() .describe( "Name of the package or URL of the library documentation to fetch" ), language: z .string() .optional() .describe( "Programming language or repository type if providing a package name (e.g., javascript, python, java, dotnet)" ), }, async ({ library, language = "javascript" }) => { console.error( `Fetching documentation for library: ${library} ${ language ? `(${language})` : "" }` ); try { // Determine if input is a URL or package name const isLibraryUrl = isUrl(library); let url = isLibraryUrl ? library : getPackageUrl(library, language); const documentationContent = await scraperService.fetchLibraryDocumentation(url); return { content: [ { type: "text", text: documentationContent, }, ], }; } catch (error) { console.error("Error fetching library documentation:", error); const errorMessage = `Error fetching library documentation: ${ error instanceof Error ? error.message : String(error) }`; return { content: [ { type: "text", text: errorMessage, }, ], isError: true, }; } } );
- src/index.ts:230-242 (schema)Zod schema defining the tool's input parameters: library (string, package or URL) and optional language.{ library: z .string() .describe( "Name of the package or URL of the library documentation to fetch" ), language: z .string() .optional() .describe( "Programming language or repository type if providing a package name (e.g., javascript, python, java, dotnet)" ), },
- src/index.ts:243-283 (handler)Inline async handler function for the tool: checks if library is URL using isUrl, resolves package URL if needed, fetches docs via scraperService, returns markdown content or error.async ({ library, language = "javascript" }) => { console.error( `Fetching documentation for library: ${library} ${ language ? `(${language})` : "" }` ); try { // Determine if input is a URL or package name const isLibraryUrl = isUrl(library); let url = isLibraryUrl ? library : getPackageUrl(library, language); const documentationContent = await scraperService.fetchLibraryDocumentation(url); return { content: [ { type: "text", text: documentationContent, }, ], }; } catch (error) { console.error("Error fetching library documentation:", error); const errorMessage = `Error fetching library documentation: ${ error instanceof Error ? error.message : String(error) }`; return { content: [ { type: "text", text: errorMessage, }, ], isError: true, }; } }
- Key helper method in ScraperService that performs web crawling (up to maxPages), compiles extracted pages into structured Markdown documentation, and appends LLM prompt instructions. Called by the tool handler.public async fetchLibraryDocumentation( url: string, maxPages = 5 ): Promise<string> { try { // If input is not a URL, assume it's a package name if (!url.startsWith("http")) { url = `https://www.npmjs.com/package/${url}`; } // Extract library name from URL const libraryName = extractLibraryName(url); // Crawl documentation const pages = await this.crawlDocumentation(url, libraryName, maxPages); if (pages.length === 0) { throw new Error(`Failed to fetch documentation from ${url}`); } // Compile documentation into a single markdown document const documentation = this.compileDocumentation(pages, libraryName); // Include instructions for using the prompt const promptInstructions = ` --- š For better summarization, use the "summarize-library-docs" prompt with: - libraryName: "${libraryName}" - documentation: <the content above> Example: @summarize-library-docs with libraryName="${libraryName}" `; return documentation + promptInstructions; } catch (error) { console.error(`Error fetching URL content:`, error); // Extract library name from URL const libraryName = extractLibraryName(url); const errorMessage = `Error fetching URL content: ${ error instanceof Error ? error.message : String(error) }`; // Include error-specific prompt instructions const promptInstructions = ` --- š For information about this library despite the fetch error, use the "summarize-library-docs" prompt with: - libraryName: "${libraryName}" - errorStatus: "${error instanceof Error ? error.message : String(error)}" Example: @summarize-library-docs with libraryName="${libraryName}" and errorStatus="fetch failed" `; return errorMessage + promptInstructions; } }
- Utility to construct package repository URLs based on language (npm, pypi, maven, nuget, etc.), used by the handler to resolve package names to documentation URLs.export function getPackageUrl( packageName: string, language = "javascript" ): string { const lang = language.toLowerCase().trim(); switch (lang) { // JavaScript/TypeScript case "javascript": case "js": case "typescript": case "ts": case "node": case "nodejs": case "npm": return `https://www.npmjs.com/package/${packageName}`; // Python case "python": case "py": case "pypi": return `https://pypi.org/project/${packageName}`; // Java case "java": case "maven": return `https://mvnrepository.com/artifact/${packageName}`; // .NET case "dotnet": case ".net": case "csharp": case "c#": case "nuget": return `https://www.nuget.org/packages/${packageName}`; // Ruby case "ruby": case "gem": case "rubygem": case "rubygems": return `https://rubygems.org/gems/${packageName}`; // PHP case "php": case "composer": case "packagist": return `https://packagist.org/packages/${packageName}`; // Rust case "rust": case "cargo": case "crate": case "crates": return `https://crates.io/crates/${packageName}`; // Go case "go": case "golang": return `https://pkg.go.dev/${packageName}`; // Swift case "swift": case "cocoapods": return `https://cocoapods.org/pods/${packageName}`; // Default to npm default: return `https://www.npmjs.com/package/${packageName}`; } }