Skip to main content
Glama
A-Niranjan

MCP Filesystem Server

by A-Niranjan

bash_execute

Execute and capture Bash commands with defined working directories, environment variables, and time limits. Provides secure access to Bash functionality within MCP Filesystem Server's controlled environment.

Instructions

Execute a Bash command directly with output capture. More flexible than execute_command but still with security restrictions. Allows for direct access to Bash functionality.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesThe bash command to execute
envNoAdditional environment variables for the command
timeoutNoMaximum execution time in milliseconds (max 60s)
workingDirNoWorking directory for command execution

Implementation Reference

  • src/index.ts:356-361 (registration)
    Registration of the 'bash_execute' tool in the listTools response, including name, description, and input schema reference.
    name: 'bash_execute', description: 'Execute a Bash command directly with output capture. ' + 'More flexible than execute_command but still with security restrictions. ' + 'Allows for direct access to Bash functionality.', inputSchema: zodToJsonSchema(BashExecuteArgsSchema) as ToolInput,
  • src/index.ts:757-758 (registration)
    Registration/dispatch in the main tool call switch statement that routes 'bash_execute' calls to handleBashExecute.
    case 'bash_execute': { return await handleBashExecute(a, config)
  • Top-level handler for bash_execute tool calls: parses args, executes core logic, formats output, handles metrics and errors.
    export async function handleBashExecute(args: any, config: Config) { const endMetric = metrics.startOperation('bash_execute') try { // Validate arguments const parsed = BashExecuteArgsSchema.safeParse(args) if (!parsed.success) { throw new FileSystemError(`Invalid arguments for bash_execute`, 'INVALID_ARGS', undefined, { errors: parsed.error.format(), }) } // Execute the command const result = await bashExecute(parsed.data, config) // Format the response const formattedResponse = formatCommandResult(result, parsed.data.command) await logger.debug(`Bash command executed: ${args.command}`, { exitCode: result.exitCode, }) endMetric() return { content: [ { type: 'text', text: formattedResponse, }, ], } } catch (error) { metrics.recordError('bash_execute') if (error instanceof FileSystemError) { await logger.error(`Error in bash_execute:`, error.toJSON()) return { content: [{ type: 'text', text: `Error: ${error.message}` }], isError: true, } } const errorMessage = error instanceof Error ? error.message : String(error) await logger.error(`Unexpected error in bash_execute:`, { error }) return { content: [{ type: 'text', text: `Error: ${errorMessage}` }], isError: true, } }
  • Zod schema for validating input arguments to the bash_execute tool.
    export const BashExecuteArgsSchema = z.object({ command: z.string().describe('The bash command to execute'), workingDir: z.string().optional().describe('Working directory for command execution'), timeout: z .number() .int() .positive() .max(60000) .optional() .default(30000) .describe('Maximum execution time in milliseconds (max 60s)'), env: z.record(z.string()).optional().describe('Additional environment variables for the command'), })
  • Core bash command execution logic: safety validation, child_process.exec execution, output capture and error handling.
    export async function bashExecute( args: z.infer<typeof BashExecuteArgsSchema>, config: Config ): Promise<{ stdout: string; stderr: string; exitCode: number }> { // Validate the command validateCommand(args.command) // Validate working directory if provided // Initialize cwd with the current directory and possibly override it const cwd = args.workingDir ? await validatePath(args.workingDir, config) : process.cwd() // Prepare execution options const options = { cwd, timeout: args.timeout || 30000, env: args.env ? { ...process.env, ...args.env } : process.env, encoding: 'utf8' as const, maxBuffer: 10 * 1024 * 1024, // 10MB buffer for output } try { await logger.debug(`Executing bash command: ${args.command}`, { workingDir: cwd, timeout: options.timeout, }) // Execute the command const { stdout, stderr } = await execPromise(args.command, options) await logger.debug(`Command executed successfully: ${args.command}`, { exitCode: 0, stdoutPreview: stdout.substring(0, 100) + (stdout.length > 100 ? '...' : ''), }) return { stdout, stderr, exitCode: 0, } } catch (error: any) { // Handle command execution errors const stderr = error.stderr || '' const stdout = error.stdout || '' const exitCode = error.code || 1 await logger.warn(`Command execution failed: ${args.command}`, { exitCode, stderr: stderr.substring(0, 100) + (stderr.length > 100 ? '...' : ''), }) return { stdout, stderr, exitCode, } }

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/A-Niranjan/mcp-filesystem'

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