Skip to content

Commit 614d974

Browse files
authored
example: Add named pipe example (#181)
1 parent e9a5ae9 commit 614d974

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

examples/transport/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ path = "src/unix_socket.rs"
5959
[[example]]
6060
name = "websocket"
6161
path = "src/websocket.rs"
62+
63+
[[example]]
64+
name = "named-pipe"
65+
path = "src/named-pipe.rs"

examples/transport/src/named-pipe.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
mod common;
2+
3+
#[cfg(target_family = "windows")]
4+
#[tokio::main]
5+
async fn main() -> anyhow::Result<()> {
6+
use common::calculator::Calculator;
7+
use rmcp::{serve_client, serve_server};
8+
use tokio::net::windows::named_pipe::{ClientOptions, ServerOptions};
9+
const PIPE_NAME: &str = r"\\.\pipe\rmcp_example";
10+
11+
async fn server(name: &str) -> anyhow::Result<()> {
12+
let mut server = ServerOptions::new()
13+
.first_pipe_instance(true)
14+
.create(name)?;
15+
while let Ok(_) = server.connect().await {
16+
let stream = server;
17+
server = ServerOptions::new().create(name)?;
18+
tokio::spawn(async move {
19+
match serve_server(Calculator, stream).await {
20+
Ok(server) => {
21+
println!("Server initialized successfully");
22+
if let Err(e) = server.waiting().await {
23+
println!("Error while server waiting: {}", e);
24+
}
25+
}
26+
Err(e) => println!("Server initialization failed: {}", e),
27+
}
28+
29+
anyhow::Ok(())
30+
});
31+
}
32+
Ok(())
33+
}
34+
35+
async fn client() -> anyhow::Result<()> {
36+
println!("Client connecting to {}", PIPE_NAME);
37+
let stream = ClientOptions::new().open(PIPE_NAME)?;
38+
39+
let client = serve_client((), stream).await?;
40+
println!("Client connected and initialized successfully");
41+
42+
// List available tools
43+
let tools = client.peer().list_tools(Default::default()).await?;
44+
println!("Available tools: {:?}", tools);
45+
46+
// Call the sum tool
47+
if let Some(sum_tool) = tools.tools.iter().find(|t| t.name.contains("sum")) {
48+
println!("Calling sum tool: {}", sum_tool.name);
49+
let result = client
50+
.peer()
51+
.call_tool(rmcp::model::CallToolRequestParam {
52+
name: sum_tool.name.clone(),
53+
arguments: Some(rmcp::object!({
54+
"a": 10,
55+
"b": 20
56+
})),
57+
})
58+
.await?;
59+
60+
println!("Result: {:?}", result);
61+
}
62+
63+
Ok(())
64+
}
65+
tokio::spawn(server(PIPE_NAME));
66+
let mut clients = vec![];
67+
68+
for _ in 0..100 {
69+
clients.push(client());
70+
}
71+
for client in clients {
72+
client.await?;
73+
}
74+
Ok(())
75+
}
76+
77+
#[cfg(not(target_family = "windows"))]
78+
fn main() {
79+
println!("Unix socket example is not supported on this platform.");
80+
}

0 commit comments

Comments
 (0)