search
Search the npm registry for packages by query. Optionally limit results to get relevant packages quickly.
Instructions
Search npm registry for packages
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| limit | No | Max results (default 20) |
Implementation Reference
- src/index.ts:132-153 (registration)Registration of the 'search' tool on the McpServer using server.tool(), with name 'search' and description 'Search npm registry for packages'.
// ── npm search ── server.tool( "search", "Search npm registry for packages", { query: z.string().describe("Search query"), limit: z.number().optional().describe("Max results (default 20)"), }, async ({ query, limit }) => { const args = ["search", query, "--json"]; if (limit) args.push("--limit", String(limit)); try { const { stdout } = await run(args); return { content: [{ type: "text", text: stdout }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:136-139 (schema)Input schema for the 'search' tool: 'query' (string, required) and 'limit' (number, optional) defined using Zod schemas.
{ query: z.string().describe("Search query"), limit: z.number().optional().describe("Max results (default 20)"), }, - src/index.ts:140-152 (handler)Handler/execution function for the 'search' tool. Runs 'npm search <query> --json' with optional limit, returns JSON output or an error.
async ({ query, limit }) => { const args = ["search", query, "--json"]; if (limit) args.push("--limit", String(limit)); try { const { stdout } = await run(args); return { content: [{ type: "text", text: stdout }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, - src/index.ts:26-38 (helper)The 'run' helper function that wraps npm execFile calls, used by the search handler to execute 'npm search'.
async function run( args: string[], cwd?: string, ): Promise<{ stdout: string; stderr: string }> { const fullArgs = [...args, ...npmrcArgs]; const opts: { cwd?: string; timeout: number; env: NodeJS.ProcessEnv; maxBuffer: number } = { timeout: 120_000, maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large outputs env: { ...process.env, NO_COLOR: "1" }, }; if (cwd) opts.cwd = cwd; return exec(NPM, fullArgs, opts); } - src/index.ts:1222-1225 (registration)Sandbox registration of the 'search' tool (no-op implementation for Smithery sandbox testing).
sandbox.tool("search", "Search npm registry for packages", { query: z.string().describe("Search query"), limit: z.number().optional().describe("Max results"), }, noop);