-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcalculator.rs
53 lines (47 loc) · 1.47 KB
/
calculator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use mcp_macros::tool;
use mcp_spec::handler::{ToolError, ToolHandler};
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Create an instance of our tool
let calculator = Calculator;
// Print tool information
println!("Tool name: {}", calculator.name());
println!("Tool description: {}", calculator.description());
println!("Tool schema: {}", calculator.schema());
// Test the tool with some sample input
let input = serde_json::json!({
"x": 5,
"y": 3,
"operation": "multiply"
});
let result = calculator.call(input).await?;
println!("Result: {}", result);
Ok(())
}
#[tool(
name = "calculator",
description = "Perform basic arithmetic operations",
params(
x = "First number in the calculation",
y = "Second number in the calculation",
operation = "The operation to perform (add, subtract, multiply, divide)"
)
)]
async fn calculator(x: i32, y: i32, operation: String) -> Result<i32, ToolError> {
match operation.as_str() {
"add" => Ok(x + y),
"subtract" => Ok(x - y),
"multiply" => Ok(x * y),
"divide" => {
if y == 0 {
Err(ToolError::ExecutionError("Division by zero".into()))
} else {
Ok(x / y)
}
}
_ => Err(ToolError::InvalidParameters(format!(
"Unknown operation: {}",
operation
))),
}
}