runCommandBatch
Execute multiple shell commands sequentially on an SSH host to automate remote administration tasks through the MCP SSH Agent.
Instructions
Executes multiple shell commands sequentially on an SSH host
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hostAlias | Yes | Alias or hostname of the SSH host | |
| commands | Yes | List of shell commands to execute |
Implementation Reference
- src/ssh-client.ts:122-161 (handler)The primary handler function implementing the 'runCommandBatch' tool. Connects to an SSH host via alias, executes each command in the provided array sequentially, collects stdout, stderr, and exit code for each, computes overall success, and returns structured results. Handles connection and execution errors gracefully.async runCommandBatch(hostAlias: string, commands: string[]): Promise<BatchCommandResult> { try { await this.connectToHost(hostAlias); const results: CommandResult[] = []; let success = true; for (const command of commands) { const result = await this.ssh.execCommand(command); const cmdResult: CommandResult = { stdout: result.stdout, stderr: result.stderr, code: result.code || 0 }; results.push(cmdResult); if (cmdResult.code !== 0) { success = false; // We don't abort, execute all commands } } this.ssh.dispose(); return { results, success }; } catch (error) { console.error(`Error during batch execution on ${hostAlias}:`, error); return { results: [{ stdout: '', stderr: error instanceof Error ? error.message : String(error), code: 1 }], success: false }; } }
- src/types.ts:24-28 (schema)Output schema/type for the runCommandBatch tool: array of individual command results and boolean indicating if all commands succeeded.// Batch result of remote commands export interface BatchCommandResult { results: CommandResult[]; success: boolean; }
- src/types.ts:11-16 (schema)Schema for individual command results used within BatchCommandResult output of runCommandBatch.// Result of a remote command export interface CommandResult { stdout: string; stderr: string; code: number; }
- src/ssh-client.ts:166-187 (helper)Helper function called by runCommandBatch to resolve host alias to connection details from SSH config and establish the NodeSSH connection.private async connectToHost(hostAlias: string): Promise<void> { // Get host information const hostInfo = await this.getHostInfo(hostAlias); if (!hostInfo) { throw new Error(`Host ${hostAlias} not found`); } // Create connection configuration const connectionConfig = { host: hostInfo.hostname, username: hostInfo.user, port: hostInfo.port || 22, privateKeyPath: hostInfo.identityFile }; try { await this.ssh.connect(connectionConfig); } catch (error) { throw new Error(`Connection to ${hostAlias} failed: ${error instanceof Error ? error.message : String(error)}`); } }