Skip to main content
Glama
gunjanjp

Linux Bash MCP Server

by gunjanjp

execute_bash_command

Execute bash commands directly in a WSL2 Linux environment on Windows. Specify the command, working directory, and optional timeout for efficient command execution.

Instructions

Execute a bash command in WSL2 Linux environment

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesThe bash command to execute
timeoutNoTimeout in milliseconds (optional, uses config default)
workingDirectoryNoWorking directory for the command (optional, defaults to current directory)

Implementation Reference

  • Full implementation of the execute_bash_command tool handler. Parses arguments, constructs WSL bash command, executes it using child_process.execAsync, handles success and error cases, and returns structured JSON response with stdout, stderr, and metadata.
    async executeBashCommand(args) { const { command, workingDirectory = ".", timeout = this.config?.defaultTimeout || 30000 } = args; console.error(`[DEBUG] Executing command: ${command}`); if (!command || typeof command !== "string") { throw new Error("Command is required and must be a string"); } if (!this.wslDistribution) { throw new Error("WSL distribution not configured"); } try { // Construct WSL command const wslCommand = `wsl -d ${this.wslDistribution} -- bash -c "cd '${workingDirectory}' && ${command}"`; console.error(`[DEBUG] WSL command: ${wslCommand}`); const { stdout, stderr } = await execAsync(wslCommand, { timeout, maxBuffer: this.config?.maxBufferSize || 10 * 1024 * 1024, }); console.error(`[DEBUG] Command executed successfully`); return { content: [ { type: "text", text: JSON.stringify({ success: true, command: command, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, stdout: stdout || "", stderr: stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } catch (error) { console.error(`[ERROR] Command execution failed:`, error); return { content: [ { type: "text", text: JSON.stringify({ success: false, command: command, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, error: error.message, stdout: error.stdout || "", stderr: error.stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } }
  • Input schema for the execute_bash_command tool, defining the expected parameters: command (required string), workingDirectory (optional string), timeout (optional number).
    inputSchema: { type: "object", properties: { command: { type: "string", description: "The bash command to execute", }, workingDirectory: { type: "string", description: "Working directory for the command (optional, defaults to current directory)", }, timeout: { type: "number", description: "Timeout in milliseconds (optional, uses config default)", } }, required: ["command"], },
  • src/index.js:165-186 (registration)
    Tool registration in the ListTools response, including name, description, and input schema.
    { name: "execute_bash_command", description: "Execute a bash command in WSL2 Linux environment", inputSchema: { type: "object", properties: { command: { type: "string", description: "The bash command to execute", }, workingDirectory: { type: "string", description: "Working directory for the command (optional, defaults to current directory)", }, timeout: { type: "number", description: "Timeout in milliseconds (optional, uses config default)", } }, required: ["command"], }, },
  • src/index.js:282-283 (registration)
    Dispatcher case in the CallToolRequest handler that routes to the executeBashCommand method.
    case "execute_bash_command": return await this.executeBashCommand(args);

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/gunjanjp/linuxshell-mcp'

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