use rust_mcp::McpServer;
use serde_json::json;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut server = McpServer::new("rust-calculator", "0.1.0");
server.add_tool(
"calculate",
"Perform basic arithmetic operations",
json!({
"type": "object",
"properties": {
"operation": {
"type": "string",
"description": "One of: add, subtract, multiply, divide"
},
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["operation", "a", "b"]
}),
|args| async move {
let operation = match args.get("operation") {
Some(serde_json::Value::String(op)) => op.as_str(),
_ => return Err("Missing or invalid operation".into()),
};
let a = match args.get("a") {
Some(serde_json::Value::Number(n)) => n.as_f64().unwrap_or(0.0),
_ => return Err("Missing or invalid first number".into()),
};
let b = match args.get("b") {
Some(serde_json::Value::Number(n)) => n.as_f64().unwrap_or(0.0),
_ => return Err("Missing or invalid second number".into()),
};
let result = match operation {
"add" => a + b,
"subtract" => a - b,
"multiply" => a * b,
"divide" => {
if b == 0.0 {
return Err("Division by zero".into());
}
a / b
}
_ => return Err(format!("Unknown operation: {}", operation)),
};
Ok(json!(result))
},
);
server.run_stdio().await?;
Ok(())
}