fastmcp-protocol 0.7.0

MCP protocol types and JSON-RPC implementation
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
//! Typed `server/discover` vocabulary for the final MCP discovery surface.
//!
//! The registry is deliberately declarative: it records only handlers and
//! notification delivery paths that the surrounding server has actually
//! installed. Discovery capabilities are derived from that immutable record,
//! so a wire claim cannot accidentally advertise an unregistered behavior.

use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::fmt;

use serde::{
    Deserialize, Deserializer, Serialize, Serializer,
    de::Error as _,
    ser::{Error as _, SerializeMap},
};
use serde_json::{Value, value::RawValue};

use crate::common_types::{Implementation, OpenMetadata};
use crate::result::{
    CacheTtl, ExactJsonObject, FinalResultMetadataRole, encode_exact_object,
    parse_exact_result_object, validate_final_result_metadata_entries,
};
use crate::{
    ExtensionId, FINAL_CLIENT_CAPABILITIES_META_KEY, FINAL_PROTOCOL_VERSION_META_KEY,
    ResultPeerDiagnostic, ServerInfo, protocol_version::FINAL_PROTOCOL_VERSION,
};

/// The exact JSON-RPC method for final server discovery.
pub const SERVER_DISCOVER_METHOD: &str = "server/discover";

/// The exact protocol-version list advertised by this final-only surface.
pub const SERVER_DISCOVER_SUPPORTED_VERSIONS: &[&str] = &[FINAL_PROTOCOL_VERSION];

/// Maximum UTF-8 bytes permitted for server-provided discovery instructions.
pub const MAX_SERVER_INSTRUCTIONS_BYTES: usize = 16 * 1024;

/// Maximum number of enabled extension settings in one discovery result.
pub const MAX_DISCOVERY_EXTENSION_SETTINGS: usize = 64;

/// Maximum UTF-8 bytes in an enabled extension setting name.
pub const MAX_DISCOVERY_EXTENSION_NAME_BYTES: usize = 256;

/// Maximum JSON bytes in an enabled extension setting value.
pub const MAX_DISCOVERY_EXTENSION_VALUE_BYTES: usize = 16 * 1024;

/// Reserved result-metadata key that identifies the responding server.
pub const SERVER_DISCOVER_SERVER_INFO_META_KEY: &str = "io.modelcontextprotocol/serverInfo";

/// Typed final `params` for a `server/discover` request.
///
/// Final requests always carry the common request metadata. Unknown
/// method-specific members remain inert and round-trip so a newer peer does
/// not become undecodable merely by extending this open object.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct ServerDiscoverRequest {
    #[serde(rename = "_meta")]
    metadata: OpenMetadata,
    #[serde(flatten)]
    extras: BTreeMap<String, Value>,
}

impl Default for ServerDiscoverRequest {
    fn default() -> Self {
        let metadata = OpenMetadata::try_from_entries([
            (
                FINAL_PROTOCOL_VERSION_META_KEY.to_owned(),
                Value::String(FINAL_PROTOCOL_VERSION.to_owned()),
            ),
            (
                FINAL_CLIENT_CAPABILITIES_META_KEY.to_owned(),
                Value::Object(serde_json::Map::new()),
            ),
        ])
        .expect("the fixed final discovery request metadata is valid");
        Self {
            metadata,
            extras: BTreeMap::new(),
        }
    }
}

impl ServerDiscoverRequest {
    /// Returns the required request metadata without granting its self-reported
    /// values any authority.
    #[must_use]
    pub fn metadata(&self) -> &OpenMetadata {
        &self.metadata
    }
}

#[derive(Deserialize)]
struct ServerDiscoverRequestWire {
    #[serde(rename = "_meta")]
    metadata: OpenMetadata,
    #[serde(flatten)]
    extras: BTreeMap<String, Value>,
}

impl<'de> Deserialize<'de> for ServerDiscoverRequest {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = ServerDiscoverRequestWire::deserialize(deserializer)?;
        let protocol_version = wire.metadata.protocol_version().map_err(D::Error::custom)?;
        let client_capabilities = wire
            .metadata
            .client_capabilities()
            .map_err(D::Error::custom)?;
        if protocol_version != Some(FINAL_PROTOCOL_VERSION) || client_capabilities.is_none() {
            return Err(D::Error::custom(
                ServerDiscoveryError::InvalidRequestMetadata,
            ));
        }
        Ok(Self {
            metadata: wire.metadata,
            extras: wire.extras,
        })
    }
}

/// A server behavior whose installation can be advertised through discovery.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ServerBehavior {
    /// The deprecated `logging/request` emitter is installed.
    LoggingRequestEmitter,
    /// The `completion/complete` dispatch target is installed.
    CompletionComplete,
    /// The `tools/list` dispatch target is installed.
    ToolsList,
    /// The `notifications/tools/list_changed` producer is installed.
    ToolsListChangedNotification,
    /// The `resources/list` dispatch target is installed.
    ResourcesList,
    /// The `notifications/resources/list_changed` producer is installed.
    ResourcesListChangedNotification,
    /// The `resources/subscribe` dispatch target is installed.
    ResourcesSubscribe,
    /// The subscription listener used by resource subscriptions is installed.
    SubscriptionsListen,
    /// The resource-update delivery path is installed.
    ResourceUpdateDelivery,
    /// The `prompts/list` dispatch target is installed.
    PromptsList,
    /// The `notifications/prompts/list_changed` producer is installed.
    PromptsListChangedNotification,
}

/// Immutable registry of server behavior actually installed by the runtime.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ServerBehaviorRegistry {
    installed: BTreeSet<ServerBehavior>,
}

impl ServerBehaviorRegistry {
    /// Creates a registry from the installed behaviors.
    #[must_use]
    pub fn from_behaviors(behaviors: impl IntoIterator<Item = ServerBehavior>) -> Self {
        Self {
            installed: behaviors.into_iter().collect(),
        }
    }

