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
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | The bash command to execute | |
| timeout | No | Timeout in milliseconds (optional, uses config default) | |
| workingDirectory | No | Working directory for the command (optional, defaults to current directory) |
Implementation Reference
- src/index.js:311-377 (handler)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), }, ], }; } }
- src/index.js:168-185 (schema)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);