Skip to content

Commit 95984d1

Browse files
committed
fix: calculator example comply MCP spec
1 parent 494a2b8 commit 95984d1

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

examples/servers/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,7 @@ path = "src/completion_stdio.rs"
105105
[[example]]
106106
name = "servers_progress_demo"
107107
path = "src/progress_demo.rs"
108+
109+
[[example]]
110+
name = "servers_calculator_stdio"
111+
path = "src/calculator_stdio.rs"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use anyhow::Result;
2+
use common::calculator::Calculator;
3+
use rmcp::{ServiceExt, transport::stdio};
4+
use tracing_subscriber::{self, EnvFilter};
5+
mod common;
6+
7+
/// npx @modelcontextprotocol/inspector cargo run -p mcp-server-examples --example servers_calculator_stdio
8+
#[tokio::main]
9+
async fn main() -> Result<()> {
10+
// Initialize the tracing subscriber with file and stdout logging
11+
tracing_subscriber::fmt()
12+
.with_env_filter(EnvFilter::from_default_env().add_directive(tracing::Level::DEBUG.into()))
13+
.with_writer(std::io::stderr)
14+
.with_ansi(false)
15+
.init();
16+
17+
tracing::info!("Starting Calculator MCP server");
18+
19+
// Create an instance of our calculator router
20+
let service = Calculator::new().serve(stdio()).await.inspect_err(|e| {
21+
tracing::error!("serving error: {:?}", e);
22+
})?;
23+
24+
service.waiting().await?;
25+
Ok(())
26+
}

examples/servers/src/common/calculator.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
use rmcp::{
44
ServerHandler,
5-
handler::server::{
6-
router::tool::ToolRouter,
7-
wrapper::{Json, Parameters},
8-
},
5+
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
96
model::{ServerCapabilities, ServerInfo},
107
schemars, tool, tool_handler, tool_router,
118
};
@@ -44,8 +41,8 @@ impl Calculator {
4441
}
4542

4643
#[tool(description = "Calculate the difference of two numbers")]
47-
fn sub(&self, Parameters(SubRequest { a, b }): Parameters<SubRequest>) -> Json<i32> {
48-
Json(a - b)
44+
fn sub(&self, Parameters(SubRequest { a, b }): Parameters<SubRequest>) -> String {
45+
(a - b).to_string()
4946
}
5047
}
5148

0 commit comments

Comments
 (0)