* 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
87 lines
2.7 KiB
Rust
87 lines
2.7 KiB
Rust
mod common;
|
|
|
|
#[cfg(target_family = "unix")]
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
use std::fs;
|
|
|
|
use common::calculator::Calculator;
|
|
use rmcp::{serve_client, serve_server};
|
|
use tokio::net::{UnixListener, UnixStream};
|
|
|
|
const SOCKET_PATH: &str = "/tmp/rmcp_example.sock";
|
|
async fn server(unix_listener: UnixListener) -> 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 {
|
|
Ok(server) => {
|
|
println!("Server initialized successfully");
|
|
if let Err(e) = server.waiting().await {
|
|
println!("Error while server waiting: {}", e);
|
|
}
|
|
}
|
|
Err(e) => println!("Server initialization failed: {}", e),
|
|
}
|
|
|
|
anyhow::Ok(())
|
|
});
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn client() -> anyhow::Result<()> {
|
|
println!("Client connecting to {}", SOCKET_PATH);
|
|
let stream = UnixStream::connect(SOCKET_PATH).await?;
|
|
|
|
let client = serve_client((), stream).await?;
|
|
println!("Client connected and initialized successfully");
|
|
|
|
// List available tools
|
|
let tools = client.peer().list_tools(Default::default()).await?;
|
|
println!("Available tools: {:?}", tools);
|
|
|
|
// Call the sum tool
|
|
if let Some(sum_tool) = tools.tools.iter().find(|t| t.name.contains("sum")) {
|
|
println!("Calling sum tool: {}", sum_tool.name);
|
|
let result = client
|
|
.peer()
|
|
.call_tool(rmcp::model::CallToolRequestParam {
|
|
name: sum_tool.name.clone(),
|
|
arguments: Some(rmcp::object!({
|
|
"a": 10,
|
|
"b": 20
|
|
})),
|
|
})
|
|
.await?;
|
|
|
|
println!("Result: {:?}", result);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// Remove any existing socket file
|
|
let _ = fs::remove_file(SOCKET_PATH);
|
|
match UnixListener::bind(SOCKET_PATH) {
|
|
Ok(unix_listener) => {
|
|
println!("Server successfully listening on {}", SOCKET_PATH);
|
|
tokio::spawn(server(unix_listener));
|
|
}
|
|
Err(e) => {
|
|
println!("Unable to bind to {}: {}", SOCKET_PATH, e);
|
|
}
|
|
}
|
|
|
|
client().await?;
|
|
|
|
// Clean up socket file
|
|
let _ = fs::remove_file(SOCKET_PATH);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
fn main() {
|
|
println!("Unix socket example is not supported on this platform.");
|
|
}
|