Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ todo = "deny"

[workspace.metadata.cargo-machete]
ignored = ["bindgen", "cbindgen", "prost_build", "serde"]

[workspace.metadata.typos]
default.extend-ignore-re = ["clonable"]
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod tests {

for _ in 0..1000 {
let id: String = generator.generate();
assert!(seen.insert(id.clone()), "Duplicate ID: {}", id);
assert!(seen.insert(id.clone()), "Duplicate ID: {id}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ mod tests {
let current_id: u64 = id.parse().expect("ID should be a valid u64");
assert!(
current_id > prev_id,
"ID not strictly increasing: {} <= {}",
current_id,
prev_id
"ID not strictly increasing: {current_id} <= {prev_id}"
);
prev_id = current_id;
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rust-mcp-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@ macros = ["rust-mcp-macros/sdk"]

[lints]
workspace = true

[package.metadata.typos]
default.extend-ignore-re = ["clonable"]
14 changes: 9 additions & 5 deletions crates/rust-mcp-sdk/src/hyper_servers/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pub mod messages_routes;
pub mod sse_routes;
pub mod streamable_http_routes;

use crate::mcp_http::McpAppState;

use super::HyperServerOptions;
use axum::Router;
use crate::mcp_http::{McpAppState, McpHttpHandler};
use axum::{Extension, Router};
use std::sync::Arc;

/// Constructs the Axum router with all application routes
Expand All @@ -21,7 +20,11 @@ use std::sync::Arc;
///
/// # Returns
/// * `Router` - An Axum router configured with all application routes and state
pub fn app_routes(state: Arc<McpAppState>, server_options: &HyperServerOptions) -> Router {
pub fn app_routes(
state: Arc<McpAppState>,
server_options: &HyperServerOptions,
http_handler: McpHttpHandler,
) -> Router {
let router: Router = Router::new()
.merge(streamable_http_routes::routes(
server_options.streamable_http_endpoint(),
Expand All @@ -42,7 +45,8 @@ pub fn app_routes(state: Arc<McpAppState>, server_options: &HyperServerOptions)
r
})
.with_state(state)
.merge(fallback_routes::routes());
.merge(fallback_routes::routes())
.layer(Extension(Arc::new(http_handler)));

router
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
mcp_http::{McpAppState, McpHttpHandler},
utils::remove_query_and_hash,
};
use axum::{extract::State, response::IntoResponse, routing::post, Router};
use axum::{extract::State, response::IntoResponse, routing::post, Extension, Router};
use http::{HeaderMap, Method, Uri};
use std::sync::Arc;

Expand All @@ -18,10 +18,13 @@ pub async fn handle_messages(
uri: Uri,
headers: HeaderMap,
State(state): State<Arc<McpAppState>>,
Extension(http_handler): Extension<Arc<McpHttpHandler>>,
message: String,
) -> TransportServerResult<impl IntoResponse> {
let request = McpHttpHandler::create_request(Method::POST, uri, headers, Some(&message));
let generic_response = McpHttpHandler::handle_sse_message(request, state.clone()).await?;
let generic_response = http_handler
.handle_sse_message(request, state.clone())
.await?;
let (parts, body) = generic_response.into_parts();
let resp = axum::response::Response::from_parts(parts, axum::body::Body::new(body));
Ok(resp)
Expand Down
6 changes: 4 additions & 2 deletions crates/rust-mcp-sdk/src/hyper_servers/routes/sse_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ pub fn routes(sse_endpoint: &str, sse_message_endpoint: &str) -> Router<Arc<McpA
/// * `TransportServerResult<impl IntoResponse>` - The SSE response stream or an error
pub async fn handle_sse(
Extension(sse_message_endpoint): Extension<SseMessageEndpoint>,
Extension(http_handler): Extension<Arc<McpHttpHandler>>,
State(state): State<Arc<McpAppState>>,
) -> TransportServerResult<impl IntoResponse> {
let SseMessageEndpoint(sse_message_endpoint) = sse_message_endpoint;
let generic_response =
McpHttpHandler::handle_sse_connection(state.clone(), Some(&sse_message_endpoint)).await?;
let generic_response = http_handler
.handle_sse_connection(state.clone(), Some(&sse_message_endpoint))
.await?;
let (parts, body) = generic_response.into_parts();
let resp = axum::response::Response::from_parts(parts, axum::body::Body::new(body));
Ok(resp)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::hyper_servers::error::TransportServerResult;
use crate::mcp_http::{McpAppState, McpHttpHandler};
use axum::routing::get;
use axum::Extension;
use axum::{
extract::{Query, State},
response::IntoResponse,
Expand All @@ -24,9 +25,10 @@ pub async fn handle_streamable_http_get(
headers: HeaderMap,
uri: Uri,
State(state): State<Arc<McpAppState>>,
Extension(http_handler): Extension<Arc<McpHttpHandler>>,
) -> TransportServerResult<impl IntoResponse> {
let request = McpHttpHandler::create_request(Method::GET, uri, headers, None);
let generic_res = McpHttpHandler::handle_streamable_http(request, state).await?;
let generic_res = http_handler.handle_streamable_http(request, state).await?;
let (parts, body) = generic_res.into_parts();
let resp = axum::response::Response::from_parts(parts, axum::body::Body::new(body));
Ok(resp)
Expand All @@ -36,12 +38,13 @@ pub async fn handle_streamable_http_post(
headers: HeaderMap,
uri: Uri,
State(state): State<Arc<McpAppState>>,
Extension(http_handler): Extension<Arc<McpHttpHandler>>,
Query(_params): Query<HashMap<String, String>>,
payload: String,
) -> TransportServerResult<impl IntoResponse> {
let request =
McpHttpHandler::create_request(Method::POST, uri, headers, Some(payload.as_str()));
let generic_res = McpHttpHandler::handle_streamable_http(request, state).await?;
let generic_res = http_handler.handle_streamable_http(request, state).await?;
let (parts, body) = generic_res.into_parts();
let resp = axum::response::Response::from_parts(parts, axum::body::Body::new(body));
Ok(resp)
Expand All @@ -51,9 +54,10 @@ pub async fn handle_streamable_http_delete(
headers: HeaderMap,
uri: Uri,
State(state): State<Arc<McpAppState>>,
Extension(http_handler): Extension<Arc<McpHttpHandler>>,
) -> TransportServerResult<impl IntoResponse> {
let request = McpHttpHandler::create_request(Method::DELETE, uri, headers, None);
let generic_res = McpHttpHandler::handle_streamable_http(request, state).await?;
let generic_res = http_handler.handle_streamable_http(request, state).await?;
let (parts, body) = generic_res.into_parts();
let resp = axum::response::Response::from_parts(parts, axum::body::Body::new(body));
Ok(resp)
Expand Down
6 changes: 4 additions & 2 deletions crates/rust-mcp-sdk/src/hyper_servers/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
utils::{
DEFAULT_MESSAGES_ENDPOINT, DEFAULT_SSE_ENDPOINT, DEFAULT_STREAMABLE_HTTP_ENDPOINT,
},
McpAppState,
McpAppState, McpHttpHandler,
},
mcp_server::hyper_runtime::HyperRuntime,
mcp_traits::{mcp_handler::McpServerHandler, IdGenerator},
Expand Down Expand Up @@ -275,7 +275,9 @@ impl HyperServer {
dns_rebinding_protection: server_options.dns_rebinding_protection,
event_store: server_options.event_store.as_ref().map(Arc::clone),
});
let app = app_routes(Arc::clone(&state), &server_options);

let http_handler = McpHttpHandler::new(); //TODO: add auth handlers
let app = app_routes(Arc::clone(&state), &server_options, http_handler);
Self {
app,
state,
Expand Down
3 changes: 3 additions & 0 deletions crates/rust-mcp-sdk/src/mcp_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ mod app_state;
mod mcp_http_handler;
pub(crate) mod mcp_http_utils;

mod mcp_http_middleware; //TODO:

pub use app_state::*;
pub use mcp_http_handler::*;
pub use mcp_http_middleware::Middleware;

pub(crate) mod utils {
pub use super::mcp_http_utils::*;
Expand Down
Loading