CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 31m21s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
271 lines
9.4 KiB
Rust
271 lines
9.4 KiB
Rust
//! #107 negotiation results: existing HTTP wire GREEN, independent SDK execution oracle RED.
|
|
use super::{CounterSet, observe, support::*};
|
|
|
|
use serde_json::{Value, json};
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
|
|
|
|
fn sdk_counts(c: &CounterSet) -> [usize; 2] {
|
|
[c.sdk_initialize_result(), c.sdk_discover_result()]
|
|
}
|
|
|
|
fn capabilities() -> Value {
|
|
// Pin the reviewed advertisement, not a call to the builder under test or SDK defaults.
|
|
json!({"completions":{},"logging":{},"prompts":{"listChanged":true},
|
|
"resources":{"listChanged":true,"subscribe":false},"tools":{"listChanged":true}})
|
|
}
|
|
|
|
fn expected_result(revision: &str) -> Value {
|
|
let server = json!({"name":"crank-mcp-server","version":env!("CARGO_PKG_VERSION")});
|
|
if revision == LEGACY_REVISION {
|
|
json!({"protocolVersion":LEGACY_REVISION,"capabilities":capabilities(),"serverInfo":server})
|
|
} else {
|
|
json!({"resultType":"complete","supportedVersions":["2026-07-28","2025-11-25"],
|
|
"capabilities":capabilities(),"_meta":{"io.modelcontextprotocol/serverInfo":server},
|
|
"ttlMs":0,"cacheScope":"private"})
|
|
}
|
|
}
|
|
|
|
async fn wire(response: reqwest::Response, revision: &str, id: &Value, sse: bool) -> Value {
|
|
assert_eq!(response.status(), 200);
|
|
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
|
|
assert_eq!(response.headers()["x-request-id"], "negotiation-request");
|
|
assert_eq!(
|
|
response.headers()["x-trace-id"],
|
|
"11111111111111111111111111111111"
|
|
);
|
|
let content_type = response.headers()["content-type"].to_str().unwrap();
|
|
assert!(content_type.starts_with(if sse {
|
|
"text/event-stream"
|
|
} else {
|
|
"application/json"
|
|
}));
|
|
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let body: Value = if sse {
|
|
let frames: Vec<Value> = raw
|
|
.lines()
|
|
.filter_map(|line| line.strip_prefix("data:"))
|
|
.map(|data| serde_json::from_str(data.trim()).unwrap())
|
|
.collect();
|
|
assert_eq!(
|
|
frames.len(),
|
|
1,
|
|
"one terminal, no unsolicited notifications: {raw}"
|
|
);
|
|
frames.into_iter().next().unwrap()
|
|
} else {
|
|
serde_json::from_str(&raw).unwrap()
|
|
};
|
|
assert_eq!(
|
|
body,
|
|
json!({"jsonrpc":"2.0","id":id,"result":expected_result(revision)}),
|
|
"exact result/envelope fields and omissions (including title/icons/instructions/defaults)"
|
|
);
|
|
body
|
|
}
|
|
|
|
async fn control(revision: &str) -> Vec<[usize; 2]> {
|
|
crank_test_support::postgres_schema_url_for_task("negotiation_owner", "t_99aa603c").await;
|
|
let h = Endpoint::started().await;
|
|
let mut deltas = vec![];
|
|
// Accept order selects JSON or a single terminal SSE frame on the real transport.
|
|
for accept in ["application/json", "text/event-stream, application/json"] {
|
|
let id = json!("negotiation/Ю");
|
|
let payload = if revision == LEGACY_REVISION {
|
|
let mut initialize = legacy_initialize(1);
|
|
initialize["id"] = id.clone();
|
|
initialize
|
|
} else {
|
|
json!({"jsonrpc":"2.0","id":id,"method":"server/discover","params":{"_meta":modern_meta()}})
|
|
};
|
|
let before = sdk_counts(&h.counters);
|
|
let admission_before = h.counters.snapshot();
|
|
let response = h
|
|
.request(revision, None, &h.key, accept, payload)
|
|
.header("x-request-id", "negotiation-request")
|
|
.header(
|
|
"traceparent",
|
|
"00-11111111111111111111111111111111-2222222222222222-01",
|
|
)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let session = response
|
|
.headers()
|
|
.get("MCP-Session-Id")
|
|
.map(|v| v.to_str().unwrap().to_owned());
|
|
if revision == LEGACY_REVISION {
|
|
assert!(
|
|
!session
|
|
.as_deref()
|
|
.expect("initialize creates a session")
|
|
.is_empty()
|
|
);
|
|
} else {
|
|
assert_eq!(
|
|
session, None,
|
|
"stateless discovery never creates a session carrier"
|
|
);
|
|
}
|
|
wire(
|
|
response,
|
|
revision,
|
|
&id,
|
|
accept.starts_with("text/event-stream"),
|
|
)
|
|
.await;
|
|
assert_deltas(
|
|
"negotiation admission positive control",
|
|
admission_before,
|
|
h.counters.snapshot(),
|
|
if revision == LEGACY_REVISION {
|
|
super::Snapshot::new(1, 0, 1, 0)
|
|
} else {
|
|
super::Snapshot::new(1, 1, 0, 0)
|
|
},
|
|
);
|
|
let after = sdk_counts(&h.counters);
|
|
deltas.push([after[0] - before[0], after[1] - before[1]]);
|
|
if let Some(session) = session {
|
|
let response = h
|
|
.request(
|
|
revision,
|
|
Some(&session),
|
|
&h.key,
|
|
accept,
|
|
initialized_notification(),
|
|
)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), 202);
|
|
assert!(response.bytes().await.unwrap().is_empty());
|
|
// Prove the minted session can authorize the next protected request.
|
|
let response = h
|
|
.request(
|
|
revision,
|
|
Some(&session),
|
|
&h.key,
|
|
"application/json",
|
|
json!({"jsonrpc":"2.0","id":2,"method":"ping"}),
|
|
)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), 200);
|
|
assert_eq!(
|
|
json_body(response).await,
|
|
json!({"jsonrpc":"2.0","id":2,"result":{"protocolVersion":LEGACY_REVISION}})
|
|
);
|
|
}
|
|
}
|
|
eprintln!("existing GREEN lifecycle negotiation wire {revision}; SDK deltas={deltas:?}");
|
|
deltas
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_wire_legacy() {
|
|
control(LEGACY_REVISION).await;
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_wire_modern() {
|
|
control(MODERN_REVISION).await;
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_sdk_legacy() {
|
|
assert_eq!(
|
|
control(LEGACY_REVISION).await,
|
|
vec![[1, 0]; 2],
|
|
"real HTTP initialize must serialize SDK InitializeResult (not admission/envelope only)"
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_sdk_modern() {
|
|
assert_eq!(
|
|
control(MODERN_REVISION).await,
|
|
vec![[0, 1]; 2],
|
|
"real HTTP discovery must serialize SDK DiscoverResult (not admission/envelope only)"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_observer_positive_control() {
|
|
let c = Arc::new(CounterSet::default());
|
|
observe(c.clone(), async {
|
|
super::admission_enter();
|
|
super::sdk_initialize_result_serialized();
|
|
assert_eq!(
|
|
sdk_counts(&c),
|
|
[1, 0],
|
|
"initialize and discovery are separate markers"
|
|
);
|
|
super::sdk_discover_result_serialized();
|
|
})
|
|
.await;
|
|
assert_eq!(sdk_counts(&c), [1, 1]);
|
|
assert_eq!(c.snapshot(), super::Snapshot::new(1, 0, 0, 0));
|
|
super::sdk_initialize_result_serialized();
|
|
super::sdk_discover_result_serialized();
|
|
assert_eq!(sdk_counts(&c), [1, 1], "out-of-scope markers are no-ops");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_lifecycle_negotiation_wire_session_operations() {
|
|
crank_test_support::postgres_schema_url_for_task("negotiation_sessions_owner", "t_99aa603c")
|
|
.await;
|
|
let h = Harness::started().await;
|
|
// Existing counted-session fixture supplies independent get/create/initialized oracles.
|
|
run_valid_legacy_initialize(&h, 10, "legacy create exactly once").await;
|
|
let session = h.initialize_legacy_session().await;
|
|
run_valid_legacy_call(
|
|
&h,
|
|
&session,
|
|
11,
|
|
"negotiation@example.test",
|
|
"initialized session works",
|
|
)
|
|
.await;
|
|
for accept in ["application/json", "text/event-stream, application/json"] {
|
|
let before = h.session_ops();
|
|
let admission_before = h.snapshot();
|
|
let response = h
|
|
.post(
|
|
&[
|
|
("MCP-Protocol-Version", MODERN_REVISION),
|
|
("Mcp-Method", "server/discover"),
|
|
("Accept", accept),
|
|
("x-request-id", "negotiation-request"),
|
|
(
|
|
"traceparent",
|
|
"00-11111111111111111111111111111111-2222222222222222-01",
|
|
),
|
|
],
|
|
&json!({"jsonrpc":"2.0","id":"discovery","method":"server/discover",
|
|
"params":{"_meta":modern_meta()}}),
|
|
)
|
|
.await;
|
|
assert!(response.headers().get("MCP-Session-Id").is_none());
|
|
wire(response, MODERN_REVISION, &json!("discovery"), false).await;
|
|
assert_deltas(
|
|
"modern discovery",
|
|
admission_before,
|
|
h.snapshot(),
|
|
super::Snapshot::new(1, 1, 0, 0),
|
|
);
|
|
assert_session_ops(
|
|
"stateless discovery",
|
|
before,
|
|
h.session_ops(),
|
|
SessionOps {
|
|
get: 0,
|
|
create: 0,
|
|
mark_initialized: 0,
|
|
},
|
|
);
|
|
}
|
|
}
|