ssh_session_send
Execute commands in active SSH sessions to manage remote servers, automate tasks, and control deployments through the MCP SSH Manager.
Instructions
Send a command to an existing SSH session
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session | Yes | Session ID from ssh_session_start | |
| command | Yes | Command to execute in the session | |
| timeout | No | Command timeout in milliseconds (default: 30000) |
Implementation Reference
- src/tool-registry.js:23-28 (registration)The 'ssh_session_send' tool is registered/listed in the 'sessions' group within the central tool registry manifest used for conditional registration and validation.sessions: [ 'ssh_session_start', 'ssh_session_send', 'ssh_session_list', 'ssh_session_close' ],
- src/session-manager.js:151-226 (helper)Core implementation for executing/sending commands in a persistent SSH session. This is the primary logic behind the ssh_session_send tool: sends command via shell.write, waits for prompt, parses output, handles state/context.async execute(command, options = {}) { if (this.state !== SESSION_STATES.READY) { throw new Error(`Session ${this.id} is not ready (state: ${this.state})`); } this.state = SESSION_STATES.BUSY; this.lastActivity = new Date(); try { // Clear buffers this.outputBuffer = ''; this.errorBuffer = ''; // Add to history unless silent if (!options.silent) { this.context.history.push({ command, timestamp: new Date(), cwd: this.context.cwd }); logger.info(`Session ${this.id} executing`, { command: command.substring(0, 100), server: this.serverName }); } // Send command this.shell.write(command + '\n'); // Wait for command to complete await this.waitForPrompt(options.timeout || 30000); // Parse output (remove command echo and prompt) let output = this.outputBuffer; // Remove the command echo (first line) const lines = output.split('\n'); if (lines[0].includes(command)) { lines.shift(); } // Remove the prompt (last line) const lastLine = lines[lines.length - 1]; if (lastLine.match(/[$#>]\s*$/)) { lines.pop(); } output = lines.join('\n').trim(); // Check for command success (basic heuristic) const success = !this.errorBuffer && !output.includes('command not found'); // Update context if command might have changed it if (command.startsWith('cd ') || command.startsWith('export ')) { await this.updateContext(); } this.state = SESSION_STATES.READY; return { success, output, error: this.errorBuffer, session: this.id }; } catch (error) { this.state = SESSION_STATES.ERROR; logger.error(`Session ${this.id} execution failed`, { command, error: error.message }); throw error; } }
- src/session-manager.js:314-326 (helper)Retrieves an active SSH session by ID, which would be called by the ssh_session_send handler to access the session before sending a command.export function getSession(sessionId) { const session = sessions.get(sessionId); if (!session) { throw new Error(`Session ${sessionId} not found`); } if (session.state === SESSION_STATES.CLOSED) { throw new Error(`Session ${sessionId} is closed`); } return session; }