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:
parent
5891b45162
commit
be321a4abe
12 changed files with 742 additions and 233 deletions
|
|
@ -17,7 +17,7 @@ async fn main() -> Result<()> {
|
|||
tracing::info!("Starting Calculator MCP server");
|
||||
|
||||
// Create an instance of our calculator router
|
||||
let service = Calculator::new().serve(stdio()).await.inspect_err(|e| {
|
||||
let service = Calculator.serve(stdio()).await.inspect_err(|e| {
|
||||
tracing::error!("serving error: {:?}", e);
|
||||
})?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use rmcp::{
|
||||
ServerHandler,
|
||||
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
||||
model::{ServerCapabilities, ServerInfo},
|
||||
schemars, tool, tool_handler, tool_router,
|
||||
};
|
||||
use rmcp::{handler::server::wrapper::Parameters, schemars, tool, tool_router};
|
||||
|
||||
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
||||
pub struct SumRequest {
|
||||
|
|
@ -23,18 +18,10 @@ pub struct SubRequest {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Calculator {
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
pub struct Calculator;
|
||||
|
||||
#[tool_router]
|
||||
#[tool_router(server_handler)]
|
||||
impl Calculator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tool_router: Self::tool_router(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(description = "Calculate the sum of two numbers")]
|
||||
fn sum(&self, Parameters(SumRequest { a, b }): Parameters<SumRequest>) -> String {
|
||||
(a + b).to_string()
|
||||
|
|
@ -45,11 +32,3 @@ impl Calculator {
|
|||
(a - b).to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[tool_handler]
|
||||
impl ServerHandler for Calculator {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
|
||||
.with_instructions("A simple calculator".to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use rmcp::{
|
||||
ServerHandler,
|
||||
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
||||
model::{ServerCapabilities, ServerInfo},
|
||||
schemars, tool, tool_handler, tool_router,
|
||||
ServerHandler, handler::server::wrapper::Parameters, schemars, tool, tool_handler, tool_router,
|
||||
};
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -41,7 +38,6 @@ impl DataService for MemoryDataService {
|
|||
pub struct GenericService<DS: DataService> {
|
||||
#[allow(dead_code)]
|
||||
data_service: Arc<DS>,
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
#[derive(Debug, schemars::JsonSchema, serde::Deserialize, serde::Serialize)]
|
||||
|
|
@ -55,7 +51,6 @@ impl<DS: DataService> GenericService<DS> {
|
|||
pub fn new(data_service: DS) -> Self {
|
||||
Self {
|
||||
data_service: Arc::new(data_service),
|
||||
tool_router: Self::tool_router(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,10 +69,5 @@ impl<DS: DataService> GenericService<DS> {
|
|||
}
|
||||
}
|
||||
|
||||
#[tool_handler]
|
||||
impl<DS: DataService> ServerHandler for GenericService<DS> {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
|
||||
.with_instructions("generic data service".to_string())
|
||||
}
|
||||
}
|
||||
#[tool_handler(instructions = "generic data service")]
|
||||
impl<DS: DataService> ServerHandler for GenericService<DS> {}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ use std::{
|
|||
|
||||
use futures::Stream;
|
||||
use rmcp::{
|
||||
ErrorData as McpError, RoleServer, ServerHandler, handler::server::tool::ToolRouter, model::*,
|
||||
service::RequestContext, tool, tool_handler, tool_router,
|
||||
ErrorData as McpError, RoleServer, ServerHandler, model::*, service::RequestContext, tool,
|
||||
tool_handler, tool_router,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tokio_stream::StreamExt;
|
||||
|
|
@ -54,7 +54,6 @@ impl Stream for StreamDataSource {
|
|||
#[derive(Clone)]
|
||||
pub struct ProgressDemo {
|
||||
data_source: StreamDataSource,
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
#[tool_router]
|
||||
|
|
@ -62,7 +61,6 @@ impl ProgressDemo {
|
|||
#[allow(dead_code)]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tool_router: Self::tool_router(),
|
||||
data_source: StreamDataSource::from_text("Hello, world!"),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,7 @@
|
|||
|
||||
use rmcp::{
|
||||
ServerHandler,
|
||||
handler::server::{
|
||||
router::tool::ToolRouter,
|
||||
wrapper::{Json, Parameters},
|
||||
},
|
||||
model::{ServerCapabilities, ServerInfo},
|
||||
handler::server::wrapper::{Json, Parameters},
|
||||
schemars, tool, tool_handler, tool_router,
|
||||
};
|
||||
|
||||
|
|
@ -26,17 +22,7 @@ pub struct SubRequest {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Calculator {
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
impl Calculator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tool_router: Self::tool_router(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Calculator;
|
||||
|
||||
#[tool_router]
|
||||
impl Calculator {
|
||||
|
|
@ -50,10 +36,5 @@ 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")
|
||||
}
|
||||
}
|
||||
#[tool_handler(instructions = "A simple calculator")]
|
||||
impl ServerHandler for Calculator {}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
async fn http_server(req: Request<Incoming>) -> Result<hyper::Response<String>, hyper::Error> {
|
||||
tokio::spawn(async move {
|
||||
let upgraded = hyper::upgrade::on(req).await?;
|
||||
let service = Calculator::new().serve(TokioIo::new(upgraded)).await?;
|
||||
let service = Calculator.serve(TokioIo::new(upgraded)).await?;
|
||||
service.waiting().await?;
|
||||
anyhow::Result::<()>::Ok(())
|
||||
});
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
let stream = server;
|
||||
server = ServerOptions::new().create(name)?;
|
||||
tokio::spawn(async move {
|
||||
match serve_server(Calculator::new(), stream).await {
|
||||
match serve_server(Calculator, stream).await {
|
||||
Ok(server) => {
|
||||
println!("Server initialized successfully");
|
||||
if let Err(e) = server.waiting().await {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ async fn server() -> anyhow::Result<()> {
|
|||
let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:8001").await?;
|
||||
while let Ok((stream, _)) = tcp_listener.accept().await {
|
||||
tokio::spawn(async move {
|
||||
let server = serve_server(Calculator::new(), stream).await?;
|
||||
let server = serve_server(Calculator, stream).await?;
|
||||
server.waiting().await?;
|
||||
anyhow::Ok(())
|
||||
});
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
while let Ok((stream, addr)) = unix_listener.accept().await {
|
||||
println!("Client connected: {:?}", addr);
|
||||
tokio::spawn(async move {
|
||||
match serve_server(Calculator::new(), stream).await {
|
||||
match serve_server(Calculator, stream).await {
|
||||
Ok(server) => {
|
||||
println!("Server initialized successfully");
|
||||
if let Err(e) = server.waiting().await {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ async fn start_server() -> anyhow::Result<()> {
|
|||
tokio::spawn(async move {
|
||||
let ws_stream = tokio_tungstenite::accept_async(stream).await?;
|
||||
let transport = WebsocketTransport::new_server(ws_stream);
|
||||
let server = Calculator::new().serve(transport).await?;
|
||||
let server = Calculator.serve(transport).await?;
|
||||
server.waiting().await?;
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
Loading…
Reference in a new issue