Skip to main content
Glama

ssh_execute

Execute commands on remote SSH servers to manage systems, run scripts, or perform administrative tasks through secure connections.

Instructions

Execute a command on a remote SSH server

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionIdYesSSH connection ID
commandYesCommand to execute on remote server
cwdNoWorking directory for command execution

Implementation Reference

  • The handler function for 'ssh_execute' tool. Parses input using ExecuteCommandSchema, retrieves SSH connection from pool, executes the command using ssh.execCommand(), and returns stdout, stderr, and exit code.
    private async handleSSHExecute(args: unknown) {
      const params = ExecuteCommandSchema.parse(args);
      
      const ssh = connectionPool.get(params.connectionId);
      if (!ssh) {
        throw new McpError(
          ErrorCode.InvalidParams,
          `Connection ID '${params.connectionId}' not found`
        );
      }
    
      try {
        const result = await ssh.execCommand(params.command, {
          cwd: params.cwd,
        });
    
        return {
          content: [
            {
              type: 'text',
              text: `Command: ${params.command}\nExit Code: ${result.code}\n\nSTDOUT:\n${result.stdout}\n\nSTDERR:\n${result.stderr}`,
            },
          ],
        };
      } catch (error) {
        throw new McpError(
          ErrorCode.InternalError,
          `Command execution failed: ${error instanceof Error ? error.message : String(error)}`
        );
      }
    }
  • Zod schema defining the input parameters for the ssh_execute tool: required connectionId and command, optional cwd.
    const ExecuteCommandSchema = z.object({
      connectionId: z.string().describe('SSH connection ID'),
      command: z.string().describe('Command to execute on remote server'),
      cwd: z.string().optional().describe('Working directory for command execution')
    });
  • src/index.ts:267-278 (registration)
    Tool registration/definition in the ListTools response, including name, description, and inputSchema matching the handler.
      name: 'ssh_execute',
      description: 'Execute a command on a remote SSH server',
      inputSchema: {
        type: 'object',
        properties: {
          connectionId: { type: 'string', description: 'SSH connection ID' },
          command: { type: 'string', description: 'Command to execute on remote server' },
          cwd: { type: 'string', description: 'Working directory for command execution' }
        },
        required: ['connectionId', 'command']
      },
    },
  • src/index.ts:489-490 (registration)
    Dispatch/registration in the CallToolRequestSchema handler switch statement, routing to handleSSHExecute.
    case 'ssh_execute':
      return await this.handleSSHExecute(args);

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/mahathirmuh/mcp-ssh-server'

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