turbomcp-protocol 3.1.4

Complete MCP protocol implementation with types, traits, context management, and message handling
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Comprehensive tests
use super::*;

//! Comprehensive tests for capabilities.rs - targeting 100% coverage
//! Testing all capability negotiation, matching, and management functionality

use std::collections::HashMap;
use crate::capabilities::*;
use crate::types::*;

// Helper functions for creating test capabilities
fn create_minimal_client_capabilities() -> ClientCapabilities {
    ClientCapabilities::default()
}

fn create_minimal_server_capabilities() -> ServerCapabilities {
    ServerCapabilities::default()
}

fn create_full_client_capabilities() -> ClientCapabilities {
    ClientCapabilities {
        sampling: Some(SamplingCapabilities),
        roots: Some(RootsCapabilities {
            list_changed: Some(true),
        }),
        elicitation: Some(ElicitationCapabilities::default()),
        experimental: Some({
            let mut experimental = HashMap::new();
            experimental.insert(
                "custom_feature".to_string(),
                serde_json::json!({"enabled": true}),
            );
            experimental
        }),
    }
}

fn create_full_server_capabilities() -> ServerCapabilities {
    ServerCapabilities {
        tools: Some(ToolsCapabilities {
            list_changed: Some(true),
        }),
        prompts: Some(PromptsCapabilities {
            list_changed: Some(true),
        }),
        resources: Some(ResourcesCapabilities {
            subscribe: Some(true),
            list_changed: Some(true),
        }),
        logging: Some(LoggingCapabilities),
        completions: Some(CompletionCapabilities),
        experimental: Some({
            let mut experimental = HashMap::new();
            experimental.insert(
                "server_custom".to_string(),
                serde_json::json!({"version": "1.0"}),
            );
            experimental
        }),
    }
}

fn create_partial_client_capabilities() -> ClientCapabilities {
    ClientCapabilities {
        sampling: Some(SamplingCapabilities),
        roots: None,
        elicitation: None,
        experimental: None,
    }
}

fn create_partial_server_capabilities() -> ServerCapabilities {
    ServerCapabilities {
        tools: Some(ToolsCapabilities {
            list_changed: Some(false),
        }),
        prompts: None,
        resources: None,
        logging: None,
        completions: None,
        experimental: None,
    }
}

// Custom compatibility function for testing
fn custom_compatibility_function(
    _client: &ClientCapabilities,
    server: &ServerCapabilities,
) -> bool {
    server.tools.is_some()
}

#[test]
fn test_capability_matcher_new() {
    let matcher = CapabilityMatcher::new();

    // Test that default rules are set correctly
    let client = create_full_client_capabilities();
    let server = create_full_server_capabilities();

    assert!(matcher.is_compatible("tools", &client, &server));
    assert!(matcher.is_compatible("prompts", &client, &server));
    assert!(matcher.is_compatible("resources", &client, &server));
    assert!(matcher.is_compatible("logging", &client, &server));
    assert!(matcher.is_compatible("sampling", &client, &server));
    assert!(matcher.is_compatible("roots", &client, &server));
    assert!(matcher.is_compatible("progress", &client, &server)); // Should be optional
}

#[test]
fn test_capability_matcher_default() {
    let matcher = CapabilityMatcher::default();
    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Progress should be optional (always compatible)
    assert!(matcher.is_compatible("progress", &client, &server));
}

#[test]
fn test_add_rule() {
    let mut matcher = CapabilityMatcher::new();

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Add a custom rule
    matcher.add_rule("custom_feature", CompatibilityRule::RequireClient);

    // Should not be compatible (client doesn't have custom_feature)
    assert!(!matcher.is_compatible("custom_feature", &client, &server));

    // Test with experimental feature
    let client_with_experimental = create_full_client_capabilities();
    assert!(matcher.is_compatible("custom_feature", &client_with_experimental, &server));
}

#[test]
fn test_set_default() {
    let mut matcher = CapabilityMatcher::new();
    matcher.set_default("test_feature", true);
    matcher.add_rule("test_feature", CompatibilityRule::Optional); // Make it compatible

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Test that negotiation includes default features
    let result = matcher.negotiate(&client, &server);
    assert!(result.is_ok());

    let capability_set = result.unwrap();
    assert!(capability_set.has_feature("test_feature"));
}

