mcpkit-core 0.6.0

Core types and traits for the Model Context Protocol (MCP)
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
//! Protocol validation utilities.
//!
//! The protocol validator checks MCP message sequences for correctness,
//! helping identify protocol violations during development.

use crate::protocol::{Message, Notification, Request, RequestId, Response};
use std::collections::{HashMap, HashSet};

/// Protocol validation error.
#[derive(Debug, Clone, thiserror::Error)]
pub enum ValidationError {
    /// Response without matching request.
    #[error("orphan response: no request found for ID {id:?}")]
    OrphanResponse {
        /// The orphan response ID.
        id: RequestId,
    },

    /// Duplicate request ID.
    #[error("duplicate request ID: {id:?}")]
    DuplicateRequestId {
        /// The duplicate request ID.
        id: RequestId,
    },

    /// Request without response (timed out).
    #[error("unmatched request: {method} (ID: {id:?})")]
    UnmatchedRequest {
        /// The unmatched request ID.
        id: RequestId,
        /// The method name.
        method: String,
    },

    /// Unknown method.
    #[error("unknown method: {method}")]
    UnknownMethod {
        /// The unknown method name.
        method: String,
    },

    /// Invalid message sequence.
    #[error("invalid sequence: {message}")]
    InvalidSequence {
        /// Description of the sequence error.
        message: String,
    },

    /// Missing required initialization.
    #[error("missing initialization: {message}")]
    MissingInitialization {
        /// Description of what is missing.
        message: String,
    },
}

/// Result of protocol validation.
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether validation passed.
    pub valid: bool,
    /// Validation errors found.
    pub errors: Vec<ValidationError>,
    /// Warnings (non-fatal issues).
    pub warnings: Vec<String>,
    /// Summary statistics.
    pub stats: ValidationStats,
}

impl ValidationResult {
    /// Create a passing result.
    #[must_use]
    pub fn pass() -> Self {
        Self {
            valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            stats: ValidationStats::default(),
        }
    }

    /// Create a failing result.
    #[must_use]
    pub fn fail(errors: Vec<ValidationError>) -> Self {
        Self {
            valid: false,
            errors,
            warnings: Vec::new(),
            stats: ValidationStats::default(),
        }
    }

    /// Add a warning.
    pub fn add_warning(&mut self, warning: impl Into<String>) {
        self.warnings.push(warning.into());
    }
}

/// Validation statistics.
#[derive(Debug, Clone, Default)]
pub struct ValidationStats {
    /// Total messages validated.
    pub total_messages: usize,
    /// Requests validated.
    pub requests: usize,
    /// Responses validated.
    pub responses: usize,
    /// Notifications validated.
    pub notifications: usize,
    /// Matched request-response pairs.
    pub matched_pairs: usize,
}

/// Protocol validator for checking MCP message sequences.
///
/// The validator tracks the protocol state and checks for:
/// - Request-response matching
/// - Duplicate request IDs
/// - Proper initialization sequence
/// - Known methods
#[derive(Debug)]
pub struct ProtocolValidator {
    /// Known request methods.
    known_request_methods: HashSet<String>,
    /// Known notification methods.
    known_notification_methods: HashSet<String>,
    /// Pending requests (waiting for response).
    pending_requests: HashMap<RequestId, String>,
    /// Seen request IDs (for duplicate detection).
    seen_request_ids: HashSet<RequestId>,
    /// Whether initialization is complete.
    initialized: bool,
    /// Collected errors.
    errors: Vec<ValidationError>,
    /// Collected warnings.
    warnings: Vec<String>,
    /// Stats.
    stats: ValidationStats,
    /// Strict mode (unknown methods are errors).
    strict_mode: bool,
}

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

impl ProtocolValidator {
    /// Create a new validator with default MCP methods.
    #[must_use]
    pub fn new() -> Self {
        let mut validator = Self {
            known_request_methods: HashSet::new(),
            known_notification_methods: HashSet::new(),
            pending_requests: HashMap::new(),
            seen_request_ids: HashSet::new(),
            initialized: false,
            errors: Vec::new(),
            warnings: Vec::new(),
            stats: ValidationStats::default(),
            strict_mode: false,
        };

        // Add standard MCP methods
        validator.register_request_methods(&[
            "initialize",
            "ping",
            "tools/list",
            "tools/call",
            "resources/list",
            "resources/read",
            "resources/subscribe",
            "resources/unsubscribe",
            "prompts/list",
            "prompts/get",
            "logging/setLevel",
            "completion/complete",
            "sampling/createMessage",
            "roots/list",
        ]);

        validator.register_notification_methods(&[
            "initialized",
            "notifications/cancelled",
            "notifications/progress",
            "notifications/message",
            "notifications/resources/updated",
            "notifications/resources/list_changed",
            "notifications/tools/list_changed",
            "notifications/prompts/list_changed",
            "notifications/roots/list_changed",
        ]);

        validator
    }

    /// Enable strict mode (unknown methods are errors).
    #[must_use]
    pub fn strict(mut self) -> Self {
        self.strict_mode = true;
        self
    }

    /// Register additional request methods.
    pub fn register_request_methods(&mut self, methods: &[&str]) {
        for method in methods {
            self.known_request_methods.insert((*method).to_string());
        }
    }

    /// Register additional notification methods.
    pub fn register_notification_methods(&mut self, methods: &[&str]) {
        for method in methods {
            self.known_notification_methods
                .insert((*method).to_string());
        }
    }

