get_system_metrics
Retrieve system metrics including CPU, memory, and disk usage to monitor system health and performance.
Instructions
Get CPU, memory, and disk metrics from the system
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_system_metrics"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| metrics | No | Which metrics to retrieve: cpu, memory, disk |
Implementation Reference
- src/tools/get_system_metrics.ts:16-40 (handler)The handler function `getMetrics` that executes the tool logic. It receives input specifying which metrics to retrieve (cpu, memory, disk), calls `getSystemMetrics()` from utils, and formats the result as text content.
export async function getMetrics(input: ToolInput): Promise<{ content: Array<{ type: string; text: string }> }> { const { metrics = ['cpu', 'memory', 'disk'] } = input as GetSystemMetricsInput; const data = await getSystemMetrics(); const lines: string[] = ['System Metrics:\n']; if (metrics.includes('cpu')) { lines.push(` CPU: ${data.cpu.usage}% (${data.cpu.cores} cores)`); } if (metrics.includes('memory')) { const usedStr = formatBytes(data.memory.used); const totalStr = formatBytes(data.memory.total); lines.push(` Memory: ${usedStr} / ${totalStr} (${data.memory.percent}%)`); } if (metrics.includes('disk')) { const usedStr = formatBytes(data.disk.used); const totalStr = formatBytes(data.disk.total); lines.push(` Disk: ${usedStr} / ${totalStr} (${data.disk.percent}%)`); } return { content: [{ type: 'text', text: lines.join('\n') }] }; } - src/tools/get_system_metrics.ts:8-14 (helper)Helper function `formatBytes` used to convert byte values into human-readable strings (e.g., '1.5 GB').
function formatBytes(bytes: number): string { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`; } - src/tools/get_system_metrics.ts:4-6 (schema)Input type definition `GetSystemMetricsInput` extending ToolInput with an optional `metrics` array of strings.
interface GetSystemMetricsInput extends ToolInput { metrics?: string[]; } - src/types/index.ts:46-50 (schema)Type definition `SystemMetrics` (and its nested `CpuMetrics`, `MemoryMetrics`, `DiskMetrics`) defining the return shape of getSystemMetrics.
export interface SystemMetrics { cpu: CpuMetrics; memory: MemoryMetrics; disk: DiskMetrics; } - src/index.ts:98-112 (registration)Tool registration block defining the name 'get_system_metrics', description, and input schema (the `metrics` array). Dispatches to handler at line 154-155.
{ name: 'get_system_metrics', description: 'Get CPU, memory, and disk metrics from the system', inputSchema: { type: 'object', properties: { metrics: { type: 'array', items: { type: 'string' }, default: ['cpu', 'memory', 'disk'], description: 'Which metrics to retrieve: cpu, memory, disk' } } } },