pkg
Programmatically manage package.json fields to get, set, or delete values like scripts, dependencies, and metadata for npm package configuration.
Instructions
Manage package.json fields programmatically
Input Schema
TableJSON 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:692-717 (handler)The handler implementation for the "pkg" tool. It calls 'npm pkg' with arguments derived from the tool input.
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, }; } }, );