rust-sdk/examples/transport/src/tcp.rs
4t145 1f7f4d3055
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
2025-06-24 04:01:22 +08:00

32 lines
907 B
Rust

use common::calculator::Calculator;
use rmcp::{serve_client, serve_server};
mod common;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tokio::spawn(server());
client().await?;
Ok(())
}
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?;
server.waiting().await?;
anyhow::Ok(())
});
}
Ok(())
}
async fn client() -> anyhow::Result<()> {
let stream = tokio::net::TcpSocket::new_v4()?
.connect("127.0.0.1:8001".parse()?)
.await?;
let client = serve_client((), stream).await?;
let tools = client.peer().list_tools(Default::default()).await?;
println!("{:?}", tools);
Ok(())
}