Skip to main content
Glama
bvisible

MCP SSH Manager

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
NameRequiredDescriptionDefault
sessionYesSession ID from ssh_session_start
commandYesCommand to execute in the session
timeoutNoCommand timeout in milliseconds (default: 30000)

Implementation Reference

  • 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'
    ],
  • 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;
      }
    }
  • 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;
    }

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

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