67 lines
1.3 KiB
Markdown
67 lines
1.3 KiB
Markdown
# Sample
|
|
|
|
This is a Rust sample for an MCP Server
|
|
|
|
Here's what the calculator portion looks like:
|
|
|
|
```rust
|
|
#[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()
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Install
|
|
|
|
Run the following command:
|
|
|
|
```bash
|
|
cargo build
|
|
```
|
|
|
|
## Run
|
|
|
|
```bash
|
|
cargo run
|
|
```
|