use std::net::SocketAddr;
use std::sync::Arc;
use axum::extract::{Request, State};
use axum::http::{HeaderMap, StatusCode, Uri, header};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse as _, Response};
use rmcp::ServerHandler;
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
use tokio::net::TcpListener;
pub use axum::Router;
pub type Verifier = Arc<dyn Fn(&str) -> bool + Send + Sync>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum HttpError {
#[error("{0} is not a loopback address; the local daemon only listens on 127.0.0.1 or ::1")]
NotLoopback(SocketAddr),
#[error("could not use the HTTP socket: {0}")]
Io(#[from] std::io::Error),
}
pub fn check_loopback(addr: SocketAddr) -> Result<SocketAddr, HttpError> {
if addr.ip().is_loopback() {
Ok(addr)
} else {
Err(HttpError::NotLoopback(addr))
}
}
pub async fn bind_loopback(addr: SocketAddr) -> Result<TcpListener, HttpError> {
Ok(TcpListener::bind(check_loopback(addr)?).await?)
}
#[must_use]
pub fn base_config() -> StreamableHttpServerConfig {
StreamableHttpServerConfig::default()
.with_legacy_session_mode(true)
.with_json_response(true)
}
pub fn mcp_service<S>(
factory: impl Fn() -> S + Send + Sync + 'static,
config: StreamableHttpServerConfig,
) -> StreamableHttpService<S, LocalSessionManager>
where
S: ServerHandler + Send + 'static,
{
StreamableHttpService::new(move || Ok(factory()), Arc::default(), config)
}
pub fn local_router<S>(
service: StreamableHttpService<S, LocalSessionManager>,
verify: Verifier,
) -> Router
where
S: ServerHandler + Send + 'static,
{
Router::new()
.nest_service("/mcp", service)
.layer(middleware::from_fn_with_state(verify, require_bearer))
.layer(middleware::from_fn(require_loopback_origin))
}
async fn require_loopback_origin(request: Request, next: Next) -> Response {
match request.headers().get(header::ORIGIN) {
None => next.run(request).await,
Some(origin) if origin.to_str().is_ok_and(is_loopback_origin) => next.run(request).await,
Some(_) => (StatusCode::FORBIDDEN, "Forbidden: Origin is not allowed").into_response(),
}
}
fn is_loopback_origin(origin: &str) -> bool {
let Ok(uri) = origin.parse::<Uri>() else {
return false;
};
let web = matches!(uri.scheme_str(), Some("http" | "https"));
let bare = uri.path_and_query().is_none_or(|path| path.as_str() == "/");
let host = uri.authority().is_some_and(|authority| {
!authority.as_str().contains('@')
&& matches!(authority.host(), "localhost" | "127.0.0.1" | "[::1]")
});
web && bare && host
}
async fn require_bearer(State(verify): State<Verifier>, request: Request, next: Next) -> Response {
if bearer_token(request.headers()).is_some_and(|token| verify(token)) {
next.run(request).await
} else {
(
StatusCode::UNAUTHORIZED,
[(header::WWW_AUTHENTICATE, "Bearer")],
"Unauthorized",
)
.into_response()
}
}
fn bearer_token(headers: &HeaderMap) -> Option<&str> {
let value = headers.get(header::AUTHORIZATION)?.to_str().ok()?;
let (scheme, token) = value.split_once(' ')?;
scheme.eq_ignore_ascii_case("Bearer").then_some(token)
}
pub async fn serve(
listener: TcpListener,
app: Router,
shutdown: impl Future<Output = ()> + Send + 'static,
) -> Result<(), HttpError> {
axum::serve(listener, app)
.with_graceful_shutdown(shutdown)
.await?;
Ok(())
}