chore: move examples out of various places into a top-level dir (#10)

This commit is contained in:
Kalvin C 2025-03-03 11:50:34 -08:00 • committed by GitHub
commit 6609aa6aa8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 163 additions and 1 deletions

View file

@ -0,0 +1,18 @@
[package]
name = "mcp-macros-examples"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
mcp-core = { path = "../../crates/mcp-core" }
mcp-macros = { path = "../../crates/mcp-macros" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
async-trait = "0.1"
schemars = "0.8"
[[example]]
name = "calculator"
path = "src/calculator.rs"

View file

@ -0,0 +1,53 @@
use mcp_core::handler::{ToolError, ToolHandler};
use mcp_macros::tool;
#[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
))),
}
}