Файли
openidea/crates/ide-mcp/tests/server.rs
T
Mr0Bread 71b4e1c367 refactor: keep test-only helpers out of production APIs (#5495)
Some `pub` items existed only to serve tests: they widened public APIs, and four were thin wrappers over the production entry point, so tests ran a different path than production. Each item below was re-checked on current `origin/main` with `lsp` references and `rg -w` across crates, `tests/`, benches, `src-tauri` and docs, then handled by the rule in the last column.
| Item | Users found | Decision |
|---|---|---|
| `LspManager::references` | only `ide-lsp/tests/{mock_server,real_servers}.rs` (38 calls); production uses `references_streaming` | wrapper deleted; tests call `references_streaming(.., true, cancel, None)` (the wrapper's exact body); doc merged |
| `McpServer::start` | in-crate test, `ide-mcp/tests/server.rs`, `ide-host/tests/mcp.rs`; production uses `start_with` | wrapper deleted; callers pass `ServerOptions::default()` (the wrapper's exact body) |
| `ide_mcp::endpoint_url` | in-crate test, `ide-mcp/tests/server.rs` | deleted; callers use `endpoint_url_at(Ipv4Addr::LOCALHOST.into(), ..)` |

Co-authored-by: Mr0Bread <[email protected]>
2026-11-24 07:01:46 +08:00

590 рядки
27 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! The server over real HTTP, and the stdio bridge in front of it, with a
//! registry standing in for the IDE host.
use ide_mcp::{Discovery, McpServer, ServerOptions, Tool, ToolCallError, ToolRegistry, discovery_path, stdio};
use serde_json::{Value, json};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
struct Registry {
project: PathBuf,
}
impl ToolRegistry for Registry {
fn projects(&self) -> Vec<PathBuf> {
vec![self.project.clone()]
}
fn tools(&self, project: &Path) -> Result<Vec<Tool>, String> {
assert_eq!(project, self.project);
Ok(vec![Tool {
name: "echo".into(),
description: "Says it back".into(),
input_schema: json!({ "type": "object", "properties": { "text": { "type": "string" } } }),
read_only: true,
destructive: false,
output_schema: None,
}])
}
fn call(
&self,
project: &Path,
name: &str,
arguments: &str,
_cancel: ide_mcp::CancellationToken,
) -> Result<String, ToolCallError> {
let args: Value = serde_json::from_str(arguments).unwrap();
match name {
"echo" if args["text"] == "fail" => Err(ToolCallError::Failed("it failed".into())),
"echo" => Ok(format!("{} in {}", args["text"].as_str().unwrap_or(""), project.display())),
"sleep" => {
std::thread::sleep(std::time::Duration::from_millis(args["ms"].as_u64().unwrap()));
Ok("slept".into())
}
_ => Err(ToolCallError::UnknownTool(format!("no tool {name:?}"))),
}
}
}
struct Setup {
_tmp: tempfile::TempDir,
project: PathBuf,
discovery: PathBuf,
server: Option<McpServer>,
}
fn setup() -> Setup {
let tmp = tempfile::tempdir().unwrap();
let project = dunce::canonicalize(tmp.path()).unwrap().join("project");
std::fs::create_dir_all(project.join("src")).unwrap();
let discovery = discovery_path(&tmp.path().join("app-data"));
let registry = Arc::new(Registry { project: project.clone() });
let server = McpServer::start_with(
registry,
Some(discovery.clone()),
&tokio::runtime::Handle::current(),
ServerOptions::default(),
)
.unwrap();
Setup { _tmp: tmp, project, discovery, server: Some(server) }
}
impl Setup {
fn server(&self) -> &McpServer {
self.server.as_ref().unwrap()
}
}
/// A raw HTTP/1.1 exchange: (status, body).
async fn http(port: u16, request: &str) -> (u16, String) {
let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port)).await.unwrap();
stream.write_all(request.as_bytes()).await.unwrap();
let mut out = String::new();
stream.read_to_string(&mut out).await.unwrap();
let status = out.split(' ').nth(1).unwrap().parse().unwrap();
let body = out.split_once("\r\n\r\n").map(|(_, b)| b.to_owned()).unwrap_or_default();
(status, body)
}
/// POSTs `body` to `path` with extra header lines; closes the connection after.
async fn post(s: &Setup, path: &str, headers: &[&str], body: &Value) -> (u16, String) {
let body = body.to_string();
let port = s.server().port();
let mut head = format!(
"POST {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nContent-Type: application/json\r\nAccept: application/json, text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n",
body.len()
);
for h in headers {
head.push_str(h);
head.push_str("\r\n");
}
http(port, &format!("{head}\r\n{body}")).await
}
fn bearer(s: &Setup) -> String {
format!("Authorization: Bearer {}", s.server().token())
}
async fn rpc(s: &Setup, method: &str, params: Value) -> Value {
let project = format!("X-OpenIDEA-Project: {}", s.project.display());
let (status, body) = post(
s,
"/mcp",
&[&bearer(s), &project],
&json!({ "jsonrpc": "2.0", "id": 7, "method": method, "params": params }),
)
.await;
assert_eq!(status, 200, "{body}");
let reply: Value = serde_json::from_str(&body).unwrap();
assert_eq!(reply["id"], 7);
reply
}
#[tokio::test(flavor = "multi_thread")]
async fn initialize_list_and_call_tools() {
let s = setup();
let init = rpc(
&s,
"initialize",
json!({ "protocolVersion": "2025-03-26", "capabilities": {}, "clientInfo": { "name": "t", "version": "1" } }),
)
.await;
assert_eq!(init["result"]["protocolVersion"], "2025-03-26");
assert_eq!(init["result"]["serverInfo"]["name"], "OpenIDEA");
assert!(init["result"]["capabilities"]["tools"].is_object());
let newer = rpc(&s, "initialize", json!({ "protocolVersion": "2099-01-01" })).await;
assert_eq!(newer["result"]["protocolVersion"], "2025-06-18");
// Notifications are accepted without a body.
let (status, body) =
post(&s, "/mcp", &[&bearer(&s)], &json!({ "jsonrpc": "2.0", "method": "notifications/initialized" })).await;
assert_eq!((status, body.as_str()), (202, ""));
let list = rpc(&s, "tools/list", json!({})).await;
let tool = &list["result"]["tools"][0];
assert_eq!(tool["name"], "echo");
assert_eq!(tool["inputSchema"]["type"], "object");
assert_eq!(tool["annotations"], json!({ "readOnlyHint": true, "destructiveHint": false }));
let call = rpc(&s, "tools/call", json!({ "name": "echo", "arguments": { "text": "hi" } })).await;
let expected = format!("hi in {}", s.project.display());
assert_eq!(call["result"], json!({ "content": [{ "type": "text", "text": expected }], "isError": false }));
let failed = rpc(&s, "tools/call", json!({ "name": "echo", "arguments": { "text": "fail" } })).await;
assert_eq!(failed["result"]["isError"], true);
assert_eq!(failed["result"]["content"][0]["text"], "it failed");
let unknown = rpc(&s, "tools/call", json!({ "name": "nope" })).await;
assert_eq!(unknown["error"]["code"], -32602);
assert_eq!(rpc(&s, "ping", json!({})).await["result"], json!({}));
assert_eq!(rpc(&s, "resources/list", json!({})).await["error"]["code"], -32601);
// The project by query, from a folder inside it; a batch.
let query = format!(
"/mcp?project={}",
ide_mcp::endpoint_url_at(std::net::Ipv4Addr::LOCALHOST.into(), 1, &s.project.join("src"))
.split_once('=')
.unwrap()
.1
);
let batch = json!([
{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "echo", "arguments": { "text": "a" } } },
{ "jsonrpc": "2.0", "method": "notifications/cancelled", "params": { "requestId": 9 } },
{ "jsonrpc": "2.0", "id": 2, "method": "ping" }
]);
let (status, body) = post(&s, &query, &[&bearer(&s)], &batch).await;
assert_eq!(status, 200, "{body}");
let replies: Value = serde_json::from_str(&body).unwrap();
assert_eq!(replies.as_array().unwrap().len(), 2);
assert_eq!(replies[0]["result"]["content"][0]["text"], expected.replacen("hi", "a", 1));
}
#[tokio::test(flavor = "multi_thread")]
async fn requests_without_the_token_or_from_web_pages_are_refused() {
let s = setup();
let ping = json!({ "jsonrpc": "2.0", "id": 1, "method": "ping" });
assert_eq!(post(&s, "/mcp", &[], &ping).await.0, 401);
assert_eq!(post(&s, "/mcp", &["Authorization: Bearer wrong"], &ping).await.0, 401);
assert_eq!(post(&s, "/mcp", &[&bearer(&s), "Origin: https://evil.example"], &ping).await.0, 403);
let port = s.server().port();
let (status, _) = http(
port,
&format!(
"POST /mcp HTTP/1.1\r\nHost: evil.example:{port}\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{{}}"
),
)
.await;
assert_eq!(status, 403, "a rebound host name");
// No SSE stream to open; other paths don't exist.
let get = format!("GET /mcp HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n{}\r\nConnection: close\r\n\r\n", bearer(&s));
assert_eq!(http(port, &get).await.0, 405);
assert_eq!(post(&s, "/other", &[&bearer(&s)], &ping).await.0, 404);
assert_eq!(post(&s, "/mcp", &[&bearer(&s), "MCP-Protocol-Version: 1999-01-01"], &ping).await.0, 400);
let (status, body) = post(&s, "/mcp", &[&bearer(&s)], &json!("{")).await;
assert_eq!(status, 200, "a JSON string is a message, just not a request: {body}");
let garbage = format!(
"POST /mcp HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n{}\r\nContent-Length: 1\r\nConnection: close\r\n\r\n{{",
bearer(&s)
);
let (status, body) = http(port, &garbage).await;
assert_eq!(status, 400);
assert_eq!(serde_json::from_str::<Value>(&body).unwrap()["error"]["code"], -32700);
}
#[tokio::test(flavor = "multi_thread")]
async fn folders_that_are_not_open_are_named_in_the_error() {
let s = setup();
let elsewhere = s.project.parent().unwrap().join("elsewhere");
let header = format!("X-OpenIDEA-Project: {}", elsewhere.display());
let (status, body) =
post(&s, "/mcp", &[&bearer(&s), &header], &json!({"jsonrpc":"2.0","id":3,"method":"tools/list"})).await;
assert_eq!(status, 200);
let reply: Value = serde_json::from_str(&body).unwrap();
assert!(reply["error"]["message"].as_str().unwrap().contains("isn't open in OpenIDEA"));
let (_, body) = post(&s, "/mcp", &[&bearer(&s)], &json!({"jsonrpc":"2.0","id":3,"method":"tools/list"})).await;
assert!(serde_json::from_str::<Value>(&body).unwrap()["result"]["tools"].is_array());
}
/// One project open in a window and, optionally, one folder served without a window.
struct WithBackground {
open: PathBuf,
background: Option<PathBuf>,
}
impl ToolRegistry for WithBackground {
fn projects(&self) -> Vec<PathBuf> {
vec![self.open.clone()]
}
fn resolve(&self, path: &Path) -> Option<PathBuf> {
self.background.clone().filter(|root| path.starts_with(root))
}
fn tools(&self, project: &Path) -> Result<Vec<Tool>, String> {
Ok(vec![Tool {
name: "where".into(),
description: format!("Names {}", project.display()),
input_schema: json!({ "type": "object" }),
read_only: true,
destructive: false,
output_schema: None,
}])
}
fn call(&self, project: &Path, _: &str, _: &str, _: ide_mcp::CancellationToken) -> Result<String, ToolCallError> {
Ok(json!({"project":project}).to_string())
}
}
/// The server over [`WithBackground`]; the folder `inventory` next to the open project.
fn setup_with_background(served: bool) -> (Setup, PathBuf) {
let mut s = setup();
s.server.take().unwrap().stop();
let inventory = s.project.parent().unwrap().join("inventory");
std::fs::create_dir_all(inventory.join("src")).unwrap();
let registry = WithBackground { open: s.project.clone(), background: served.then(|| inventory.clone()) };
s.server = Some(
McpServer::start_with(Arc::new(registry), None, &tokio::runtime::Handle::current(), ServerOptions::default())
.unwrap(),
);
(s, inventory)
}
async fn rpc_for(s: &Setup, folder: &Path, method: &str, params: Value) -> Value {
let project = format!("X-OpenIDEA-Project: {}", folder.display());
let body = json!({ "jsonrpc": "2.0", "id": 7, "method": method, "params": params });
let (status, body) = post(s, "/mcp", &[&bearer(s), &project], &body).await;
assert_eq!(status, 200, "{body}");
serde_json::from_str(&body).unwrap()
}
#[tokio::test(flavor = "multi_thread")]
async fn a_folder_served_in_the_background_answers_initialize_list_and_call() {
let (s, inventory) = setup_with_background(true);
let inside = inventory.join("src");
let init = rpc_for(&s, &inside, "initialize", json!({})).await;
let instructions = init["result"]["instructions"].as_str().unwrap();
assert!(instructions.contains(inventory.to_str().unwrap()), "{instructions}");
let list = rpc_for(&s, &inside, "tools/list", json!({})).await;
assert_eq!(list["result"]["tools"][0]["description"], format!("Names {}", inventory.display()), "{list}");
let call = rpc_for(&s, &inside, "tools/call", json!({ "name": "where" })).await;
assert_eq!(call["result"]["structuredContent"]["project"], json!(inventory), "{call}");
// The argument names it as well, whatever project the header is for.
let routed = rpc(&s, "tools/call", json!({ "name": "where", "arguments": { "projectPath": inside } })).await;
assert_eq!(routed["result"]["structuredContent"]["project"], json!(inventory), "{routed}");
// A folder inside the open project stays with that project.
let open = rpc(&s, "tools/call", json!({ "name": "where" })).await;
assert_eq!(open["result"]["structuredContent"]["project"], json!(s.project), "{open}");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_folder_that_is_neither_open_nor_served_in_the_background_says_where_to_turn_it_on() {
let (s, inventory) = setup_with_background(false);
for (method, params) in [("tools/list", json!({})), ("tools/call", json!({ "name": "where" }))] {
let reply = rpc_for(&s, &inventory, method, params).await;
assert_eq!(reply["error"]["code"], -32001, "{method}: {reply}");
let message = reply["error"]["message"].as_str().unwrap();
assert!(message.contains("isn't open in OpenIDEA and isn't served in the background"), "{message}");
assert!(message.contains("Settings › Agents › IDE MCP Server"), "{message}");
assert!(message.contains(inventory.to_str().unwrap()) && message.contains(s.project.to_str().unwrap()));
}
let init = rpc_for(&s, &inventory, "initialize", json!({})).await;
assert!(init["result"]["instructions"].is_string(), "initialize answers without a project: {init}");
}
#[tokio::test(flavor = "multi_thread")]
async fn the_discovery_file_lives_while_the_server_runs() {
let mut s = setup();
let info = Discovery::read(&s.discovery).unwrap();
assert_eq!((info.port, info.token.as_str(), info.pid), (s.server().port(), s.server().token(), std::process::id()));
let port = info.port;
s.server.take().unwrap().stop();
assert!(!s.discovery.exists());
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
assert!(tokio::net::TcpStream::connect(("127.0.0.1", port)).await.is_err(), "the port is closed");
}
#[tokio::test(flavor = "multi_thread")]
async fn the_stdio_bridge_relays_lines_and_explains_a_missing_ide() {
let mut s = setup();
let input = [
json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-06-18" } }),
json!({ "jsonrpc": "2.0", "method": "notifications/initialized" }),
json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "echo", "arguments": { "text": "x" } } }),
]
.iter()
.map(|m| format!("{m}\n"))
.collect::<String>()
+ "\n";
let mut output = Vec::new();
stdio::bridge(&s.discovery, &s.project.join("src"), input.as_bytes(), &mut output).await.unwrap();
let mut replies: Vec<Value> =
String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
replies.sort_by_key(|r| r["id"].as_i64());
assert_eq!(replies.len(), 2, "nothing for the notification: {replies:?}");
assert_eq!(replies[0]["result"]["protocolVersion"], "2025-06-18");
assert_eq!(replies[1]["result"]["content"][0]["text"], format!("x in {}", s.project.display()));
// The IDE quit: each request gets an error saying what to do.
s.server.take().unwrap().stop();
let mut output = Vec::new();
let input = format!("{}\n", json!({ "jsonrpc": "2.0", "id": 5, "method": "initialize", "params": {} }));
stdio::bridge(&s.discovery, &s.project, input.as_bytes(), &mut output).await.unwrap();
let reply: Value = serde_json::from_slice(&output).unwrap();
assert_eq!(reply["id"], 5);
assert!(reply["error"]["message"].as_str().unwrap().contains("MCP server is not running"), "{reply}");
}
#[tokio::test(flavor = "multi_thread")]
async fn the_stdio_bridge_relays_a_long_call_answered_on_an_event_stream() {
let s = setup();
let call = json!({ "jsonrpc": "2.0", "id": 4, "method": "tools/call",
"params": { "name": "sleep", "arguments": { "ms": 1500 }, "_meta": { "progressToken": 4 } } });
let mut output = Vec::new();
stdio::bridge(&s.discovery, &s.project, format!("{call}\n").as_bytes(), &mut output).await.unwrap();
let replies: Vec<Value> =
String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
let answer = replies.last().unwrap();
assert_eq!(answer["id"], 4, "{replies:?}");
assert_eq!(answer["result"]["content"][0]["text"], "slept");
}
#[tokio::test(flavor = "multi_thread")]
async fn routing_an_argument_overrides_the_header_and_ambiguity_lists_projects() {
struct Many(Vec<PathBuf>);
impl ToolRegistry for Many {
fn projects(&self) -> Vec<PathBuf> {
self.0.clone()
}
fn tools(&self, _: &Path) -> Result<Vec<Tool>, String> {
Ok(Vec::new())
}
fn call(
&self,
project: &Path,
_: &str,
_: &str,
_: ide_mcp::CancellationToken,
) -> Result<String, ToolCallError> {
Ok(json!({"project":project}).to_string())
}
}
let mut s = setup();
s.server.take().unwrap().stop();
let second = s.project.parent().unwrap().join("second");
std::fs::create_dir_all(second.join("nested")).unwrap();
s.server = Some(
McpServer::start_with(
Arc::new(Many(vec![s.project.clone(), second.clone()])),
None,
&tokio::runtime::Handle::current(),
ServerOptions::default(),
)
.unwrap(),
);
let (_, body) = post(&s, "/mcp", &[&bearer(&s)], &json!({"jsonrpc":"2.0","id":1,"method":"tools/list"})).await;
let reply: Value = serde_json::from_str(&body).unwrap();
let message = reply["error"]["message"].as_str().unwrap();
assert!(message.contains(s.project.to_str().unwrap()) && message.contains(second.to_str().unwrap()));
let result = rpc(&s, "tools/call", json!({"name":"where","arguments":{"projectPath":second.join("nested")}})).await;
assert_eq!(result["result"]["structuredContent"]["project"], json!(second));
let unknown = rpc(&s, "tools/call", json!({"name":"where","arguments":{"projectPath":"/unknown/project"}})).await;
assert_eq!(unknown["error"]["code"], -32001);
}
#[tokio::test(flavor = "multi_thread")]
async fn cancellation_reaches_a_running_tool_without_affecting_another_client() {
struct Waiting(Arc<tokio::sync::Notify>);
impl ToolRegistry for Waiting {
fn projects(&self) -> Vec<PathBuf> {
vec![PathBuf::from("/project")]
}
fn tools(&self, _: &Path) -> Result<Vec<Tool>, String> {
Ok(Vec::new())
}
fn call(
&self,
_: &Path,
_: &str,
_: &str,
cancel: ide_mcp::CancellationToken,
) -> Result<String, ToolCallError> {
self.0.notify_one();
tokio::runtime::Handle::current().block_on(cancel.cancelled());
Err(ToolCallError::Failed("cancelled".into()))
}
}
let mut s = setup();
s.server.take().unwrap().stop();
let started = Arc::new(tokio::sync::Notify::new());
s.server = Some(
McpServer::start_with(
Arc::new(Waiting(started.clone())),
None,
&tokio::runtime::Handle::current(),
ServerOptions::default(),
)
.unwrap(),
);
let body = json!({"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"wait"}});
let auth = bearer(&s);
let headers = [&*auth, "Mcp-Session-Id: client-one"];
let call = post(&s, "/mcp", &headers, &body);
let cancel = async {
started.notified().await;
let notification = json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":11}});
assert_eq!(post(&s, "/mcp", &[&bearer(&s), "Mcp-Session-Id: client-two"], &notification).await.0, 202);
assert_eq!(post(&s, "/mcp", &[&bearer(&s), "Mcp-Session-Id: client-one"], &notification).await.0, 202);
};
let ((status, body), ()) =
tokio::time::timeout(std::time::Duration::from_secs(5), async { tokio::join!(call, cancel) }).await.unwrap();
assert_eq!(status, 200);
assert_eq!(serde_json::from_str::<Value>(&body).unwrap()["result"]["isError"], true);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_cancellation_that_arrives_before_the_call_is_remembered() {
let s = setup();
let auth = bearer(&s);
let headers = [auth.as_str(), "Mcp-Session-Id: early-cancel"];
let notification = json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":8}});
assert_eq!(post(&s, "/mcp", &headers, &notification).await.0, 202);
let call = json!({"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"echo","arguments":{"text":"hi"}}});
let (_, body) = post(&s, "/mcp", &headers, &call).await;
assert_eq!(serde_json::from_str::<Value>(&body).unwrap()["result"]["isError"], true);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_fixed_port_and_token_are_used_and_a_taken_port_falls_back_to_a_free_one() {
let mut s = setup();
s.server.take().unwrap().stop();
let free = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap().local_addr().unwrap().port();
let token = "ab".repeat(32);
let options = ServerOptions { port: Some(free), token: Some(token.clone()), ..Default::default() };
let registry = Arc::new(Registry { project: s.project.clone() });
let runtime = tokio::runtime::Handle::current();
s.server =
Some(McpServer::start_with(registry.clone(), Some(s.discovery.clone()), &runtime, options.clone()).unwrap());
assert_eq!((s.server().port(), s.server().token()), (free, token.as_str()));
assert!(s.server().port_unavailable().is_none());
assert_eq!(Discovery::read(&s.discovery).unwrap().port, free);
let list = rpc(&s, "tools/list", json!({})).await;
assert_eq!(list["result"]["tools"][0]["name"], "echo");
// The port is taken (by the first server): a second one listens elsewhere and says why.
let second = McpServer::start_with(registry, None, &runtime, options).unwrap();
assert_ne!(second.port(), free);
assert!(
second.port_unavailable().is_some_and(|why| why.contains(&free.to_string())),
"{:?}",
second.port_unavailable()
);
}
#[tokio::test(flavor = "multi_thread")]
async fn another_listen_address_is_used_and_one_not_on_this_computer_fails() {
let mut s = setup();
s.server.take().unwrap().stop();
let registry = Arc::new(Registry { project: s.project.clone() });
let runtime = tokio::runtime::Handle::current();
// Every address: clients here use loopback; other computers name the server by an address of it.
let every = ServerOptions { address: Some("0.0.0.0".parse().unwrap()), ..Default::default() };
s.server = Some(McpServer::start_with(registry.clone(), Some(s.discovery.clone()), &runtime, every).unwrap());
let port = s.server().port();
assert_eq!(s.server().address().to_string(), "0.0.0.0");
assert!(s.server().url(&s.project).starts_with(&format!("http://127.0.0.1:{port}/mcp?")));
assert_eq!(Discovery::read(&s.discovery).unwrap().address.to_string(), "127.0.0.1");
let ping = json!({ "jsonrpc": "2.0", "id": 1, "method": "ping" }).to_string();
let request = |host: String| {
format!(
"POST /mcp HTTP/1.1\r\nHost: {host}\r\n{}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{ping}",
bearer(&s),
ping.len()
)
};
assert_eq!(http(port, &request(format!("192.0.2.7:{port}"))).await.0, 200);
assert_eq!(http(port, &request(format!("evil.example:{port}"))).await.0, 403, "never a host name");
// Not an address of this computer: the server does not start, and says which address.
let elsewhere = ServerOptions { address: Some("192.0.2.1".parse().unwrap()), ..Default::default() };
let error = McpServer::start_with(registry, None, &runtime, elsewhere).err().expect("no such address here");
assert!(error.to_string().contains("192.0.2.1"), "{error}");
}
#[tokio::test(flavor = "multi_thread")]
async fn multiple_servers_sharing_app_data_route_by_project_and_survive_one_closing() {
let tmp = tempfile::tempdir().unwrap();
let base = dunce::canonicalize(tmp.path()).unwrap();
let (proj_a, proj_b) = (base.join("proj-a"), base.join("proj-b"));
std::fs::create_dir_all(&proj_a).unwrap();
std::fs::create_dir_all(&proj_b).unwrap();
let discovery = discovery_path(&base.join("app-data"));
let runtime = tokio::runtime::Handle::current();
let server_a = McpServer::start_with(
Arc::new(Registry { project: proj_a.clone() }),
Some(discovery.clone()),
&runtime,
ServerOptions::default(),
)
.unwrap();
let server_b = McpServer::start_with(
Arc::new(Registry { project: proj_b.clone() }),
Some(discovery.clone()),
&runtime,
ServerOptions::default(),
)
.unwrap();
let session = |text: &str| {
[
json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-06-18", "clientInfo": { "name": "agent", "version": "1" } } }),
json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "echo", "arguments": { "text": text } } }),
]
.iter()
.map(|m| format!("{m}\n"))
.collect::<String>()
};
for (proj, tag) in [(&proj_a, "alpha"), (&proj_b, "beta")] {
let mut out = Vec::new();
stdio::bridge(&discovery, proj, session(tag).as_bytes(), &mut out).await.unwrap();
let mut replies: Vec<Value> =
String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
replies.sort_by_key(|r| r["id"].as_i64());
assert!(replies[0]["result"]["instructions"].as_str().unwrap().contains(&proj.display().to_string()));
assert_eq!(replies[1]["result"]["content"][0]["text"], format!("{tag} in {}", proj.display()));
}
// Closing the newer server promotes the remaining server back to server.json.
server_b.stop();
assert_eq!(Discovery::read(&discovery).unwrap().port, server_a.port());
let mut out = Vec::new();
stdio::bridge(&discovery, &proj_a, session("still-here").as_bytes(), &mut out).await.unwrap();
let mut replies: Vec<Value> =
String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
replies.sort_by_key(|r| r["id"].as_i64());
assert_eq!(replies[1]["result"]["content"][0]["text"], format!("still-here in {}", proj_a.display()));
server_a.stop();
assert!(!discovery.exists());
}