Skip to main content
Glama
NNNNzs

Server Status MCP Server

get_remote_server_status

Monitor CPU, memory, and uptime of remote servers via SSH. Retrieve real-time server status data using host information for efficient system health tracking.

Instructions

获取远程服务器的CPU、内存和运行状态

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostYes远程服务器地址或SSH配置中的主机名

Implementation Reference

  • The asynchronous handler function for the get_remote_server_status tool. It establishes an SSH connection to the specified host using configuration from SSH config file if available, executes commands to retrieve CPU info (/proc/cpuinfo), memory (free -m), and uptime, then returns the stdout as JSON. Handles errors and ensures SSH disposal.
    async (args) => {
      const ssh = new NodeSSH();
    
      try {
        const sshConfigPath = options.sshConfigPath.replace(/^~/, os.homedir());
        let sshConfig;
    
        try {
          const configContent = await fs.readFile(sshConfigPath, 'utf-8');
          sshConfig = await parseSSHConfig(configContent, args.host);
        } catch (error: any) {
          console.warn(`无法读取 SSH 配置文件 ${sshConfigPath}:`, error.message);
          sshConfig = null;
        }
    
        const connectionConfig = sshConfig ? {
          host: sshConfig.hostname || args.host,
          username: sshConfig.user,
          port: sshConfig.port ? parseInt(sshConfig.port) : 22,
          privateKey: sshConfig.identityfile ?
            await fs.readFile(sshConfig.identityfile.replace(/^~/, os.homedir()), 'utf-8') :
            undefined
        } : {
          host: args.host,
          username: os.userInfo().username,
          privateKey: path.join(os.homedir(), '.ssh', 'id_rsa')
        };
    
        await ssh.connect(connectionConfig);
    
        const [cpuInfo, memInfo, uptimeInfo] = await Promise.all([
          ssh.execCommand('cat /proc/cpuinfo'),
          ssh.execCommand('free -m'),
          ssh.execCommand('uptime'),
        ]);
    
        return {
          content: [{
            type: 'text',
            text: JSON.stringify({
              cpu: cpuInfo.stdout,
              memory: memInfo.stdout,
              uptime: uptimeInfo.stdout,
              type: 'remote'
            }, null, 2)
          }]
        };
      } catch (error: any) {
        return {
          content: [{
            type: 'text',
            text: JSON.stringify({
              error: error.message
            }, null, 2)
          }]
        };
      } finally {
        ssh.dispose();
      }
    }
  • Input schema for the tool using Zod: requires a 'host' string parameter describing the remote server address or SSH config host name.
    {
      host: z.string().describe("远程服务器地址或SSH配置中的主机名")
    },
  • index.ts:135-201 (registration)
    Registration of the get_remote_server_status tool on the McpServer instance within addServerTools function, including name, description, input schema, and inline handler.
    server.tool(
      "get_remote_server_status",
      "获取远程服务器的CPU、内存和运行状态",
      {
        host: z.string().describe("远程服务器地址或SSH配置中的主机名")
      },
      async (args) => {
        const ssh = new NodeSSH();
    
        try {
          const sshConfigPath = options.sshConfigPath.replace(/^~/, os.homedir());
          let sshConfig;
    
          try {
            const configContent = await fs.readFile(sshConfigPath, 'utf-8');
            sshConfig = await parseSSHConfig(configContent, args.host);
          } catch (error: any) {
            console.warn(`无法读取 SSH 配置文件 ${sshConfigPath}:`, error.message);
            sshConfig = null;
          }
    
          const connectionConfig = sshConfig ? {
            host: sshConfig.hostname || args.host,
            username: sshConfig.user,
            port: sshConfig.port ? parseInt(sshConfig.port) : 22,
            privateKey: sshConfig.identityfile ?
              await fs.readFile(sshConfig.identityfile.replace(/^~/, os.homedir()), 'utf-8') :
              undefined
          } : {
            host: args.host,
            username: os.userInfo().username,
            privateKey: path.join(os.homedir(), '.ssh', 'id_rsa')
          };
    
          await ssh.connect(connectionConfig);
    
          const [cpuInfo, memInfo, uptimeInfo] = await Promise.all([
            ssh.execCommand('cat /proc/cpuinfo'),
            ssh.execCommand('free -m'),
            ssh.execCommand('uptime'),
          ]);
    
          return {
            content: [{
              type: 'text',
              text: JSON.stringify({
                cpu: cpuInfo.stdout,
                memory: memInfo.stdout,
                uptime: uptimeInfo.stdout,
                type: 'remote'
              }, null, 2)
            }]
          };
        } catch (error: any) {
          return {
            content: [{
              type: 'text',
              text: JSON.stringify({
                error: error.message
              }, null, 2)
            }]
          };
        } finally {
          ssh.dispose();
        }
      }
    );
  • Helper function to parse the SSH configuration file content and extract settings for a specific target host, used in the tool handler to configure SSH connection.
    export async function parseSSHConfig(configContent: string, targetHost: string) {
      const lines = configContent.split('\n');
      let currentHost: string | null = null;
      let config: Record<string, Record<string, string>> = {};
    
      for (const line of lines) {
        const trimmedLine = line.trim();
        if (!trimmedLine || trimmedLine.startsWith('#')) continue;
    
        const [key, ...values] = trimmedLine.split(/\s+/);
        const value = values.join(' ');
    
        if (key.toLowerCase() === 'host') {
          currentHost = value;
          if (currentHost && !config[currentHost]) {
            config[currentHost] = {};
          }
        } else if (currentHost) {
          config[currentHost][key.toLowerCase()] = value;
        }
      }
    
      return targetHost ? config[targetHost] : null;
    }
Install Server

Other Tools

Related Tools

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/NNNNzs/server-status-mcp-server'

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