prune
Remove extraneous packages not listed in package.json from a project directory. Optionally remove devDependencies and preview changes before making them.
Instructions
Remove extraneous packages not listed in package.json
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the package directory | |
| production | No | Remove devDependencies | |
| dryRun | No | Preview changes without making them |
Implementation Reference
- src/index.ts:955-978 (handler)Primary handler for the 'prune' tool. Runs `npm prune` with optional --omit=dev and --dry-run flags, executing via the `run()` helper.
// ── npm prune ── server.tool( "prune", "Remove extraneous packages not listed in package.json", { path: z.string().describe("Absolute path to the package directory"), production: z.boolean().optional().describe("Remove devDependencies"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, async ({ path, production, dryRun }) => { const args = ["prune"]; if (production) args.push("--omit=dev"); if (dryRun) args.push("--dry-run"); try { const { stdout, stderr } = await run(args, path); return { content: [{ type: "text", text: stdout + stderr || "Prune complete" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:959-963 (schema)Input schema for the 'prune' tool: path (required), production (optional boolean), dryRun (optional boolean).
{ path: z.string().describe("Absolute path to the package directory"), production: z.boolean().optional().describe("Remove devDependencies"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, - src/index.ts:956-978 (registration)Registration of the 'prune' tool via server.tool() on the main McpServer instance.
server.tool( "prune", "Remove extraneous packages not listed in package.json", { path: z.string().describe("Absolute path to the package directory"), production: z.boolean().optional().describe("Remove devDependencies"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, async ({ path, production, dryRun }) => { const args = ["prune"]; if (production) args.push("--omit=dev"); if (dryRun) args.push("--dry-run"); try { const { stdout, stderr } = await run(args, path); return { content: [{ type: "text", text: stdout + stderr || "Prune complete" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:1401-1405 (registration)Stub/placeholder registration of 'prune' tool in the sandbox server (createSandboxServer), using a noop handler.
sandbox.tool("prune", "Remove extraneous packages", { path: z.string().describe("Absolute path to the package directory"), production: z.boolean().optional().describe("Remove devDependencies"), dryRun: z.boolean().optional().describe("Preview changes"), }, noop); - src/index.ts:26-38 (helper)The `run()` helper function that executes npm commands as child processes. Used by the prune tool handler.
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); }