install-package
Adds npm packages to a React project via CLI, specifying package name, directory, and dependency type. Simplifies dependency management for React app development.
Instructions
Install a npm package in a project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dev | No | Whether to install as a dev dependency | |
| directory | No | Directory of the project (defaults to current directory) | |
| packageName | Yes | Name of the package to install (can include version) |
Implementation Reference
- index.js:372-413 (handler)The main execution logic for the 'install-package' tool. It destructures input params, validates packageName, determines working directory, checks for directory and package.json existence, builds npm install command based on dev flag, starts the process using startProcess helper, and returns success message with processId or error.async function handleInstallPackage(params) { try { const { packageName, directory, dev } = params; if (!packageName) { throw new Error("Package name is required"); } // Determine directory const workingDir = directory || process.cwd(); // Check if directory exists if (!fs.existsSync(workingDir)) { throw new Error(`Directory ${workingDir} does not exist`); } // Check if package.json exists const packageJsonPath = path.join(workingDir, "package.json"); if (!fs.existsSync(packageJsonPath)) { throw new Error( `Not a valid Node.js project: package.json not found in ${workingDir}` ); } // Install the package const installCommand = dev ? `npm install ${packageName} --save-dev` : `npm install ${packageName}`; const processId = startProcess(installCommand, [], workingDir); return { message: `Installing ${packageName} in ${workingDir}`, processId: processId, command: installCommand, }; } catch (error) { return { error: `Error installing package: ${error.message}`, }; } }
- index.js:461-465 (schema)Zod schema used for input validation in the install-package tool handler.const InstallPackageSchema = z.object({ packageName: z.string(), directory: z.string().optional(), dev: z.boolean().optional(), });
- index.js:597-619 (registration)Tool registration entry in the list_tools handler response, defining name, description, and input schema advertised to MCP clients.name: "install-package", description: "Install a npm package in a project", inputSchema: { type: "object", properties: { packageName: { type: "string", description: "Name of the package to install (can include version)", }, directory: { type: "string", description: "Directory of the project (defaults to current directory)", }, dev: { type: "boolean", description: "Whether to install as a dev dependency", }, }, required: ["packageName"], }, },
- index.js:674-677 (registration)Dispatch case in the CallToolRequestHandler switch statement that routes 'install-package' calls to the handler function after schema validation.case "install-package": const installArgs = InstallPackageSchema.parse(args); result = await handleInstallPackage(installArgs); break;