explain
Show why a specific npm package is installed by displaying its dependency chain. Provide the absolute path to the package directory and the package name to get the reason for its presence.
Instructions
Explain why a package is installed (show dependency chain)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the package directory | |
| package | Yes | Package name to explain |
Implementation Reference
- src/index.ts:766-785 (handler)The tool handler that executes 'npm explain --json <pkg>' and returns the dependency chain output.
server.tool( "explain", "Explain why a package is installed (show dependency chain)", { path: z.string().describe("Absolute path to the package directory"), package: z.string().describe("Package name to explain"), }, async ({ path, package: pkg }) => { const args = ["explain", pkg, "--json"]; try { const { stdout } = await run(args, path); return { content: [{ type: "text", text: stdout }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:769-772 (schema)Input schema defining the 'path' (absolute directory) and 'package' (package name to explain) parameters using Zod.
{ path: z.string().describe("Absolute path to the package directory"), package: z.string().describe("Package name to explain"), }, - src/index.ts:766-785 (registration)Tool registration on the MCP server via server.tool() — this is the primary registration.
server.tool( "explain", "Explain why a package is installed (show dependency chain)", { path: z.string().describe("Absolute path to the package directory"), package: z.string().describe("Package name to explain"), }, async ({ path, package: pkg }) => { const args = ["explain", pkg, "--json"]; try { const { stdout } = await run(args, path); return { content: [{ type: "text", text: stdout }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:1361-1364 (registration)Sandbox-only registration (noop handler) for the explain tool, used in sandbox mode.
sandbox.tool("explain", "Explain why a package is installed", { path: z.string().describe("Absolute path to the package directory"), package: z.string().describe("Package name to explain"), }, noop); - src/index.ts:26-38 (helper)The run() helper that executes npm CLI commands via execFile. Used by the explain handler to run 'npm explain --json'.
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); }