    /// Returns whether a behavior has been installed.
    #[must_use]
    pub fn contains(&self, behavior: ServerBehavior) -> bool {
        self.installed.contains(&behavior)
    }
}

/// A validated server instruction string.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ServerInstructions(String);

impl ServerInstructions {
    /// Validates and retains discovery instructions.
    pub fn new(value: impl Into<String>) -> Result<Self, ServerInstructionError> {
        let value = value.into();
        if value.len() > MAX_SERVER_INSTRUCTIONS_BYTES {
            return Err(ServerInstructionError::TooLarge {
                actual: value.len(),
                maximum: MAX_SERVER_INSTRUCTIONS_BYTES,
            });
        }
        Ok(Self(value))
    }

    /// Returns the validated instruction text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Serialize for ServerInstructions {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for ServerInstructions {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::new(value).map_err(D::Error::custom)
    }
}

/// Why a server instruction string was rejected.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ServerInstructionError {
    /// The UTF-8 instruction string exceeded the fixed discovery bound.
    TooLarge {
        /// Observed UTF-8 byte length.
        actual: usize,
        /// Maximum accepted UTF-8 byte length.
        maximum: usize,
    },
}

impl fmt::Display for ServerInstructionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooLarge { actual, maximum } => {
                write!(
                    formatter,
                    "server instructions are {actual} bytes; maximum is {maximum}"
                )
            }
        }
    }
}

impl Error for ServerInstructionError {}

/// A strict cache scope received from or emitted on the discovery wire.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
enum DiscoveryCacheScope {
    Public,
    Private,
}

impl<'de> Deserialize<'de> for DiscoveryCacheScope {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match String::deserialize(deserializer)?.as_str() {
            "public" => Ok(Self::Public),
            "private" => Ok(Self::Private),
            _ => Err(D::Error::custom("cacheScope must be `public` or `private`")),
        }
    }
}

/// The only final `server/discover` discriminator that can establish a
/// modern session. A missing discriminator keeps the pinned compatibility
/// path, but no other final result branch is a discovery result.
const COMPLETE_DISCOVERY_RESULT_TYPE: &str = "complete";

/// Final-result branch members that contradict a complete discovery result.
///
/// Discovery retains schema-open extension members, but it must never treat a
/// continuation, task, or generic result envelope as discovery merely because
/// it also carries the required discovery fields.
const DISCOVERY_CONTRADICTORY_RESULT_MEMBERS: [&str; 13] = [
    "serverInfo",
    "input",
    "inputRequests",
    "request",
    "requestState",
    "taskId",
    "status",
    "statusMessage",
    "createdAt",
    "lastUpdatedAt",
    "pollIntervalMs",
    "result",
    "error",
];

/// Required final caching hints for a `server/discover` result.
///
/// Safe local construction is intentionally limited to the private scope.
/// A public cache scope is peer provenance admitted only while decoding an
/// already-received wire result; it is not a general authority grant.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct DiscoveryCacheHints {
    #[serde(rename = "ttlMs")]
    ttl_ms: CacheTtl,
    #[serde(rename = "cacheScope")]
    scope: DiscoveryCacheScope,
}

impl DiscoveryCacheHints {
    /// Creates a server-generated, private cache hint with a nonnegative TTL
    /// in milliseconds.
    #[must_use]
    pub fn private_ttl_ms(ttl_ms: u64) -> Self {
        Self {
            ttl_ms: CacheTtl::milliseconds(ttl_ms),
            scope: DiscoveryCacheScope::Private,
        }
    }

    /// Returns the lossless cache TTL wire value.
    #[must_use]
    pub fn ttl_ms(&self) -> &CacheTtl {
        &self.ttl_ms
    }

    /// Returns whether this was an admitted public peer cache hint.
    #[must_use]
    pub const fn is_public(&self) -> bool {
        matches!(self.scope, DiscoveryCacheScope::Public)
    }

    const fn from_peer_wire(ttl_ms: CacheTtl, scope: DiscoveryCacheScope) -> Self {
        Self { ttl_ms, scope }
    }
}

/// Typed capability shape derived from an installed behavior registry.
///
/// `ServerCapabilities` is deliberately an open object in the final schema.
/// Retaining its members as JSON preserves both known capability settings and
/// future peer-defined capabilities without recasting them as local authority.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ServerDiscoverCapabilities {
    members: BTreeMap<String, Value>,
}

impl Serialize for ServerDiscoverCapabilities {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.members.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ServerDiscoverCapabilities {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let members = BTreeMap::<String, Value>::deserialize(deserializer)?;
        validate_capability_members(&members).map_err(D::Error::custom)?;
        Ok(Self { members })
    }
}

impl ServerDiscoverCapabilities {
    /// Derives discovery capabilities from installed behavior and extensions.
    pub fn from_registry(
        registry: &ServerBehaviorRegistry,
        extensions: BTreeMap<String, Value>,
    ) -> Result<Self, ServerDiscoveryError> {
        validate_extensions(&extensions)?;

        let tools_list = registry.contains(ServerBehavior::ToolsList);
        let resources_list = registry.contains(ServerBehavior::ResourcesList);
        let prompts_list = registry.contains(ServerBehavior::PromptsList);
        let resources_subscribe = registry.contains(ServerBehavior::ResourcesSubscribe)
            && registry.contains(ServerBehavior::SubscriptionsListen)
            && registry.contains(ServerBehavior::ResourceUpdateDelivery);
        let mut members = BTreeMap::new();

        if registry.contains(ServerBehavior::LoggingRequestEmitter) {
            members.insert("logging".to_owned(), Value::Object(serde_json::Map::new()));
        }
        if registry.contains(ServerBehavior::CompletionComplete) {
            members.insert(
                "completions".to_owned(),
                Value::Object(serde_json::Map::new()),
            );
        }
        if tools_list {
            let mut tools = serde_json::Map::new();
            if registry.contains(ServerBehavior::ToolsListChangedNotification) {
                tools.insert("listChanged".to_owned(), Value::Bool(true));
            }
            members.insert("tools".to_owned(), Value::Object(tools));
        }
        if resources_list {
            let mut resources = serde_json::Map::new();
            if resources_subscribe {
                resources.insert("subscribe".to_owned(), Value::Bool(true));
            }
            if registry.contains(ServerBehavior::ResourcesListChangedNotification) {
                resources.insert("listChanged".to_owned(), Value::Bool(true));
            }
            members.insert("resources".to_owned(), Value::Object(resources));
        }
        if prompts_list {
            let mut prompts = serde_json::Map::new();
            if registry.contains(ServerBehavior::PromptsListChangedNotification) {
                prompts.insert("listChanged".to_owned(), Value::Bool(true));
            }
            members.insert("prompts".to_owned(), Value::Object(prompts));
        }
        if !extensions.is_empty() {
            members.insert(
                "extensions".to_owned(),
                Value::Object(extensions.into_iter().collect()),
            );
        }

        Ok(Self { members })
    }
}

