pmcp 2.3.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
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
//! Observability events emitted by the middleware.
//!
//! Events are emitted at key points in request processing:
//! - `McpRequestEvent` - When a request is received
//! - `McpResponseEvent` - When a response is sent
//! - `McpMetric` - For metric data points
//!
//! # User Identity
//!
//! User identity (`user_id`, `tenant_id`) is captured from `AuthContext` at event
//! creation time. This is the SINGLE SOURCE OF TRUTH for user identity.
//! The observability types (`TraceContext`, `RequestMetadata`) do not duplicate
//! this information.

use super::types::{McpOperationDetails, RequestMetadata, TraceContext};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

/// Event emitted when an MCP request is received.
///
/// This event is recorded at the start of request processing, before
/// the tool/resource/prompt handler is invoked.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpRequestEvent {
    /// Trace context for correlation.
    pub trace: TraceContext,

    /// Server name (e.g., "advanced-mcp-course").
    pub server_name: String,

    /// Operation details (method, `tool_name`, etc.).
    pub operation: McpOperationDetails,

    /// Request metadata (`client_type`, `session_id` - NOT user identity).
    pub metadata: RequestMetadata,

    /// User ID from `AuthContext` (single source of truth).
    /// This is the ONLY user identity field - sourced from `AuthContext`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,

    /// Tenant ID from `AuthContext` (for multi-tenant servers).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant_id: Option<String>,

    /// Timestamp when request was received.
    pub timestamp: DateTime<Utc>,
}

impl McpRequestEvent {
    /// Create a new request event.
    pub fn new(
        trace: TraceContext,
        server_name: impl Into<String>,
        operation: McpOperationDetails,
    ) -> Self {
        Self {
            trace,
            server_name: server_name.into(),
            operation,
            metadata: RequestMetadata::default(),
            user_id: None,
            tenant_id: None,
            timestamp: Utc::now(),
        }
    }

    /// Set the request metadata.
    pub fn with_metadata(mut self, metadata: RequestMetadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// Set the user ID from `AuthContext`.
    pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    /// Set the tenant ID from `AuthContext`.
    pub fn with_tenant_id(mut self, tenant_id: impl Into<String>) -> Self {
        self.tenant_id = Some(tenant_id.into());
        self
    }
}

/// Event emitted when an MCP response is sent.
///
/// This event is recorded after request processing completes, including
/// duration, success/failure status, and error details if applicable.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpResponseEvent {
    /// Trace context for correlation.
    pub trace: TraceContext,

    /// Server name.
    pub server_name: String,

    /// Operation details.
    pub operation: McpOperationDetails,

    /// Request metadata (`client_type`, `session_id` - NOT user identity).
    pub metadata: RequestMetadata,

    /// User ID from `AuthContext` (single source of truth).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,

    /// Tenant ID from `AuthContext`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant_id: Option<String>,

    /// Request duration in milliseconds.
    pub duration_ms: u64,

    /// Whether the request succeeded.
    pub success: bool,

    /// Error code if failed (MCP error code).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_code: Option<i32>,

    /// Error message if failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,

    /// Response payload size in bytes (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_size: Option<usize>,

    /// Timestamp when response was sent.
    pub timestamp: DateTime<Utc>,
}

impl McpResponseEvent {
    /// Create a new response event for a successful request.
    pub fn success(
        trace: TraceContext,
        server_name: impl Into<String>,
        operation: McpOperationDetails,
        duration_ms: u64,
    ) -> Self {
        Self {
            trace,
            server_name: server_name.into(),
            operation,
            metadata: RequestMetadata::default(),
            user_id: None,
            tenant_id: None,
            duration_ms,
            success: true,
            error_code: None,
            error_message: None,
            response_size: None,
            timestamp: Utc::now(),
        }
    }