#[test]
fn test_compatibility_rules_require_both() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("both_required", CompatibilityRule::RequireBoth);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Neither has the feature
    assert!(!matcher.is_compatible("both_required", &client, &server));

    // Only client has it (via experimental)
    let client_with_experimental = create_full_client_capabilities();
    assert!(!matcher.is_compatible("both_required", &client_with_experimental, &server));

    // Only server has it (via experimental)
    let server_with_experimental = create_full_server_capabilities();
    assert!(!matcher.is_compatible("both_required", &client, &server_with_experimental));

    // Both have it
    assert!(!matcher.is_compatible(
        "both_required",
        &client_with_experimental,
        &server_with_experimental
    ));
}

#[test]
fn test_compatibility_rules_require_client() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("custom_feature", CompatibilityRule::RequireClient);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Client doesn't have it
    assert!(!matcher.is_compatible("custom_feature", &client, &server));

    // Client has it via experimental (custom_feature is in the experimental features)
    let client_with_experimental = create_full_client_capabilities();
    assert!(matcher.is_compatible("custom_feature", &client_with_experimental, &server));
}

#[test]
fn test_compatibility_rules_require_server() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("server_custom", CompatibilityRule::RequireServer);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Server doesn't have it
    assert!(!matcher.is_compatible("server_custom", &client, &server));

    // Server has it via experimental (server_custom is in the experimental features)
    let server_with_experimental = create_full_server_capabilities();
    assert!(matcher.is_compatible("server_custom", &client, &server_with_experimental));
}

#[test]
fn test_compatibility_rules_optional() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("optional_feature", CompatibilityRule::Optional);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Should always be compatible for optional features
    assert!(matcher.is_compatible("optional_feature", &client, &server));
}

#[test]
fn test_compatibility_rules_custom() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule(
        "custom_rule",
        CompatibilityRule::Custom(custom_compatibility_function),
    );

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Custom function requires server.tools
    assert!(!matcher.is_compatible("custom_rule", &client, &server));

    let server_with_tools = create_full_server_capabilities();
    assert!(matcher.is_compatible("custom_rule", &client, &server_with_tools));
}

#[test]
fn test_unknown_feature_compatibility() {
    let matcher = CapabilityMatcher::new();

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Unknown features should check if either side supports it
    assert!(!matcher.is_compatible("unknown_feature", &client, &server));

    // With experimental features
    let client_with_experimental = create_full_client_capabilities();
    let server_with_experimental = create_full_server_capabilities();

    // Should find custom_feature in client experimental
    assert!(matcher.is_compatible("custom_feature", &client_with_experimental, &server));

    // Should find server_custom in server experimental
    assert!(matcher.is_compatible("server_custom", &client, &server_with_experimental));
}

// NOTE: Removed tests for private methods client_has_feature, server_has_feature, get_all_features
// These are tested indirectly through the public negotiate and is_compatible methods

#[test]
fn test_negotiate_success() {
    let matcher = CapabilityMatcher::new();

    let client = create_full_client_capabilities();
    let server = create_full_server_capabilities();

    let result = matcher.negotiate(&client, &server);
    assert!(result.is_ok());

    let capability_set = result.unwrap();
    assert!(capability_set.has_feature("sampling"));
    assert!(capability_set.has_feature("tools"));
    assert!(capability_set.has_feature("roots"));
    assert!(capability_set.has_feature("prompts"));
    assert!(capability_set.has_feature("resources"));
    assert!(capability_set.has_feature("logging"));
    assert!(capability_set.has_feature("progress"));

    // Check that experimental features are included
    assert!(capability_set.has_feature("custom_feature"));
    assert!(capability_set.has_feature("server_custom"));
}

#[test]
fn test_negotiate_incompatible_features() {
    let mut matcher = CapabilityMatcher::new();

    // Add a rule that will fail
    matcher.add_rule("impossible_feature", CompatibilityRule::RequireBoth);
    matcher.set_default("impossible_feature", true); // Force it to be checked

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = matcher.negotiate(&client, &server);
    assert!(result.is_err());

    match result.unwrap_err() {
        CapabilityError::IncompatibleFeatures(features) => {
            assert!(features.contains(&"impossible_feature".to_string()));
        }
        _ => panic!("Expected IncompatibleFeatures error"),
    }
}

