turbomcp-client 3.0.8

MCP client with full protocol support, bidirectional communication, and plugin middleware
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
//! Metrics middleware for MCP client.
//!
//! Tower Layer that collects request/response metrics including:
//! - Request counts (total, success, error)
//! - Response latency (min, max, average, percentiles)
//! - Method-specific statistics
//! - Requests per second
//!
//! ## Usage
//!
//! ```rust,ignore
//! use turbomcp_client::middleware::{MetricsLayer, Metrics};
//! use tower::ServiceBuilder;
//! use std::sync::Arc;
//!
//! // Create shared metrics collector
//! let metrics = Arc::new(Metrics::new());
//!
//! // Add to service stack
//! let service = ServiceBuilder::new()
//!     .layer(MetricsLayer::new(Arc::clone(&metrics)))
//!     .service(inner_service);
//!
//! // Query metrics
//! let snapshot = metrics.snapshot();
//! println!("Total requests: {}", snapshot.total_requests);
//! ```

use super::request::{McpRequest, McpResponse};
use futures_util::future::BoxFuture;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use tower_layer::Layer;
use tower_service::Service;
use turbomcp_protocol::McpError;

/// Thread-safe metrics collector.
///
/// Uses atomic operations for counters and a read-write lock for
/// more complex statistics to minimize contention.
#[derive(Debug)]
pub struct Metrics {
    /// Total request count
    total_requests: AtomicU64,
    /// Successful response count
    successful_responses: AtomicU64,
    /// Error response count
    error_responses: AtomicU64,
    /// Response time tracking (protected by RwLock)
    response_times: RwLock<ResponseTimeStats>,
    /// Per-method metrics
    method_metrics: RwLock<HashMap<String, MethodMetrics>>,
    /// Collection start time
    start_time: Instant,
}

#[derive(Debug, Default)]
struct ResponseTimeStats {
    total_ms: u64,
    count: u64,
    min_ms: Option<u64>,
    max_ms: u64,
    /// Recent response times for percentile calculation (ring buffer)
    recent: Vec<u64>,
}

/// Per-method metrics.
#[derive(Debug, Clone, Default)]
pub struct MethodMetrics {
    /// Total calls to this method
    pub count: u64,
    /// Average duration in milliseconds
    pub avg_duration_ms: f64,
    /// Successful calls
    pub success_count: u64,
    /// Error calls
    pub error_count: u64,
}

/// Metrics snapshot for reporting.
#[derive(Debug, Clone)]
pub struct MetricsSnapshot {
    /// Total requests made
    pub total_requests: u64,
    /// Successful responses received
    pub successful_responses: u64,
    /// Error responses received
    pub error_responses: u64,
    /// Average response time in milliseconds
    pub avg_response_time_ms: f64,
    /// Minimum response time in milliseconds
    pub min_response_time_ms: Option<u64>,
    /// Maximum response time in milliseconds
    pub max_response_time_ms: u64,
    /// Requests per second since start
    pub requests_per_second: f64,
    /// Per-method statistics
    pub method_metrics: HashMap<String, MethodMetrics>,
    /// Duration since metrics collection started
    pub uptime: Duration,
}

impl Metrics {
    /// Create a new metrics collector.
    #[must_use]
    pub fn new() -> Self {
        Self {
            total_requests: AtomicU64::new(0),
            successful_responses: AtomicU64::new(0),
            error_responses: AtomicU64::new(0),
            response_times: RwLock::new(ResponseTimeStats::default()),
            method_metrics: RwLock::new(HashMap::new()),
            start_time: Instant::now(),
        }
    }