fn validate_capability_members(
    members: &BTreeMap<String, Value>,
) -> Result<(), ServerDiscoveryError> {
    for capability in ["logging", "completions"] {
        if members
            .get(capability)
            .is_some_and(|value| !value.is_object())
        {
            return Err(ServerDiscoveryError::InvalidCapabilityShape);
        }
    }

    for capability in ["tools", "prompts"] {
        if let Some(Value::Object(settings)) = members.get(capability) {
            if settings
                .get("listChanged")
                .is_some_and(|value| !value.is_boolean())
            {
                return Err(ServerDiscoveryError::InvalidCapabilityShape);
            }
        } else if members.contains_key(capability) {
            return Err(ServerDiscoveryError::InvalidCapabilityShape);
        }
    }

    if let Some(Value::Object(settings)) = members.get("resources") {
        for field in ["listChanged", "subscribe"] {
            if settings.get(field).is_some_and(|value| !value.is_boolean()) {
                return Err(ServerDiscoveryError::InvalidCapabilityShape);
            }
        }
    } else if members.contains_key("resources") {
        return Err(ServerDiscoveryError::InvalidCapabilityShape);
    }

    if let Some(Value::Object(settings)) = members.get("experimental") {
        if settings.values().any(|value| !value.is_object()) {
            return Err(ServerDiscoveryError::InvalidCapabilityShape);
        }
    } else if members.contains_key("experimental") {
        return Err(ServerDiscoveryError::InvalidCapabilityShape);
    }

    if let Some(Value::Object(settings)) = members.get("extensions") {
        let extensions = settings
            .iter()
            .map(|(name, value)| (name.clone(), value.clone()))
            .collect();
        validate_extensions(&extensions)?;
    } else if members.contains_key("extensions") {
        return Err(ServerDiscoveryError::InvalidCapabilityShape);
    }

    Ok(())
}

fn validate_extensions(extensions: &BTreeMap<String, Value>) -> Result<(), ServerDiscoveryError> {
    if extensions.len() > MAX_DISCOVERY_EXTENSION_SETTINGS {
        return Err(ServerDiscoveryError::TooManyExtensionSettings {
            actual: extensions.len(),
            maximum: MAX_DISCOVERY_EXTENSION_SETTINGS,
        });
    }

    for (name, value) in extensions {
        if name.is_empty() || name.len() > MAX_DISCOVERY_EXTENSION_NAME_BYTES {
            return Err(ServerDiscoveryError::InvalidExtensionName {
                length: name.len(),
                maximum: MAX_DISCOVERY_EXTENSION_NAME_BYTES,
            });
        }
        ExtensionId::parse(name.clone()).map_err(|_| {
            ServerDiscoveryError::InvalidExtensionName {
                length: name.len(),
                maximum: MAX_DISCOVERY_EXTENSION_NAME_BYTES,
            }
        })?;
        if !value.is_object() {
            return Err(ServerDiscoveryError::InvalidCapabilityShape);
        }
        let encoded_len = serde_json::to_vec(value)
            .map_err(|_| ServerDiscoveryError::ExtensionValueEncoding)?
            .len();
        if encoded_len > MAX_DISCOVERY_EXTENSION_VALUE_BYTES {
            return Err(ServerDiscoveryError::ExtensionValueTooLarge {
                actual: encoded_len,
                maximum: MAX_DISCOVERY_EXTENSION_VALUE_BYTES,
            });
        }
    }
    Ok(())
}

/// Result metadata carried by `server/discover`.
///
/// `serverInfo` belongs in the common `_meta` object in final MCP, not in the
/// method-specific discovery payload. Other admitted metadata is preserved as
/// inert result metadata instead of being reinterpreted as a capability.
#[derive(Clone, Debug, Default)]
struct ServerDiscoverResultMetadata {
    server_info: Option<ServerInfo>,
    implementation: Option<Implementation>,
    extras: BTreeMap<String, Value>,
}

impl ServerDiscoverResultMetadata {
    fn server_generated(server_info: ServerInfo) -> Self {
        Self {
            server_info: Some(server_info),
            implementation: None,
            extras: BTreeMap::new(),
        }
    }

    fn with_implementation(mut self, implementation: Implementation) -> Self {
        self.implementation = Some(implementation);
        self
    }

    fn is_empty(&self) -> bool {
        self.server_info.is_none() && self.implementation.is_none() && self.extras.is_empty()
    }
}

impl Serialize for ServerDiscoverResultMetadata {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(
            self.extras.len()
                + usize::from(self.implementation.is_some() || self.server_info.is_some()),
        ))?;
        if let Some(implementation) = &self.implementation {
            map.serialize_entry(SERVER_DISCOVER_SERVER_INFO_META_KEY, implementation)?;
        } else if let Some(server_info) = &self.server_info {
            map.serialize_entry(SERVER_DISCOVER_SERVER_INFO_META_KEY, server_info)?;
        }
        for (name, value) in &self.extras {
            map.serialize_entry(name, value)?;
        }
        map.end()
    }
}

