init
Create a new package.json file in a specified directory, optionally with a scope. Sets up the foundation for npm package configuration.
Instructions
Initialize a new package.json
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path to the directory | |
| scope | No | Scope for the package (e.g. @myorg) |
Implementation Reference
- src/index.ts:309-321 (handler)The actual handler function for the 'init' tool. It runs `npm init -y` in the specified directory with an optional scope argument, returning stdout or an error message.
async ({ path, scope }) => { const args = ["init", "-y"]; if (scope) args.push("--scope", scope); 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:305-308 (schema)Input schema for the 'init' tool: 'path' (required string) and 'scope' (optional string).
{ path: z.string().describe("Absolute path to the directory"), scope: z.string().optional().describe("Scope for the package (e.g. @myorg)"), }, - src/index.ts:301-322 (registration)Registration of the 'init' tool on the MCP server via server.tool('init', ...) with description, schema, and handler.
// ── npm init ── server.tool( "init", "Initialize a new package.json", { path: z.string().describe("Absolute path to the directory"), scope: z.string().optional().describe("Scope for the package (e.g. @myorg)"), }, async ({ path, scope }) => { const args = ["init", "-y"]; if (scope) args.push("--scope", scope); 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:26-38 (helper)The 'run' helper function that executes npm commands, used by the 'init' 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:1259-1262 (registration)Sandbox registration of the 'init' tool (noop handler) in the createSandboxServer function.
sandbox.tool("init", "Initialize a new package.json", { path: z.string().describe("Absolute path to the directory"), scope: z.string().optional().describe("Scope for the package"), }, noop);