Skip to main content
Glama

executeCommand

Execute shell commands with automatic OS translation and project-specific overrides, adapting to different package managers and environments.

Instructions

Executes a command after applying project overrides and OS translation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
projectNameYesProject name used to select overrides
commandKeyYesLogical command key, e.g. install, run, test
argsNoExtra CLI args to append
optionsNo

Implementation Reference

  • src/server.ts:263-381 (registration)
    Registration of the 'executeCommand' tool using McpServer.registerTool, including schema and handler.
    server.registerTool( "executeCommand", { title: "Execute a project-aware command", description: "Executes a command after applying project overrides and OS translation", inputSchema: { projectName: z.string().describe("Project name used to select overrides"), commandKey: z.string().describe("Logical command key, e.g. install, run, test"), args: z.array(z.string()).optional().describe("Extra CLI args to append"), options: z.object({ shell: z.enum(["auto", "cmd", "powershell", "bash"]).optional(), activateVenv: z.enum(["auto", "on", "off"]).optional(), venvPath: z.string().optional(), cwd: z.string().optional(), env: z.record(z.string()).optional() }).optional() } }, async ({ projectName, commandKey, args, options }) => { const os = getOS(); const cmdMap = await loadJson<any>(COMMAND_MAP_PATH, { base: {} }); const projectCmd = await resolveProjectCommand(projectName, commandKey); const flavor = await detectProjectFlavor(); if (!projectCmd) { const result = { errorCode: "COMMAND_NOT_FOUND", message: `No command mapping found for key "${commandKey}"`, suggestion: undefined as string | undefined }; return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } const translated = translateCommandForOS(projectCmd, os, cmdMap); const fullBase = [translated, ...(args || []).map(quoteArg)].filter(Boolean).join(" "); const cwd = options?.cwd || process.cwd(); const selShell = (options?.shell || "auto") as "auto" | "cmd" | "powershell" | "bash"; const activatePref = (options?.activateVenv || "auto") as "auto" | "on" | "off"; function findVenv(base?: string) { const candidates = base ? [base] : [".venv", "venv", "env"]; for (const c of candidates) { const p = path.resolve(cwd, c); if (!fssync.existsSync(p)) continue; const posix = path.join(p, "bin", "activate"); const winCmd = path.join(p, "Scripts", "activate.bat"); const winPwsh = path.join(p, "Scripts", "Activate.ps1"); const out: any = { root: p }; if (fssync.existsSync(posix)) out.posix = posix; if (fssync.existsSync(winCmd)) out.winCmd = winCmd; if (fssync.existsSync(winPwsh)) out.winPwsh = winPwsh; if (out.posix || out.winCmd || out.winPwsh) return out; } return undefined; } const venvInfo = findVenv(options?.venvPath); const containsPy = /\b(python|pip|uvicorn|pytest|flask|django|poetry|pipenv)\b/i.test(fullBase); const shouldActivate = activatePref === "on" || (activatePref === "auto" && !!venvInfo && containsPy); function escapePwsh(s: string) { return s.replace(/`/g, "``").replace(/"/g, "`\""); } let final = fullBase; if (shouldActivate && venvInfo) { if (os === "windows") { if (selShell === "powershell") { const act = venvInfo.winPwsh || venvInfo.winCmd; if (act && venvInfo.winPwsh) { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "& { . '${venvInfo.winPwsh.replace(/\\/g, "/")}'; ${escapePwsh(fullBase)} }"`; } else if (act && venvInfo.winCmd) { final = `call \"${venvInfo.winCmd}\" && ${fullBase}`; } else { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "${escapePwsh(fullBase)}"`; } } else { if (venvInfo.winCmd) { final = `call "${venvInfo.winCmd}" && ${fullBase}`; } } } else { if (venvInfo.posix) { final = `. "${venvInfo.posix}" && ${fullBase}`; } } } else { if (os === "windows" && selShell === "powershell") { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "${escapePwsh(fullBase)}"`; } else if (os !== "windows" && selShell === "bash") { final = `/bin/bash -lc ${quoteArg(fullBase)}`; } } const run = await runShell(final); if (run.exitCode !== 0) { const suggestion = buildSuggestion(projectCmd, run.stderr, flavor, commandKey); const result = { errorCode: "COMMAND_FAILED", message: `Command failed with exit code ${run.exitCode}`, suggestion, resolvedCommand: final, stdout: run.stdout, stderr: run.stderr, exitCode: run.exitCode }; return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } const output = { stdout: run.stdout, stderr: run.stderr, exitCode: run.exitCode, resolvedCommand: final }; return { content: [{ type: "text", text: JSON.stringify(output, null, 2) }] }; } );
  • Input schema definition using Zod for validating tool inputs: projectName, commandKey, args, and options.
    inputSchema: { projectName: z.string().describe("Project name used to select overrides"), commandKey: z.string().describe("Logical command key, e.g. install, run, test"), args: z.array(z.string()).optional().describe("Extra CLI args to append"), options: z.object({ shell: z.enum(["auto", "cmd", "powershell", "bash"]).optional(), activateVenv: z.enum(["auto", "on", "off"]).optional(), venvPath: z.string().optional(), cwd: z.string().optional(), env: z.record(z.string()).optional() }).optional() }
  • Handler function that resolves project-specific commands, translates for OS, handles venv activation, executes via child_process.spawn, and returns structured output with suggestions on failure.
    async ({ projectName, commandKey, args, options }) => { const os = getOS(); const cmdMap = await loadJson<any>(COMMAND_MAP_PATH, { base: {} }); const projectCmd = await resolveProjectCommand(projectName, commandKey); const flavor = await detectProjectFlavor(); if (!projectCmd) { const result = { errorCode: "COMMAND_NOT_FOUND", message: `No command mapping found for key "${commandKey}"`, suggestion: undefined as string | undefined }; return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } const translated = translateCommandForOS(projectCmd, os, cmdMap); const fullBase = [translated, ...(args || []).map(quoteArg)].filter(Boolean).join(" "); const cwd = options?.cwd || process.cwd(); const selShell = (options?.shell || "auto") as "auto" | "cmd" | "powershell" | "bash"; const activatePref = (options?.activateVenv || "auto") as "auto" | "on" | "off"; function findVenv(base?: string) { const candidates = base ? [base] : [".venv", "venv", "env"]; for (const c of candidates) { const p = path.resolve(cwd, c); if (!fssync.existsSync(p)) continue; const posix = path.join(p, "bin", "activate"); const winCmd = path.join(p, "Scripts", "activate.bat"); const winPwsh = path.join(p, "Scripts", "Activate.ps1"); const out: any = { root: p }; if (fssync.existsSync(posix)) out.posix = posix; if (fssync.existsSync(winCmd)) out.winCmd = winCmd; if (fssync.existsSync(winPwsh)) out.winPwsh = winPwsh; if (out.posix || out.winCmd || out.winPwsh) return out; } return undefined; } const venvInfo = findVenv(options?.venvPath); const containsPy = /\b(python|pip|uvicorn|pytest|flask|django|poetry|pipenv)\b/i.test(fullBase); const shouldActivate = activatePref === "on" || (activatePref === "auto" && !!venvInfo && containsPy); function escapePwsh(s: string) { return s.replace(/`/g, "``").replace(/"/g, "`\""); } let final = fullBase; if (shouldActivate && venvInfo) { if (os === "windows") { if (selShell === "powershell") { const act = venvInfo.winPwsh || venvInfo.winCmd; if (act && venvInfo.winPwsh) { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "& { . '${venvInfo.winPwsh.replace(/\\/g, "/")}'; ${escapePwsh(fullBase)} }"`; } else if (act && venvInfo.winCmd) { final = `call \"${venvInfo.winCmd}\" && ${fullBase}`; } else { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "${escapePwsh(fullBase)}"`; } } else { if (venvInfo.winCmd) { final = `call "${venvInfo.winCmd}" && ${fullBase}`; } } } else { if (venvInfo.posix) { final = `. "${venvInfo.posix}" && ${fullBase}`; } } } else { if (os === "windows" && selShell === "powershell") { final = `powershell -NoProfile -ExecutionPolicy Bypass -Command "${escapePwsh(fullBase)}"`; } else if (os !== "windows" && selShell === "bash") { final = `/bin/bash -lc ${quoteArg(fullBase)}`; } } const run = await runShell(final); if (run.exitCode !== 0) { const suggestion = buildSuggestion(projectCmd, run.stderr, flavor, commandKey); const result = { errorCode: "COMMAND_FAILED", message: `Command failed with exit code ${run.exitCode}`, suggestion, resolvedCommand: final, stdout: run.stdout, stderr: run.stderr, exitCode: run.exitCode }; return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } const output = { stdout: run.stdout, stderr: run.stderr, exitCode: run.exitCode, resolvedCommand: final }; return { content: [{ type: "text", text: JSON.stringify(output, null, 2) }] }; }

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/mr-wolf-gb/smart-shell-mcp'

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