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
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | Command to execute | |
| connectionId | Yes | ID of an active SSH connection | |
| cwd | No | Working directory for the command | |
| timeout | No | Command 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
- src/index.ts:383-450 (handler)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 }; } }
- src/index.ts:74-98 (schema)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);
- src/index.ts:204-215 (schema)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'] }