impl<'de> Deserialize<'de> for ServerDiscoverResultMetadata {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let mut members = BTreeMap::<String, Value>::deserialize(deserializer)?;
        validate_final_result_metadata_entries(&members, FinalResultMetadataRole::Ordinary)
            .map_err(D::Error::custom)?;
        let identity = members.remove(SERVER_DISCOVER_SERVER_INFO_META_KEY);
        let implementation = identity
            .as_ref()
            .and_then(|value| serde_json::from_value::<Implementation>(value.clone()).ok())
            .filter(|implementation| {
                implementation.title.is_some()
                    || implementation.description.is_some()
                    || implementation.website_url.is_some()
                    || !implementation.icons.is_empty()
                    || !implementation.additional.is_empty()
            });
        let server_info = identity
            .map(serde_json::from_value)
            .transpose()
            .map_err(D::Error::custom)?;
        Ok(Self {
            server_info,
            implementation,
            extras: members,
        })
    }
}

/// A presence-aware optional instruction field.
///
/// Serde's ordinary `Option<T>` accepts explicit `null`; the final discovery
/// vocabulary permits absence but rejects `null` and every non-string value.
#[derive(Default)]
struct OptionalServerInstructions(Option<ServerInstructions>);

impl<'de> Deserialize<'de> for OptionalServerInstructions {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        ServerInstructions::deserialize(deserializer).map(|instructions| Self(Some(instructions)))
    }
}

/// Typed `server/discover` result whose wire vocabulary is fixed to final MCP.
#[derive(Clone, Debug)]
pub struct ServerDiscoverResult {
    result_type: String,
    peer_missing_result_type: bool,
    supported_versions: Vec<String>,
    capabilities: ServerDiscoverCapabilities,
    metadata: ServerDiscoverResultMetadata,
    instructions: Option<ServerInstructions>,
    cache_hints: DiscoveryCacheHints,
    extras: BTreeMap<String, Value>,
    /// Exact peer source retained after typed discovery admission.
    ///
    /// This covers both schema-open top-level siblings and nested `_meta`
    /// members, whose order and number lexemes are otherwise lost by typed
    /// `BTreeMap<String, Value>` validation.
    exact_peer_result: Option<ExactJsonObject>,
}

impl ServerDiscoverResult {
    /// Creates a final discovery response with its exact supported-version
    /// list, server identity in `_meta`, and required cache hints.
    #[must_use]
    pub fn new(
        capabilities: ServerDiscoverCapabilities,
        server_info: ServerInfo,
        instructions: Option<ServerInstructions>,
        cache_hints: DiscoveryCacheHints,
    ) -> Self {
        Self {
            result_type: COMPLETE_DISCOVERY_RESULT_TYPE.to_owned(),
            peer_missing_result_type: false,
            supported_versions: SERVER_DISCOVER_SUPPORTED_VERSIONS
                .iter()
                .map(|version| (*version).to_owned())
                .collect(),
            capabilities,
            metadata: ServerDiscoverResultMetadata::server_generated(server_info),
            instructions,
            cache_hints,
            extras: BTreeMap::new(),
            exact_peer_result: None,
        }
    }

    /// Replaces discovery `_meta` server identity with a final Implementation.
    ///
    /// Exact-2024 initialize still projects name and version only. This richer
    /// identity is for modern `server/discover`.
    #[must_use]
    pub fn with_implementation(mut self, implementation: Implementation) -> Self {
        self.metadata = self.metadata.with_implementation(implementation);
        self
    }

    /// Returns the final Implementation identity when one was stored.
    #[must_use]
    pub fn implementation(&self) -> Option<&Implementation> {
        self.metadata.implementation.as_ref()
    }

    /// Returns the protocol versions advertised by this server.
    #[must_use]
    pub fn supported_versions(&self) -> &[String] {
        &self.supported_versions
    }

    /// Returns the admitted final discovery discriminator.
    ///
    /// An absent peer discriminator is normalized to the compatibility default
    /// `complete`; [`Self::peer_diagnostic`] distinguishes that wire omission
    /// from an explicitly emitted discriminator.
    #[must_use]
    pub fn result_type(&self) -> &str {
        &self.result_type
    }

    /// Returns bounded evidence for a final peer whose otherwise-valid
    /// discovery result omitted its required `resultType` discriminator.
    ///
    /// Re-encoding canonicalizes the peer omission to the required
    /// `resultType: "complete"`, so compatibility evidence cannot cause a
    /// locally emitted final discovery result to omit its discriminator.
    #[must_use]
    pub const fn peer_diagnostic(&self) -> Option<ResultPeerDiagnostic> {
        if self.peer_missing_result_type {
            Some(ResultPeerDiagnostic::ModernMissingResultType)
        } else {
            None
        }
    }

    /// Returns the derived capability shape.
    #[must_use]
    pub fn capabilities(&self) -> &ServerDiscoverCapabilities {
        &self.capabilities
    }

    /// Returns the self-reported server identity when the peer supplied one.
    #[must_use]
    pub fn server_info(&self) -> Option<&ServerInfo> {
        self.metadata.server_info.as_ref()
    }

    /// Returns optional server guidance without assigning it any authority.
    #[must_use]
    pub fn instructions(&self) -> Option<&ServerInstructions> {
        self.instructions.as_ref()
    }

    /// Returns the required cache hints attached to this discovery result.
    #[must_use]
    pub const fn cache_hints(&self) -> &DiscoveryCacheHints {
        &self.cache_hints
    }
}

impl Serialize for ServerDiscoverResult {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if let Some(exact_peer_result) = &self.exact_peer_result {
            let raw = RawValue::from_string(encode_exact_object(exact_peer_result))
                .map_err(S::Error::custom)?;
            return raw.serialize(serializer);
        }

