Skip to content

Commit c95989c

Browse files
committed
examples for tests
1 parent ffb6bc9 commit c95989c

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

examples/echo.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use anyhow::Context;
2+
use clap::Parser;
3+
use rmcp::transport::SseServer;
4+
use tracing_subscriber::FmtSubscriber;
5+
6+
use rmcp::{
7+
ServerHandler,
8+
model::{ServerCapabilities, ServerInfo},
9+
schemars, tool,
10+
};
11+
#[derive(Debug, Clone, Default)]
12+
pub struct Echo;
13+
#[tool(tool_box)]
14+
impl Echo {
15+
#[tool(description = "Echo a message")]
16+
fn echo(&self, #[tool(param)] message: String) -> String {
17+
message
18+
}
19+
}
20+
21+
#[tool(tool_box)]
22+
impl ServerHandler for Echo {
23+
fn get_info(&self) -> ServerInfo {
24+
ServerInfo {
25+
instructions: Some("A simple echo server".into()),
26+
capabilities: ServerCapabilities::builder().enable_tools().build(),
27+
..Default::default()
28+
}
29+
}
30+
}
31+
32+
#[derive(Parser)]
33+
#[command(author, version, about, long_about = None)]
34+
struct Args {
35+
/// Address to bind the server to
36+
#[arg(short, long, default_value = "127.0.0.1:8080")]
37+
address: std::net::SocketAddr,
38+
}
39+
40+
#[tokio::main]
41+
async fn main() -> anyhow::Result<()> {
42+
let subscriber = FmtSubscriber::builder()
43+
.with_max_level(tracing::Level::DEBUG)
44+
.with_writer(std::io::stderr)
45+
.finish();
46+
47+
// Parse command line arguments
48+
let args = Args::parse();
49+
50+
tracing::subscriber::set_global_default(subscriber).context("Failed to set up logging")?;
51+
52+
let ct = SseServer::serve(args.address)
53+
.await?
54+
.with_service(Echo::default);
55+
56+
tokio::signal::ctrl_c().await?;
57+
ct.cancel();
58+
Ok(())
59+
}

examples/echo_streamable.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use anyhow::Context;
2+
use clap::Parser;
3+
use rmcp::transport::StreamableHttpServer;
4+
use tracing_subscriber::FmtSubscriber;
5+
6+
use rmcp::{
7+
ServerHandler,
8+
model::{ServerCapabilities, ServerInfo},
9+
schemars, tool,
10+
};
11+
#[derive(Debug, Clone, Default)]
12+
pub struct Echo;
13+
#[tool(tool_box)]
14+
impl Echo {
15+
#[tool(description = "Echo a message")]
16+
fn echo(&self, #[tool(param)] message: String) -> String {
17+
message
18+
}
19+
}
20+
21+
#[tool(tool_box)]
22+
impl ServerHandler for Echo {
23+
fn get_info(&self) -> ServerInfo {
24+
ServerInfo {
25+
instructions: Some("A simple echo server".into()),
26+
capabilities: ServerCapabilities::builder().enable_tools().build(),
27+
..Default::default()
28+
}
29+
}
30+
}
31+
32+
#[derive(Parser)]
33+
#[command(author, version, about, long_about = None)]
34+
struct Args {
35+
/// Address to bind the server to
36+
#[arg(short, long, default_value = "127.0.0.1:8080")]
37+
address: std::net::SocketAddr,
38+
}
39+
40+
#[tokio::main]
41+
async fn main() -> anyhow::Result<()> {
42+
let subscriber = FmtSubscriber::builder()
43+
.with_max_level(tracing::Level::DEBUG)
44+
.with_writer(std::io::stderr)
45+
.finish();
46+
47+
// Parse command line arguments
48+
let args = Args::parse();
49+
50+
tracing::subscriber::set_global_default(subscriber).context("Failed to set up logging")?;
51+
52+
let ct = StreamableHttpServer::serve(args.address)
53+
.await?
54+
.with_service(Echo::default);
55+
56+
tokio::signal::ctrl_c().await?;
57+
ct.cancel();
58+
Ok(())
59+
}

0 commit comments

Comments
 (0)