#![cfg(feature = "server")]
#[path = "support/stel_surface_env.rs"]
mod stel_surface_env;
use std::net::SocketAddr;
use std::sync::Arc;
use parking_lot::Mutex;
use serde_json::{Value, json};
use symforge::live_index::LiveIndex;
use symforge::protocol::SymForgeServer;
use symforge::server::{
AuthConfig, AuthLayerState, ServerRuntime, apply_bearer_auth, mcp_http::build_mcp_router,
};
use symforge::sidecar::governor::RequestGovernor;
use symforge::watcher::WatcherInfo;
fn normalize_root(root: &std::path::Path) -> std::path::PathBuf {
dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf())
}
const TEST_KEY: &str = "sf_rmcp3_key";
const MODERN_VERSION: &str = "2026-07-28";
const EVIDENCE_KEY: &str = "symforge/project_evidence";
fn test_runtime() -> ServerRuntime {
let index = LiveIndex::empty();
let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
let protocol = Arc::new(SymForgeServer::new(
Arc::clone(&index),
"rmcp3-protocol-test".to_string(),
watcher_info,
None,
None,
));
let governor = Arc::new(RequestGovernor::new());
ServerRuntime::build_runtime(
index,
protocol,
governor,
AuthConfig::new(Some(TEST_KEY.to_string())),
None,
)
}
struct TestServer {
addr: SocketAddr,
shutdown: Option<tokio::sync::oneshot::Sender<()>>,
join: tokio::task::JoinHandle<()>,
}
impl TestServer {
fn mcp_url(&self) -> String {
format!("http://{}/mcp", self.addr)
}
async fn shutdown(mut self) {
if let Some(tx) = self.shutdown.take() {
let _ = tx.send(());
}
let _ = self.join.await;
}
}
async fn start_server(runtime: ServerRuntime) -> TestServer {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind ephemeral loopback");
let addr = listener.local_addr().expect("local_addr");
let router = build_mcp_router(&runtime, addr);
let auth_state = AuthLayerState::new(runtime.auth().clone(), true);
let app = apply_bearer_auth(router, auth_state);
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
let join = tokio::spawn(async move {
let shutdown = async {
let _ = rx.await;
};
let _ = axum::serve(listener, app)
.with_graceful_shutdown(shutdown)
.await;
});
TestServer {
addr,
shutdown: Some(tx),
join,
}
}
struct McpHttpClient {
url: String,
client: reqwest::Client,
next_id: u32,
sent_methods: Vec<String>,
}
type Headers<'a> = &'a [(&'a str, &'a str)];
impl McpHttpClient {
fn new(url: String) -> Self {
Self {
url,
client: reqwest::Client::new(),
next_id: 1,
sent_methods: Vec::new(),
}
}
async fn call_raw(
&mut self,
method: &str,
params: Value,
headers: Headers<'_>,
) -> (reqwest::StatusCode, Value) {
let id = self.next_id;
self.next_id += 1;
self.sent_methods.push(method.to_string());
let body = json!({
"jsonrpc": "2.0",
"id": id,
"method": method,
"params": params,
});
let mut request = self
.client
.post(&self.url)
.header("Content-Type", "application/json")
.header("Accept", "application/json, text/event-stream")
.header("Authorization", format!("Bearer {TEST_KEY}"))
.json(&body);
for (name, value) in headers {
request = request.header(*name, *value);
}
let resp = request.send().await.expect("request sent");
let status = resp.status();
let body = resp.json::<Value>().await.expect("JSON-RPC body");
(status, body)
}
async fn call(&mut self, method: &str, params: Value, headers: Headers<'_>) -> Value {
let (status, body) = self.call_raw(method, params, headers).await;
assert!(
status.is_success(),
"{method} should succeed, got {status}: {body}"
);
assert!(
body.get("error").is_none(),
"{method} should not error: {body}"
);
body["result"].clone()
}
}
fn modern_meta() -> Value {
json!({
"io.modelcontextprotocol/protocolVersion": MODERN_VERSION,
"io.modelcontextprotocol/clientCapabilities": {},
})
}
fn with_modern_meta(mut params: Value) -> Value {
params["_meta"] = modern_meta();
params
}
fn modern_headers(method: &str) -> Vec<(&str, &str)> {
vec![
("MCP-Protocol-Version", MODERN_VERSION),
("Mcp-Method", method),
]
}
fn evidence_of(result: &Value) -> &Value {
result
.get("_meta")
.and_then(|meta| meta.get(EVIDENCE_KEY))
.unwrap_or_else(|| panic!("result must carry {EVIDENCE_KEY}: {result}"))
}
#[tokio::test]
async fn negotiation_modern_and_legacy_versions() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
for version in [MODERN_VERSION, "2025-06-18"] {
let result = client
.call(
"initialize",
json!({
"protocolVersion": version,
"capabilities": {},
"clientInfo": {"name": "sc310", "version": "0.0.0"},
}),
&[],
)
.await;
assert_eq!(
result["protocolVersion"], *version,
"requested {version} must negotiate {version}"
);
}
server.shutdown().await;
}
#[tokio::test]
async fn discover_first_serves_full_surface_without_handshake() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let discover = client
.call(
"server/discover",
json!({"_meta": modern_meta()}),
&modern_headers("server/discover"),
)
.await;
assert_eq!(
discover["_meta"]["io.modelcontextprotocol/serverInfo"]["name"],
"symforge"
);
let versions: Vec<&str> = discover["supportedVersions"]
.as_array()
.expect("discover lists protocolVersions")
.iter()
.filter_map(Value::as_str)
.collect();
assert!(
versions.contains(&MODERN_VERSION),
"discover must list {MODERN_VERSION}, got {versions:?}"
);
let tools = client
.call(
"tools/list",
with_modern_meta(json!({})),
&modern_headers("tools/list"),
)
.await;
assert!(!tools["tools"].as_array().expect("tools array").is_empty());
let mut call_headers = modern_headers("tools/call");
call_headers.push(("Mcp-Name", "status"));
let call = client
.call(
"tools/call",
with_modern_meta(json!({"name": "status", "arguments": {}})),
&call_headers,
)
.await;
assert!(call["content"][0]["text"].is_string());
let prompts = client
.call(
"prompts/list",
with_modern_meta(json!({})),
&modern_headers("prompts/list"),
)
.await;
assert!(prompts["prompts"].is_array());
let resources = client
.call(
"resources/list",
with_modern_meta(json!({})),
&modern_headers("resources/list"),
)
.await;
assert!(
!resources["resources"]
.as_array()
.expect("resources array")
.is_empty()
);
let mut read_headers = modern_headers("resources/read");
read_headers.push(("Mcp-Name", "symforge://glossary"));
let read = client
.call(
"resources/read",
with_modern_meta(json!({"uri": "symforge://glossary"})),
&read_headers,
)
.await;
assert!(read["contents"][0]["text"].is_string());
assert!(
!client
.sent_methods
.iter()
.any(|m| m == "initialize" || m == "notifications/initialized"),
"SC-311 must complete with zero handshake, sent: {:?}",
client.sent_methods
);
server.shutdown().await;
}
#[tokio::test]
async fn binding_rung_env_override() {
let _lock = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let dir = tempfile::TempDir::new().expect("tempdir");
let root = dir.path().canonicalize().expect("canonical tempdir");
let guard = stel_surface_env::EnvVarGuard::set(
"SYMFORGE_WORKSPACE_ROOT",
root.to_str().expect("utf8 root"),
);
let resolved = symforge::discovery::find_project_root().expect("env rung binds");
assert_eq!(normalize_root(&resolved), normalize_root(&root),);
drop(guard);
}
#[tokio::test]
async fn binding_rung_index_folder_over_modern_dispatch() {
let dir = tempfile::TempDir::new().expect("tempdir");
std::fs::write(dir.path().join("lib.rs"), "pub fn seeded() {}\n").expect("seed file");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let mut call_headers = modern_headers("tools/call");
call_headers.push(("Mcp-Name", "index_folder"));
let path = dir.path().to_str().expect("utf8 tempdir").to_string();
client
.call(
"tools/call",
with_modern_meta(json!({"name": "index_folder", "arguments": {"path": path}})),
&call_headers,
)
.await;
let mut status_headers = modern_headers("tools/call");
status_headers.push(("Mcp-Name", "status"));
let status = client
.call(
"tools/call",
with_modern_meta(json!({"name": "status", "arguments": {}})),
&status_headers,
)
.await;
let bound_root = evidence_of(&status)["canonical_root"]
.as_str()
.expect("bound root disclosed")
.to_string();
let expected = dir
.path()
.canonicalize()
.expect("canonical tempdir")
.to_string_lossy()
.replace('\\', "/");
assert!(
normalize_root(std::path::Path::new(&bound_root))
== normalize_root(std::path::Path::new(&expected)),
"index_folder rung must bind {expected}, evidence says {bound_root}"
);
assert!(
!client
.sent_methods
.iter()
.any(|m| m == "initialize" || m == "notifications/initialized"),
"binding must not require a handshake"
);
server.shutdown().await;
}
#[tokio::test]
async fn mutation_no_apply_is_error_over_http_transport() {
let dir = tempfile::TempDir::new().expect("tempdir");
std::fs::create_dir_all(dir.path().join("src")).expect("src dir");
std::fs::write(
dir.path().join("src/lib.rs"),
"pub fn present() {}\npub fn other() {}\n",
)
.expect("seed file");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let path = dir.path().to_str().expect("utf8 tempdir").to_string();
let mut index_headers = modern_headers("tools/call");
index_headers.push(("Mcp-Name", "index_folder"));
client
.call(
"tools/call",
with_modern_meta(json!({"name": "index_folder", "arguments": {"path": path}})),
&index_headers,
)
.await;
let mut edit_headers = modern_headers("tools/call");
edit_headers.push(("Mcp-Name", "delete_symbol"));
let missing = client
.call(
"tools/call",
with_modern_meta(json!({
"name": "delete_symbol",
"arguments": {"path": "src/lib.rs", "name": "missing", "dry_run": false}
})),
&edit_headers,
)
.await;
assert_eq!(
missing["isError"],
json!(true),
"a delete that found nothing to delete must be a transport-visible error: {missing}"
);
assert_eq!(
missing["_meta"]["symforge/result_status"]["status"],
json!("not_found"),
"{missing}"
);
assert_eq!(
std::fs::read_to_string(dir.path().join("src/lib.rs")).expect("read back"),
"pub fn present() {}\npub fn other() {}\n",
"a failed mutation must not touch the file"
);
let preview = client
.call(
"tools/call",
with_modern_meta(json!({
"name": "delete_symbol",
"arguments": {"path": "src/lib.rs", "name": "other", "dry_run": true}
})),
&edit_headers,
)
.await;
assert_eq!(
preview["isError"],
json!(false),
"a dry run is not a failure: {preview}"
);
assert_eq!(
preview["_meta"]["symforge/result_status"]["status"],
json!("dry_run_success"),
"{preview}"
);
server.shutdown().await;
}
#[tokio::test]
async fn binding_rung_cwd_walk() {
let _lock = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _env = stel_surface_env::EnvVarGuard::unset("SYMFORGE_WORKSPACE_ROOT");
let dir = tempfile::TempDir::new().expect("tempdir");
std::fs::create_dir(dir.path().join(".git")).expect("plant .git");
let nested = dir.path().join("src");
std::fs::create_dir(&nested).expect("nested dir");
let original = std::env::current_dir().expect("cwd");
std::env::set_current_dir(&nested).expect("enter nested");
let resolved = symforge::discovery::find_project_root();
std::env::set_current_dir(&original).expect("restore cwd");
let resolved = resolved.expect("cwd walk binds the .git ancestor");
assert_eq!(
normalize_root(&resolved),
normalize_root(&dir.path().canonicalize().expect("canonical tempdir")),
);
}
#[test]
fn list_roots_is_solicited_only_from_on_initialized() {
let source =
std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/src/protocol/mod.rs"))
.expect("read protocol/mod.rs");
let solicitations = source
.lines()
.filter(|line| !line.trim_start().starts_with("//"))
.filter(|line| line.contains(".list_roots("))
.count();
assert_eq!(
solicitations, 1,
"Peer::list_roots must be solicited only inside bind_workspace_from_client_roots"
);
let grep_others = |path: &str| {
let full = format!("{}/{}", env!("CARGO_MANIFEST_DIR"), path);
std::fs::read_to_string(&full)
.map(|text| text.matches(".list_roots(").count())
.unwrap_or(0)
};
assert_eq!(grep_others("src/protocol/tools.rs"), 0);
assert_eq!(grep_others("src/server/mcp_http.rs"), 0);
}
#[tokio::test]
async fn cache_hints_on_all_five_surfaces() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let static_lists = [
("tools/list", json!({})),
("prompts/list", json!({})),
("resources/list", json!({})),
("resources/templates/list", json!({})),
];
for (method, params) in static_lists {
let result = client
.call(method, with_modern_meta(params), &modern_headers(method))
.await;
assert_eq!(
result["ttlMs"], 3_600_000,
"{method} must carry the FR-311 static-list TTL: {result}"
);
assert_eq!(
result["cacheScope"], "public",
"{method} must be publicly cacheable: {result}"
);
}
let mut read_headers = modern_headers("resources/read");
read_headers.push(("Mcp-Name", "symforge://glossary"));
let read = client
.call(
"resources/read",
with_modern_meta(json!({"uri": "symforge://glossary"})),
&read_headers,
)
.await;
assert_eq!(read["ttlMs"], 0, "reads are immediately stale (INV-4)");
assert_eq!(
read["cacheScope"], "private",
"reads must never be shared-cacheable (INV-4)"
);
let names = |result: &Value| -> Vec<String> {
result["tools"]
.as_array()
.expect("tools array")
.iter()
.map(|tool| tool["name"].as_str().expect("tool name").to_string())
.collect()
};
let first = client
.call(
"tools/list",
with_modern_meta(json!({})),
&modern_headers("tools/list"),
)
.await;
let second = client
.call(
"tools/list",
with_modern_meta(json!({})),
&modern_headers("tools/list"),
)
.await;
assert_eq!(
names(&first),
names(&second),
"tools/list ordering must be deterministic"
);
server.shutdown().await;
}
#[tokio::test]
async fn strict_metadata_mixed_endpoint() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let (status, body) = client
.call_raw("tools/list", json!({}), &modern_headers("tools/list"))
.await;
assert_eq!(status, reqwest::StatusCode::BAD_REQUEST, "{body}");
assert!(
body["error"]["message"]
.as_str()
.unwrap_or_default()
.contains("required fields"),
"missing-_meta rejection must name the gap: {body}"
);
let (status, body) = client
.call_raw(
"tools/list",
json!({"_meta": {
"io.modelcontextprotocol/protocolVersion": "2025-11-25",
"io.modelcontextprotocol/clientCapabilities": {},
}}),
&modern_headers("tools/list"),
)
.await;
assert_eq!(status, reqwest::StatusCode::BAD_REQUEST, "{body}");
assert!(
body["error"]["message"]
.as_str()
.unwrap_or_default()
.contains("does not match"),
"version disagreement must be named: {body}"
);
let (status, body) = client
.call_raw(
"tools/list",
with_modern_meta(json!({})),
&[
("MCP-Protocol-Version", MODERN_VERSION),
("Mcp-Method", "prompts/list"),
],
)
.await;
assert_eq!(status, reqwest::StatusCode::BAD_REQUEST, "{body}");
assert!(
body["error"]["message"]
.as_str()
.unwrap_or_default()
.contains("Mcp-Method"),
"standard-header disagreement must be named: {body}"
);
let result = client
.call(
"tools/list",
with_modern_meta(json!({})),
&modern_headers("tools/list"),
)
.await;
assert!(!result["tools"].as_array().expect("tools").is_empty());
let (status, body) = client.call_raw("tools/list", json!({}), &[]).await;
assert_eq!(status, reqwest::StatusCode::OK, "{body}");
assert!(body.get("error").is_none(), "legacy accept intact: {body}");
server.shutdown().await;
}
#[tokio::test]
async fn compact_gate_rejects_off_surface_calls_via_real_dispatch() {
let _lock = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _guard = stel_surface_env::set_symforge_surface("compact");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let (status, body) = client
.call_raw(
"tools/call",
json!({"name": "get_symbol", "arguments": {"name": "anything"}}),
&[],
)
.await;
let error = &body["error"];
assert!(
!error.is_null(),
"off-surface call must be rejected (status {status}): {body}"
);
assert_eq!(
error["code"], -32600,
"compact gate rejects with InvalidRequest: {body}"
);
let (status, body) = client
.call_raw(
"tools/call",
json!({"name": "status", "arguments": {}}),
&[],
)
.await;
assert_eq!(status, reqwest::StatusCode::OK, "{body}");
assert!(
body.get("error").is_none(),
"on-surface compact call must succeed: {body}"
);
server.shutdown().await;
}
#[tokio::test]
async fn admitted_facade_measurement_preserves_error_prefixed_source_at_wire() {
let _lock = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let full_surface = stel_surface_env::set_symforge_surface("full");
let dir = tempfile::TempDir::new().expect("tempdir");
let source = "Error: ordinary repository text, not a tool failure\nstill ordinary\n";
std::fs::write(dir.path().join("error_prefix.rs"), source).expect("seed relay fixture");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let root = dir.path().to_str().expect("utf8 tempdir").to_string();
client
.call(
"tools/call",
json!({"name": "index_folder", "arguments": {"path": root}}),
&[],
)
.await;
drop(full_surface);
let _compact_surface = stel_surface_env::set_symforge_surface("compact");
let result = client
.call(
"tools/call",
json!({
"name": "symforge",
"arguments": {
"_probe_legacy_tool": "get_file_content",
"_probe_legacy_args": {"path": "error_prefix.rs"}
}
}),
&[],
)
.await;
assert_eq!(result["content"][0]["text"], source, "{result}");
assert_ne!(
result.get("isError"),
Some(&Value::Bool(true)),
"raw source must not be inferred as an MCP error: {result}"
);
assert!(
result
.get("_meta")
.and_then(|meta| meta.get("symforge/result_status"))
.is_none(),
"measurement relay must remain result-status-free: {result}"
);
let evidence = evidence_of(&result);
assert!(evidence["project_id"].is_string(), "{evidence}");
server.shutdown().await;
}
#[tokio::test]
async fn evidence_disclosed_on_statused_plain_and_resource_results() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let mut status_headers = modern_headers("tools/call");
status_headers.push(("Mcp-Name", "status"));
let statused = client
.call(
"tools/call",
with_modern_meta(json!({"name": "status", "arguments": {}})),
&status_headers,
)
.await;
let evidence = evidence_of(&statused);
assert!(evidence["project_id"].is_string(), "{evidence}");
let mut health_headers = modern_headers("tools/call");
health_headers.push(("Mcp-Name", "health"));
let health = client
.call(
"tools/call",
with_modern_meta(json!({"name": "health", "arguments": {}})),
&health_headers,
)
.await;
let evidence = evidence_of(&health);
assert!(evidence["project_id"].is_string(), "{evidence}");
let mut read_headers = modern_headers("resources/read");
read_headers.push(("Mcp-Name", "symforge://glossary"));
let read = client
.call(
"resources/read",
with_modern_meta(json!({"uri": "symforge://glossary"})),
&read_headers,
)
.await;
let evidence = evidence_of(&read);
assert!(evidence["project_id"].is_string(), "{evidence}");
assert_eq!(evidence["project_id"], "unbound", "{evidence}");
assert!(evidence["canonical_root"].is_null(), "{evidence}");
server.shutdown().await;
}
#[tokio::test]
async fn evidence_discloses_foreign_binding() {
let repo_a = tempfile::TempDir::new().expect("repo A");
std::fs::write(repo_a.path().join("a.rs"), "pub fn a() {}\n").expect("seed A");
let repo_b = tempfile::TempDir::new().expect("repo B");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let mut index_headers = modern_headers("tools/call");
index_headers.push(("Mcp-Name", "index_folder"));
let path_a = repo_a.path().to_str().expect("utf8 A").to_string();
client
.call(
"tools/call",
with_modern_meta(json!({"name": "index_folder", "arguments": {"path": path_a}})),
&index_headers,
)
.await;
let mut status_headers = modern_headers("tools/call");
status_headers.push(("Mcp-Name", "status"));
let statused = client
.call(
"tools/call",
with_modern_meta(json!({"name": "status", "arguments": {}})),
&status_headers,
)
.await;
let disclosed = evidence_of(&statused)["canonical_root"]
.as_str()
.expect("bound root disclosed")
.to_string();
let normalize = |p: &std::path::Path| normalize_root(p);
let root_a = repo_a.path().canonicalize().expect("canonical A");
let root_b = repo_b.path().canonicalize().expect("canonical B");
assert_eq!(
normalize(std::path::Path::new(&disclosed)),
normalize(&root_a),
"evidence must name the ACTUAL binding"
);
assert_ne!(
normalize(std::path::Path::new(&disclosed)),
normalize(&root_b),
"a client expecting repo B can detect the foreign binding"
);
server.shutdown().await;
}
#[tokio::test]
async fn foreign_selector_early_errors_never_attach_home_evidence() {
let repo_a = tempfile::TempDir::new().expect("repo A");
std::fs::write(repo_a.path().join("a.rs"), "pub fn a() {}\n").expect("seed A");
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let mut index_headers = modern_headers("tools/call");
index_headers.push(("Mcp-Name", "index_folder"));
client
.call(
"tools/call",
with_modern_meta(json!({
"name": "index_folder",
"arguments": {"path": repo_a.path().to_str().expect("utf8 A")},
})),
&index_headers,
)
.await;
let mut symforge_headers = modern_headers("tools/call");
symforge_headers.push(("Mcp-Name", "symforge"));
let cases = [
json!({
"name": "symforge",
"arguments": {"query": "", "project": "foreign-project"},
}),
json!({
"name": "symforge",
"arguments": {
"query": "measurement",
"_probe_legacy_tool": "get_file_content",
"_probe_legacy_args": {"path": 7, "project": "foreign-project"},
},
}),
];
for params in cases {
let result = client
.call("tools/call", with_modern_meta(params), &symforge_headers)
.await;
assert_eq!(
evidence_of(&result),
&json!({
"bound": false,
"reason": "project_evidence_unavailable",
}),
"an early foreign-selector error must not inherit HOME: {result}"
);
}
server.shutdown().await;
}
#[tokio::test]
async fn result_type_present_for_modern_stripped_for_legacy() {
let server = start_server(test_runtime()).await;
let mut client = McpHttpClient::new(server.mcp_url());
let modern = client
.call(
"tools/list",
with_modern_meta(json!({})),
&modern_headers("tools/list"),
)
.await;
assert_eq!(
modern["resultType"], "complete",
"modern peers must see resultType: {modern}"
);
let (status, body) = client.call_raw("tools/list", json!({}), &[]).await;
assert_eq!(status, reqwest::StatusCode::OK);
assert!(
body["result"].get("resultType").is_none(),
"legacy peers must not see resultType: {body}"
);
server.shutdown().await;
}