Skip to main content
Glama

execute_command

Execute commands on a VPS to manage services, configure domains, set up SSL certificates, and deploy applications via SSH connections.

Instructions

Execute a command on the connected VPS

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesCommand to execute

Implementation Reference

  • The main handler function for the MCP 'execute_command' tool. It validates the input arguments, checks if SSH is connected, executes the command using SSHService, and formats the response with stdout, stderr, and exit code.
    private async handleExecuteCommand(
      args: unknown
    ): Promise<{ content: Array<{ type: 'text'; text: string }> }> {
      if (!this.sshService) {
        throw new Error('SSH connection not established. Please connect first.');
      }
    
      const { command } = z.object({ command: z.string() }).parse(args);
      const result = await this.sshService.executeCommand(command);
    
      return {
        content: [
          {
            type: 'text',
            text: `Command: ${command}\nExit Code: ${result.exitCode}\nOutput:\n${result.stdout}\n${result.stderr ? `Error:\n${result.stderr}` : ''}`,
          },
        ],
      };
    }
  • Input schema definition for the 'execute_command' tool, specifying the required 'command' string parameter.
    inputSchema: {
      type: 'object',
      properties: {
        command: { type: 'string', description: 'Command to execute' },
      },
      required: ['command'],
    },
  • Registration of the 'execute_command' tool handler in the CallToolRequestSchema switch statement.
    case 'execute_command':
      return await this.handleExecuteCommand(args);
  • Tool definition and registration in the ListToolsRequestSchema response, including name, description, and schema.
      name: 'execute_command',
      description: 'Execute a command on the connected VPS',
      inputSchema: {
        type: 'object',
        properties: {
          command: { type: 'string', description: 'Command to execute' },
        },
        required: ['command'],
      },
    },
  • Core helper function that performs the actual SSH command execution using node-ssh library, handling connection check, execution, logging, and error handling.
    async executeCommand(command: string): Promise<CommandResult> {
      if (!this.isConnected) {
        throw new Error('SSH connection not established');
      }
    
      try {
        logger.debug('Executing command', { command });
        const result = await this.ssh.execCommand(command);
    
        const commandResult: CommandResult = {
          success: result.code === 0,
          stdout: result.stdout,
          stderr: result.stderr,
          exitCode: result.code || 0,
        };
    
        if (commandResult.success) {
          logger.debug('Command executed successfully', {
            command,
            exitCode: commandResult.exitCode,
          });
        } else {
          logger.warn('Command execution failed', {
            command,
            exitCode: commandResult.exitCode,
            stderr: commandResult.stderr,
          });
        }
    
        return commandResult;
      } catch (error) {
        logger.error('Error executing command', {
          command,
          error: error instanceof Error ? error.message : 'Unknown error',
        });
        return {
          success: false,
          stdout: '',
          stderr: error instanceof Error ? error.message : 'Unknown error',
          exitCode: -1,
        };
      }

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