Skip to main content
Glama
gunjanjp

Linux Bash MCP Server

by gunjanjp

execute_bash_script

Run bash script files in WSL2 Linux environments with optional arguments, working directory, and timeout settings for automated Linux operations.

Instructions

Execute a bash script file in WSL2 Linux environment

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptPathYesPath to the bash script file
argsNoArguments to pass to the script (optional)
workingDirectoryNoWorking directory for the script (optional)
timeoutNoTimeout in milliseconds (optional, uses config default)

Implementation Reference

  • The core handler function that executes the specified bash script in the WSL Linux environment. It constructs a WSL command, handles arguments escaping, executes via execAsync, and returns structured output with success status, stdout, stderr.
    async executeBashScript(args) { const { scriptPath, args: scriptArgs = [], workingDirectory = ".", timeout = this.config?.scriptTimeout || 60000 } = args; if (!scriptPath || typeof scriptPath !== "string") { throw new Error("Script path is required and must be a string"); } if (!this.wslDistribution) { throw new Error("WSL distribution not configured"); } try { // Prepare arguments string const argsString = scriptArgs.map(arg => `'${arg.replace(/'/g, "'\"'\"'")}'`).join(' '); // Construct WSL command const wslCommand = `wsl -d ${this.wslDistribution} -- bash -c "cd '${workingDirectory}' && bash '${scriptPath}' ${argsString}"`; console.error(`[DEBUG] Executing script: ${wslCommand}`); const { stdout, stderr } = await execAsync(wslCommand, { timeout, maxBuffer: this.config?.maxBufferSize || 10 * 1024 * 1024, }); return { content: [ { type: "text", text: JSON.stringify({ success: true, scriptPath: scriptPath, args: scriptArgs, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, stdout: stdout || "", stderr: stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } catch (error) { return { content: [ { type: "text", text: JSON.stringify({ success: false, scriptPath: scriptPath, args: scriptArgs, workingDirectory: workingDirectory, wslDistribution: this.wslDistribution, error: error.message, stdout: error.stdout || "", stderr: error.stderr || "", timestamp: new Date().toISOString() }, null, 2), }, ], }; } }
  • Defines the tool's metadata, description, and input schema specification for validation in the ListTools response.
    { name: "execute_bash_script", description: "Execute a bash script file in WSL2 Linux environment", inputSchema: { type: "object", properties: { scriptPath: { type: "string", description: "Path to the bash script file", }, args: { type: "array", items: { type: "string" }, description: "Arguments to pass to the script (optional)", }, workingDirectory: { type: "string", description: "Working directory for the script (optional)", }, timeout: { type: "number", description: "Timeout in milliseconds (optional, uses config default)", } }, required: ["scriptPath"], },
  • src/index.js:284-285 (registration)
    Maps the tool name to its handler function in the CallToolRequestSchema switch dispatcher.
    case "execute_bash_script": return await this.executeBashScript(args);

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