-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathhttp_upgrade.rs
67 lines (63 loc) · 2.38 KB
/
http_upgrade.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use common::calculator::Calculator;
use hyper::{
Request, StatusCode,
body::Incoming,
header::{HeaderValue, UPGRADE},
};
use hyper_util::rt::TokioIo;
use rmcp::{RoleClient, ServiceExt, service::RunningService};
use tracing_subscriber::EnvFilter;
mod common;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(tracing::Level::INFO.into()))
.init();
start_server().await?;
let client = http_client("127.0.0.1:8001").await?;
let tools = client.list_all_tools().await?;
client.cancel().await?;
tracing::info!("{:#?}", tools);
Ok(())
}
async fn http_server(req: Request<Incoming>) -> Result<hyper::Response<String>, hyper::Error> {
tokio::spawn(async move {
let upgraded = hyper::upgrade::on(req).await?;
let service = Calculator.serve(TokioIo::new(upgraded)).await?;
service.waiting().await?;
anyhow::Result::<()>::Ok(())
});
let mut response = hyper::Response::new(String::new());
*response.status_mut() = StatusCode::SWITCHING_PROTOCOLS;
response
.headers_mut()
.insert(UPGRADE, HeaderValue::from_static("mcp"));
Ok(response)
}
async fn http_client(uri: &str) -> anyhow::Result<RunningService<RoleClient, ()>> {
let tcp_stream = tokio::net::TcpStream::connect(uri).await?;
let (mut s, c) =
hyper::client::conn::http1::handshake::<_, String>(TokioIo::new(tcp_stream)).await?;
tokio::spawn(c.with_upgrades());
let mut req = Request::new(String::new());
req.headers_mut()
.insert(UPGRADE, HeaderValue::from_static("mcp"));
let response = s.send_request(req).await?;
let upgraded = hyper::upgrade::on(response).await?;
let client = ().serve(TokioIo::new(upgraded)).await?;
Ok(client)
}
async fn start_server() -> anyhow::Result<()> {
let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:8001").await?;
let service = hyper::service::service_fn(http_server);
tokio::spawn(async move {
while let Ok((stream, addr)) = tcp_listener.accept().await {
tracing::info!("accepted connection from: {}", addr);
let conn = hyper::server::conn::http1::Builder::new()
.serve_connection(TokioIo::new(stream), service)
.with_upgrades();
tokio::spawn(conn);
}
});
Ok(())
}