cache
Manage the npm cache: clear outdated packages to free disk space, verify cache integrity to prevent corruption, or list cached contents to inspect stored packages.
Instructions
Manage the npm cache
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action: clean (clear cache), verify (check integrity), ls (list contents) | |
| force | No | Force clean (required for clean action) |
Implementation Reference
- src/index.ts:909-929 (handler)The tool handler for 'cache' - calls 'npm cache <action>' with optional --force flag, returns stdout/stderr or error.
server.tool( "cache", "Manage the npm cache", { action: z.enum(["clean", "verify", "ls"]).describe("Action: clean (clear cache), verify (check integrity), ls (list contents)"), force: z.boolean().optional().describe("Force clean (required for clean action)"), }, async ({ action, force }) => { const args = ["cache", action]; if (action === "clean" && force) args.push("--force"); try { const { stdout, stderr } = await run(args); return { content: [{ type: "text", text: stdout + stderr || "Done" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:912-914 (schema)Input schema for 'cache' tool - requires 'action' enum (clean/verify/ls) and optional 'force' boolean.
{ action: z.enum(["clean", "verify", "ls"]).describe("Action: clean (clear cache), verify (check integrity), ls (list contents)"), force: z.boolean().optional().describe("Force clean (required for clean action)"), - src/index.ts:909-929 (registration)Registration of the 'cache' tool on the primary 'server' McpServer instance via server.tool().
server.tool( "cache", "Manage the npm cache", { action: z.enum(["clean", "verify", "ls"]).describe("Action: clean (clear cache), verify (check integrity), ls (list contents)"), force: z.boolean().optional().describe("Force clean (required for clean action)"), }, async ({ action, force }) => { const args = ["cache", action]; if (action === "clean" && force) args.push("--force"); try { const { stdout, stderr } = await run(args); return { content: [{ type: "text", text: stdout + stderr || "Done" }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:1391-1394 (registration)Registration of the 'cache' tool in the sandbox/exported createSandboxServer() (stub/noop handler).
sandbox.tool("cache", "Manage the npm cache", { action: z.enum(["clean", "verify", "ls"]).describe("Action"), force: z.boolean().optional().describe("Force clean"), }, noop); - src/index.ts:26-38 (helper)Helper 'run' function used by the cache handler to execute npm commands via execFile.
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); }