        ServerDiscoverResultCanonical {
            result_type: &self.result_type,
            supported_versions: &self.supported_versions,
            capabilities: &self.capabilities,
            metadata: &self.metadata,
            instructions: self.instructions.as_ref(),
            cache_hints: &self.cache_hints,
            extras: &self.extras,
        }
        .serialize(serializer)
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ServerDiscoverResultCanonical<'a> {
    #[serde(rename = "resultType")]
    result_type: &'a str,
    #[serde(rename = "supportedVersions")]
    supported_versions: &'a [String],
    capabilities: &'a ServerDiscoverCapabilities,
    #[serde(
        rename = "_meta",
        skip_serializing_if = "ServerDiscoverResultMetadata::is_empty"
    )]
    metadata: &'a ServerDiscoverResultMetadata,
    #[serde(skip_serializing_if = "Option::is_none")]
    instructions: Option<&'a ServerInstructions>,
    #[serde(flatten)]
    cache_hints: &'a DiscoveryCacheHints,
    #[serde(flatten)]
    extras: &'a BTreeMap<String, Value>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ServerDiscoverResultWire {
    #[serde(rename = "resultType", default)]
    result_type: OptionalDiscoveryResultType,
    #[serde(rename = "supportedVersions")]
    supported_versions: Vec<String>,
    capabilities: ServerDiscoverCapabilities,
    #[serde(rename = "_meta", default)]
    metadata: ServerDiscoverResultMetadata,
    #[serde(default)]
    instructions: OptionalServerInstructions,
    #[serde(rename = "ttlMs")]
    ttl_ms: CacheTtl,
    #[serde(rename = "cacheScope")]
    cache_scope: DiscoveryCacheScope,
    #[serde(flatten)]
    extras: BTreeMap<String, Value>,
}

/// Presence-aware peer discriminator.
///
/// `Option<String>` alone would conflate explicit `null` with absence. This
/// wrapper is constructed only when the member is present, so `null` and every
/// non-string JSON value fail `String` deserialization while true absence uses
/// `Default` and selects the compatibility rule.
#[derive(Default)]
struct OptionalDiscoveryResultType(Option<String>);

impl<'de> Deserialize<'de> for OptionalDiscoveryResultType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        String::deserialize(deserializer).map(|result_type| Self(Some(result_type)))
    }
}

impl<'de> Deserialize<'de> for ServerDiscoverResult {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = Box::<RawValue>::deserialize(deserializer)?;
        let exact_peer_result = parse_exact_result_object(raw.get()).map_err(D::Error::custom)?;
        let wire = serde_json::from_str::<ServerDiscoverResultWire>(raw.get())
            .map_err(D::Error::custom)?;
        let peer_missing_result_type = wire.result_type.0.is_none();
        if wire
            .result_type
            .0
            .as_deref()
            .is_some_and(|result_type| result_type != COMPLETE_DISCOVERY_RESULT_TYPE)
        {
            return Err(D::Error::custom(
                "server/discover resultType must be `complete`",
            ));
        }
        if wire.extras.keys().any(|name| {
            DISCOVERY_CONTRADICTORY_RESULT_MEMBERS
                .iter()
                .any(|forbidden| name == forbidden)
        }) {
            return Err(D::Error::custom(
                "server/discover result contains a contradictory final result member",
            ));
        }
        Ok(Self {
            result_type: COMPLETE_DISCOVERY_RESULT_TYPE.to_owned(),
            peer_missing_result_type,
            supported_versions: wire.supported_versions,
            capabilities: wire.capabilities,
            metadata: wire.metadata,
            instructions: wire.instructions.0,
            cache_hints: DiscoveryCacheHints::from_peer_wire(wire.ttl_ms, wire.cache_scope),
            extras: wire.extras,
            exact_peer_result: (!peer_missing_result_type).then_some(exact_peer_result),
        })
    }
}

/// Why a server discovery value could not be safely constructed or admitted.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ServerDiscoveryError {
    /// The request did not carry the required final request metadata.
    InvalidRequestMetadata,
    /// A known capability field did not use its schema-required object shape.
    InvalidCapabilityShape,
    /// The registry attempted to advertise more extension settings than allowed.
    TooManyExtensionSettings {
        /// Observed extension setting count.
        actual: usize,
        /// Maximum allowed extension setting count.
        maximum: usize,
    },
    /// An extension setting name was empty or exceeded its fixed bound.
    InvalidExtensionName {
        /// Observed UTF-8 byte length.
        length: usize,
        /// Maximum allowed UTF-8 byte length.
        maximum: usize,
    },
    /// An extension setting value exceeded its exact JSON byte bound.
    ExtensionValueTooLarge {
        /// Observed encoded JSON byte length.
        actual: usize,
        /// Maximum allowed encoded JSON byte length.
        maximum: usize,
    },
    /// An extension setting value could not be encoded as JSON.
    ExtensionValueEncoding,
}

impl fmt::Display for ServerDiscoveryError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRequestMetadata => write!(
                formatter,
                "server/discover requires final protocol version and client capabilities metadata"
            ),
            Self::InvalidCapabilityShape => {
                write!(
                    formatter,
                    "server/discover capability has an invalid schema shape"
                )
            }
            Self::TooManyExtensionSettings { actual, maximum } => {
                write!(
                    formatter,
                    "{actual} extension settings exceed the maximum {maximum}"
                )
            }
            Self::InvalidExtensionName { length, maximum } => {
                write!(
                    formatter,
                    "extension name is {length} bytes; maximum is {maximum}"
                )
            }
            Self::ExtensionValueTooLarge { actual, maximum } => write!(
                formatter,
                "extension setting is {actual} JSON bytes; maximum is {maximum}"
            ),
            Self::ExtensionValueEncoding => {
                write!(formatter, "extension setting could not be encoded")
            }
        }
    }
}

