use std::io::{BufRead, Write};
use serde_json::{Value, json};
const TOKEN_VAR: &str = "TCODE_FIXTURE_TOKEN";
const HANG_VAR: &str = "TCODE_FIXTURE_HANG";
fn main() {
if std::env::var_os(HANG_VAR).is_some() {
for line in std::io::stdin().lock().lines() {
if line.is_err() {
break;
}
}
std::thread::sleep(std::time::Duration::from_secs(300));
return;
}
let stdin = std::io::stdin();
let mut stdout = std::io::stdout();
for line in stdin.lock().lines() {
let Ok(line) = line else { break };
let Ok(request) = serde_json::from_str::<Value>(&line) else {
continue;
};
let Some(id) = request.get("id").cloned() else {
continue;
};
let method = request
.get("method")
.and_then(Value::as_str)
.unwrap_or_default();
let Some(result) = respond(method, request.get("params")) else {
continue;
};
let envelope = json!({"jsonrpc": "2.0", "id": id, "result": result});
if writeln!(stdout, "{envelope}").is_err() || stdout.flush().is_err() {
break;
}
}
}
fn respond(method: &str, params: Option<&Value>) -> Option<Value> {
match method {
"initialize" => Some(json!({
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"serverInfo": {"name": "tcode-mcp-fixture", "version": "0.1.0"},
})),
"tools/list" => Some(json!({
"tools": [
{
"name": "echo",
"description": "Echoes its message back.",
"inputSchema": {
"type": "object",
"properties": {"message": {"type": "string"}},
"required": ["message"],
},
},
{
"name": "echo_env",
"description": "Reports the fixture token from the environment.",
"inputSchema": {"type": "object", "properties": {}},
},
]
})),
"tools/call" => Some(call(params)),
"ping" => Some(json!({})),
_ => None,
}
}
fn call(params: Option<&Value>) -> Value {
let name = params
.and_then(|p| p.get("name"))
.and_then(Value::as_str)
.unwrap_or_default();
let text = match name {
"echo" => params
.and_then(|p| p.get("arguments"))
.and_then(|a| a.get("message"))
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
"echo_env" => std::env::var(TOKEN_VAR).unwrap_or_else(|_| "<unset>".to_string()),
other => {
return json!({
"isError": true,
"content": [{"type": "text", "text": format!("no such tool: {other}")}],
});
}
};
json!({"content": [{"type": "text", "text": text}]})
}