System Resource Monitor

System Resource Monitor

🚀 系统资源监控MCP服务器开发指南(中文版)

本指南详细介绍了如何基于Node.js构建一个MCP(Model Context Protocol)服务器,用于实时监控系统资源状态。该服务器提供多个工具函数,允许Claude等AI模型获取CPU、内存、磁盘、网络、电池和互联网速度的实时数据。

🚀 快速开始

安装依赖

运行以下命令安装所需库:

npm install express systeminformation

启动服务器

import express from 'express';
import { getCpuUsage, getMemoryUsage, getDiskSpace, getNetworkUsage, getBatteryStatus, getInternetSpeed } from './tools';

const app = express();
const port = 3000;

app.get('/system/cpu', async (req, res) => {
res.send(await getCpuUsage());
});

app.get('/system/mem', async (req, res) => {
res.send(await getMemoryUsage());
});

// 其他路由类似实现

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

使用示例

通过curl命令调用各接口:

curl http://localhost:3000/system/cpu
curl http://localhost:3000/system/mem
# 其他接口类似调用

✨ 主要特性

  • 基于Node.js构建,提供实时系统资源监控功能。
  • 提供多个工具函数,允许Claude等AI模型获取CPU、内存、磁盘、网络、电池和互联网速度的实时数据。
  • 利用systeminformation库,实现跨平台的系统资源监控。

📦 安装指南

运行以下命令安装所需库:

npm install express systeminformation

💻 使用示例

基础用法

通过curl命令调用各接口:

curl http://localhost:3000/system/cpu
curl http://localhost:3000/system/mem
# 其他接口类似调用

📚 详细文档

项目结构

  • index.ts:主入口文件,启动HTTP服务器并注册各工具。
  • 工具目录:
    • get_cpu_usage.ts
    • get_memory_usage.ts
    • get_disk_space.ts
    • get_network_usage.ts
    • get_battery_status.ts
    • get_internet_speed.ts

核心组件

系统信息收集

所有工具均基于systeminformation库,该库提供跨平台的系统资源监控能力。

工具实现

CPU使用率
  • 功能:获取各核心的负载百分比。
  • 实现:
import { systeminformation } from 'systeminformation';

export async function getCpuUsage(): Promise<string> {
const usage = await systeminformation.cpu();
const cores = usage.map(u => u.usage.toFixed(2)).join(', ');
return `CPU Load: ${usage[0].usage.toFixed(2)}% (Cores: ${cores})`;
}
内存使用情况
  • 功能:报告内存总量和使用百分比。
export async function getMemoryUsage(): Promise<string> {
const mem = await systeminformation.mem();
return `Memory: ${(mem.used / 1024 / 1024).toFixed(2)}GB used / ${(mem.total / 1024 / 1024).toFixed(2)}GB total (${((mem.used / mem.total) * 100).toFixed(2)}%)`;
}
磁盘空间
  • 功能:监控最大分区的使用情况。
export async function getDiskSpace(): Promise<string> {
const stats = await systeminformation.diskSpace();
const root = stats.find(s => s.mountpoint === '/');
if (!root) return 'No disk space data available';
return `Disk (/): ${((root.used / root.total) * 100).toFixed(2)}% used (${(root.used / 1024 / 1024).toFixed(2)}GB / ${(root.total / 1024 / 1024).toFixed(2)}GB)`;
}
网络流量
  • 功能:获取实时网络带宽。
export async function getNetworkUsage(): Promise<string> {
const net = await systeminformation.network();
return `RX: ${net.rx_bytes.toFixed(2)}KB/s, TX: ${net.tx_bytes.toFixed(2)}KB/s`;
}
电池状态(仅限移动设备)
  • 功能:监控电池充电情况。
export async function getBatteryStatus(): Promise<string> {
const battery = await systeminformation.battery();
if (!battery || !battery.isCharging) return 'No battery detected';
return `Battery: ${battery.capacity}% (charging), ${(Math.floor(battery.timeLeft / 60)).toString()}min remaining`;
}
网络速度测试
  • 功能:测量互联网连接速度。
export async function getInternetSpeed(): Promise<string> {
try {
const speed = await systeminformation.internetSpeed();
return `Download: ${speed.download.toFixed(2)}MB/s, Upload: ${speed.upload.toFixed(2)}MB/s`;
} catch (e) {
return 'Network test failed';
}
}

服务器实现

启动HTTP服务

使用express框架搭建REST API。

import express from 'express';
import { getCpuUsage, getMemoryUsage, getDiskSpace, getNetworkUsage, getBatteryStatus, getInternetSpeed } from './tools';

const app = express();
const port = 3000;

app.get('/system/cpu', async (req, res) => {
res.send(await getCpuUsage());
});

app.get('/system/mem', async (req, res) => {
res.send(await getMemoryUsage());
});

// 其他路由类似实现

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

🔧 技术细节

本项目基于Node.js和express框架构建,利用systeminformation库实现跨平台的系统资源监控。通过REST API提供系统资源信息,方便Claude等AI模型调用。

⚠️ 注意事项

  1. 权限要求:在非root用户下运行时,某些系统信息可能无法获取。建议以适当权限启动服务。
  2. 性能优化:高频率调用可能导致资源消耗增加,建议设置合理的数据采集间隔。
  3. 错误处理:各工具需添加充分的异常捕捉和日志记录,确保服务稳定性。

总结

通过本指南,开发者可以快速搭建一个功能全面的系统资源监控MCP服务器。该方案利用现成库简化了底层实现,并提供了灵活的扩展接口。

  • 0 关注
  • 0 收藏,28 浏览
  • system 提出于 2025-10-06 20:36

相似服务问题

相关AI产品