    /// Record a request being sent.
    pub fn record_request(&self) {
        self.total_requests.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a response received.
    pub fn record_response(&self, method: &str, duration: Duration, is_success: bool) {
        let duration_ms = duration.as_millis() as u64;

        // Update success/error counters
        if is_success {
            self.successful_responses.fetch_add(1, Ordering::Relaxed);
        } else {
            self.error_responses.fetch_add(1, Ordering::Relaxed);
        }

        // Update response time stats
        {
            let mut stats = self.response_times.write();
            stats.total_ms += duration_ms;
            stats.count += 1;
            stats.max_ms = stats.max_ms.max(duration_ms);
            stats.min_ms = Some(stats.min_ms.map_or(duration_ms, |min| min.min(duration_ms)));

            // Keep last 1000 response times for percentile calculation
            if stats.recent.len() >= 1000 {
                stats.recent.remove(0);
            }
            stats.recent.push(duration_ms);
        }

        // Update method-specific metrics
        {
            let mut methods = self.method_metrics.write();
            let entry = methods.entry(method.to_string()).or_default();
            entry.count += 1;
            if is_success {
                entry.success_count += 1;
            } else {
                entry.error_count += 1;
            }
            // Running average
            entry.avg_duration_ms = (entry.avg_duration_ms * (entry.count - 1) as f64
                + duration_ms as f64)
                / entry.count as f64;
        }
    }

    /// Get a snapshot of current metrics.
    #[must_use]
    pub fn snapshot(&self) -> MetricsSnapshot {
        let total = self.total_requests.load(Ordering::Relaxed);
        let successful = self.successful_responses.load(Ordering::Relaxed);
        let errors = self.error_responses.load(Ordering::Relaxed);
        let uptime = self.start_time.elapsed();

        let (avg_ms, min_ms, max_ms) = {
            let stats = self.response_times.read();
            let avg = if stats.count > 0 {
                stats.total_ms as f64 / stats.count as f64
            } else {
                0.0
            };
            (avg, stats.min_ms, stats.max_ms)
        };

        let method_metrics = self.method_metrics.read().clone();

        MetricsSnapshot {
            total_requests: total,
            successful_responses: successful,
            error_responses: errors,
            avg_response_time_ms: avg_ms,
            min_response_time_ms: min_ms,
            max_response_time_ms: max_ms,
            requests_per_second: if uptime.as_secs() > 0 {
                total as f64 / uptime.as_secs_f64()
            } else {
                total as f64
            },
            method_metrics,
            uptime,
        }
    }

    /// Reset all metrics.
    pub fn reset(&self) {
        self.total_requests.store(0, Ordering::Relaxed);
        self.successful_responses.store(0, Ordering::Relaxed);
        self.error_responses.store(0, Ordering::Relaxed);
        *self.response_times.write() = ResponseTimeStats::default();
        self.method_metrics.write().clear();
    }
}

impl Default for Metrics {
    fn default() -> Self {
        Self::new()
    }
}

/// Tower Layer that adds metrics collection.
#[derive(Debug, Clone)]
pub struct MetricsLayer {
    metrics: Arc<Metrics>,
}

impl MetricsLayer {
    /// Create a new metrics layer with a shared metrics collector.
    #[must_use]
    pub fn new(metrics: Arc<Metrics>) -> Self {
        Self { metrics }
    }

    /// Create a new metrics layer with a new internal collector.
    ///
    /// Note: If you need to query metrics, use `new()` with a shared `Arc<Metrics>`.
    #[must_use]
    pub fn with_internal_metrics() -> Self {
        Self {
            metrics: Arc::new(Metrics::new()),
        }
    }

    /// Get a reference to the metrics collector.
    #[must_use]
    pub fn metrics(&self) -> &Arc<Metrics> {
        &self.metrics
    }
}

impl<S> Layer<S> for MetricsLayer {
    type Service = MetricsService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        MetricsService {
            inner,
            metrics: Arc::clone(&self.metrics),
        }
    }
}

/// Tower Service that collects metrics.
#[derive(Debug, Clone)]
pub struct MetricsService<S> {
    inner: S,
    metrics: Arc<Metrics>,
}

impl<S> MetricsService<S> {
    /// Get a reference to the inner service.
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Get a mutable reference to the inner service.
    pub fn inner_mut(&mut self) -> &mut S {
        &mut self.inner
    }

    /// Get a reference to the metrics collector.
    pub fn metrics(&self) -> &Arc<Metrics> {
        &self.metrics
    }
}

