use std::collections::BTreeMap;
use std::path::Path;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::mpsc::RecvTimeoutError;
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Map, Value, json};
use crate::mcp::transport::McpTransport;
pub const MODERN_VERSION: &str = "2026-07-28";
pub const LEGACY_VERSION: &str = "2025-11-25";
pub const SUPPORTED_VERSIONS: [&str; 2] = [MODERN_VERSION, LEGACY_VERSION];
pub const PROTOCOL_VERSION_KEY: &str = "io.modelcontextprotocol/protocolVersion";
pub const CLIENT_INFO_KEY: &str = "io.modelcontextprotocol/clientInfo";
pub const CLIENT_CAPABILITIES_KEY: &str = "io.modelcontextprotocol/clientCapabilities";
pub const SERVER_INFO_KEY: &str = "io.modelcontextprotocol/serverInfo";
pub const UNSUPPORTED_PROTOCOL_VERSION: i64 = -32022;
const MISSING_REQUIRED_CLIENT_CAPABILITY: i64 = -32021;
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
const PROBE_TIMEOUT: Duration = Duration::from_secs(5);
const EXIT_REASON_WAIT: Duration = Duration::from_millis(500);
pub fn request(id: i64, method: &str, params: Value) -> Value {
json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params })
}
pub fn notification(method: &str, params: Value) -> Value {
json!({ "jsonrpc": "2.0", "method": method, "params": params })
}
pub fn initialize_params(client_name: &str, client_version: &str) -> Value {
json!({
"protocolVersion": LEGACY_VERSION,
"capabilities": {},
"clientInfo": { "name": client_name, "version": client_version },
})
}
pub fn request_meta(client_name: &str, client_version: &str) -> Map<String, Value> {
let mut meta = Map::new();
meta.insert(PROTOCOL_VERSION_KEY.into(), json!(MODERN_VERSION));
meta.insert(
CLIENT_INFO_KEY.into(),
json!({ "name": client_name, "version": client_version }),
);
meta.insert(CLIENT_CAPABILITIES_KEY.into(), json!({}));
meta
}
pub fn with_meta(params: Value, meta: &Map<String, Value>) -> Value {
let mut params = match params {
Value::Object(map) => map,
_ => Map::new(),
};
let mut slot = match params.remove("_meta") {
Some(Value::Object(map)) => map,
_ => Map::new(),
};
slot.extend(meta.clone());
params.insert("_meta".into(), Value::Object(slot));
Value::Object(params)
}
pub fn tools_call_params(name: &str, arguments: Value) -> Value {
json!({ "name": name, "arguments": arguments })
}
pub fn response_result(message: &Value) -> Result<Value> {
if let Some(err) = message.get("error") {
let msg = err
.get("message")
.and_then(Value::as_str)
.unwrap_or("unknown error");
let code = err.get("code").and_then(Value::as_i64).unwrap_or(0);
bail!("MCP error {code}: {msg}");
}
let result = message
.get("result")
.cloned()
.context("JSON-RPC response has neither result nor error")?;
match result.get("resultType").and_then(Value::as_str) {
None | Some("complete") => Ok(result),
Some(other) => bail!("the server answered with an unsupported result type `{other}`"),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Era {
Modern,
Legacy,
}
fn era_for(versions: &[Value]) -> Result<Era> {
let versions: Vec<&str> = versions.iter().filter_map(Value::as_str).collect();
if versions.contains(&MODERN_VERSION) {
Ok(Era::Modern)
} else if versions.iter().any(|v| *v < MODERN_VERSION) {
Ok(Era::Legacy)
} else {
bail!(
"the server speaks MCP {}, and croft speaks {}",
versions.join(", "),
SUPPORTED_VERSIONS.join(", ")
)
}
}
pub fn classify_probe(reply: &Value) -> Result<Era> {
if let Some(versions) = reply
.pointer("/result/supportedVersions")
.and_then(Value::as_array)
{
return era_for(versions);
}
match reply.pointer("/error/code").and_then(Value::as_i64) {
Some(UNSUPPORTED_PROTOCOL_VERSION) => era_for(
reply
.pointer("/error/data/supported")
.and_then(Value::as_array)
.map_or(&[], Vec::as_slice),
),
Some(MISSING_REQUIRED_CLIENT_CAPABILITY) => bail!(
"the server needs client capabilities croft does not offer: {}",
reply
.pointer("/error/data/requiredCapabilities")
.unwrap_or(&Value::Null)
),
_ => Ok(Era::Legacy),
}
}
fn is_response_to(message: &Value, id: i64) -> bool {
message.get("id").and_then(Value::as_i64) == Some(id)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tool {
pub name: String,
pub description: String,
}
pub fn parse_tools(result: &Value) -> Vec<Tool> {
result
.get("tools")
.and_then(Value::as_array)
.map(|tools| {
tools
.iter()
.filter_map(|t| {
let name = t.get("name")?.as_str()?.to_string();
let description = t
.get("description")
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
Some(Tool { name, description })
})
.collect()
})
.unwrap_or_default()
}
pub fn tool_result_text(result: &Value) -> String {
result
.get("content")
.and_then(Value::as_array)
.map(|blocks| {
blocks
.iter()
.filter_map(|b| b.get("text").and_then(Value::as_str))
.collect::<Vec<_>>()
.join("\n\n")
})
.unwrap_or_default()
}
pub fn tool_fingerprint(tools_list_result: &Value, name: &str) -> String {
let Some(entry) = tools_list_result
.get("tools")
.and_then(Value::as_array)
.and_then(|a| {
a.iter()
.find(|t| t.get("name").and_then(Value::as_str) == Some(name))
})
else {
return String::new();
};
json!({
"name": entry.get("name").cloned().unwrap_or(Value::Null),
"description": entry.get("description").cloned().unwrap_or(Value::Null),
"inputSchema": entry.get("inputSchema").cloned().unwrap_or(Value::Null),
})
.to_string()
}
pub struct McpClient {
transport: McpTransport,
next_id: AtomicI64,
era: Era,
client_name: String,
client_version: String,
}
impl McpClient {
pub fn connect(
program: &str,
args: &[String],
cwd: &Path,
env: &BTreeMap<String, String>,
client_name: &str,
client_version: &str,
) -> Result<McpClient> {
let mut client = McpClient::spawn(program, args, cwd, env, client_name, client_version)?;
client.handshake(PROBE_TIMEOUT)?;
Ok(client)
}
fn spawn(
program: &str,
args: &[String],
cwd: &Path,
env: &BTreeMap<String, String>,
client_name: &str,
client_version: &str,
) -> Result<McpClient> {
Ok(McpClient {
transport: McpTransport::spawn(program, args, cwd, env)?,
next_id: AtomicI64::new(0),
era: Era::Legacy,
client_name: client_name.to_string(),
client_version: client_version.to_string(),
})
}
fn next_id(&self) -> i64 {
self.next_id.fetch_add(1, Ordering::SeqCst) + 1
}
fn meta(&self) -> Map<String, Value> {
request_meta(&self.client_name, &self.client_version)
}
fn send(&self, method: &str, params: Value) -> Result<i64> {
let id = self.next_id();
if let Err(e) = self.transport.send(&request(id, method, params)) {
let broken_pipe = e
.root_cause()
.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() == std::io::ErrorKind::BrokenPipe);
if broken_pipe {
bail!("{}", self.transport.exit_reason(EXIT_REASON_WAIT));
}
return Err(e);
}
Ok(id)
}
fn wait(&self, ids: &[i64], timeout: Duration) -> Result<Option<Value>> {
let deadline = Instant::now() + timeout;
loop {
let Some(remaining) = deadline.checked_duration_since(Instant::now()) else {
return Ok(None);
};
match self.transport.incoming.recv_timeout(remaining) {
Ok(msg) if ids.iter().any(|&id| is_response_to(&msg, id)) => return Ok(Some(msg)),
Ok(_) => {}
Err(RecvTimeoutError::Timeout) => return Ok(None),
Err(RecvTimeoutError::Disconnected) => {
bail!("{}", self.transport.exit_reason(EXIT_REASON_WAIT))
}
}
}
}
pub fn call(&self, method: &str, params: Value) -> Result<Value> {
let params = match self.era {
Era::Modern => with_meta(params, &self.meta()),
Era::Legacy => params,
};
let id = self.send(method, params)?;
let reply = self
.wait(&[id], REQUEST_TIMEOUT)?
.context("MCP server timed out before responding")?;
response_result(&reply)
}
pub fn notify(&self, method: &str, params: Value) -> Result<()> {
self.transport.send(¬ification(method, params))
}
fn handshake(&mut self, probe_timeout: Duration) -> Result<()> {
let probe = self.send("server/discover", json!({ "_meta": self.meta() }))?;
let mut probe_reply = self.wait(&[probe], probe_timeout)?;
if let Some(reply) = &probe_reply {
self.era = classify_probe(reply)?;
if self.era == Era::Modern {
return Ok(());
}
}
let init = self.send(
"initialize",
initialize_params(&self.client_name, &self.client_version),
)?;
let deadline = Instant::now() + REQUEST_TIMEOUT;
let mut rejected = None;
loop {
let ids: &[i64] = if rejected.is_some() {
&[probe]
} else {
&[probe, init]
};
match self.wait(ids, deadline.saturating_duration_since(Instant::now()))? {
Some(reply) if is_response_to(&reply, probe) => probe_reply = Some(reply),
Some(reply) => match response_result(&reply) {
Ok(_) => {
self.era = Era::Legacy;
return self.notify("notifications/initialized", json!({}));
}
Err(e) => rejected = Some(e),
},
None => {
return Err(rejected
.unwrap_or_else(|| anyhow!("MCP server timed out before responding")));
}
}
rejected = match (rejected, &probe_reply) {
(Some(rejection), Some(reply)) => {
return match classify_probe(reply) {
Ok(Era::Modern) => {
self.era = Era::Modern;
Ok(())
}
Ok(Era::Legacy) => Err(rejection),
Err(why) => Err(why),
};
}
(rejected, _) => rejected,
};
}
}
pub fn call_tool(&self, name: &str, arguments: Value) -> Result<Value> {
self.call("tools/call", tools_call_params(name, arguments))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn request_and_notification_envelopes_are_jsonrpc_2_0() {
let r = request(1, "tools/list", json!({}));
assert_eq!(r["jsonrpc"], "2.0");
assert_eq!(r["id"], 1);
assert_eq!(r["method"], "tools/list");
let n = notification("notifications/initialized", json!({}));
assert_eq!(n["jsonrpc"], "2.0");
assert_eq!(n["method"], "notifications/initialized");
assert!(n.get("id").is_none(), "a notification carries no id");
}
#[test]
fn initialize_params_advertise_the_protocol_version_and_client() {
let p = initialize_params("croft", "0.1.0");
assert_eq!(p["protocolVersion"], LEGACY_VERSION);
assert_eq!(p["clientInfo"]["name"], "croft");
assert_eq!(p["clientInfo"]["version"], "0.1.0");
}
#[test]
fn with_meta_adds_the_modern_fields_and_keeps_the_callers() {
let meta = request_meta("croft", "0.1.0");
let p = with_meta(
json!({"name": "echo", "_meta": {"progressToken": 7}}),
&meta,
);
assert_eq!(p["name"], "echo");
assert_eq!(p["_meta"]["progressToken"], 7);
assert_eq!(p["_meta"][PROTOCOL_VERSION_KEY], MODERN_VERSION);
assert_eq!(p["_meta"][CLIENT_INFO_KEY]["name"], "croft");
assert_eq!(p["_meta"][CLIENT_CAPABILITIES_KEY], json!({}));
assert_eq!(with_meta(Value::Null, &meta)["_meta"], Value::Object(meta));
}
#[test]
fn classify_probe_reads_the_era_off_the_reply() {
let discover = |v: Value| json!({"id": 1, "result": {"supportedVersions": v}});
let error = |code: i64, data: Value| json!({"id": 1, "error": {"code": code, "message": "m", "data": data}});
let era = |reply: Value| classify_probe(&reply).unwrap();
assert_eq!(
era(discover(json!([MODERN_VERSION, LEGACY_VERSION]))),
Era::Modern
);
assert_eq!(era(discover(json!(["2025-06-18"]))), Era::Legacy);
assert_eq!(
era(error(-32022, json!({"supported": [LEGACY_VERSION]}))),
Era::Legacy
);
assert_eq!(era(error(-32601, Value::Null)), Era::Legacy);
assert_eq!(era(error(-32602, Value::Null)), Era::Legacy);
assert_eq!(era(json!({"id": 1, "result": {}})), Era::Legacy);
let none = classify_probe(&error(-32022, json!({"supported": ["2099-01-01"]})))
.unwrap_err()
.to_string();
assert!(
none.contains("2099-01-01") && none.contains(MODERN_VERSION),
"{none}"
);
let caps = classify_probe(&error(
-32021,
json!({"requiredCapabilities": {"roots": {}}}),
))
.unwrap_err()
.to_string();
assert!(caps.contains("roots"), "{caps}");
}
#[test]
fn response_result_extracts_result_and_maps_error_to_err() {
let ok = json!({"jsonrpc": "2.0", "id": 1, "result": {"tools": []}});
assert_eq!(response_result(&ok).unwrap(), json!({"tools": []}));
let err = json!({"jsonrpc": "2.0", "id": 1, "error": {"code": -32601, "message": "Method not found"}});
let e = response_result(&err).unwrap_err().to_string();
assert!(e.contains("-32601"), "{e}");
assert!(e.contains("Method not found"), "{e}");
let malformed = json!({"jsonrpc": "2.0", "id": 1});
assert!(response_result(&malformed).is_err());
let complete = json!({"id": 1, "result": {"resultType": "complete", "tools": []}});
assert!(response_result(&complete).is_ok());
let input = json!({"id": 1, "result": {"resultType": "input_required"}});
let e = response_result(&input).unwrap_err().to_string();
assert!(e.contains("input_required"), "{e}");
}
#[test]
fn is_response_to_matches_only_the_request_id() {
assert!(is_response_to(&json!({"id": 5, "result": {}}), 5));
assert!(!is_response_to(&json!({"id": 6, "result": {}}), 5));
assert!(!is_response_to(
&json!({"method": "notifications/message"}),
5
));
}
#[test]
fn parse_tools_projects_name_and_description() {
let result = json!({
"tools": [
{"name": "fetch", "description": "Fetch a URL", "inputSchema": {}},
{"name": "echo"},
]
});
let tools = parse_tools(&result);
assert_eq!(tools.len(), 2);
assert_eq!(
tools[0],
Tool {
name: "fetch".into(),
description: "Fetch a URL".into()
}
);
assert_eq!(tools[1].name, "echo");
assert_eq!(
tools[1].description, "",
"missing description defaults empty"
);
}
#[test]
fn tool_fingerprint_is_stable_and_changes_with_the_definition() {
let list = json!({"tools": [
{"name": "fetch", "description": "Fetch a URL", "inputSchema": {"type": "object"}},
{"name": "other"},
]});
let fp = tool_fingerprint(&list, "fetch");
assert!(!fp.is_empty());
assert_eq!(fp, tool_fingerprint(&list, "fetch"));
let mutated = json!({"tools": [
{"name": "fetch", "description": "Fetch a URL and exfiltrate it", "inputSchema": {"type": "object"}},
]});
assert_ne!(fp, tool_fingerprint(&mutated, "fetch"));
assert_eq!(tool_fingerprint(&list, "nope"), "");
}
#[test]
fn tool_result_text_joins_text_content_blocks() {
let result = json!({
"content": [
{"type": "text", "text": "# Heading"},
{"type": "image", "data": "..."},
{"type": "text", "text": "body"},
]
});
assert_eq!(tool_result_text(&result), "# Heading\n\nbody");
}
fn path_env() -> BTreeMap<String, String> {
std::env::var("PATH")
.map(|p| BTreeMap::from([(String::from("PATH"), p)]))
.unwrap_or_default()
}
#[test]
fn a_server_that_dies_before_answering_names_its_error() {
let script = "echo 'Traceback (most recent call last):' >&2; \
echo \"ImportError: cannot import name 'McpError' from 'mcp.shared.exceptions'\" >&2; \
echo >&2; exit 1";
let err = McpClient::connect(
"/bin/sh",
&[String::from("-c"), script.to_string()],
&std::env::temp_dir(),
&path_env(),
"croft",
"test",
)
.err()
.expect("a server that exits never connects")
.to_string();
assert_eq!(
err,
"the server exited: ImportError: cannot import name 'McpError' from 'mcp.shared.exceptions'"
);
}
#[test]
fn a_silent_server_exit_is_reported_as_an_exit() {
let err = McpClient::connect(
"/bin/sh",
&[String::from("-c"), String::from("exit 3")],
&std::env::temp_dir(),
&path_env(),
"croft",
"test",
)
.err()
.expect("a server that exits never connects")
.to_string();
assert_eq!(err, "the server exited before responding");
}
#[test]
fn a_server_logging_past_the_pipe_buffer_still_answers() {
let script = format!(
"head -c 300000 /dev/zero | tr '\\000' x >&2; read l; echo '{DISCOVERED}'; cat >/dev/null"
);
McpClient::connect(
"/bin/sh",
&[String::from("-c"), script],
&std::env::temp_dir(),
&path_env(),
"croft",
"test",
)
.expect("a chatty server still completes the handshake");
}
const DISCOVERED: &str = r#"{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{"tools":{}}}}"#;
fn connect_sh(script: &str, probe_timeout: Duration) -> Result<McpClient> {
let args = [String::from("-c"), script.to_string()];
let mut client = McpClient::spawn(
"/bin/sh",
&args,
&std::env::temp_dir(),
&path_env(),
"croft",
"test",
)?;
client.handshake(probe_timeout)?;
Ok(client)
}
#[test]
fn a_modern_server_is_spoken_to_with_per_request_meta() {
let script = format!(
r#"read l; echo '{DISCOVERED}'; read l
case "$l" in *'"method":"tools/list"'*'"io.modelcontextprotocol/protocolVersion":"2026-07-28"'*)
echo '{{"jsonrpc":"2.0","id":2,"result":{{"resultType":"complete","tools":[{{"name":"echo"}}]}}}}';;
*) echo "no modern tools/list: $l" >&2; exit 1;; esac; cat >/dev/null"#
);
let client = connect_sh(&script, PROBE_TIMEOUT).expect("the probe settles modern");
assert_eq!(client.era, Era::Modern);
let list = client.call("tools/list", json!({})).expect("tools/list");
assert_eq!(parse_tools(&list)[0].name, "echo");
}
#[test]
fn a_legacy_server_that_rejects_the_probe_gets_initialize() {
let script = r#"read l; echo '{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}'
read l; case "$l" in *'"method":"initialize"'*) ;; *) exit 1;; esac
echo '{"jsonrpc":"2.0","id":2,"result":{"protocolVersion":"2025-11-25","capabilities":{}}}'
read l; case "$l" in *notifications/initialized*) ;; *) exit 1;; esac
read l; case "$l" in *_meta*) exit 1;; esac
echo '{"jsonrpc":"2.0","id":3,"result":{"tools":[]}}'; cat >/dev/null"#;
let client = connect_sh(script, PROBE_TIMEOUT).expect("falls back to initialize");
assert_eq!(client.era, Era::Legacy);
client.call("tools/list", json!({})).expect("tools/list");
}
#[test]
fn a_legacy_server_silent_on_the_probe_gets_initialize() {
let script = r#"read l; read l; case "$l" in *'"method":"initialize"'*) ;; *) exit 1;; esac
echo '{"jsonrpc":"2.0","id":2,"result":{"protocolVersion":"2025-11-25","capabilities":{}}}'
cat >/dev/null"#;
let client =
connect_sh(script, Duration::from_millis(100)).expect("falls back to initialize");
assert_eq!(client.era, Era::Legacy);
}
#[test]
fn a_modern_server_slow_to_answer_the_probe_is_still_modern() {
let reject =
r#"{"jsonrpc":"2.0","id":2,"error":{"code":-32601,"message":"Method not found"}}"#;
for replies in [
format!("echo '{DISCOVERED}'; echo '{reject}'"),
format!("echo '{reject}'; echo '{DISCOVERED}'"),
] {
let script = format!("read l; sleep 1; read l; {replies}; cat >/dev/null");
let client = connect_sh(&script, Duration::from_millis(100)).expect("recovers modern");
assert_eq!(client.era, Era::Modern, "{replies}");
}
}
#[test]
#[ignore = "requires python3 on PATH"]
fn mcp_handshake_lists_and_calls_a_tool_against_a_real_server() {
use std::io::Write;
const SERVER: &str = r#"
import sys, json
def reply(i, result):
sys.stdout.write(json.dumps({"jsonrpc":"2.0","id":i,"result":result}) + "\n")
sys.stdout.flush()
for line in sys.stdin:
line = line.strip()
if not line:
continue
msg = json.loads(line)
m = msg.get("method")
if m == "initialize":
reply(msg["id"], {"protocolVersion":"2025-11-25","capabilities":{},"serverInfo":{"name":"mock","version":"0"}})
elif m == "notifications/initialized":
pass
elif m == "tools/list":
reply(msg["id"], {"tools":[{"name":"echo","description":"Echo text"}]})
elif m == "tools/call":
text = msg["params"]["arguments"].get("text","")
reply(msg["id"], {"content":[{"type":"text","text":text}]})
elif "id" in msg:
reply(msg["id"], {})
"#;
let dir = std::env::temp_dir().join(format!("croft-mcp-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("server.py");
let mut f = std::fs::File::create(&script).unwrap();
f.write_all(SERVER.as_bytes()).unwrap();
drop(f);
let env = BTreeMap::new();
let client = McpClient::connect(
"python3",
&[script.to_string_lossy().into_owned()],
&dir,
&env,
"croft",
"test",
)
.expect("connect + initialize against the mock MCP server");
let list = client.call("tools/list", json!({})).expect("tools/list");
let tools = parse_tools(&list);
assert_eq!(tools.len(), 1);
assert_eq!(tools[0].name, "echo");
let result = client
.call_tool("echo", json!({"text": "hello mcp"}))
.expect("tools/call echo");
assert_eq!(tool_result_text(&result), "hello mcp");
let _ = std::fs::remove_dir_all(&dir);
}
}