Skip to main content
Glama

shell_command

Execute terminal commands with configurable timeout for malware analysis, allowing background processing if the command exceeds the set duration.

Instructions

Execute a command in the terminal with timeout. Command will continue running in background if it doesn't complete within timeout.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesThe command to execute in the terminal
timeout_msNoOptional timeout in milliseconds (default: 30000)

Implementation Reference

  • Core handler function that spawns the shell command using child_process.spawn with shell: true, captures stdout/stderr, handles timeout and process exit, manages sessions for active/completed processes, and returns result with pid, output, and blocked status.
    async shellCommand(command, timeoutMs = DEF_TIMEOUT) { const process = spawn(command, [], { shell: true }); let output = ''; // Ensure process.pid is defined before proceeding if (!process.pid) { return { pid: -1, // Use -1 to indicate an error state output: 'Error: Failed to get process ID. The command could not be executed.', isBlocked: false }; } // Create a session object to track this process const session = { pid: process.pid, process, lastOutput: '', isBlocked: false, startTime: new Date() }; this.sessions.set(process.pid, session); return new Promise((resolve) => { // Handle standard output process.stdout.on('data', (data) => { const text = data.toString(); output += text; session.lastOutput += text; }); // Handle error output process.stderr.on('data', (data) => { const text = data.toString(); output += text; session.lastOutput += text; }); // Set timeout to mark process as blocked if it exceeds timeoutMs setTimeout(() => { session.isBlocked = true; resolve({ pid: process.pid, output, isBlocked: true }); }, timeoutMs); // Handle process completion process.on('exit', (code) => { if (process.pid) { // Store completed session before removing active session this.completedSessions.set(process.pid, { pid: process.pid, output: output + session.lastOutput, // Combine all output exitCode: code, startTime: session.startTime, endTime: new Date() }); // Keep only last 50 completed sessions if (this.completedSessions.size > 50) { const oldestKey = Array.from(this.completedSessions.keys())[0]; this.completedSessions.delete(oldestKey); } this.sessions.delete(process.pid); } resolve({ pid: process.pid, output, isBlocked: false }); }); }); }
  • MCP tool dispatch handler for 'shell_command' that validates input arguments, calls TerminalManager.shellCommand, and returns the result in MCP format.
    case 'shell_command': try { // Type-check and validate arguments if (!args || typeof args.command !== 'string') { return { content: [{ type: "text", text: "Error: Invalid command parameter" }], isError: true, }; } console.error(`Executing command: ${args.command}`); const result = await terminalManager.shellCommand( args.command, typeof args.timeout_ms === 'number' ? args.timeout_ms : undefined ); console.error(`Command executed with PID: ${result.pid}, blocked: ${result.isBlocked}`); return { content: [{ type: "text", text: JSON.stringify(result) }], }; } catch (error) { console.error('Error executing command:', error); return { content: [{ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true, }; }
  • Zod schema defining the input parameters for the shell_command tool: required 'command' string and optional 'timeout_ms' number.
    const shellCommandSchema = z.object({ command: z.string().min(1).describe("The command to execute in the terminal"), timeout_ms: z.number().optional().describe("Optional timeout in milliseconds (default: 30000)") });
  • serverMCP.js:100-104 (registration)
    Registration of the 'shell_command' tool in the listTools response, including name, description, and input schema.
    { name: 'shell_command', description: 'Execute a command in the terminal with timeout. Command will continue running in background if it doesn\'t complete within timeout.', inputSchema: zodToJsonSchema(shellCommandSchema), },

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/abdessamad-elamrani/MalwareAnalyzerMCP'

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