rust-sdk/examples/transport/src/named-pipe.rs
jokemanfire 621c9f619e
feat(task): add task support (SEP-1686) (#536)
Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
2025-12-22 09:01:00 -05:00

81 lines
2.5 KiB
Rust

mod common;
#[cfg(target_family = "windows")]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
use common::calculator::Calculator;
use rmcp::{serve_client, serve_server};
use tokio::net::windows::named_pipe::{ClientOptions, ServerOptions};
const PIPE_NAME: &str = r"\\.\pipe\rmcp_example";
async fn server(name: &str) -> anyhow::Result<()> {
let mut server = ServerOptions::new()
.first_pipe_instance(true)
.create(name)?;
while server.connect().await.is_ok() {
let stream = server;
server = ServerOptions::new().create(name)?;
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 {}", PIPE_NAME);
let stream = ClientOptions::new().open(PIPE_NAME)?;
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
})),
task: None,
})
.await?;
println!("Result: {:?}", result);
}
Ok(())
}
tokio::spawn(server(PIPE_NAME));
let mut clients = vec![];
for _ in 0..100 {
clients.push(client());
}
for client in clients {
client.await?;
}
Ok(())
}
#[cfg(not(target_family = "windows"))]
fn main() {
println!("Unix socket example is not supported on this platform.");
}