mcpkit-server 0.6.0

Server implementation for mcpkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Health check utilities for MCP servers.
//!
//! This module provides a standardized health check mechanism for MCP servers,
//! supporting both simple and detailed health status reporting.
//!
//! # Example
//!
//! ```rust
//! use mcpkit_server::health::{HealthChecker, HealthStatus, ComponentHealth};
//!
//! // Create a health checker
//! let mut checker = HealthChecker::new("my-mcp-server");
//!
//! // Add component checks
//! checker.add_check("database", || {
//!     // Your database health check logic
//!     ComponentHealth::healthy()
//! });
//!
//! checker.add_check("cache", || {
//!     ComponentHealth::healthy().with_detail("hit_rate", "95%")
//! });
//!
//! // Get overall health status
//! let status = checker.check();
//! assert!(status.is_healthy());
//! ```

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Overall health status of the service.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HealthStatus {
    /// All components are healthy.
    Healthy,
    /// Some components are degraded but the service is functional.
    Degraded,
    /// The service is unhealthy and may not function correctly.
    Unhealthy,
}

impl HealthStatus {
    /// Check if the status is healthy.
    #[must_use]
    pub fn is_healthy(&self) -> bool {
        matches!(self, Self::Healthy)
    }

    /// Check if the status is degraded.
    #[must_use]
    pub fn is_degraded(&self) -> bool {
        matches!(self, Self::Degraded)
    }

    /// Check if the status is unhealthy.
    #[must_use]
    pub fn is_unhealthy(&self) -> bool {
        matches!(self, Self::Unhealthy)
    }

    /// Get the status as an HTTP status code.
    #[must_use]
    pub fn http_status_code(&self) -> u16 {
        match self {
            Self::Healthy => 200,
            Self::Degraded => 200, // Still operational
            Self::Unhealthy => 503,
        }
    }

    /// Get the status as a string.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Healthy => "healthy",
            Self::Degraded => "degraded",
            Self::Unhealthy => "unhealthy",
        }
    }
}

impl std::fmt::Display for HealthStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Health status of a single component.
#[derive(Debug, Clone)]
pub struct ComponentHealth {
    /// Component health status.
    pub status: HealthStatus,
    /// Optional message describing the status.
    pub message: Option<String>,
    /// Additional details about the component.
    pub details: HashMap<String, String>,
    /// Time taken to check this component.
    pub check_duration: Duration,
}

impl ComponentHealth {
    /// Create a healthy component status.
    #[must_use]
    pub fn healthy() -> Self {
        Self {
            status: HealthStatus::Healthy,
            message: None,
            details: HashMap::new(),
            check_duration: Duration::ZERO,
        }
    }

    /// Create a degraded component status.
    #[must_use]
    pub fn degraded(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Degraded,
            message: Some(message.into()),
            details: HashMap::new(),
            check_duration: Duration::ZERO,
        }
    }

    /// Create an unhealthy component status.
    #[must_use]
    pub fn unhealthy(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Unhealthy,
            message: Some(message.into()),
            details: HashMap::new(),
            check_duration: Duration::ZERO,
        }
    }

    /// Add a detail to the health status.
    #[must_use]
    pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.details.insert(key.into(), value.into());
        self
    }

    /// Add multiple details to the health status.
    #[must_use]
    pub fn with_details(mut self, details: impl IntoIterator<Item = (String, String)>) -> Self {
        self.details.extend(details);
        self
    }

    /// Set the check duration.
    #[must_use]
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.check_duration = duration;
        self
    }
}

impl Default for ComponentHealth {
    fn default() -> Self {
        Self::healthy()
    }
}

/// Type alias for health check functions.
pub type HealthCheckFn = Arc<dyn Fn() -> ComponentHealth + Send + Sync>;

/// Detailed health check result.
#[derive(Debug, Clone)]
pub struct HealthReport {
    /// Service name.
    pub service: String,
    /// Overall health status.
    pub status: HealthStatus,
    /// Service version (if available).
    pub version: Option<String>,
    /// How long the health check took.
    pub check_duration: Duration,
    /// Individual component health statuses.
    pub components: HashMap<String, ComponentHealth>,
    /// Timestamp of the health check.
    pub timestamp: std::time::SystemTime,
}