    /// Create a new response event for a failed request.
    pub fn failure(
        trace: TraceContext,
        server_name: impl Into<String>,
        operation: McpOperationDetails,
        duration_ms: u64,
        error_code: i32,
        error_message: impl Into<String>,
    ) -> Self {
        Self {
            trace,
            server_name: server_name.into(),
            operation,
            metadata: RequestMetadata::default(),
            user_id: None,
            tenant_id: None,
            duration_ms,
            success: false,
            error_code: Some(error_code),
            error_message: Some(error_message.into()),
            response_size: None,
            timestamp: Utc::now(),
        }
    }

    /// Set the request metadata.
    pub fn with_metadata(mut self, metadata: RequestMetadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// Set the user ID from `AuthContext`.
    pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    /// Set the tenant ID from `AuthContext`.
    pub fn with_tenant_id(mut self, tenant_id: impl Into<String>) -> Self {
        self.tenant_id = Some(tenant_id.into());
        self
    }

    /// Set the response size.
    pub fn with_response_size(mut self, size: usize) -> Self {
        self.response_size = Some(size);
        self
    }
}

/// Metric unit for observability metrics.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum MetricUnit {
    /// Time in milliseconds.
    Milliseconds,
    /// Count of items.
    Count,
    /// Size in bytes.
    Bytes,
    /// Percentage (0-100).
    Percent,
    /// No unit (dimensionless).
    None,
}

impl MetricUnit {
    /// Get the unit name as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Milliseconds => "Milliseconds",
            Self::Count => "Count",
            Self::Bytes => "Bytes",
            Self::Percent => "Percent",
            Self::None => "None",
        }
    }
}

/// A metric data point for observability.
///
/// Metrics can be counters, gauges, or histograms depending on how
/// they're used by the backend.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpMetric {
    /// Metric name (e.g., "mcp.request.duration").
    pub name: String,

    /// Metric value.
    pub value: f64,

    /// Metric unit.
    pub unit: MetricUnit,

    /// Dimensions for grouping/filtering.
    pub dimensions: HashMap<String, String>,

    /// Timestamp when the metric was recorded.
    pub timestamp: DateTime<Utc>,
}

impl McpMetric {
    /// Create a new metric.
    pub fn new(name: impl Into<String>, value: f64, unit: MetricUnit) -> Self {
        Self {
            name: name.into(),
            value,
            unit,
            dimensions: HashMap::new(),
            timestamp: Utc::now(),
        }
    }

    /// Add a dimension.
    pub fn with_dimension(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.dimensions.insert(key.into(), value.into());
        self
    }

    /// Add multiple dimensions.
    pub fn with_dimensions(mut self, dimensions: HashMap<String, String>) -> Self {
        self.dimensions.extend(dimensions);
        self
    }

    /// Create a duration metric.
    pub fn duration(name: impl Into<String>, duration_ms: u64) -> Self {
        Self::new(name, duration_ms as f64, MetricUnit::Milliseconds)
    }

    /// Create a count metric.
    pub fn count(name: impl Into<String>, count: u64) -> Self {
        Self::new(name, count as f64, MetricUnit::Count)
    }

    /// Create a size metric.
    pub fn bytes(name: impl Into<String>, bytes: usize) -> Self {
        Self::new(name, bytes as f64, MetricUnit::Bytes)
    }
}

/// Standard MCP metrics emitted by the observability middleware.
#[derive(Debug, Clone, Copy)]
pub struct StandardMetrics;

impl StandardMetrics {
    /// Metric name for request duration.
    pub const REQUEST_DURATION: &'static str = "mcp.request.duration";

    /// Metric name for request count.
    pub const REQUEST_COUNT: &'static str = "mcp.request.count";

    /// Metric name for error count.
    pub const REQUEST_ERRORS: &'static str = "mcp.request.errors";

    /// Metric name for response size.
    pub const RESPONSE_SIZE: &'static str = "mcp.response.size";

    /// Metric name for composition depth.
    pub const COMPOSITION_DEPTH: &'static str = "mcp.composition.depth";
}

