npmInstall
Install npm dependencies in a specified directory using npm install command to manage project packages.
Instructions
執行npm install命令安裝依賴
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "npmInstall"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | ||
| options | No |
Implementation Reference
- tools/npmInstallTool.ts:16-39 (handler)The static method implementing the core logic of the npmInstall tool, executing 'npm install' in the specified directory with optional flags using promisified child_process.exec.
static async executeInstall(path: string = '.', options: string = ''): Promise<{ stdout: string; stderr: string }> { try { console.log(`正在執行 npm install ${options} 在路徑 ${path}`); // 構建完整指令 const command = `cd ${path} && npm install ${options}`; // 執行指令 const { stdout, stderr } = await execPromise(command); if (stdout) { console.log('Install輸出:', stdout); } if (stderr && !stderr.includes('npm notice')) { console.error('Install錯誤:', stderr); } return { stdout, stderr }; } catch (error) { console.error('Install執行失敗:', error); throw error; } } - main.ts:51-66 (registration)Registers the 'npmInstall' tool with the MCP server, including input schema definition using Zod and the async handler that calls NpmInstallTool.executeInstall.
server.tool("npmInstall", "執行npm install命令安裝依賴", { path: z.string().optional(), options: z.string().optional() }, async ({ path = ".", options = "" }) => { try { const result = await NpmInstallTool.executeInstall(path, options); return { content: [{ type: "text", text: `Dependencies installed successfully: ${result.stdout}` }] }; } catch (error) { return { content: [{ type: "text", text: `Installation failed: ${error instanceof Error ? error.message : "Unknown error"}` }] }; } } ); - main.ts:53-53 (schema)Zod schema defining the input parameters for the npmInstall tool: optional path (defaults to '.') and options.
{ path: z.string().optional(), options: z.string().optional() },