Skip to main content
Glama

ssh_connect

Establish SSH connections to VPS servers using password authentication or private keys for remote management and initialization tasks.

Instructions

Connect to a VPS via SSH using password or private key

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostYesVPS IP address or hostname
portNoSSH port (default: 22)
usernameYesSSH username
passwordNoSSH password (optional)
privateKeyPathNoPrivate key file path (optional)
passphraseNoPrivate key passphrase (optional)

Implementation Reference

  • Main handler function for the 'ssh_connect' tool. Parses input arguments with SSHConfigSchema, instantiates SSHService with the config, calls connect(), initializes other services if successful, and returns a success message or throws on failure.
    private async handleSSHConnect(
      args: unknown
    ): Promise<{ content: Array<{ type: 'text'; text: string }> }> {
      const parsedConfig = SSHConfigSchema.parse(args);
      const config: SSHConfig = {
        host: parsedConfig.host,
        port: parsedConfig.port,
        username: parsedConfig.username,
        password: parsedConfig.password || undefined,
        privateKeyPath: parsedConfig.privateKeyPath || undefined,
        passphrase: parsedConfig.passphrase || undefined,
      };
    
      this.sshService = new SSHService(config);
      const connected = await this.sshService.connect();
    
      if (connected) {
        // Initialize other services
        this.vpsInitializer = new VPSInitializer(this.sshService);
        this.nginxManager = new NginxManager(this.sshService);
        this.githubCICD = new GitHubCICD(this.sshService);
    
        return {
          content: [
            {
              type: 'text',
              text: `Successfully connected to ${config.host} as ${config.username}`,
            },
          ],
        };
      } else {
        throw new Error('Failed to establish SSH connection');
      }
    }
  • Zod schema used for input validation in the ssh_connect handler.
    const SSHConfigSchema = z.object({
      host: z.string().describe('The IP address or hostname of the VPS'),
      port: z.number().optional().default(22).describe('SSH port (default: 22)'),
      username: z.string().describe('SSH username'),
      password: z.string().optional().describe('SSH password (if not using key)'),
      privateKeyPath: z.string().optional().describe('Path to private key file'),
      passphrase: z.string().optional().describe('Passphrase for private key'),
    });
  • Tool registration in the MCP server's listTools handler, defining name, description, and input schema for ssh_connect.
    {
      name: 'ssh_connect',
      description: 'Connect to a VPS via SSH using password or private key',
      inputSchema: {
        type: 'object',
        properties: {
          host: { type: 'string', description: 'VPS IP address or hostname' },
          port: { type: 'number', description: 'SSH port (default: 22)' },
          username: { type: 'string', description: 'SSH username' },
          password: { type: 'string', description: 'SSH password (optional)' },
          privateKeyPath: { type: 'string', description: 'Private key file path (optional)' },
          passphrase: { type: 'string', description: 'Private key passphrase (optional)' },
        },
        required: ['host', 'username'],
      },
    },
  • Supporting method in SSHService class that performs the actual SSH connection using the node-ssh library, supporting both password and private key (with optional passphrase) authentication.
    async connect(): Promise<boolean> {
      try {
        const connectionConfig: Parameters<NodeSSH['connect']>[0] = {
          host: this.config.host,
          port: this.config.port || 22,
          username: this.config.username,
        };
    
        // Handle authentication methods
        if (this.config.privateKeyPath) {
          logger.info('Authenticating with private key', {
            keyPath: this.config.privateKeyPath,
          });
          connectionConfig.privateKey = readFileSync(this.config.privateKeyPath, 'utf8');
          if (this.config.passphrase) {
            connectionConfig.passphrase = this.config.passphrase;
          }
        } else if (this.config.password) {
          logger.info('Authenticating with password');
          connectionConfig.password = this.config.password;
        } else {
          throw new Error('Either password or privateKeyPath must be provided');
        }
    
        await this.ssh.connect(connectionConfig);
        this.isConnected = true;
        logger.info('Successfully connected to SSH server', {
          host: this.config.host,
          username: this.config.username,
        });
        return true;
      } catch (error) {
        logger.error('Failed to connect to SSH server', {
          error: error instanceof Error ? error.message : 'Unknown error',
          host: this.config.host,
        });
        this.isConnected = false;
        return false;
      }
    }

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/oxy-Op/DevPilot'

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