Skip to main content
Glama

execute_command

Execute terminal commands on macOS securely via the Mac Shell MCP Server, using built-in whitelisting and approval mechanisms for controlled access.

Instructions

Execute a shell command on macOS

Input Schema

NameRequiredDescriptionDefault
argsNoCommand arguments
commandYesThe command to execute

Input Schema (JSON Schema)

{ "properties": { "args": { "description": "Command arguments", "items": { "type": "string" }, "type": "array" }, "command": { "description": "The command to execute", "type": "string" } }, "required": [ "command" ], "type": "object" }

Implementation Reference

  • src/index.ts:90-110 (registration)
    Registration of the 'execute_command' tool in the MCP server's listTools handler, defining its name, description, and input schema.
    { name: 'execute_command', description: 'Execute a shell command on macOS', inputSchema: { type: 'object', properties: { command: { type: 'string', description: 'The command to execute', }, args: { type: 'array', items: { type: 'string', }, description: 'Command arguments', }, }, required: ['command'], }, },
  • MCP tool handler for 'execute_command': validates input parameters using Zod schema and delegates to CommandService for execution, returning formatted MCP response.
    private async handleExecuteCommand(args: unknown) { const schema = z.object({ command: z.string(), args: z.array(z.string()).optional(), }); const { command, args: commandArgs = [] } = schema.parse(args); try { const result = await this.commandService.executeCommand(command, commandArgs); return { content: [ { type: 'text', text: result.stdout, }, { type: 'text', text: result.stderr ? `Error output: ${result.stderr}` : '', }, ], }; } catch (error) { if (error instanceof Error) { return { content: [ { type: 'text', text: `Command execution failed: ${error.message}`, }, ], isError: true, }; } throw error; } }
  • Zod schema for validating 'execute_command' tool arguments within the handler.
    const schema = z.object({ command: z.string(), args: z.array(z.string()).optional(), });
  • Core command execution logic: validates against whitelist, handles security levels (safe/approval/forbidden), executes safe commands directly or queues others for approval using child_process.execFile.
    public async executeCommand( command: string, args: string[] = [], options: { timeout?: number; requestedBy?: string; } = {}, ): Promise<CommandResult> { const securityLevel = this.validateCommand(command, args); // If command is not whitelisted, reject if (securityLevel === null) { throw new Error(`Command not whitelisted: ${command}`); } // If command is forbidden, reject if (securityLevel === CommandSecurityLevel.FORBIDDEN) { throw new Error(`Command is forbidden: ${command}`); } // If command requires approval, add to pending queue if (securityLevel === CommandSecurityLevel.REQUIRES_APPROVAL) { return this.queueCommandForApproval(command, args, options.requestedBy); } // For safe commands, execute immediately try { const timeout = options.timeout || this.defaultTimeout; const { stdout, stderr } = await execFileAsync(command, args, { timeout, shell: this.shell, }); return { stdout, stderr }; } catch (error) { if (error instanceof Error) { throw new Error(`Command execution failed: ${error.message}`); } throw error; } }

Other Tools

Related Tools

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/cfdude/mac-shell-mcp'

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