feat(macros): auto-generate get_info and default router (#785)

* feat(macros): auto-generate get_info and default router

* docs: simplify examples and docs with new defaults

* feat(macros): add tool_router(server_handler) to elide separate #[tool_handler] impl

* docs: add Tools section to README and simplify calculator examples with server_handler
This commit is contained in:
Dale Seo 2026-04-08 15:06:26 -04:00 • committed by GitHub
commit be321a4abe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 742 additions and 233 deletions

View file

@ -1,13 +1,8 @@
#![allow(dead_code)]
use rmcp::{
ServerHandler,
handler::server::{
router::tool::ToolRouter,
wrapper::{Json, Parameters},
},
model::{ServerCapabilities, ServerInfo},
schemars, tool, tool_handler, tool_router,
handler::server::wrapper::{Json, Parameters},
schemars, tool, tool_router,
};
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
@ -25,26 +20,10 @@ pub struct SubRequest {
pub b: i32,
}
#[derive(Debug, Clone)]
pub struct Calculator {
tool_router: ToolRouter<Self>,
}
#[derive(Debug, Clone, Default)]
pub struct Calculator;
impl Calculator {
pub fn new() -> Self {
Self {
tool_router: Self::tool_router(),
}
}
}
impl Default for Calculator {
fn default() -> Self {
Self::new()
}
}
#[tool_router]
#[tool_router(server_handler)]
impl Calculator {
#[tool(description = "Calculate the sum of two numbers")]
fn sum(&self, Parameters(SumRequest { a, b }): Parameters<SumRequest>) -> String {
@ -56,11 +35,3 @@ impl Calculator {
Json(a - b)
}
}
#[tool_handler]
impl ServerHandler for Calculator {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
.with_instructions("A simple calculator")
}
}

View file

@ -112,10 +112,7 @@ impl wasi::exports::cli::run::Guest for TokioCliRunner {
.with_writer(std::io::stderr)
.with_ansi(false)
.init();
let server = calculator::Calculator::new()
.serve(wasi_io())
.await
.unwrap();
let server = calculator::Calculator.serve(wasi_io()).await.unwrap();
server.waiting().await.unwrap();
});
Ok(())