/// Tracks the start time of a request for duration calculation.
///
/// This is stored in request context and used to calculate duration
/// when the response is sent.
#[derive(Debug, Clone)]
pub struct RequestStart {
    /// The instant when the request started.
    pub instant: Instant,

    /// The trace context for this request.
    pub trace: TraceContext,

    /// The operation details.
    pub operation: McpOperationDetails,

    /// The request metadata.
    pub metadata: RequestMetadata,
}

impl RequestStart {
    /// Create a new request start marker.
    pub fn new(trace: TraceContext, operation: McpOperationDetails) -> Self {
        Self {
            instant: Instant::now(),
            trace,
            operation,
            metadata: RequestMetadata::default(),
        }
    }

    /// Calculate elapsed time in milliseconds.
    pub fn elapsed_ms(&self) -> u64 {
        self.instant.elapsed().as_millis() as u64
    }

    /// Set the request metadata.
    pub fn with_metadata(mut self, metadata: RequestMetadata) -> Self {
        self.metadata = metadata;
        self
    }
}

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

    #[test]
    fn test_request_event_creation() {
        let trace = TraceContext::new_root();
        let operation = McpOperationDetails::tool_call("get_weather");

        let event = McpRequestEvent::new(trace.clone(), "test-server", operation)
            .with_user_id("user-123")
            .with_tenant_id("tenant-456");

        assert_eq!(event.server_name, "test-server");
        assert_eq!(event.user_id, Some("user-123".to_string()));
        assert_eq!(event.tenant_id, Some("tenant-456".to_string()));
        assert_eq!(event.trace.trace_id, trace.trace_id);
    }

    #[test]
    fn test_response_event_success() {
        let trace = TraceContext::new_root();
        let operation = McpOperationDetails::tool_call("get_weather");

        let event = McpResponseEvent::success(trace.clone(), "test-server", operation, 150)
            .with_response_size(1024);

        assert!(event.success);
        assert_eq!(event.duration_ms, 150);
        assert_eq!(event.response_size, Some(1024));
        assert!(event.error_code.is_none());
    }

    #[test]
    fn test_response_event_failure() {
        let trace = TraceContext::new_root();
        let operation = McpOperationDetails::tool_call("get_weather");

        let event = McpResponseEvent::failure(
            trace.clone(),
            "test-server",
            operation,
            50,
            -32600,
            "Invalid request",
        );

        assert!(!event.success);
        assert_eq!(event.error_code, Some(-32600));
        assert_eq!(event.error_message, Some("Invalid request".to_string()));
    }

    #[test]
    fn test_metric_creation() {
        let metric = McpMetric::duration("mcp.request.duration", 150)
            .with_dimension("server", "test-server")
            .with_dimension("method", "tools/call");

        assert_eq!(metric.name, "mcp.request.duration");
        assert!((metric.value - 150.0).abs() < f64::EPSILON);
        assert_eq!(metric.unit, MetricUnit::Milliseconds);
        assert_eq!(
            metric.dimensions.get("server"),
            Some(&"test-server".to_string())
        );
    }

    #[test]
    fn test_request_start_elapsed() {
        let trace = TraceContext::new_root();
        let operation = McpOperationDetails::tool_call("test");
        let start = RequestStart::new(trace, operation);

        // Just verify it doesn't panic and returns something reasonable
        let elapsed = start.elapsed_ms();
        assert!(elapsed < 1000); // Should complete in less than a second
    }

    #[test]
    fn test_metric_unit_as_str() {
        assert_eq!(MetricUnit::Milliseconds.as_str(), "Milliseconds");
        assert_eq!(MetricUnit::Count.as_str(), "Count");
        assert_eq!(MetricUnit::Bytes.as_str(), "Bytes");
        assert_eq!(MetricUnit::Percent.as_str(), "Percent");
        assert_eq!(MetricUnit::None.as_str(), "None");
    }
}