#![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 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;
const TEST_KEY: &str = "sf_attach_key";
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),
"serve-attach-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,
}
}
async fn mcp_call(url: &str, body: serde_json::Value) -> serde_json::Value {
let client = reqwest::Client::new();
let resp = client
.post(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json, text/event-stream")
.header("Authorization", format!("Bearer {TEST_KEY}"))
.json(&body)
.send()
.await
.expect("request sent");
assert!(
resp.status().is_success(),
"authenticated /mcp call should succeed, got {}",
resp.status()
);
resp.json::<serde_json::Value>()
.await
.expect("response is JSON-RPC json")
}
fn jsonrpc(id: u32, method: &str, params: serde_json::Value) -> serde_json::Value {
serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"method": method,
"params": params,
})
}
fn result_text(call_result: &serde_json::Value) -> String {
call_result["content"][0]["text"]
.as_str()
.expect("CallToolResult must carry text content")
.to_string()
}
#[tokio::test]
async fn tools_list_over_http_advertises_active_surface() {
let runtime = test_runtime();
let server = start_server(runtime).await;
let response = mcp_call(
&server.mcp_url(),
jsonrpc(1, "tools/list", serde_json::json!({})),
)
.await;
let tools = response["result"]["tools"]
.as_array()
.expect("tools/list result has a tools array");
assert!(!tools.is_empty(), "advertised surface must be non-empty");
let http_names: std::collections::BTreeSet<String> = tools
.iter()
.filter_map(|t| t["name"].as_str().map(str::to_string))
.collect();
let expected_names: std::collections::BTreeSet<String> =
symforge::protocol::surface_probe::list_tools_for_profile(
symforge::protocol::surface_probe::surface_profile_from_env(),
)
.into_iter()
.map(|t| t.name.to_string())
.collect();
assert_eq!(
http_names, expected_names,
"HTTP tools/list surface must equal the in-process list_tools surface"
);
assert!(
http_names.contains("status"),
"status tool must be advertised"
);
server.shutdown().await;
}
#[tokio::test]
async fn status_tools_call_parity_http_vs_dispatch() {
let runtime = test_runtime();
let dispatch_result = runtime
.dispatch_tool_call("status", serde_json::json!({}))
.await
.expect("in-process status dispatch");
let dispatch_json = serde_json::to_value(&dispatch_result).expect("serialize CallToolResult");
let dispatch_text = result_text(&dispatch_json);
let server = start_server(runtime).await;
let response = mcp_call(
&server.mcp_url(),
jsonrpc(
2,
"tools/call",
serde_json::json!({ "name": "status", "arguments": {} }),
),
)
.await;
let http_call_result = &response["result"];
let http_text = result_text(http_call_result);
assert_eq!(
http_text, dispatch_text,
"HTTP tools/call(status) must equal in-process dispatch(status) — one dispatch path"
);
server.shutdown().await;
}
fn pff_symforge_args() -> serde_json::Value {
serde_json::json!({
"query": "read the entire src/main.rs file line by line"
})
}
fn assert_structured_bypass(text: &str) {
assert!(
text.contains("--- bypass payload ---"),
"bypass output must carry the machine-readable payload marker, got:\n{text}"
);
assert!(
text.contains("\"action\":") && text.contains("\"path\":") && text.contains("\"reason\":"),
"bypass payload must contain structured StelBypassBody fields, got:\n{text}"
);
assert!(
text.contains("host_read"),
"P-FF bypass action should be host_read, got:\n{text}"
);
let marker = "--- bypass payload ---";
let after_marker = &text[text.find(marker).expect("payload marker present") + marker.len()..];
let json_start = after_marker
.find('{')
.expect("bypass payload has a JSON object");
let mut stream = serde_json::Deserializer::from_str(&after_marker[json_start..])
.into_iter::<serde_json::Value>();
let parsed: serde_json::Value = stream
.next()
.expect("a JSON value follows the marker")
.expect("bypass payload is valid JSON");
assert_eq!(parsed["action"], "host_read");
assert!(
parsed["reason"]
.as_str()
.is_some_and(|r| r.contains("policy=P-FF")),
"reason must name policy=P-FF"
);
}
#[tokio::test]
async fn bypass_over_http_returns_structured_envelope() {
let _guard = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _surface = stel_surface_env::set_symforge_surface("compact");
let runtime = test_runtime();
let server = start_server(runtime).await;
let response = mcp_call(
&server.mcp_url(),
jsonrpc(
3,
"tools/call",
serde_json::json!({ "name": "symforge", "arguments": pff_symforge_args() }),
),
)
.await;
let text = result_text(&response["result"]);
assert_structured_bypass(&text);
server.shutdown().await;
}
#[tokio::test]
async fn bypass_at_dispatch_returns_structured_envelope() {
let _guard = stel_surface_env::COMPACT_ENV_LOCK.lock().await;
let _surface = stel_surface_env::set_symforge_surface("compact");
let runtime = test_runtime();
let result = runtime
.dispatch_tool_call("symforge", pff_symforge_args())
.await
.expect("symforge dispatch");
let json = serde_json::to_value(&result).expect("serialize CallToolResult");
let text = result_text(&json);
assert_structured_bypass(&text);
}