refactor: refactor tool macros and router implementation (#261)
* refactor: refactor tool macros and router implementation - Updated the `#[tool(tool_box)]` macro to `#[tool_router]` across various modules for consistency. - Enhanced the `Calculator`, `Counter`, and `GenericService` structs to utilize `ToolRouter` for handling tool calls. - Introduced `Parameters` struct for better parameter handling in tool functions. - Added new methods for listing tools and calling tools in server handlers. - Improved test cases to reflect changes in tool routing and parameter handling. - Updated documentation and examples to align with the new router structure. * fix: fix fmt and build error * fix: fix test failure * docs: documents for macros, fix ci * fix: fix ci * fix: fix wrongly replaced documents * fix: remove useless file * fix: change the parameter format for tool_router * fix: update extract_doc_line to handle existing documentation and clean up unused code in server handler * doc: update document for macro and examples * doc: update readme and add contribute guide * fix: fix type
This commit is contained in:
parent
6d3190504c
commit
1f7f4d3055
5 changed files with 1498 additions and 1147 deletions
|
|
@ -1,4 +1,11 @@
|
|||
use rmcp::{ServerHandler, model::ServerInfo, schemars, tool, tool_box};
|
||||
#![allow(dead_code)]
|
||||
|
||||
use rmcp::{
|
||||
ServerHandler,
|
||||
handler::server::{router::tool::ToolRouter, tool::Parameters, wrapper::Json},
|
||||
model::{ServerCapabilities, ServerInfo},
|
||||
schemars, tool, tool_handler, tool_router,
|
||||
};
|
||||
|
||||
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
||||
pub struct SumRequest {
|
||||
|
|
@ -6,35 +13,44 @@ pub struct SumRequest {
|
|||
pub a: i32,
|
||||
pub b: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
|
||||
pub struct SubRequest {
|
||||
#[schemars(description = "the left hand side number")]
|
||||
pub a: i32,
|
||||
#[schemars(description = "the right hand side number")]
|
||||
pub b: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Calculator;
|
||||
pub struct Calculator {
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
#[tool_router]
|
||||
impl Calculator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tool_router: Self::tool_router(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(description = "Calculate the sum of two numbers")]
|
||||
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
|
||||
fn sum(&self, Parameters(SumRequest { a, b }): Parameters<SumRequest>) -> String {
|
||||
(a + b).to_string()
|
||||
}
|
||||
|
||||
#[tool(description = "Calculate the sub of two numbers")]
|
||||
fn sub(
|
||||
&self,
|
||||
#[tool(param)]
|
||||
#[schemars(description = "the left hand side number")]
|
||||
a: i32,
|
||||
#[tool(param)]
|
||||
#[schemars(description = "the right hand side number")]
|
||||
b: i32,
|
||||
) -> String {
|
||||
(a - b).to_string()
|
||||
#[tool(description = "Calculate the difference of two numbers")]
|
||||
fn sub(&self, Parameters(SubRequest { a, b }): Parameters<SubRequest>) -> Json<i32> {
|
||||
Json(a - b)
|
||||
}
|
||||
|
||||
tool_box!(Calculator { sum, sub });
|
||||
}
|
||||
|
||||
#[tool_handler]
|
||||
impl ServerHandler for Calculator {
|
||||
tool_box!(@derive);
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo {
|
||||
instructions: Some("A simple calculator".into()),
|
||||
capabilities: ServerCapabilities::builder().enable_tools().build(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.serve(TokioIo::new(upgraded)).await?;
|
||||
let service = Calculator::new().serve(TokioIo::new(upgraded)).await?;
|
||||
service.waiting().await?;
|
||||
anyhow::Result::<()>::Ok(())
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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, stream).await?;
|
||||
let server = serve_server(Calculator::new(), 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, stream).await {
|
||||
match serve_server(Calculator::new(), 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.serve(transport).await?;
|
||||
let server = Calculator::new().serve(transport).await?;
|
||||
server.waiting().await?;
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue