install
Add npm packages to a project directory with options for dev dependencies, exact versions, global installation, and dry-run preview.
Instructions
Install packages in a project
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the package directory | |
| packages | No | Package names to install (empty = install all from package.json) | |
| saveDev | No | Save as devDependency | |
| saveExact | No | Save exact version instead of semver range | |
| global | No | Install globally | |
| dryRun | No | Preview install without making changes |
Implementation Reference
- src/index.ts:405-433 (handler)Primary tool handler for 'install'. Constructs npm install args (packages, saveDev, saveExact, global, dryRun), executes via the run() helper, and returns stdout/stderr or an error response.
server.tool( "install", "Install packages in a project", { path: z.string().describe("Absolute path to the package directory"), packages: z.array(z.string()).optional().describe("Package names to install (empty = install all from package.json)"), saveDev: z.boolean().optional().describe("Save as devDependency"), saveExact: z.boolean().optional().describe("Save exact version instead of semver range"), global: z.boolean().optional().describe("Install globally"), dryRun: z.boolean().optional().describe("Preview install without making changes"), }, async ({ path, packages, saveDev, saveExact, global: isGlobal, dryRun }) => { const args = ["install"]; if (packages && packages.length > 0) args.push(...packages); if (saveDev) args.push("--save-dev"); if (saveExact) args.push("--save-exact"); if (isGlobal) args.push("-g"); if (dryRun) args.push("--dry-run"); try { const { stdout, stderr } = await run(args, path); return { content: [{ type: "text", text: stdout + stderr }] }; } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:408-414 (schema)Input schema for 'install': validates path (string), packages (optional string array), saveDev, saveExact, global, dryRun (optional booleans).
{ path: z.string().describe("Absolute path to the package directory"), packages: z.array(z.string()).optional().describe("Package names to install (empty = install all from package.json)"), saveDev: z.boolean().optional().describe("Save as devDependency"), saveExact: z.boolean().optional().describe("Save exact version instead of semver range"), global: z.boolean().optional().describe("Install globally"), dryRun: z.boolean().optional().describe("Preview install without making changes"), - src/index.ts:405-406 (registration)Registration of the 'install' tool on the primary server via server.tool().
server.tool( "install", - src/index.ts:1286-1293 (registration)Secondary (sandbox) registration of 'install' tool with a noop handler in createSandboxServer().
sandbox.tool("install", "Install packages in a project", { path: z.string().describe("Absolute path to the package directory"), packages: z.array(z.string()).optional().describe("Package names to install"), saveDev: z.boolean().optional().describe("Save as devDependency"), saveExact: z.boolean().optional().describe("Save exact version"), global: z.boolean().optional().describe("Install globally"), dryRun: z.boolean().optional().describe("Preview install"), }, noop); - src/index.ts:26-38 (helper)Helper function run() that executes npm commands via execFile with timeout, maxBuffer, and NO_COLOR env.
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); }