Skip to main content
Glama
gunjanjp

Linux Bash MCP Server

by gunjanjp

execute_bash_command

Execute bash commands in a WSL2 Linux environment from Claude Desktop to run scripts, manage files, and perform system operations on Windows.

Instructions

Execute a bash command in WSL2 Linux environment

Input Schema

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

Implementation Reference

  • The core handler function that destructures the input arguments, constructs a WSL bash command, executes it using execAsync, and returns a structured JSON response with success status, output, and error details if applicable.
    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), }, ], }; } }
  • Defines the JSON schema for tool inputs, specifying properties for command (required string), optional workingDirectory and timeout.
    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)
    Registers the tool in the ListTools response, providing 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)
    In the CallToolRequestSchema handler, routes requests for this tool to the executeBashCommand method.
    case "execute_bash_command": return await this.executeBashCommand(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