get_metrics
Retrieve server performance metrics to monitor system health and optimize resource allocation for memory management operations.
Instructions
獲取服務器性能指標
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_metrics"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.js:191-193 (handler)Handler function for the 'get_metrics' MCP tool. It executes the tool logic by calling globalMetrics.getMetrics() to retrieve comprehensive server performance metrics.
async handler() { return globalMetrics.getMetrics(); } - src/index.js:186-194 (registration)Registration of the 'get_metrics' tool in the tool registry. Includes name, description, empty input schema (no parameters), system scope, and inline handler.
registerTool({ name: 'get_metrics', description: '獲取服務器性能指標', inputSchema: z.object({}), scope: 'system', async handler() { return globalMetrics.getMetrics(); } }, 'system'); - src/index.js:189-189 (schema)Zod input schema for the 'get_metrics' tool: empty object schema indicating no input parameters are required.
inputSchema: z.object({}), - src/monitoring/metrics.js:130-194 (helper)Core implementation of getMetrics() method in MetricsCollector class. Computes and returns detailed performance metrics: uptime, request counts/error rates, latency percentiles (p50/p95/p99), cache statistics, memory usage, and tool-specific stats.
getMetrics() { const now = Date.now(); const uptime = now - this.startTime; const uptimeSeconds = Math.floor(uptime / 1000); // 計算請求率 const requestsPerSecond = this.requestCount / (uptimeSeconds || 1); // 計算錯誤率 const errorRate = this.requestCount > 0 ? (this.errorCount / this.requestCount * 100).toFixed(2) : 0; // 計算持續時間統計 const avgDuration = this.durations.length > 0 ? this.durations.reduce((sum, d) => sum + d, 0) / this.durations.length : 0; // 計算緩存命中率 const totalCacheAccess = this.cacheHits + this.cacheMisses; const cacheHitRate = totalCacheAccess > 0 ? (this.cacheHits / totalCacheAccess * 100).toFixed(2) : 0; // 獲取最新內存使用 const latestMemory = this.memorySnapshots.length > 0 ? this.memorySnapshots[this.memorySnapshots.length - 1] : process.memoryUsage(); return { uptime: { ms: uptime, seconds: uptimeSeconds, human: this.formatUptime(uptimeSeconds) }, requests: { total: this.requestCount, success: this.successCount, error: this.errorCount, errorRate: `${errorRate}%`, requestsPerSecond: requestsPerSecond.toFixed(2) }, latency: { avg: avgDuration.toFixed(2), p50: percentile(this.durations, 0.50).toFixed(2), p95: percentile(this.durations, 0.95).toFixed(2), p99: percentile(this.durations, 0.99).toFixed(2), max: this.durations.length > 0 ? Math.max(...this.durations).toFixed(2) : 0, unit: 'ms' }, cache: { hits: this.cacheHits, misses: this.cacheMisses, total: totalCacheAccess, hitRate: `${cacheHitRate}%` }, memory: { rss: `${(latestMemory.rss / 1024 / 1024).toFixed(2)} MB`, heapTotal: `${(latestMemory.heapTotal / 1024 / 1024).toFixed(2)} MB`, heapUsed: `${(latestMemory.heapUsed / 1024 / 1024).toFixed(2)} MB`, external: `${(latestMemory.external / 1024 / 1024).toFixed(2)} MB` }, toolStats: this.getToolStats() }; }