🚀 系統資源監控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模型調用。
⚠️ 注意事項
- 權限要求:在非root用戶下運行時,某些系統信息可能無法獲取。建議以適當權限啟動服務。
- 性能優化:高頻率調用可能導致資源消耗增加,建議設置合理的數據採集間隔。
- 錯誤處理:各工具需添加充分的異常捕捉和日誌記錄,確保服務穩定性。
總結
通過本指南,開發者可以快速搭建一個功能全面的系統資源監控MCP服務器。該方案利用現成庫簡化了底層實現,並提供了靈活的擴展接口。