rust-sdk/examples/transport/src/http_upgrade.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

67 lines
2.4 KiB
Rust

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::new().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(())
}