    /// Validate a single message.
    pub fn validate(&mut self, message: &Message) {
        self.stats.total_messages += 1;

        match message {
            Message::Request(req) => self.validate_request(req),
            Message::Response(res) => self.validate_response(res),
            Message::Notification(notif) => self.validate_notification(notif),
        }
    }

    fn validate_request(&mut self, request: &Request) {
        self.stats.requests += 1;

        // Check for duplicate ID
        if self.seen_request_ids.contains(&request.id) {
            self.errors.push(ValidationError::DuplicateRequestId {
                id: request.id.clone(),
            });
        }
        self.seen_request_ids.insert(request.id.clone());

        // Track pending request
        self.pending_requests
            .insert(request.id.clone(), request.method.to_string());

        // Check method
        let method = request.method.as_ref();
        if !self.known_request_methods.contains(method) {
            if self.strict_mode {
                self.errors.push(ValidationError::UnknownMethod {
                    method: method.to_string(),
                });
            } else {
                self.warnings
                    .push(format!("Unknown request method: {method}"));
            }
        }

        // Check initialization
        if method == "initialize" {
            if self.initialized {
                self.warnings
                    .push("Duplicate initialize request".to_string());
            }
        } else if !self.initialized && method != "ping" {
            self.warnings
                .push(format!("Request before initialization: {method}"));
        }
    }

    fn validate_response(&mut self, response: &Response) {
        self.stats.responses += 1;

        // Check for matching request
        if self.pending_requests.remove(&response.id).is_some() {
            self.stats.matched_pairs += 1;
        } else {
            self.errors.push(ValidationError::OrphanResponse {
                id: response.id.clone(),
            });
        }
    }

    fn validate_notification(&mut self, notification: &Notification) {
        self.stats.notifications += 1;

        let method = notification.method.as_ref();

        // Check method
        if !self.known_notification_methods.contains(method) {
            if self.strict_mode {
                self.errors.push(ValidationError::UnknownMethod {
                    method: method.to_string(),
                });
            } else {
                self.warnings
                    .push(format!("Unknown notification method: {method}"));
            }
        }

        // Track initialization
        if method == "initialized" {
            self.initialized = true;
        }
    }

    /// Check for unmatched requests (call after all messages are validated).
    pub fn check_unmatched_requests(&mut self) {
        for (id, method) in self.pending_requests.drain() {
            self.errors
                .push(ValidationError::UnmatchedRequest { id, method });
        }
    }

    /// Get the validation result.
    #[must_use]
    pub fn result(&self) -> ValidationResult {
        ValidationResult {
            valid: self.errors.is_empty(),
            errors: self.errors.clone(),
            warnings: self.warnings.clone(),
            stats: self.stats.clone(),
        }
    }

    /// Finalize validation and get result.
    #[must_use]
    pub fn finalize(mut self) -> ValidationResult {
        self.check_unmatched_requests();
        self.result()
    }

    /// Reset the validator state.
    pub fn reset(&mut self) {
        self.pending_requests.clear();
        self.seen_request_ids.clear();
        self.initialized = false;
        self.errors.clear();
        self.warnings.clear();
        self.stats = ValidationStats::default();
    }
}

/// Validate a sequence of messages.
#[must_use]
pub fn validate_message_sequence(messages: &[Message]) -> ValidationResult {
    let mut validator = ProtocolValidator::new();

    for msg in messages {
        validator.validate(msg);
    }

    validator.finalize()
}

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

    #[test]
    fn test_valid_sequence() {
        let messages = vec![
            Message::Request(Request::new("initialize", 1)),
            Message::Response(Response::success(RequestId::from(1), serde_json::json!({}))),
            Message::Notification(Notification::new("initialized")),
            Message::Request(Request::new("tools/list", 2)),
            Message::Response(Response::success(
                RequestId::from(2),
                serde_json::json!({ "tools": [] }),
            )),
        ];

        let result = validate_message_sequence(&messages);
        assert!(result.valid);
        assert!(result.errors.is_empty());
        assert_eq!(result.stats.matched_pairs, 2);
    }

    #[test]
    fn test_orphan_response() {
        let messages = vec![Message::Response(Response::success(
            RequestId::from(999),
            serde_json::json!({}),
        ))];

        let result = validate_message_sequence(&messages);
        assert!(!result.valid);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::OrphanResponse { .. }))
        );
    }

    #[test]
    fn test_duplicate_request_id() {
        let messages = vec![
            Message::Request(Request::new("ping", 1)),
            Message::Request(Request::new("ping", 1)), // Duplicate!
        ];

        let result = validate_message_sequence(&messages);
        assert!(!result.valid);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::DuplicateRequestId { .. }))
        );
    }

    #[test]
    fn test_unmatched_request() {
        let messages = vec![
            Message::Request(Request::new("ping", 1)),
            // No response!
        ];

        let result = validate_message_sequence(&messages);
        assert!(!result.valid);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::UnmatchedRequest { .. }))
        );
    }

    #[test]
    fn test_strict_mode() {
        let mut validator = ProtocolValidator::new().strict();

        validator.validate(&Message::Request(Request::new("unknown/method", 1)));

        let result = validator.result();
        assert!(!result.valid);
        assert!(
            result
                .errors
                .iter()
                .any(|e| matches!(e, ValidationError::UnknownMethod { .. }))
        );
    }
}