search_npm
Search the npm registry for JavaScript packages by query, with options to limit results. Helps developers find and integrate relevant npm packages efficiently.
Instructions
Search npm registry for JavaScript packages
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Maximum number of results (default: 5) | |
| query | Yes | Search query |
Implementation Reference
- src/index.ts:207-239 (handler)Core handler function that searches the npm registry API for packages matching the query, caches results, and formats the output with package details.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:378-397 (registration)Registers the 'search_npm' tool in the MCP server's tool list, including name, 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:480-491 (handler)Dispatcher case in CallToolRequestHandler that invokes the searchNpm handler and returns the formatted results as MCP 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 } ] }; }
- src/index.ts:285-285 (helper)Helper call to searchNpm within the searchAll tool implementation.this.searchNpm(query, limit).catch(error =>