search_npm
Search the npm registry for JavaScript packages to find dependencies and libraries for development projects.
Instructions
Search npm registry for JavaScript packages
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| limit | No | Maximum number of results (default: 5) |
Implementation Reference
- src/index.ts:207-239 (handler)The core handler function that searches the npm registry API for packages matching the query, caches the results, formats them with name, version, description, downloads, and link, and returns the formatted string.private async searchNpm(query: string, limit: number = 5): Promise<string> { const cacheKey = `npm:${query}:${limit}`; const cached = cache.get<string>(cacheKey); if (cached) return cached; try { const response = await this.axiosInstance.get( `https://registry.npmjs.org/-/v1/search`, { params: { text: query, size: limit } } ); const results = response.data.objects.map((item: any, i: number) => { const pkg = item.package; return `${i + 1}. ${pkg.name} (v${pkg.version})\n` + ` ${pkg.description || 'No description'}\n` + ` Weekly Downloads: ${pkg.downloads}\n` + ` ${pkg.links.npm}\n`; }).join('\n'); cache.set(cacheKey, results); return results; } catch (error) { throw new McpError( ErrorCode.InternalError, `npm API error: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:379-397 (registration)Registers the 'search_npm' tool in the MCP server's list of tools, including its description and input schema.name: 'search_npm', description: 'Search npm registry for JavaScript packages', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, limit: { type: 'number', description: 'Maximum number of results (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] } },
- src/index.ts:381-396 (schema)Defines the input schema for the search_npm tool, specifying query (required string) and optional limit (number 1-10).inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, limit: { type: 'number', description: 'Maximum number of results (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] }
- src/index.ts:480-491 (helper)The dispatch handler in the CallToolRequestSchema that extracts arguments, calls the searchNpm function, and returns the result as MCP text content.case 'search_npm': { const { query, limit } = request.params.arguments as { query: string; limit?: number }; const results = await this.searchNpm(query, limit); return { content: [ { type: 'text', text: results } ] }; }