runRemoteCommand
Execute shell commands on remote SSH hosts securely via MCP SSH Agent, using host aliases and specified commands for efficient remote management.
Instructions
Executes a shell command on an SSH host
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | The shell command to execute | |
| hostAlias | Yes | Alias or hostname of the SSH host |
Implementation Reference
- src/ssh-client.ts:24-47 (handler)The primary handler function implementing the 'runRemoteCommand' tool logic. It connects to an SSH host using the provided alias, executes the command, and returns a CommandResult with stdout, stderr, and exit code. Handles connection and execution errors gracefully.async runRemoteCommand(hostAlias: string, command: string): Promise<CommandResult> { try { // First connect to the host await this.connectToHost(hostAlias); // Execute the command const result = await this.ssh.execCommand(command); return { stdout: result.stdout, stderr: result.stderr, code: result.code || 0 }; } catch (error) { console.error(`Error executing command on ${hostAlias}:`, error); return { stdout: '', stderr: error instanceof Error ? error.message : String(error), code: 1 }; } finally { this.ssh.dispose(); } }
- src/types.ts:12-16 (schema)TypeScript interface defining the output schema for the runRemoteCommand tool, specifying stdout, stderr, and exit code.export interface CommandResult { stdout: string; stderr: string; code: number; }
- src/ssh-client.ts:166-187 (helper)Private helper method used by runRemoteCommand to establish SSH connection to the host based on alias, parsing config and connecting via node-ssh.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)}`); } }
- src/types.ts:2-9 (schema)TypeScript interface for SSHHostInfo, used internally by the handler to resolve host details from SSH config.export interface SSHHostInfo { hostname: string; alias?: string; user?: string; port?: number; identityFile?: string; [key: string]: any; // For other configuration options }