This is a Rust sample for an MCP Server
Here's what the calculator portion looks like:
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
pub struct CalculatorRequest {
pub a: f64,
pub b: f64,
}
#[tool_router]
impl Calculator {
#[tool(description = "Adds a and b")]
async fn add(
&self,
Parameters(CalculatorRequest { a, b }): Parameters<CalculatorRequest>,
) -> String {
(a + b).to_string()
}
#[tool(description = "Subtracts b from a")]
async fn subtract(
&self,
Parameters(CalculatorRequest { a, b }): Parameters<CalculatorRequest>,
) -> String {
(a - b).to_string()
}
#[tool(description = "Multiply a with b")]
async fn multiply(
&self,
Parameters(CalculatorRequest { a, b }): Parameters<CalculatorRequest>,
) -> String {
(a * b).to_string()
}
#[tool(description = "Divides a by b")]
async fn divide(
&self,
Parameters(CalculatorRequest { a, b }): Parameters<CalculatorRequest>,
) -> String {
if b == 0.0 {
"Error: Division by zero".to_string()
} else {
(a / b).to_string()
}
}
}Run the following command:
cargo buildcargo run