Клиент, согласовавший tools list-changed, получает bounded `notifications/tools/list_changed` после коммита видимой Tool-проекции опубликованного Agent snapshot; `tools/list` остаётся авторитетным для клиента без notification и после reconnect. - signals.rs: третий тип сигнала в существующем stream-local watcher (один tick = максимум один кадр на тип, fixed-capacity queue, без cross-process bus и без post-commit hook); fingerprint сравнивает tool_name/operation.version/title/description, поэтому Tool-only commit не даёт Resource/Prompt сигналов и наоборот. - registry.rs: tools.listChanged=true (реально отправляется в обеих revision); modern filter toolsListChanged теперь подтверждается acknowledge и не шлётся вне согласованного набора. - legacy GET-стрим несёт Tools-сигнал вместе с Resource/Prompt. - ATDD: RED на baseline (ack пуст, Tools-кадра нет, capability false), GREEN — 8 integration-сценариев × обе revision, unit-оракулы wire, representatives official MCP SDK 1.32.2 smoke со штатными headers. - capability inventory/baseline manifest и frozen projection обновлены под tools.listChanged=true; ci-story-checks.sh дополнен AC 3.5.
801 lines
29 KiB
Rust
801 lines
29 KiB
Rust
//! In-crate tests of `protocol::registry` (module path `protocol::registry::tests`).
|
|
//!
|
|
//! Kept as a `#[path]` module to stay inside the repository code-health caps (an inline test
|
|
//! module may not exceed 500 lines and `registry.rs` may not exceed 1000 lines). The module path,
|
|
//! and therefore the sanctioned selector
|
|
//! `cargo test -p crank-community-mcp --lib protocol::registry::`, is identical to the inline
|
|
//! module it replaces, and every test name is unchanged.
|
|
|
|
use super::*;
|
|
use crate::protocol::{advertised_server_capabilities, dispatch_decision};
|
|
use serde_json::json;
|
|
|
|
fn production_registry() -> CapabilityRegistry {
|
|
CapabilityRegistry::new(CAPABILITY_DESCRIPTORS, METHOD_DESCRIPTORS)
|
|
.expect("the static capability table is valid")
|
|
}
|
|
|
|
fn inert_descriptor() -> CapabilityIdDescriptor {
|
|
CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["enabled"]),
|
|
advertised: Some(("inert_test_capability", &[("enabled", true)])),
|
|
revisions: MODERN_AND_LEGACY,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn static_registration_is_unique_and_advertises_only_the_registered_capability() {
|
|
let registry = production_registry();
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
assert_eq!(
|
|
registry.advertised_capabilities(revision),
|
|
json!({
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
}),
|
|
"the registered advertisement must be the same reviewed object in {revision}"
|
|
);
|
|
assert_eq!(
|
|
advertised_server_capabilities(revision),
|
|
json!({
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
}),
|
|
"the production advertisement entry point must serve {revision}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn advertisement_and_dispatch_share_one_eligibility_predicate() {
|
|
// F6: for every capability that is *advertised by design* advertisement and dispatch are the
|
|
// same predicate, and dispatch eligibility follows the registered revision window. The only
|
|
// registrations that are dispatchable without being advertised are the explicit method-kind
|
|
// ones (`ping`, `server/discover`, the legacy lifecycle).
|
|
let registry = production_registry();
|
|
for descriptor in ®istry.capabilities {
|
|
let advertised_by_design = matches!(descriptor.kind, Kind::Capability | Kind::Extension);
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
let eligible = registry.dispatchable_for(descriptor.id, revision);
|
|
assert_eq!(
|
|
eligible,
|
|
descriptor.revisions.contains(&revision),
|
|
"dispatch eligibility must follow the revision window of {:?}",
|
|
descriptor.id
|
|
);
|
|
if advertised_by_design {
|
|
assert_eq!(
|
|
registry.advertise_for(descriptor.id, revision),
|
|
eligible,
|
|
"advertisement and dispatch drifted for {:?} in {revision}",
|
|
descriptor.id
|
|
);
|
|
} else {
|
|
assert!(
|
|
!registry.advertise_for(descriptor.id, revision),
|
|
"a dispatch-only registration {:?} must never be advertised in {revision}",
|
|
descriptor.id
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
assert!(registry.advertise_for(CapabilityId::Tools, MODERN_PROTOCOL_VERSION));
|
|
assert!(registry.dispatchable_for(CapabilityId::Tools, MODERN_PROTOCOL_VERSION));
|
|
assert!(registry.advertise_for(CapabilityId::Tools, LEGACY_PROTOCOL_VERSION));
|
|
// `ping` is dispatched in both eras but advertised nowhere: it declares no fragment by
|
|
// contract, and the advertisement object carries no `ping` key.
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
assert!(registry.dispatchable_for(CapabilityId::Ping, revision));
|
|
assert!(!registry.advertise_for(CapabilityId::Ping, revision));
|
|
assert!(
|
|
registry
|
|
.advertised_capabilities(revision)
|
|
.as_object()
|
|
.is_some_and(|object| !object.contains_key("ping")),
|
|
"the advertisement of {revision} must not grow a `ping` key"
|
|
);
|
|
}
|
|
assert!(registry.dispatchable_for(CapabilityId::Discovery, MODERN_PROTOCOL_VERSION));
|
|
assert!(!registry.dispatchable_for(CapabilityId::Discovery, LEGACY_PROTOCOL_VERSION));
|
|
assert!(registry.dispatchable_for(CapabilityId::LegacyLifecycle, LEGACY_PROTOCOL_VERSION));
|
|
assert!(!registry.dispatchable_for(CapabilityId::LegacyLifecycle, MODERN_PROTOCOL_VERSION));
|
|
}
|
|
|
|
#[test]
|
|
fn a_fragment_less_advertisable_capability_is_rejected_at_registration() {
|
|
// F6: without this invariant a capability-owned method could stay `Allowed` while its
|
|
// capability is advertised nowhere, which is exactly the drift the reviewer measured.
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&[]),
|
|
advertised: None,
|
|
revisions: MODERN_AND_LEGACY,
|
|
});
|
|
assert_eq!(
|
|
CapabilityRegistry::new(&capabilities, METHOD_DESCRIPTORS).err(),
|
|
Some(RegistrationError::MissingAdvertisement {
|
|
capability: CapabilityId::InertTest
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_dispatch_only_registration_cannot_claim_an_advertisement_fragment() {
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Method,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&[]),
|
|
advertised: Some(("inert_method_advertisement", &[("enabled", true)])),
|
|
revisions: MODERN_AND_LEGACY,
|
|
});
|
|
assert_eq!(
|
|
CapabilityRegistry::new(&capabilities, METHOD_DESCRIPTORS).err(),
|
|
Some(RegistrationError::MisdeclaredAdvertisement {
|
|
capability: CapabilityId::InertTest
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_client_roster_declaration_never_dispatches_its_methods() {
|
|
const INERT_METHOD: MethodDescriptor = MethodDescriptor {
|
|
name: "inert/client-only",
|
|
capability: CapabilityId::InertTest,
|
|
kind: Kind::Method,
|
|
required_headers: HEADERS_NONE,
|
|
handler: MethodHandler::Ping,
|
|
};
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Client,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["enabled"]),
|
|
advertised: Some(("inert_client_capability", &[("enabled", true)])),
|
|
revisions: MODERN_AND_LEGACY,
|
|
});
|
|
let mut methods = METHOD_DESCRIPTORS.to_vec();
|
|
methods.push(INERT_METHOD);
|
|
let registry =
|
|
CapabilityRegistry::new(&capabilities, &methods).expect("a client declaration registers");
|
|
|
|
assert!(!registry.advertise_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION));
|
|
assert!(!registry.dispatchable_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION));
|
|
assert_eq!(
|
|
registry.dispatch_decision(INERT_METHOD.name, MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::Ping,
|
|
kind: Kind::Method,
|
|
},
|
|
"a client-side declaration must never be Allowed for a server dispatch"
|
|
);
|
|
assert!(
|
|
registry
|
|
.advertised_capabilities(MODERN_PROTOCOL_VERSION)
|
|
.as_object()
|
|
.is_some_and(|object| !object.contains_key("inert_client_capability")),
|
|
"a client declaration is never advertised by this server"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_method_registration_is_rejected() {
|
|
let duplicate = [
|
|
METHOD_DESCRIPTORS[0],
|
|
MethodDescriptor {
|
|
name: METHOD_DESCRIPTORS[0].name,
|
|
capability: METHOD_DESCRIPTORS[0].capability,
|
|
kind: METHOD_DESCRIPTORS[0].kind,
|
|
required_headers: HEADERS_NONE,
|
|
handler: MethodHandler::Discover,
|
|
},
|
|
];
|
|
assert_eq!(
|
|
CapabilityRegistry::new(CAPABILITY_DESCRIPTORS, &duplicate).unwrap_err(),
|
|
RegistrationError::DuplicateMethod { name: "tools/list" }
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn conflicting_method_registration_is_rejected() {
|
|
let conflicting = [
|
|
METHOD_DESCRIPTORS[0],
|
|
MethodDescriptor {
|
|
name: METHOD_DESCRIPTORS[0].name,
|
|
capability: CapabilityId::Ping,
|
|
kind: METHOD_DESCRIPTORS[0].kind,
|
|
required_headers: HEADERS_NONE,
|
|
handler: MethodHandler::Ping,
|
|
},
|
|
];
|
|
assert_eq!(
|
|
CapabilityRegistry::new(CAPABILITY_DESCRIPTORS, &conflicting).unwrap_err(),
|
|
RegistrationError::ConflictingMethod { name: "tools/list" }
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_capability_registration_is_rejected() {
|
|
let mut duplicated = CAPABILITY_DESCRIPTORS.to_vec();
|
|
duplicated.push(CAPABILITY_DESCRIPTORS[0]);
|
|
assert_eq!(
|
|
CapabilityRegistry::new(&duplicated, METHOD_DESCRIPTORS).unwrap_err(),
|
|
RegistrationError::DuplicateCapability {
|
|
capability: CapabilityId::Tools
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_inert_test_descriptor_changes_both_projections_and_is_never_registered_in_production() {
|
|
let without = production_registry();
|
|
let mut with_capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
with_capabilities.push(inert_descriptor());
|
|
let with = CapabilityRegistry::new(&with_capabilities, METHOD_DESCRIPTORS)
|
|
.expect("the inert descriptor is a valid capability entry");
|
|
|
|
assert_eq!(
|
|
without.advertised_capabilities(MODERN_PROTOCOL_VERSION),
|
|
json!({
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
})
|
|
);
|
|
assert_eq!(
|
|
with.advertised_capabilities(MODERN_PROTOCOL_VERSION),
|
|
json!({
|
|
"inert_test_capability": {"enabled": true},
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
})
|
|
);
|
|
assert_eq!(
|
|
with.advertised_capabilities(LEGACY_PROTOCOL_VERSION),
|
|
json!({
|
|
"inert_test_capability": {"enabled": true},
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
})
|
|
);
|
|
|
|
// Both projections move together: the inert capability is advertisable *and* dispatchable.
|
|
assert!(!without.advertise_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION));
|
|
assert!(with.advertise_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION));
|
|
assert_eq!(
|
|
with.advertise_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION),
|
|
with.dispatchable_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION)
|
|
);
|
|
|
|
// The inert descriptor stays a test-only object: it is absent from the production table.
|
|
assert!(
|
|
CAPABILITY_DESCRIPTORS
|
|
.iter()
|
|
.all(|descriptor| descriptor.id != CapabilityId::InertTest)
|
|
);
|
|
let _ = with.methods();
|
|
}
|
|
|
|
#[test]
|
|
fn the_advertised_revision_window_is_exactly_the_two_approved_revisions() {
|
|
let registry = production_registry();
|
|
for descriptor in ®istry.capabilities {
|
|
for revision in descriptor.revisions {
|
|
assert!(
|
|
[MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION].contains(revision),
|
|
"capability {:?} declares an unapproved revision {revision}",
|
|
descriptor.id
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Story 3.1 #8 remaining-foundation regressions (R6 era window, R7 advertisement conflict) --
|
|
|
|
/// A test-only descriptor that declares no revision at all, so every revision is ineligible.
|
|
fn empty_window_descriptor() -> CapabilityIdDescriptor {
|
|
CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["enabled"]),
|
|
advertised: Some(("inert_empty_window_capability", &[("enabled", true)])),
|
|
revisions: &[],
|
|
}
|
|
}
|
|
|
|
/// A test-only descriptor that claims the top-level advertisement key already owned by `Tools`.
|
|
fn conflicting_advertisement_descriptor() -> CapabilityIdDescriptor {
|
|
CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["listChanged"]),
|
|
advertised: Some(("tools", &[("listChanged", true)])),
|
|
revisions: MODERN_AND_LEGACY,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_revision_window_is_not_vacuously_advertisable() {
|
|
// R6 (empty-window half): `revisions: &[]` must not make a capability eligible for every
|
|
// revision through a vacuously true `all()`.
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(empty_window_descriptor());
|
|
let registry = match CapabilityRegistry::new(&capabilities, METHOD_DESCRIPTORS) {
|
|
Ok(registry) => registry,
|
|
// Rejecting a revision-less capability at registration is an equivalent, stronger
|
|
// resolution of the same defect (a typed error, never a panic).
|
|
Err(_rejected_at_registration) => return,
|
|
};
|
|
|
|
assert!(
|
|
!registry.advertise_for(CapabilityId::InertTest, MODERN_PROTOCOL_VERSION),
|
|
"a capability with an empty revision window is eligible in no revision"
|
|
);
|
|
assert!(
|
|
!registry.advertise_for(CapabilityId::InertTest, LEGACY_PROTOCOL_VERSION),
|
|
"a capability with an empty revision window is eligible in no revision"
|
|
);
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
let advertised = registry.advertised_capabilities(revision);
|
|
assert!(
|
|
advertised
|
|
.as_object()
|
|
.is_some_and(|object| !object.contains_key("inert_empty_window_capability")),
|
|
"an empty revision window must not be advertised in {revision}: {advertised}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_conflicting_advertisement_key_is_rejected_not_silently_overwritten() {
|
|
// R7: the constructor validates duplicate capability ids and duplicate method names but not
|
|
// a conflicting top-level advertisement key, so `Value::Object::insert` silently overwrites.
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(conflicting_advertisement_descriptor());
|
|
let outcome = CapabilityRegistry::new(&capabilities, METHOD_DESCRIPTORS);
|
|
assert_eq!(
|
|
outcome.err(),
|
|
Some(RegistrationError::ConflictingAdvertisement { key: "tools" }),
|
|
"two capabilities claiming the same top-level advertisement key must be rejected with a \
|
|
typed registration error instead of being silently overwritten"
|
|
);
|
|
}
|
|
|
|
// --- R6: the advertisement is indexed by the requested era, not by every declared revision ------
|
|
|
|
/// A test-only server capability available in the modern revision only.
|
|
fn modern_only_descriptor() -> CapabilityIdDescriptor {
|
|
CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["enabled"]),
|
|
advertised: Some(("inert_modern_capability", &[("enabled", true)])),
|
|
revisions: MODERN_ONLY,
|
|
}
|
|
}
|
|
|
|
/// A test-only server capability available in the legacy revision only.
|
|
fn legacy_only_descriptor() -> CapabilityIdDescriptor {
|
|
CapabilityIdDescriptor {
|
|
id: CapabilityId::InertTest,
|
|
roster: Roster::Server,
|
|
kind: Kind::Capability,
|
|
extension: None,
|
|
deprecated: None,
|
|
sub_features: FeatureSet(&["enabled"]),
|
|
advertised: Some(("inert_legacy_capability", &[("enabled", true)])),
|
|
revisions: LEGACY_ONLY,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_single_era_descriptor_never_leaks_into_the_other_era_advertisement() {
|
|
for (inert, this_era, other_era, key) in [
|
|
(
|
|
modern_only_descriptor(),
|
|
MODERN_PROTOCOL_VERSION,
|
|
LEGACY_PROTOCOL_VERSION,
|
|
"inert_modern_capability",
|
|
),
|
|
(
|
|
legacy_only_descriptor(),
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MODERN_PROTOCOL_VERSION,
|
|
"inert_legacy_capability",
|
|
),
|
|
] {
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(inert);
|
|
let registry = CapabilityRegistry::new(&capabilities, METHOD_DESCRIPTORS)
|
|
.expect("a single-era inert capability is a valid registration");
|
|
|
|
let this_era_advertisement = registry.advertised_capabilities(this_era);
|
|
assert!(
|
|
this_era_advertisement
|
|
.as_object()
|
|
.is_some_and(|object| object.contains_key(key)),
|
|
"{key} must be advertised in its own era {this_era}: {this_era_advertisement}"
|
|
);
|
|
let other_era_advertisement = registry.advertised_capabilities(other_era);
|
|
assert!(
|
|
other_era_advertisement
|
|
.as_object()
|
|
.is_some_and(|object| !object.contains_key(key)),
|
|
"{key} must never leak into {other_era}: {other_era_advertisement}"
|
|
);
|
|
assert_eq!(
|
|
other_era_advertisement,
|
|
json!({
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
}),
|
|
"the other era advertisement stays the reviewed object"
|
|
);
|
|
}
|
|
}
|
|
|
|
// --- R5: the router's dispatch decision is the registration, never a handler list --------------
|
|
|
|
#[test]
|
|
fn the_production_registration_routes_every_declared_method_in_its_own_era() {
|
|
for (method, era, handler, kind) in [
|
|
(
|
|
"tools/list",
|
|
MODERN_PROTOCOL_VERSION,
|
|
MethodHandler::ToolList,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"tools/call",
|
|
MODERN_PROTOCOL_VERSION,
|
|
MethodHandler::ToolCall,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"tools/list",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::ToolList,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"tools/call",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::ToolCall,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"ping",
|
|
MODERN_PROTOCOL_VERSION,
|
|
MethodHandler::Ping,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"resources/templates/list",
|
|
MODERN_PROTOCOL_VERSION,
|
|
MethodHandler::ResourceTemplateList,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"resources/templates/list",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::ResourceTemplateList,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"ping",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::Ping,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"server/discover",
|
|
MODERN_PROTOCOL_VERSION,
|
|
MethodHandler::Discover,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"initialize",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::Lifecycle,
|
|
Kind::Method,
|
|
),
|
|
(
|
|
"notifications/initialized",
|
|
LEGACY_PROTOCOL_VERSION,
|
|
MethodHandler::Lifecycle,
|
|
Kind::Notification,
|
|
),
|
|
] {
|
|
assert_eq!(
|
|
dispatch_decision(method, era),
|
|
DispatchDecision::Allowed { handler, kind },
|
|
"{method} must route to its registered handler with its registered kind in {era}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_lifecycle_dispatch_decision_carries_the_registered_method_kind() {
|
|
// G2: `initialize` (a request) and `notifications/initialized` (a notification) share the
|
|
// Lifecycle handler but are not interchangeable. The decision must carry the registered kind, or
|
|
// the adapter can only tell them apart by the envelope shape and would serve one as the other.
|
|
assert_eq!(
|
|
dispatch_decision("initialize", LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Lifecycle,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
dispatch_decision("notifications/initialized", LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Lifecycle,
|
|
kind: Kind::Notification,
|
|
}
|
|
);
|
|
assert_ne!(
|
|
dispatch_decision("initialize", LEGACY_PROTOCOL_VERSION),
|
|
dispatch_decision("notifications/initialized", LEGACY_PROTOCOL_VERSION),
|
|
"the two lifecycle wire names must never collapse into the same dispatch decision"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_method_of_the_other_era_is_a_wrong_era_decision() {
|
|
assert_eq!(
|
|
dispatch_decision("server/discover", LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::Discover,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
dispatch_decision("initialize", MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::Lifecycle,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
dispatch_decision("notifications/initialized", MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::Lifecycle,
|
|
kind: Kind::Notification,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unregistered_method_has_no_handler_and_no_service() {
|
|
let registry = production_registry();
|
|
for method in [
|
|
"resources/subscribe",
|
|
"logging/setLogLevel",
|
|
"tasks/list",
|
|
"frobnicate",
|
|
] {
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
assert_eq!(
|
|
registry.dispatch_decision(method, revision),
|
|
DispatchDecision::Unregistered,
|
|
"{method} is not registered and must never reach a handler in {revision}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn story317_completion_is_advertised_and_dispatchable_in_both_revisions() {
|
|
let registry = production_registry();
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
assert!(
|
|
registry.advertise_for(CapabilityId::Completions, revision),
|
|
"the implemented completion surface must be advertised in {revision}"
|
|
);
|
|
assert_eq!(
|
|
registry.dispatch_decision("completion/complete", revision),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Completion,
|
|
kind: Kind::Method,
|
|
},
|
|
"`completion/complete` must reach its handler in {revision}"
|
|
);
|
|
assert_eq!(
|
|
registry.advertised_capabilities(revision)["completions"],
|
|
json!({}),
|
|
"the pinned advertisement is an empty `completions` object in {revision}"
|
|
);
|
|
}
|
|
let descriptor = METHOD_DESCRIPTORS
|
|
.iter()
|
|
.find(|descriptor| descriptor.name == "completion/complete")
|
|
.expect("completion/complete is a registered method");
|
|
assert_eq!(
|
|
descriptor.required_headers.0,
|
|
&["Mcp-Method"],
|
|
"the `ref` union declares no single `Mcp-Name` value for the modern header rule"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn story318_logging_is_advertised_on_both_revisions_and_set_level_is_legacy_only() {
|
|
let registry = production_registry();
|
|
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
|
|
assert!(
|
|
registry.advertise_for(CapabilityId::Logging, revision),
|
|
"the implemented logging surface must be advertised in {revision}"
|
|
);
|
|
assert_eq!(
|
|
registry.advertised_capabilities(revision)["logging"],
|
|
json!({}),
|
|
"the pinned advertisement is an empty `logging` object in {revision}"
|
|
);
|
|
}
|
|
|
|
// `logging/setLevel` is a legacy request only: the pinned modern revision replaces it with the
|
|
// per-request `_meta` log level, so the modern era must never serve it.
|
|
assert_eq!(
|
|
registry.dispatch_decision("logging/setLevel", LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::LoggingSetLevel,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
registry.dispatch_decision("logging/setLevel", MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::LoggingSetLevel,
|
|
kind: Kind::Method,
|
|
},
|
|
"a method the modern schema does not declare must never reach a handler there"
|
|
);
|
|
let descriptor = METHOD_DESCRIPTORS
|
|
.iter()
|
|
.find(|descriptor| descriptor.name == "logging/setLevel")
|
|
.expect("logging/setLevel is a registered method");
|
|
assert_eq!(
|
|
descriptor.required_headers.0,
|
|
&["Mcp-Method"],
|
|
"the request declares a level, not a server-side name"
|
|
);
|
|
assert!(
|
|
CAPABILITY_DESCRIPTORS
|
|
.iter()
|
|
.filter(|descriptor| descriptor.id == CapabilityId::LoggingSetLevel)
|
|
.all(|descriptor| descriptor.advertised.is_none()),
|
|
"the client request must never be advertised as a server capability"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn adding_and_removing_a_method_descriptor_changes_the_dispatch_decision() {
|
|
// The inert capability owns one inert method. With the method descriptor registered the
|
|
// router routes it; without it the same request has no handler at all - the decision is the
|
|
// registration, not a fixed list inside the router.
|
|
const INERT_METHOD: MethodDescriptor = MethodDescriptor {
|
|
name: "inert/echo",
|
|
capability: CapabilityId::InertTest,
|
|
kind: Kind::Method,
|
|
required_headers: HEADERS_NONE,
|
|
handler: MethodHandler::Ping,
|
|
};
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(inert_descriptor());
|
|
let mut methods = METHOD_DESCRIPTORS.to_vec();
|
|
|
|
let without = CapabilityRegistry::new(&capabilities, &methods)
|
|
.expect("the production method table stays valid");
|
|
assert_eq!(
|
|
without.dispatch_decision(INERT_METHOD.name, MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::Unregistered
|
|
);
|
|
|
|
methods.push(INERT_METHOD);
|
|
let with = CapabilityRegistry::new(&capabilities, &methods)
|
|
.expect("the inert method descriptor is a valid registration");
|
|
assert_eq!(
|
|
with.dispatch_decision(INERT_METHOD.name, MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Ping,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
with.dispatch_decision(INERT_METHOD.name, LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Ping,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
|
|
// The advertisement projection is unaffected by a method descriptor: only capability
|
|
// declarations advertise.
|
|
assert_eq!(
|
|
with.advertised_capabilities(MODERN_PROTOCOL_VERSION),
|
|
json!({
|
|
"inert_test_capability": {"enabled": true},
|
|
"completions": {},
|
|
"logging": {},
|
|
"prompts": {"listChanged": true},
|
|
"resources": {"listChanged": true, "subscribe": false},
|
|
"tools": {"listChanged": true}
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_method_of_an_unavailable_capability_is_never_dispatched() {
|
|
// The inert capability is modern-only; its method must not be reachable in the legacy era.
|
|
const INERT_METHOD: MethodDescriptor = MethodDescriptor {
|
|
name: "inert/echo",
|
|
capability: CapabilityId::InertTest,
|
|
kind: Kind::Method,
|
|
required_headers: HEADERS_NONE,
|
|
handler: MethodHandler::Ping,
|
|
};
|
|
let mut capabilities = CAPABILITY_DESCRIPTORS.to_vec();
|
|
capabilities.push(modern_only_descriptor());
|
|
let mut methods = METHOD_DESCRIPTORS.to_vec();
|
|
methods.push(INERT_METHOD);
|
|
let registry =
|
|
CapabilityRegistry::new(&capabilities, &methods).expect("valid inert registration");
|
|
|
|
assert_eq!(
|
|
registry.dispatch_decision(INERT_METHOD.name, MODERN_PROTOCOL_VERSION),
|
|
DispatchDecision::Allowed {
|
|
handler: MethodHandler::Ping,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
registry.dispatch_decision(INERT_METHOD.name, LEGACY_PROTOCOL_VERSION),
|
|
DispatchDecision::WrongEra {
|
|
handler: MethodHandler::Ping,
|
|
kind: Kind::Method,
|
|
}
|
|
);
|
|
}
|