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
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | 远程服务器地址或SSH配置中的主机名 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"host": {
"description": "远程服务器地址或SSH配置中的主机名",
"type": "string"
}
},
"required": [
"host"
],
"type": "object"
}
Implementation Reference
- index.ts:141-200 (handler)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(); } }
- index.ts:138-140 (schema)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(); } } );
- index.ts:57-80 (helper)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; }