Skip to main content
Glama

ssh_exec

Execute commands on remote servers via SSH by specifying connection ID, command, working directory, and timeout for streamlined remote task management.

Instructions

Execute a command on the remote server

Input Schema

NameRequiredDescriptionDefault
commandYesCommand to execute
connectionIdYesID of an active SSH connection
cwdNoWorking directory for the command
timeoutNoCommand timeout in milliseconds

Input Schema (JSON Schema)

{ "properties": { "command": { "description": "Command to execute", "type": "string" }, "connectionId": { "description": "ID of an active SSH connection", "type": "string" }, "cwd": { "description": "Working directory for the command", "type": "string" }, "timeout": { "description": "Command timeout in milliseconds", "type": "number" } }, "required": [ "connectionId", "command" ], "type": "object" }

Implementation Reference

  • The core handler function for the 'ssh_exec' tool. It retrieves the SSH connection by ID, executes the specified command with optional cwd and timeout, captures stdout/stderr, and returns the result including exit code.
    private async handleSSHExec(params: any) { const { connectionId, command, cwd, timeout = 60000 } = params; // Check if the connection exists if (!this.connections.has(connectionId)) { return { content: [{ type: "text", text: `No active SSH connection with ID: ${connectionId}` }], isError: true }; } const { conn } = this.connections.get(connectionId)!; // Execute the command try { const result: any = await new Promise((resolve, reject) => { const execOptions: any = {}; if (cwd) execOptions.cwd = cwd; // Set up timeout const timeoutId = setTimeout(() => { reject(new Error(`Command execution timed out after ${timeout}ms`)); }, timeout); conn.exec(command, execOptions, (err: Error | undefined, stream: any) => { if (err) { clearTimeout(timeoutId); return reject(new Error(`Failed to execute command: ${err.message}`)); } let stdout = ''; let stderr = ''; stream.on('close', (code: number, signal: string) => { clearTimeout(timeoutId); resolve({ code, signal, stdout: stdout.trim(), stderr: stderr.trim() }); }); stream.on('data', (data: Buffer) => { stdout += data.toString(); }); stream.stderr.on('data', (data: Buffer) => { stderr += data.toString(); }); }); }); const output = result.stdout || result.stderr || '(no output)'; return { content: [{ type: "text", text: `Command: ${command}\nExit code: ${result.code}\nOutput:\n${output}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `Command execution failed: ${error.message}` }], isError: true }; } }
  • The input schema definition for the 'ssh_exec' tool, declared in the MCP server capabilities. Defines parameters: connectionId (required), command (required), cwd, timeout.
    ssh_exec: { description: "Execute a command on the remote server", inputSchema: { type: "object", properties: { connectionId: { type: "string", description: "ID of an active SSH connection" }, command: { type: "string", description: "Command to execute" }, cwd: { type: "string", description: "Working directory for the command" }, timeout: { type: "number", description: "Command timeout in milliseconds" } }, required: ["connectionId", "command"] } },
  • src/index.ts:278-279 (registration)
    Registration of the 'ssh_exec' tool handler in the CallToolRequestSchema switch statement. Dispatches tool calls to the handleSSHExec method.
    case 'ssh_exec': return this.handleSSHExec(request.params.arguments);
  • Duplicate schema for 'ssh_exec' in the ListToolsRequestSchema handler response.
    name: 'ssh_exec', description: 'Execute a command on the remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'ID of an active SSH connection' }, command: { type: 'string', description: 'Command to execute' }, cwd: { type: 'string', description: 'Working directory for the command' }, timeout: { type: 'number', description: 'Command timeout in milliseconds' } }, required: ['connectionId', 'command'] }

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/mixelpixx/SSH-MCP'

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