npmInstall
Run npm install commands to install dependencies for Node.js projects, specifying paths and options via GonMCPtool's MCP server.
Instructions
執行npm install命令安裝依賴
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | ||
| path | No |
Implementation Reference
- tools/npmInstallTool.ts:16-39 (handler)The core handler function that executes the npm install command using child_process.exec after changing to the specified path.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)The MCP server.tool registration for 'npmInstall', including input schema with zod and wrapper that calls the handler.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 optional path and options parameters for the npmInstall tool.{ path: z.string().optional(), options: z.string().optional() },
- tools/npmInstallTool.ts:9-40 (helper)The NpmInstallTool class containing the static executeInstall method used by the registration.export class NpmInstallTool { /** * 執行npm install指令 * @param path 要執行install的專案路徑,默認為當前路徑 * @param options 額外的npm指令選項,例如 --save-dev 或 --production * @returns 包含stdout和stderr的Promise */ 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; } } }