本指南详细介绍了如何基于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
# 其他接口类似调用
systeminformation库,实现跨平台的系统资源监控。运行以下命令安装所需库:
npm install express systeminformation
通过curl命令调用各接口:
curl http://localhost:3000/system/cpu
curl http://localhost:3000/system/mem
# 其他接口类似调用
index.ts:主入口文件,启动HTTP服务器并注册各工具。get_cpu_usage.tsget_memory_usage.tsget_disk_space.tsget_network_usage.tsget_battery_status.tsget_internet_speed.ts所有工具均基于systeminformation库,该库提供跨平台的系统资源监控能力。
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';
}
}
使用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模型调用。
通过本指南,开发者可以快速搭建一个功能全面的系统资源监控MCP服务器。该方案利用现成库简化了底层实现,并提供了灵活的扩展接口。