#[test]
fn test_negotiate_with_defaults() {
    let mut matcher = CapabilityMatcher::new();
    matcher.set_default("default_enabled", true);
    matcher.set_default("default_disabled", false);
    matcher.add_rule("default_enabled", CompatibilityRule::Optional);
    matcher.add_rule("default_disabled", CompatibilityRule::Optional);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = matcher.negotiate(&client, &server);
    assert!(result.is_ok());

    let capability_set = result.unwrap();
    assert!(capability_set.has_feature("default_enabled"));
    // Note: Even false defaults may be included if the feature is in all_features
    // The test logic checks if feature is in all_features AND default is enabled
}

#[test]
fn test_capability_negotiator_new() {
    let matcher = CapabilityMatcher::new();
    let negotiator = CapabilityNegotiator::new(matcher);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = negotiator.negotiate(&client, &server);
    assert!(result.is_ok());
}

#[test]
fn test_capability_negotiator_default() {
    let negotiator = CapabilityNegotiator::default();

    let client = create_full_client_capabilities();
    let server = create_full_server_capabilities();

    let result = negotiator.negotiate(&client, &server);
    assert!(result.is_ok());
}

#[test]
fn test_capability_negotiator_with_strict_mode() {
    let negotiator = CapabilityNegotiator::default().with_strict_mode();

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = negotiator.negotiate(&client, &server);
    assert!(result.is_ok()); // Should work with minimal capabilities
}

#[test]
fn test_capability_negotiator_strict_mode_failure() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("impossible", CompatibilityRule::RequireBoth);
    matcher.set_default("impossible", true);

    let negotiator = CapabilityNegotiator::new(matcher).with_strict_mode();

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = negotiator.negotiate(&client, &server);
    assert!(result.is_err());
}

#[test]
fn test_capability_negotiator_non_strict_mode() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("incompatible", CompatibilityRule::RequireBoth);
    matcher.set_default("incompatible", true);

    let negotiator = CapabilityNegotiator::new(matcher); // Non-strict by default

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    let result = negotiator.negotiate(&client, &server);
    assert!(result.is_ok()); // Should succeed in non-strict mode

    let capability_set = result.unwrap();
    assert!(!capability_set.has_feature("incompatible")); // Feature should be excluded
}

#[test]
fn test_is_feature_enabled() {
    let negotiator = CapabilityNegotiator::default();
    let client = create_full_client_capabilities();
    let server = create_full_server_capabilities();

    let capability_set = negotiator.negotiate(&client, &server).unwrap();

    assert!(CapabilityNegotiator::is_feature_enabled(
        &capability_set,
        "tools"
    ));
    assert!(CapabilityNegotiator::is_feature_enabled(
        &capability_set,
        "sampling"
    ));
    assert!(!CapabilityNegotiator::is_feature_enabled(
        &capability_set,
        "nonexistent"
    ));
}

#[test]
fn test_get_enabled_features() {
    let negotiator = CapabilityNegotiator::default();
    let client = create_full_client_capabilities();
    let server = create_full_server_capabilities();

    let capability_set = negotiator.negotiate(&client, &server).unwrap();
    let features = CapabilityNegotiator::get_enabled_features(&capability_set);

    // Should be sorted
    assert!(features.windows(2).all(|w| w[0] <= w[1]));

    // Should contain expected features
    assert!(features.contains(&"tools".to_string()));
    assert!(features.contains(&"sampling".to_string()));
}

#[test]
fn test_capability_set_empty() {
    let capability_set = CapabilitySet::empty();

    assert_eq!(capability_set.feature_count(), 0);
    assert!(!capability_set.has_feature("anything"));
}

#[test]
fn test_capability_set_enable_disable_feature() {
    let mut capability_set = CapabilitySet::empty();

    assert!(!capability_set.has_feature("test"));
    assert_eq!(capability_set.feature_count(), 0);

    capability_set.enable_feature("test".to_string());
    assert!(capability_set.has_feature("test"));
    assert_eq!(capability_set.feature_count(), 1);

    capability_set.disable_feature("test");
    assert!(!capability_set.has_feature("test"));
    assert_eq!(capability_set.feature_count(), 0);
}

