Rust/WASM系统监视器
一个轻量级的系统监控工具,演示 代码优先方法 如Anthropic关于使用MCP执行代码的论文所述。该项目实现了 代币使用量减少98.7% 与传统的MCP服务器相比。
传统MCP的问题
大多数MCP客户端将所有工具定义直接预先加载到上下文中,造成了两个主要的低效:
- 工具定义块:AI代理在读取用户请求之前仅处理约150000个令牌以加载工具定义
- 中间结果开销:在工具调用之间复制数据时,大型文档或复杂的数据结构会增加错误
代码优先解决方案
AI代理没有加载大量的工具定义,而是:
- 发现工具 通过探索文件系统(
./tools/目录) - 编写代码 只进口和使用需要的东西
- 本地处理 -中间数据保留在执行环境中
- 返回摘要 -只有经过过滤的输出才能进入模型
代币节省
- 传统MCP:约150000个代币
- 代码优先:约2000个代币
- 减少:98.7%
建筑
rust-wasm-monitor/
├── src/
│ ├── lib.rs # Rust core with sysinfo integration
│ └── main.rs # CLI binary (optional)
├── tools/
│ └── system-monitor/
│ └── index.ts # TypeScript wrappers (lazy-loaded)
├── pkg/ # WASM output (gitignored)
├── demo.html # Interactive demo
└── build.sh # Build script特性
- 系统信息:操作系统、版本、内核、主机名、CPU计数、内存、正常运行时间
- 内存监控:总内存、已用内存、可用内存及其使用百分比
- 磁盘信息:所有装载都有空间和使用统计信息
- CPU指标:每个核心的使用情况和频率
- 零配置:没有API密钥,没有服务器,完全在浏览器或Node.js中运行
快速开始
先决条件
# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install wasm-pack
cargo install wasm-pack构建
# Build WASM package
./build.sh运行演示
# Start a local web server
python3 -m http.server 8080
# Open browser to http://localhost:8080/demo.html用法示例
对于AI代理(代码优先方法)
AI代理通过文件系统发现工具并编写代码:
// Agent explores: ls ./tools/
// Agent reads: cat ./tools/system-monitor/index.ts
// Agent writes code:
import { getSystemInfo, getMemoryInfo } from './tools/system-monitor';
async function checkSystemHealth() {
const sys = await getSystemInfo();
const mem = await getMemoryInfo();
console.log(`OS: ${sys.os} ${sys.os_version}`);
console.log(`Memory: ${mem.usage_percent.toFixed(1)}% used`);
if (mem.usage_percent > 90) {
return { alert: 'High memory usage!', percentage: mem.usage_percent };
}
return { status: 'healthy' };
}无需工具定义 -只有带有类型检查的TypeScript!
API直接使用(Browser/Node.js)
import init, { SystemMonitor } from './pkg/rust_wasm_monitor.js';
await init();
const monitor = SystemMonitor.new();
// Get system info
const sysInfo = JSON.parse(monitor.get_system_info());
console.log(`Running ${sysInfo.os} ${sysInfo.os_version}`);
// Get memory info
const memInfo = JSON.parse(monitor.get_memory_info());
console.log(`Memory: ${memInfo.usage_percent.toFixed(1)}% used`);
// List disks
const disks = JSON.parse(monitor.list_disks());
disks.forEach(disk => {
console.log(`${disk.mount_point}: ${disk.usage_percent.toFixed(1)}% used`);
});
// Get CPU info
const cpus = JSON.parse(monitor.get_cpu_info());
console.log(`CPU 0: ${cpus[0].usage.toFixed(1)}% @ ${cpus[0].frequency} MHz`);API 参考
系统监视器
从Rust导出的主WASM接口。
方法
new()
创建新的系统监视器实例。
const monitor = SystemMonitor.new();refresh()
刷新所有系统指标。
monitor.refresh();get_system_info(): string
返回包含系统信息的JSON字符串。
interface SystemInfo {
os: string;
os_version: string;
kernel_version: string;
hostname: string;
cpu_count: number;
total_memory: number;
used_memory: number;
uptime: number;
}get_memory_info(): string
返回包含内存使用情况的JSON字符串。
interface MemoryInfo {
total: number;
used: number;
available: number;
usage_percent: number;
}list_disks(): string
返回磁盘信息的JSON数组。
interface DiskInfo {
name: string;
mount_point: string;
total_space: number;
available_space: number;
usage_percent: number;
}get_cpu_info(): string
返回CPU信息的JSON数组。
interface CpuInfo {
name: string;
usage: number;
frequency: number;
}TypeScript包装器
这 tools/system-monitor/index.ts 模块提供类型安全包装:
import * as monitor from './tools/system-monitor';
const sysInfo = await monitor.getSystemInfo(); // Returns SystemInfo
const memInfo = await monitor.getMemoryInfo(); // Returns MemoryInfo
const disks = await monitor.listDisks(); // Returns DiskInfo[]
const cpus = await monitor.getCpuInfo(); // Returns CpuInfo[]为您的用例进行扩展
这个项目演示了一个最小的系统监视器。您可以将其扩展为:
服务器监控
#[wasm_bindgen]
pub fn list_online_hosts(&self) -> String {
// Query your REST API
}
#[wasm_bindgen]
pub fn get_gpu_info(&self, host: &str) -> String {
// Return GPU VRAM summary only (not full details)
}UI测试(剧作家替代)
#[wasm_bindgen]
pub fn test_form(&self, selectors: &str) -> String {
// Run test, return pass/fail
// NOT the entire DOM
}数据库查询
#[wasm_bindgen]
pub fn query_metrics(&self, sql: &str) -> String {
// Execute query, return aggregated results
// NOT raw result set
}关键设计原则
- 渐进式发现:工具是通过文件系统发现的,而不是预先加载的
- 代码即文件:TypeScript包装器是自文档化的
- 本地处理:在WASM中处理数据,仅返回摘要
- 类型安全:通过接口完全支持TypeScript
- 零依赖:在浏览器中运行,无需外部服务
比较:MCP与代码优先
| 方面 | 传统MCP | 代码优先 |
|---|---|---|
| 代币使用量 | ~150000 | ~2000 |
| 发现 | 已加载所有工具 | 文件系统探索 |
| 类型安全 | 通过工具模式 | 原生TypeScript |
| 中间数据 | 发送到模型 | 保持在执行环境中 |
| 隐私 | 所有数据可见 | 可以标记PII |
| 性能 | 工具调用开销 | 直接函数调用 |
发展
运行测试
# Rust tests
cargo test
# WASM tests (requires wasm-pack)
wasm-pack test --headless --firefox为生产而建
./build.sh
# Output in pkg/ directory:
# - rust_wasm_monitor.js
# - rust_wasm_monitor_bg.wasm
# - rust_wasm_monitor.d.ts项目结构
src/lib.rs:核心Rust实现使用sysinfo板条箱src/main.rs:可选CLI工具tools/:用于代码优先方法的TypeScript包装器demo.html:交互式浏览器演示build.sh:WASM构建脚本
演出
- 构建大小:~50KB WASM(压缩)
- 启动时间:\<100ms
- 内存开销:\<1MB
- 无网络呼叫:一切都在本地运行
许可证
请参阅docs/目录中的LICENSE和COPYRIGHT文件。
贡献
该项目演示了MCP替代方案的代码优先方法。欢迎捐款:
- 其他监测指标
- 替代用例(UI测试、数据库查询等)
- 性能优化
- 文档改进
参考文献
- Anthropic:使用MCP执行代码
- MCP代币缩减文件
- 原始研究:docs/research.md
相关项目
______________________________________________________________________
使用Rust+WASM+TypeScript构建 通过代码优先设计演示98.7%的令牌减少
