Skip to main content
Glama

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
NameRequiredDescriptionDefault
commandYesThe shell command to execute
hostAliasYesAlias or hostname of the SSH host

Implementation Reference

  • 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(); } }
  • 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; }
  • 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)}`); } }
  • 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 }

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/AiondaDotCom/mcp-ssh'

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