#[test]
fn test_capability_set_metadata() {
    let mut capability_set = CapabilitySet::empty();

    assert!(capability_set.get_metadata("key").is_none());

    capability_set.add_metadata("key".to_string(), serde_json::json!({"value": 42}));

    let metadata = capability_set.get_metadata("key");
    assert!(metadata.is_some());
    assert_eq!(metadata.unwrap()["value"], 42);
}

#[test]
fn test_capability_set_summary() {
    let mut capability_set = CapabilitySet::empty();

    // Set up client capabilities
    capability_set.client_capabilities.sampling = Some(SamplingCapabilities);
    capability_set.client_capabilities.roots = Some(RootsCapabilities::default());

    // Set up server capabilities
    capability_set.server_capabilities.tools = Some(ToolsCapabilities::default());
    capability_set.server_capabilities.prompts = Some(PromptsCapabilities::default());
    capability_set.server_capabilities.resources = Some(ResourcesCapabilities::default());

    // Enable some features
    capability_set.enable_feature("feature1".to_string());
    capability_set.enable_feature("feature2".to_string());

    let summary = capability_set.summary();

    assert_eq!(summary.total_features, 2);
    assert_eq!(summary.client_features, 2); // sampling + roots
    assert_eq!(summary.server_features, 3); // tools + prompts + resources
    assert!(summary.enabled_features.contains(&"feature1".to_string()));
    assert!(summary.enabled_features.contains(&"feature2".to_string()));
}

// NOTE: Removed tests for private methods count_client_features and count_server_features
// These are tested indirectly through the public summary() method

#[test]
fn test_capability_error_display() {
    let error =
        CapabilityError::IncompatibleFeatures(vec!["feature1".to_string(), "feature2".to_string()]);

    let display = format!("{error}");
    assert!(display.contains("Incompatible features"));
    assert!(display.contains("feature1"));
    assert!(display.contains("feature2"));

    let error = CapabilityError::RequiredFeatureMissing("required_feature".to_string());
    let display = format!("{error}");
    assert!(display.contains("Required feature missing"));
    assert!(display.contains("required_feature"));

    let error = CapabilityError::VersionMismatch {
        client: "1.0".to_string(),
        server: "2.0".to_string(),
    };
    let display = format!("{error}");
    assert!(display.contains("Protocol version mismatch"));
    assert!(display.contains("client=1.0"));
    assert!(display.contains("server=2.0"));

    let error = CapabilityError::NegotiationFailed("test reason".to_string());
    let display = format!("{error}");
    assert!(display.contains("Capability negotiation failed"));
    assert!(display.contains("test reason"));
}

#[test]
fn test_capability_summary_serialization() {
    let summary = CapabilitySummary {
        total_features: 5,
        client_features: 2,
        server_features: 3,
        enabled_features: vec!["feature1".to_string(), "feature2".to_string()],
    };

    // Test serialization
    let json = serde_json::to_string(&summary).unwrap();
    let deserialized: CapabilitySummary = serde_json::from_str(&json).unwrap();

    assert_eq!(summary.total_features, deserialized.total_features);
    assert_eq!(summary.client_features, deserialized.client_features);
    assert_eq!(summary.server_features, deserialized.server_features);
    assert_eq!(summary.enabled_features, deserialized.enabled_features);
}

// Test utility functions
#[test]
fn test_utils_minimal_capabilities() {
    let client = utils::minimal_client_capabilities();
    assert!(client.sampling.is_none());
    assert!(client.roots.is_none());
    assert!(client.experimental.is_none());

    let server = utils::minimal_server_capabilities();
    assert!(server.tools.is_none());
    assert!(server.prompts.is_none());
    assert!(server.resources.is_none());
    assert!(server.logging.is_none());
    assert!(server.experimental.is_none());
}

#[test]
fn test_utils_full_capabilities() {
    let client = utils::full_client_capabilities();
    assert!(client.sampling.is_some());
    assert!(client.roots.is_some());
    assert!(client.elicitation.is_some());

    let server = utils::full_server_capabilities();
    assert!(server.tools.is_some());
    assert!(server.prompts.is_some());
    assert!(server.resources.is_some());
    assert!(server.logging.is_some());
    assert!(server.completions.is_some());
}