impl HealthReport {
    /// Check if the service is healthy.
    #[must_use]
    pub fn is_healthy(&self) -> bool {
        self.status.is_healthy()
    }

    /// Get the number of healthy components.
    #[must_use]
    pub fn healthy_count(&self) -> usize {
        self.components
            .values()
            .filter(|c| c.status.is_healthy())
            .count()
    }

    /// Get the number of degraded components.
    #[must_use]
    pub fn degraded_count(&self) -> usize {
        self.components
            .values()
            .filter(|c| c.status.is_degraded())
            .count()
    }

    /// Get the number of unhealthy components.
    #[must_use]
    pub fn unhealthy_count(&self) -> usize {
        self.components
            .values()
            .filter(|c| c.status.is_unhealthy())
            .count()
    }

    /// Convert to a JSON-serializable structure.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let components: HashMap<String, serde_json::Value> = self
            .components
            .iter()
            .map(|(name, health)| {
                let mut obj = serde_json::json!({
                    "status": health.status.as_str(),
                    "check_duration_ms": health.check_duration.as_millis(),
                });

                if let Some(msg) = &health.message {
                    obj["message"] = serde_json::json!(msg);
                }

                if !health.details.is_empty() {
                    obj["details"] = serde_json::json!(health.details);
                }

                (name.clone(), obj)
            })
            .collect();

        let mut result = serde_json::json!({
            "status": self.status.as_str(),
            "service": self.service,
            "check_duration_ms": self.check_duration.as_millis(),
            "components": components,
        });

        if let Some(version) = &self.version {
            result["version"] = serde_json::json!(version);
        }

        result
    }
}

/// Health checker for MCP servers.
///
/// Provides a centralized way to register and execute health checks.
#[derive(Default)]
pub struct HealthChecker {
    service_name: String,
    version: Option<String>,
    checks: HashMap<String, HealthCheckFn>,
}

impl HealthChecker {
    /// Create a new health checker.
    #[must_use]
    pub fn new(service_name: impl Into<String>) -> Self {
        Self {
            service_name: service_name.into(),
            version: None,
            checks: HashMap::new(),
        }
    }

    /// Set the service version.
    #[must_use]
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Add a health check for a component.
    pub fn add_check<F>(&mut self, name: impl Into<String>, check: F)
    where
        F: Fn() -> ComponentHealth + Send + Sync + 'static,
    {
        self.checks.insert(name.into(), Arc::new(check));
    }

    /// Add a simple health check that just returns healthy.
    pub fn add_simple_check(&mut self, name: impl Into<String>) {
        self.add_check(name, ComponentHealth::healthy);
    }

    /// Run all health checks and return a report.
    #[must_use]
    pub fn check(&self) -> HealthReport {
        let start = Instant::now();
        let mut components = HashMap::new();
        let mut overall_status = HealthStatus::Healthy;

        for (name, check_fn) in &self.checks {
            let check_start = Instant::now();
            let mut result = check_fn();
            result.check_duration = check_start.elapsed();

            // Update overall status based on component status
            match (&overall_status, &result.status) {
                (HealthStatus::Healthy, HealthStatus::Degraded) => {
                    overall_status = HealthStatus::Degraded;
                }
                (_, HealthStatus::Unhealthy) => {
                    overall_status = HealthStatus::Unhealthy;
                }
                _ => {}
            }

            components.insert(name.clone(), result);
        }

        HealthReport {
            service: self.service_name.clone(),
            status: overall_status,
            version: self.version.clone(),
            check_duration: start.elapsed(),
            components,
            timestamp: std::time::SystemTime::now(),
        }
    }

    /// Run a quick liveness check (just verifies the service is running).
    #[must_use]
    pub fn liveness(&self) -> bool {
        true
    }

    /// Run a readiness check (verifies all components are ready).
    #[must_use]
    pub fn readiness(&self) -> bool {
        self.check().is_healthy()
    }
}

impl std::fmt::Debug for HealthChecker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HealthChecker")
            .field("service_name", &self.service_name)
            .field("version", &self.version)
            .field("check_count", &self.checks.len())
            .finish()
    }
}

/// Liveness probe response.
#[derive(Debug, Clone)]
pub struct LivenessResponse {
    /// Whether the service is alive.
    pub alive: bool,
    /// Service name.
    pub service: String,
}

