Skip to main content
Glama

npmInstall

Install npm dependencies in a specified directory using npm install command to manage project packages.

Instructions

執行npm install命令安裝依賴

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathNo
optionsNo

Implementation Reference

  • 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() },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GonTwVn/GonMCPtool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server