#[test]
fn test_utils_are_compatible() {
    let client = utils::full_client_capabilities();
    let server = utils::full_server_capabilities();

    assert!(utils::are_compatible(&client, &server));

    // Test with incompatible setup
    let minimal_client = utils::minimal_client_capabilities();
    let minimal_server = utils::minimal_server_capabilities();

    assert!(utils::are_compatible(&minimal_client, &minimal_server)); // Should work with defaults
}

#[test]
fn test_compatibility_rule_clone_debug() {
    let rule1 = CompatibilityRule::RequireBoth;
    let rule2 = rule1.clone();

    // Test Debug formatting
    let debug_str = format!("{rule2:?}");
    assert!(debug_str.contains("RequireBoth"));

    let custom_rule = CompatibilityRule::Custom(custom_compatibility_function);
    let debug_str = format!("{custom_rule:?}");
    assert!(debug_str.contains("Custom"));
}

#[test]
fn test_capability_matcher_clone_debug() {
    let matcher = CapabilityMatcher::new();
    let cloned_matcher = matcher.clone();

    let debug_str = format!("{cloned_matcher:?}");
    assert!(debug_str.contains("CapabilityMatcher"));
}

#[test]
fn test_capability_negotiator_clone_debug() {
    let negotiator = CapabilityNegotiator::default();
    let cloned_negotiator = negotiator.clone();

    let debug_str = format!("{cloned_negotiator:?}");
    assert!(debug_str.contains("CapabilityNegotiator"));
}

#[test]
fn test_compatibility_edge_cases() {
    let matcher = CapabilityMatcher::new();

    // Test with partial capabilities
    let partial_client = create_partial_client_capabilities();
    let partial_server = create_partial_server_capabilities();

    // Sampling should be compatible (client has it)
    assert!(matcher.is_compatible("sampling", &partial_client, &partial_server));

    // Tools should be compatible (server has it)
    assert!(matcher.is_compatible("tools", &partial_client, &partial_server));

    // Roots should not be compatible (client doesn't have it, rule requires client)
    assert!(!matcher.is_compatible("roots", &partial_client, &partial_server));

    // Prompts should not be compatible (server doesn't have it, rule requires server)
    assert!(!matcher.is_compatible("prompts", &partial_client, &partial_server));
}

#[test]
fn test_complex_negotiation_scenario() {
    let mut matcher = CapabilityMatcher::new();

    // Add custom rules for complex scenario
    matcher.add_rule("advanced_feature", CompatibilityRule::RequireBoth);
    matcher.add_rule("optional_enhancement", CompatibilityRule::Optional);
    matcher.add_rule("client_specific", CompatibilityRule::RequireClient);
    matcher.set_default("optional_enhancement", true);

    let mut client = create_partial_client_capabilities();
    client.experimental = Some({
        let mut exp = HashMap::new();
        exp.insert("advanced_feature".to_string(), serde_json::json!(true));
        exp.insert("client_specific".to_string(), serde_json::json!(true));
        exp
    });

    let mut server = create_partial_server_capabilities();
    server.experimental = Some({
        let mut exp = HashMap::new();
        exp.insert("advanced_feature".to_string(), serde_json::json!(true));
        exp
    });

    let result = matcher.negotiate(&client, &server);
    assert!(result.is_ok());

    let capability_set = result.unwrap();
    assert!(capability_set.has_feature("sampling")); // Client standard feature
    assert!(capability_set.has_feature("tools")); // Server standard feature
    assert!(capability_set.has_feature("advanced_feature")); // Both have it
    assert!(capability_set.has_feature("client_specific")); // Client has it
    assert!(capability_set.has_feature("optional_enhancement")); // Default enabled
    assert!(capability_set.has_feature("progress")); // Always default
}

#[test]
fn test_error_propagation() {
    let mut matcher = CapabilityMatcher::new();
    matcher.add_rule("failing_feature", CompatibilityRule::Custom(|_, _| false));
    matcher.set_default("failing_feature", true);

    let client = create_minimal_client_capabilities();
    let server = create_minimal_server_capabilities();

    // Should fail in strict mode
    let strict_negotiator = CapabilityNegotiator::new(matcher.clone()).with_strict_mode();
    let result = strict_negotiator.negotiate(&client, &server);
    assert!(result.is_err());

    // Should succeed in non-strict mode
    let lenient_negotiator = CapabilityNegotiator::new(matcher);
    let result = lenient_negotiator.negotiate(&client, &server);
    assert!(result.is_ok());
}