use std::collections::BTreeMap;
use std::path::Path;
use std::sync::atomic::{AtomicI64, Ordering};
use std::time::Duration;
use anyhow::{Context, Result, bail};
use serde_json::{Value, json};
use crate::mcp::transport::McpTransport;
pub const PROTOCOL_VERSION: &str = "2025-11-25";
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
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": PROTOCOL_VERSION,
"capabilities": {},
"clientInfo": { "name": client_name, "version": client_version },
})
}
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}");
}
message
.get("result")
.cloned()
.context("JSON-RPC response has neither result nor error")
}
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,
}
impl McpClient {
pub fn connect(
program: &str,
args: &[String],
cwd: &Path,
env: &BTreeMap<String, String>,
client_name: &str,
client_version: &str,
) -> Result<McpClient> {
let transport = McpTransport::spawn(program, args, cwd, env)?;
let client = McpClient {
transport,
next_id: AtomicI64::new(0),
};
client.initialize(client_name, client_version)?;
Ok(client)
}
fn next_id(&self) -> i64 {
self.next_id.fetch_add(1, Ordering::SeqCst) + 1
}
pub fn call(&self, method: &str, params: Value) -> Result<Value> {
let id = self.next_id();
self.transport.send(&request(id, method, params))?;
let deadline = std::time::Instant::now() + REQUEST_TIMEOUT;
loop {
let remaining = deadline
.checked_duration_since(std::time::Instant::now())
.context("MCP request timed out")?;
let msg = self
.transport
.incoming
.recv_timeout(remaining)
.context("MCP server closed or timed out before responding")?;
if is_response_to(&msg, id) {
return response_result(&msg);
}
}
}
pub fn notify(&self, method: &str, params: Value) -> Result<()> {
self.transport.send(¬ification(method, params))
}
fn initialize(&self, client_name: &str, client_version: &str) -> Result<Value> {
let result = self.call("initialize", initialize_params(client_name, client_version))?;
self.notify("notifications/initialized", json!({}))?;
Ok(result)
}
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"], PROTOCOL_VERSION);
assert_eq!(p["clientInfo"]["name"], "croft");
assert_eq!(p["clientInfo"]["version"], "0.1.0");
}
#[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());
}
#[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");
}
#[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);
}
}