turbomcp_server/handlers/traits/
ping.rs

1//! Ping handler trait for bidirectional health monitoring
2
3use async_trait::async_trait;
4use serde_json::Value;
5use turbomcp_protocol::RequestContext;
6use turbomcp_protocol::types::{PingRequest, PingResult};
7
8use crate::ServerResult;
9
10/// Ping handler trait for bidirectional health monitoring
11#[async_trait]
12pub trait PingHandler: Send + Sync {
13    /// Handle a ping request
14    async fn handle(&self, request: PingRequest, ctx: RequestContext) -> ServerResult<PingResult>;
15
16    /// Get current health status
17    async fn get_health_status(&self, _ctx: RequestContext) -> ServerResult<Value> {
18        Ok(serde_json::json!({
19            "status": "healthy",
20            "timestamp": chrono::Utc::now().to_rfc3339(),
21        }))
22    }
23
24    /// Get connection metrics if available
25    async fn get_connection_metrics(&self, _ctx: RequestContext) -> ServerResult<Option<Value>> {
26        Ok(None) // Default: no metrics
27    }
28
29    /// Handle ping timeout
30    async fn handle_timeout(&self, _request_id: &str, _ctx: RequestContext) -> ServerResult<()> {
31        Ok(())
32    }
33
34    /// Check if ping should include detailed health information
35    fn include_health_details(&self) -> bool {
36        false
37    }
38
39    /// Get expected response time threshold in milliseconds
40    fn response_threshold_ms(&self) -> u64 {
41        5_000 // 5 seconds default
42    }
43
44    /// Process custom ping payload
45    async fn process_ping_payload(
46        &self,
47        payload: Option<&Value>,
48        _ctx: RequestContext,
49    ) -> ServerResult<Option<Value>> {
50        // Default: echo back the payload
51        Ok(payload.cloned())
52    }
53}