pkg
Read, update, or delete package.json fields such as name, scripts, and keywords using get, set, and delete actions. Specify field paths and values to manage project configuration programmatically.
Instructions
Manage package.json fields programmatically
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the package directory | |
| action | Yes | Action to perform | |
| field | Yes | Field name (e.g. 'name', 'scripts.build', 'keywords') | |
| value | No | Value to set (required for set action, use JSON for objects/arrays) |
Implementation Reference
- src/index.ts:691-717 (registration)Registration of the 'pkg' tool via server.tool() on the McpServer instance
// ── npm pkg ── server.tool( "pkg", "Manage package.json fields programmatically", { path: z.string().describe("Absolute path to the package directory"), action: z.enum(["get", "set", "delete"]).describe("Action to perform"), field: z.string().describe("Field name (e.g. 'name', 'scripts.build', 'keywords')"), value: z.string().optional().describe("Value to set (required for set action, use JSON for objects/arrays)"), }, async ({ path, action, field, value }) => { const args = ["pkg", action, field]; if (action === "set" && value !== undefined) { args.push(value); } args.push("--json"); try { const { stdout, stderr } = await run(args, path); 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:695-700 (schema)Zod schema definitions for the 'pkg' tool inputs: path (string), action (enum: get/set/delete), field (string), value (optional string)
{ path: z.string().describe("Absolute path to the package directory"), action: z.enum(["get", "set", "delete"]).describe("Action to perform"), field: z.string().describe("Field name (e.g. 'name', 'scripts.build', 'keywords')"), value: z.string().optional().describe("Value to set (required for set action, use JSON for objects/arrays)"), }, - src/index.ts:701-717 (handler)Handler function for the 'pkg' tool - runs npm pkg command with action, field, and optional value to manage package.json fields programmatically
async ({ path, action, field, value }) => { const args = ["pkg", action, field]; if (action === "set" && value !== undefined) { args.push(value); } args.push("--json"); try { const { stdout, stderr } = await run(args, path); 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:26-38 (helper)The run() helper function that executes npm commands via child_process execFile, used by the pkg 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); } - src/index.ts:1344-1349 (registration)Sandbox registration of the 'pkg' tool (noop implementation) in createSandboxServer()
sandbox.tool("pkg", "Manage package.json fields programmatically", { path: z.string().describe("Absolute path to the package directory"), action: z.enum(["get", "set", "delete"]).describe("Action to perform"), field: z.string().describe("Field name"), value: z.string().optional().describe("Value to set"), }, noop);