dedupe
Remove duplicate dependencies from an npm package's dependency tree at a given path, with an optional dry-run to preview changes without applying them.
Instructions
Reduce duplication in the dependency tree
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the package directory | |
| dryRun | No | Preview changes without making them |
Implementation Reference
- src/index.ts:26-38 (helper)The `run` helper function that executes npm commands via execFile (promisified). It assembles arguments, sets timeout, and returns stdout/stderr.
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:742-763 (handler)The handler function for the 'dedupe' tool. It runs `npm dedupe` (with optional --dry-run) and returns the output.
// ── npm dedupe ── server.tool( "dedupe", "Reduce duplication in the dependency tree", { path: z.string().describe("Absolute path to the package directory"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, async ({ path, dryRun }) => { const args = ["dedupe"]; if (dryRun) args.push("--dry-run"); try { const { stdout, stderr } = await run(args, path); return { content: [{ type: "text", text: stdout + stderr || "Deduplication complete" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:743-749 (schema)Schema definition for the 'dedupe' tool: requires path (string), accepts optional dryRun (boolean).
server.tool( "dedupe", "Reduce duplication in the dependency tree", { path: z.string().describe("Absolute path to the package directory"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, - src/index.ts:743-763 (registration)Registration of the 'dedupe' tool via server.tool() with its schema and handler.
server.tool( "dedupe", "Reduce duplication in the dependency tree", { path: z.string().describe("Absolute path to the package directory"), dryRun: z.boolean().optional().describe("Preview changes without making them"), }, async ({ path, dryRun }) => { const args = ["dedupe"]; if (dryRun) args.push("--dry-run"); try { const { stdout, stderr } = await run(args, path); return { content: [{ type: "text", text: stdout + stderr || "Deduplication complete" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, );