impl Error for ServerDiscoveryError {}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use serde_json::{Value, json};

    use crate::{
        DiscoveryCacheHints, ResultPeerDiagnostic, SERVER_DISCOVER_METHOD,
        SERVER_DISCOVER_SERVER_INFO_META_KEY, SERVER_DISCOVER_SUPPORTED_VERSIONS, ServerBehavior,
        ServerBehaviorRegistry, ServerDiscoverCapabilities, ServerDiscoverRequest,
        ServerDiscoverResult, ServerDiscoveryError, ServerInfo, ServerInstructions,
        common_types::Implementation,
    };

    fn fully_installed_capabilities() -> ServerDiscoverCapabilities {
        ServerDiscoverCapabilities::from_registry(
            &ServerBehaviorRegistry::from_behaviors([
                ServerBehavior::LoggingRequestEmitter,
                ServerBehavior::CompletionComplete,
                ServerBehavior::ToolsList,
                ServerBehavior::ToolsListChangedNotification,
                ServerBehavior::ResourcesList,
                ServerBehavior::ResourcesListChangedNotification,
                ServerBehavior::ResourcesSubscribe,
                ServerBehavior::SubscriptionsListen,
                ServerBehavior::ResourceUpdateDelivery,
                ServerBehavior::PromptsList,
                ServerBehavior::PromptsListChangedNotification,
            ]),
            BTreeMap::from([("io.fastmcp/example".to_owned(), json!({"enabled": true}))]),
        )
        .expect("the bounded installed behavior registry is discoverable")
    }

    #[test]
    fn srv_02_b_positive() {
        let result = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            Some(ServerInstructions::new("").expect("empty guidance is present guidance")),
            DiscoveryCacheHints::private_ttl_ms(60_000),
        );

        let request = serde_json::to_value(ServerDiscoverRequest::default())
            .expect("the typed request encodes through the public API");
        let wire =
            serde_json::to_value(&result).expect("the typed result encodes through the public API");

        assert_eq!(SERVER_DISCOVER_METHOD, "server/discover");
        assert_eq!(
            request,
            json!({
                "_meta": {
                    "io.modelcontextprotocol/protocolVersion": "2026-07-28",
                    "io.modelcontextprotocol/clientCapabilities": {},
                },
            })
        );
        assert_eq!(wire["resultType"], json!("complete"));
        assert_eq!(
            wire["supportedVersions"],
            json!(SERVER_DISCOVER_SUPPORTED_VERSIONS)
        );
        assert!(wire.get("protocolVersions").is_none());
        assert!(wire.get("serverInfo").is_none());
        assert!(wire.get("cacheHints").is_none());
        assert_eq!(
            wire["_meta"]["io.modelcontextprotocol/serverInfo"],
            json!({"name": "contract-server", "version": "1.0.0"})
        );
        assert_eq!(wire["instructions"], json!(""));
        assert_eq!(wire["ttlMs"], json!(60_000));
        assert_eq!(wire["cacheScope"], json!("private"));
        assert_eq!(wire["capabilities"]["tools"]["listChanged"], json!(true));
        assert_eq!(wire["capabilities"]["resources"]["subscribe"], json!(true));
        assert_eq!(
            wire["capabilities"]["resources"]["listChanged"],
            json!(true)
        );
        assert_eq!(wire["capabilities"]["prompts"]["listChanged"], json!(true));
        assert!(wire["capabilities"].get("subscriptions").is_none());
        assert_eq!(
            wire["capabilities"]["extensions"]["io.fastmcp/example"]["enabled"],
            json!(true)
        );

        let decoded: ServerDiscoverResult = serde_json::from_value(wire)
            .expect("the final server/discover vocabulary decodes deterministically");
        assert_eq!(
            decoded
                .supported_versions()
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>(),
            ["2026-07-28"],
            "only the final protocol version is advertised"
        );
        assert_eq!(
            decoded
                .server_info()
                .map(|server_info| server_info.name.as_str()),
            Some("contract-server")
        );
        assert_eq!(
            decoded.instructions().map(ServerInstructions::as_str),
            Some("")
        );
        assert_eq!(
            decoded
                .cache_hints()
                .ttl_ms()
                .try_as_millis()
                .expect("local TTL fits the runtime domain"),
            60_000
        );
        assert!(!decoded.cache_hints().is_public());
    }

    #[test]
    fn server_discover_ttl_ms_preserves_an_unbounded_wire_integer() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(u64::MAX),
        );
        let mut accepted = serde_json::to_value(&admitted).expect("local discovery result encodes");
        accepted["ttlMs"] = serde_json::from_str("18446744073709551616")
            .expect("the one-over-u64 TTL is valid JSON");

        let decoded: ServerDiscoverResult = serde_json::from_value(accepted.clone())
            .expect("the unbounded nonnegative discovery TTL is admitted");
        assert_eq!(
            decoded.cache_hints().ttl_ms().as_str(),
            "18446744073709551616"
        );
        assert_eq!(
            decoded.cache_hints().ttl_ms().try_as_millis(),
            Err(crate::result::CacheTtlConversionError::RuntimeOutOfRange),
            "only the runtime conversion rejects the one-over-u64 TTL"
        );
        assert_eq!(
            serde_json::to_value(&decoded).expect("unbounded discovery TTL re-encodes"),
            accepted
        );

        let mut fractional = accepted;
        fractional["ttlMs"] = serde_json::from_str("18446744073709551616.5")
            .expect("the fractional mutation is valid JSON");
        assert!(
            serde_json::from_value::<ServerDiscoverResult>(fractional).is_err(),
            "changing only ttlMs from an unbounded integer to a fraction violates the final cache schema"
        );
    }

    #[test]
    fn server_discover_retains_exact_admitted_source_for_open_members() {
        let source = r#"{"com.example/top":{"second":1.20e+4,"first":0e0},"cacheScope":"private","_meta":{"io.modelcontextprotocol/futureResultMetadata":{"later":1.20e+4,"earlier":0e0},"io.modelcontextprotocol/serverInfo":{"version":"1.0.0","name":"contract-server"}},"capabilities":{"tools":{"listChanged":true}},"ttlMs":0,"supportedVersions":["2026-07-28"],"resultType":"complete"}"#;

        let decoded = serde_json::from_str::<ServerDiscoverResult>(source)
            .expect("typed final discovery fields admit the exact peer source");
        assert_eq!(
            decoded
                .server_info()
                .map(|server_info| (server_info.name.as_str(), server_info.version.as_str())),
            Some(("contract-server", "1.0.0")),
            "exact retention does not bypass serverInfo validation"
        );
        assert_eq!(
            serde_json::to_string(&decoded).expect("admitted discovery result replays"),
            source,
            "top-level and nested schema-open member order and number lexemes replay exactly"
        );
    }

    #[test]
    fn server_discover_accepts_schema_valid_supported_versions() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let mut peer_wire: Value =
            serde_json::to_value(&admitted).expect("the admitted result encodes");
        peer_wire["supportedVersions"] = json!(["2024-11-05", "2026-07-28"]);

        let decoded: ServerDiscoverResult = serde_json::from_value(peer_wire)
            .expect("the final schema permits any string version advertisement");
        assert_eq!(
            decoded
                .supported_versions()
                .iter()
                .map(String::as_str)
                .collect::<Vec<_>>(),
            ["2024-11-05", "2026-07-28"],
            "version selection remains a negotiation-layer concern"
        );
    }

    #[test]
    fn discovery_extensions_require_final_identifiers_on_peer_and_local_paths() {
        let registry = ServerBehaviorRegistry::default();
        let valid = BTreeMap::from([("com.example/".to_owned(), json!({}))]);
        assert!(ServerDiscoverCapabilities::from_registry(&registry, valid).is_ok());

        let invalid_names = [
            "com.example",                // missing mandatory prefix delimiter
            "com.example//name",          // more than one delimiter
            "org.modelcontextprotocol/x", // reserved namespace misuse
            "1com.example/name",          // invalid prefix label
        ];
        for name in invalid_names {
            let extensions = BTreeMap::from([(name.to_owned(), json!({}))]);
            assert!(
                matches!(
                    ServerDiscoverCapabilities::from_registry(&registry, extensions),
                    Err(ServerDiscoveryError::InvalidExtensionName { .. })
                ),
                "local discovery must reject invalid extension identifier {name:?}"
            );
        }

        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let unchanged_before = serde_json::to_value(&admitted).expect("admitted discovery encodes");
        for name in invalid_names {
            let mut peer_wire = unchanged_before.clone();
            let mut extensions = serde_json::Map::new();
            extensions.insert(name.to_owned(), json!({}));
            peer_wire["capabilities"]["extensions"] = Value::Object(extensions);
            assert!(
                serde_json::from_value::<ServerDiscoverResult>(peer_wire).is_err(),
                "peer discovery must reject invalid extension identifier {name:?}"
            );
            assert_eq!(
                serde_json::to_value(&admitted).expect("admitted discovery remains unchanged"),
                unchanged_before,
                "rejected peer extension identifiers cannot mutate locally admitted discovery state"
            );
        }
    }

    #[test]
    fn server_discover_rejects_non_complete_result_types_and_contradictory_shapes() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let unchanged_before =
            serde_json::to_vec(&admitted).expect("the admitted result has a stable wire image");
        let peer_wire: Value =
            serde_json::to_value(&admitted).expect("the admitted result encodes");

        for result_type in [
            json!("input_required"),
            json!("task"),
            json!("com.example/deferred-discovery"),
            Value::Null,
            json!(false),
            json!({"complete": true}),
        ] {
            let mut planted = peer_wire.clone();
            planted["resultType"] = result_type;
            assert!(
                serde_json::from_value::<ServerDiscoverResult>(planted).is_err(),
                "only complete or absence can establish discovery"
            );
        }

        for (name, value) in [
            ("inputRequests", json!({"roots": {"method": "roots/list"}})),
            ("requestState", json!("retry-1")),
            ("taskId", json!("task-1")),
            (
                "serverInfo",
                json!({"name": "wrong-location", "version": "1.0"}),
            ),
        ] {
            let mut planted = peer_wire.clone();
            planted[name] = value;
            assert!(
                serde_json::from_value::<ServerDiscoverResult>(planted).is_err(),
                "complete discovery cannot carry the {name} result branch member"
            );
        }
        assert_eq!(
            serde_json::to_vec(&admitted).expect("the admitted result still encodes"),
            unchanged_before,
            "rejecting a contradictory result cannot mutate locally admitted state"
        );
    }

    #[test]
    fn server_discover_matches_ordinary_result_metadata_roles_without_mutating_admission() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let admitted_wire = serde_json::to_value(&admitted).expect("admitted discovery encodes");
        let baseline = serde_json::from_value::<ServerDiscoverResult>(admitted_wire.clone())
            .expect("response-only discovery metadata is admitted");
        let baseline_wire = serde_json::to_value(&baseline).expect("baseline re-encodes");

        for (member, value) in [
            (
                "io.modelcontextprotocol/protocolVersion",
                json!("2026-07-28"),
            ),
            ("io.modelcontextprotocol/clientCapabilities", json!({})),
            (
                "io.modelcontextprotocol/clientInfo",
                json!({"name": "client", "version": "1"}),
            ),
            ("io.modelcontextprotocol/logLevel", json!("notice")),
            (
                "io.modelcontextprotocol/subscriptionId",
                json!("subscription-7"),
            ),
        ] {
            let mut planted = admitted_wire.clone();
            planted["_meta"][member] = value;
            assert!(
                serde_json::from_value::<ServerDiscoverResult>(planted).is_err(),
                "adding only {member} rejects the ordinary discovery response"
            );
        }

        let mut invalid_server_info = admitted_wire.clone();
        invalid_server_info["_meta"][SERVER_DISCOVER_SERVER_INFO_META_KEY] = Value::Null;
        assert!(
            serde_json::from_value::<ServerDiscoverResult>(invalid_server_info).is_err(),
            "only a null reserved serverInfo type rejects discovery"
        );

        let mut schema_open = admitted_wire.clone();
        schema_open["_meta"]["io.modelcontextprotocol/futureResultMetadata"] =
            json!({"future": true});
        let accepted_open = serde_json::from_value::<ServerDiscoverResult>(schema_open.clone())
            .expect("unknown reserved result metadata remains inert and admitted");
        assert_eq!(
            serde_json::to_value(accepted_open).expect("schema-open discovery re-encodes"),
            schema_open,
            "direct discovery retains inert reserved metadata"
        );

        let reaccepted = serde_json::from_value::<ServerDiscoverResult>(admitted_wire)
            .expect("rejection does not mutate subsequent discovery admission");
        assert_eq!(
            serde_json::to_value(reaccepted).expect("reaccepted discovery re-encodes"),
            baseline_wire
        );
    }

    #[test]
    fn server_discover_missing_result_type_defaults_complete_with_diagnostic() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let unchanged_before =
            serde_json::to_vec(&admitted).expect("the admitted result has a stable wire image");
        let mut missing_result_type: Value =
            serde_json::to_value(&admitted).expect("the admitted result encodes");
        missing_result_type
            .as_object_mut()
            .expect("the discovery result is an object")
            .remove("resultType");

        let decoded = serde_json::from_value::<ServerDiscoverResult>(missing_result_type.clone())
            .expect("an otherwise-valid omitted discriminator uses the client compatibility rule");
        assert_eq!(decoded.result_type(), "complete");
        assert_eq!(
            decoded.peer_diagnostic(),
            Some(ResultPeerDiagnostic::ModernMissingResultType)
        );
        assert_eq!(
            serde_json::to_value(decoded).expect("compatibility discovery re-encodes"),
            serde_json::to_value(&admitted).expect("local discovery result re-encodes"),
            "peer compatibility input is canonicalized before local emission"
        );
        assert_eq!(
            serde_json::to_vec(&admitted).expect("the admitted result still encodes"),
            unchanged_before,
            "rejecting the one-field variant cannot mutate locally admitted state"
        );
    }

    #[test]
    fn srv_02_b_planted_negative() {
        let admitted = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let unchanged_before =
            serde_json::to_vec(&admitted).expect("the admitted result has a stable wire image");
        let admitted_wire = serde_json::to_value(&admitted).expect("the admitted result encodes");

        let mut planted = admitted_wire.clone();
        planted["resultType"] = Value::Null;
        assert!(
            serde_json::from_value::<ServerDiscoverResult>(planted).is_err(),
            "explicit null never uses the absence compatibility rule"
        );
        assert_eq!(
            serde_json::to_vec(&admitted).expect("the admitted result still encodes"),
            unchanged_before,
            "rejecting one-field variants cannot mutate locally admitted state"
        );
    }

    #[test]
    fn server_discover_instructions_preserve_presence_and_reject_null() {
        let absent = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let absent_wire = serde_json::to_value(&absent).expect("absent instructions encode");
        assert!(absent_wire.get("instructions").is_none());

        let mut explicit_null = absent_wire.clone();
        explicit_null["instructions"] = Value::Null;
        assert!(
            serde_json::from_value::<ServerDiscoverResult>(explicit_null).is_err(),
            "explicit null is not interchangeable with absent instructions"
        );
        assert_eq!(
            serde_json::to_value(&absent).expect("the admitted result remains unchanged"),
            absent_wire,
            "the rejected instruction value cannot mutate the admitted result"
        );
    }

    #[test]
    fn server_discover_retains_implementation_identity_only_when_extras_are_present() {
        let bare = ServerDiscoverResult::new(
            fully_installed_capabilities(),
            ServerInfo {
                name: "contract-server".to_owned(),
                version: "1.0.0".to_owned(),
            },
            None,
            DiscoveryCacheHints::private_ttl_ms(0),
        );
        let bare_wire = serde_json::to_value(&bare).expect("bare discovery encodes");
        let bare_decoded: ServerDiscoverResult =
            serde_json::from_value(bare_wire).expect("bare discovery decodes");
        assert!(
            bare_decoded.implementation().is_none(),
            "name/version-only serverInfo must stay Implementation-absent: {:?}",
            bare_decoded.implementation()
        );
        assert_eq!(
            bare_decoded
                .server_info()
                .map(|info| (info.name.as_str(), info.version.as_str())),
            Some(("contract-server", "1.0.0"))
        );

        let mut implementation = Implementation::try_new("contract-server", "1.0.0")
            .expect("the identity name and version are nonempty");
        implementation.title = Some("Identity Title".to_owned());
        implementation.description = Some("Identity description".to_owned());
        implementation.website_url = Some(
            crate::common_types::AbsoluteUri::parse("https://example.test/fastmcp")
                .expect("the identity website is an absolute URI"),
        );
        implementation.icons = vec![
            crate::common_types::RawIcon::try_new("https://example.test/e2e-icon.png")
                .expect("the identity icon source is an absolute URI"),
        ];
        let identified = bare.with_implementation(implementation);
        let identified_wire =
            serde_json::to_value(&identified).expect("identified discovery encodes");
        let identified_decoded: ServerDiscoverResult =
            serde_json::from_value(identified_wire).expect("identified discovery decodes");
        let identified_impl = identified_decoded
            .implementation()
            .expect("title/description/website/icons must retain Implementation");
        assert_eq!(identified_impl.title.as_deref(), Some("Identity Title"));
        assert_eq!(
            identified_impl.description.as_deref(),
            Some("Identity description")
        );
        assert_eq!(
            identified_impl.website_url.as_ref().map(|uri| uri.as_str()),
            Some("https://example.test/fastmcp")
        );
        assert_eq!(
            identified_impl.icons.first().map(|icon| icon.src.as_str()),
            Some("https://example.test/e2e-icon.png")
        );
    }
}