impl LivenessResponse {
    /// Create a new liveness response.
    #[must_use]
    pub fn new(service: impl Into<String>, alive: bool) -> Self {
        Self {
            alive,
            service: service.into(),
        }
    }

    /// Create an alive response.
    #[must_use]
    pub fn alive(service: impl Into<String>) -> Self {
        Self::new(service, true)
    }

    /// Convert to JSON.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "alive": self.alive,
            "service": self.service,
        })
    }
}

/// Readiness probe response.
#[derive(Debug, Clone)]
pub struct ReadinessResponse {
    /// Whether the service is ready.
    pub ready: bool,
    /// Service name.
    pub service: String,
    /// Optional reason if not ready.
    pub reason: Option<String>,
}

impl ReadinessResponse {
    /// Create a new readiness response.
    #[must_use]
    pub fn new(service: impl Into<String>, ready: bool) -> Self {
        Self {
            ready,
            service: service.into(),
            reason: None,
        }
    }

    /// Create a ready response.
    #[must_use]
    pub fn ready(service: impl Into<String>) -> Self {
        Self::new(service, true)
    }

    /// Create a not-ready response with a reason.
    #[must_use]
    pub fn not_ready(service: impl Into<String>, reason: impl Into<String>) -> Self {
        Self {
            ready: false,
            service: service.into(),
            reason: Some(reason.into()),
        }
    }

    /// Convert to JSON.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let mut result = serde_json::json!({
            "ready": self.ready,
            "service": self.service,
        });

        if let Some(reason) = &self.reason {
            result["reason"] = serde_json::json!(reason);
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_health_status() {
        assert!(HealthStatus::Healthy.is_healthy());
        assert!(!HealthStatus::Degraded.is_healthy());
        assert!(!HealthStatus::Unhealthy.is_healthy());

        assert_eq!(HealthStatus::Healthy.http_status_code(), 200);
        assert_eq!(HealthStatus::Degraded.http_status_code(), 200);
        assert_eq!(HealthStatus::Unhealthy.http_status_code(), 503);
    }

    #[test]
    fn test_component_health() {
        let health = ComponentHealth::healthy()
            .with_detail("connections", "10")
            .with_detail("memory_mb", "256");

        assert!(health.status.is_healthy());
        assert_eq!(health.details.get("connections"), Some(&"10".to_string()));
    }

    #[test]
    fn test_health_checker() {
        let mut checker = HealthChecker::new("test-service").with_version("1.0.0");

        checker.add_check("component_a", ComponentHealth::healthy);
        checker.add_check("component_b", || {
            ComponentHealth::healthy().with_detail("status", "ok")
        });

        let report = checker.check();

        assert!(report.is_healthy());
        assert_eq!(report.healthy_count(), 2);
        assert_eq!(report.unhealthy_count(), 0);
    }

    #[test]
    fn test_degraded_status() {
        let mut checker = HealthChecker::new("test-service");

        checker.add_check("healthy_component", ComponentHealth::healthy);
        checker.add_check("degraded_component", || {
            ComponentHealth::degraded("High latency detected")
        });

        let report = checker.check();

        assert!(!report.is_healthy());
        assert_eq!(report.status, HealthStatus::Degraded);
    }

    #[test]
    fn test_unhealthy_status() {
        let mut checker = HealthChecker::new("test-service");

        checker.add_check("healthy_component", ComponentHealth::healthy);
        checker.add_check("unhealthy_component", || {
            ComponentHealth::unhealthy("Connection refused")
        });

        let report = checker.check();

        assert!(report.status.is_unhealthy());
    }

    #[test]
    fn test_liveness_and_readiness() {
        let mut checker = HealthChecker::new("test-service");
        checker.add_check("component", ComponentHealth::healthy);

        assert!(checker.liveness());
        assert!(checker.readiness());
    }

    #[test]
    fn test_health_report_json() {
        let mut checker = HealthChecker::new("test-service").with_version("1.0.0");
        checker.add_check("database", ComponentHealth::healthy);

        let report = checker.check();
        let json = report.to_json();

        assert_eq!(json["status"], "healthy");
        assert_eq!(json["service"], "test-service");
        assert_eq!(json["version"], "1.0.0");
    }
}