mcp-monitoring
Monitoring, metrics, and observability for MCP servers
This crate provides monitoring and observability features for MCP servers, including metrics collection, health checks, and performance tracking.
What This Provides
Metrics Collection:
- Request/response timing and throughput
- Error rates and types
- Tool usage statistics
- Resource access patterns
- Client connection metrics
Health Monitoring:
- Server health checks with detailed status
- Backend connectivity validation
- Resource availability checks
- Performance threshold monitoring
Observability:
- Structured logging integration
- Request tracing with correlation IDs
- Performance profiling hooks
- Custom metric collection
Real-World Usage
This monitoring system is actively used in the Loxone MCP Server where it:
- Tracks usage of 30+ home automation tools
- Monitors device response times and errors
- Provides health checks for HTTP transport endpoints
- Collects performance metrics for optimization
- Integrates with system monitoring dashboards
Quick Start
[]
= "0.1.0"
= "0.1.0"
= { = "1.0", = ["full"] }
Basic Usage
Health Checks
use ;
// Configure health checks
let config = HealthConfig ;
let mut health_checker = new;
// Add custom health checks
health_checker.add_check;
// Start monitoring
health_checker.start.await?;
// Check current health
let status = health_checker.get_status.await;
println!;
Metrics Collection
use ;
let collector = new;
// Track tool usage
collector.record;
// Track response times
collector.record;
Performance Tracking
use ;
let tracker = new;
// Track a request
let request_id = tracker.start_request.await;
// Your business logic here
let result = execute_tool_call.await;
// Complete tracking
tracker.finish_request.await;
Current Status
Useful for basic monitoring with room for advanced features. The core monitoring functionality works well for understanding server behavior and performance.
What works well:
- ✅ Basic health check system
- ✅ Request timing and error tracking
- ✅ Tool usage statistics
- ✅ Integration with HTTP transport
- ✅ Structured logging integration
Areas for improvement:
- 📊 More sophisticated metrics aggregation
- 🔧 Better alerting and notification systems
- 📝 More examples for different monitoring setups
- 🧪 Testing utilities for monitoring scenarios
Health Check System
Built-in Health Checks
use builtin_checks;
// Add standard health checks
health_checker.add_check; // 80% threshold
health_checker.add_check;
health_checker.add_check;
Custom Health Checks
use ;
health_checker.add_check_instance;
Health Endpoints
// Expose health checks via HTTP
use ;
use HealthChecker;
async
let app = new
.route;
Metrics System
Metric Types
use MetricType;
// Counter - Always increasing values
Counter // Total requests, total errors
// Gauge - Current value
Gauge // Active connections, memory usage
// Histogram - Distribution of values
Histogram // Request durations, response sizes
// Summary - Similar to histogram with quantiles
Summary // Response time percentiles
Common Metrics
// Request metrics
collector.increment_counter;
collector.record_histogram;
// Tool usage metrics
collector.increment_counter;
// Error tracking
collector.increment_counter;
Integration with MCP Server
use ;
use MonitoringMiddleware;
let monitoring_config = MonitoringConfig ;
let server_config = ServerConfig ;
// Monitoring happens automatically
Performance Tracking
Request Tracing
use RequestTracer;
let tracer = new;
// Start tracing a request
let trace_id = tracer.start_trace;
tracer.add_span;
tracer.add_span;
tracer.add_span;
// Complete the trace
tracer.finish_trace;
Memory and Resource Monitoring
use ResourceMonitor;
let monitor = new;
// Track resource usage
let snapshot = monitor.take_snapshot.await;
println!;
println!;
println!;
Real-World Examples
Loxone Server Monitoring
// Monitor home automation tool performance
collector.record_histogram;
// Track automation success rates
collector.increment_counter;
// Monitor connection health
health_checker.add_check;
Dashboard Integration
// Expose metrics for Grafana/Prometheus
use prometheus_exporter;
let exporter = new;
let metrics_data = exporter.export.await;
// Returns Prometheus format:
// # HELP tool_calls_total Total number of tool calls
// # TYPE tool_calls_total counter
// tool_calls_total{tool="control_device",status="success"} 150
Contributing
Monitoring and observability can always be improved. Most valuable contributions:
- New metric types - Domain-specific metrics for MCP servers
- Integration examples - How to integrate with popular monitoring systems
- Performance optimization - Low-overhead monitoring approaches
- Alerting systems - Smart alerting based on MCP server patterns
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Repository: https://github.com/avrabe/mcp-loxone