impl<S> Service<McpRequest> for MetricsService<S>
where
    S: Service<McpRequest, Response = McpResponse> + Clone + Send + 'static,
    S::Future: Send,
    S::Error: Into<McpError>,
{
    type Response = McpResponse;
    type Error = McpError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx).map_err(Into::into)
    }

    fn call(&mut self, req: McpRequest) -> Self::Future {
        let method = req.method().to_string();
        let metrics = Arc::clone(&self.metrics);
        let start = Instant::now();

        // Clone inner service for the async block
        let mut inner = self.inner.clone();
        std::mem::swap(&mut self.inner, &mut inner);

        // Record request
        metrics.record_request();

        Box::pin(async move {
            let result = inner.call(req).await.map_err(Into::into);
            let duration = start.elapsed();

            match &result {
                Ok(response) => {
                    metrics.record_response(&method, duration, response.is_success());
                }
                Err(_) => {
                    metrics.record_response(&method, duration, false);
                }
            }

            result
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use turbomcp_protocol::MessageId;
    use turbomcp_protocol::jsonrpc::{JsonRpcRequest, JsonRpcVersion};

    #[test]
    fn test_metrics_creation() {
        let metrics = Metrics::new();
        let snapshot = metrics.snapshot();

        assert_eq!(snapshot.total_requests, 0);
        assert_eq!(snapshot.successful_responses, 0);
        assert_eq!(snapshot.error_responses, 0);
    }

    #[test]
    fn test_metrics_recording() {
        let metrics = Metrics::new();

        metrics.record_request();
        metrics.record_request();
        metrics.record_response("test/method", Duration::from_millis(100), true);
        metrics.record_response("test/method", Duration::from_millis(200), false);

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.total_requests, 2);
        assert_eq!(snapshot.successful_responses, 1);
        assert_eq!(snapshot.error_responses, 1);
        assert_eq!(snapshot.min_response_time_ms, Some(100));
        assert_eq!(snapshot.max_response_time_ms, 200);
    }

    #[test]
    fn test_method_metrics() {
        let metrics = Metrics::new();

        metrics.record_response("tools/call", Duration::from_millis(50), true);
        metrics.record_response("tools/call", Duration::from_millis(100), true);
        metrics.record_response("resources/read", Duration::from_millis(75), false);

        let snapshot = metrics.snapshot();

        let tool_metrics = snapshot.method_metrics.get("tools/call").unwrap();
        assert_eq!(tool_metrics.count, 2);
        assert_eq!(tool_metrics.success_count, 2);
        assert_eq!(tool_metrics.error_count, 0);
        assert_eq!(tool_metrics.avg_duration_ms, 75.0);

        let resource_metrics = snapshot.method_metrics.get("resources/read").unwrap();
        assert_eq!(resource_metrics.count, 1);
        assert_eq!(resource_metrics.success_count, 0);
        assert_eq!(resource_metrics.error_count, 1);
    }

    #[test]
    fn test_metrics_reset() {
        let metrics = Metrics::new();

        metrics.record_request();
        metrics.record_response("test", Duration::from_millis(100), true);

        metrics.reset();

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.total_requests, 0);
        assert!(snapshot.method_metrics.is_empty());
    }

    #[test]
    fn test_metrics_layer_creation() {
        let metrics = Arc::new(Metrics::new());
        let layer = MetricsLayer::new(Arc::clone(&metrics));

        assert!(Arc::ptr_eq(&metrics, layer.metrics()));
    }

    #[tokio::test]
    async fn test_metrics_service() {
        use tower::ServiceExt;

        let metrics = Arc::new(Metrics::new());

        let mock_service = tower::service_fn(|_req: McpRequest| async {
            Ok::<_, McpError>(McpResponse::success(
                json!({"result": "ok"}),
                Duration::from_millis(10),
            ))
        });

        let mut service = MetricsLayer::new(Arc::clone(&metrics)).layer(mock_service);

        let request = McpRequest::new(JsonRpcRequest {
            jsonrpc: JsonRpcVersion,
            id: MessageId::from("test-1"),
            method: "test/method".to_string(),
            params: None,
        });

        let _ = service.ready().await.unwrap().call(request).await.unwrap();

        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.total_requests, 1);
        assert_eq!(snapshot.successful_responses, 1);
    }
}