|
| 1 | +// Copyright 2025 |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use axum::Router; |
| 5 | +use axum::serve::ListenerExt; |
| 6 | +use serde_json::json; |
| 7 | +use std::env; |
| 8 | +use tracing::info; |
| 9 | +use tracing::trace; |
| 10 | +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; |
| 11 | + |
| 12 | +use crate::config::{APP_NAME, APP_VERSION, DEFAULT_BIND_ADDRESS, MCP_PROTOCOL_VERSION}; |
| 13 | +use crate::rest; |
| 14 | +use crate::transports::{sse, streamable_http}; |
| 15 | + |
| 16 | +pub async fn run() -> anyhow::Result<()> { |
| 17 | + init_logging(); |
| 18 | + |
| 19 | + let bind_address = |
| 20 | + env::var("BIND_ADDRESS").unwrap_or_else(|_| DEFAULT_BIND_ADDRESS.to_string()); |
| 21 | + |
| 22 | + info!("{} v{} starting...", APP_NAME, APP_VERSION); |
| 23 | + info!("Binding to: {}", bind_address); |
| 24 | + |
| 25 | + let tcp_listener = tokio::net::TcpListener::bind(&bind_address) |
| 26 | + .await? |
| 27 | + .tap_io(|tcp_stream| { |
| 28 | + if let Err(err) = tcp_stream.set_nodelay(true) { |
| 29 | + trace!("failed to set TCP_NODELAY on incoming connection: {err:#}"); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + info!("MCP endpoint: http://{}/mcp", bind_address); |
| 34 | + info!("MCP SSE: http://{}/sse", bind_address); |
| 35 | + info!( |
| 36 | + "REST API: http://{}/api/echo (POST), /api/time (GET)", |
| 37 | + bind_address |
| 38 | + ); |
| 39 | + info!("Health check: http://{}/health", bind_address); |
| 40 | + info!("Version info: http://{}/version", bind_address); |
| 41 | + info!(""); |
| 42 | + info!("Benchmark with:"); |
| 43 | + info!(" hey -n 1000000 -c 200 -m POST -T 'application/json' \\"); |
| 44 | + info!( |
| 45 | + " -d '{{\"message\":\"hello\"}}' http://{}/api/echo", |
| 46 | + bind_address |
| 47 | + ); |
| 48 | + |
| 49 | + axum::serve(tcp_listener, router()) |
| 50 | + .with_graceful_shutdown(async move { |
| 51 | + tokio::signal::ctrl_c().await.unwrap(); |
| 52 | + info!("Shutting down..."); |
| 53 | + }) |
| 54 | + .await?; |
| 55 | + |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +fn init_logging() { |
| 60 | + tracing_subscriber::registry() |
| 61 | + .with( |
| 62 | + tracing_subscriber::EnvFilter::try_from_default_env() |
| 63 | + .unwrap_or_else(|_| "info".to_string().into()), |
| 64 | + ) |
| 65 | + .with(tracing_subscriber::fmt::layer()) |
| 66 | + .init(); |
| 67 | +} |
| 68 | + |
| 69 | +fn router() -> Router { |
| 70 | + Router::new() |
| 71 | + .route("/health", axum::routing::get(health_handler)) |
| 72 | + .route("/version", axum::routing::get(version_handler)) |
| 73 | + .route("/api/echo", axum::routing::post(rest::echo_handler)) |
| 74 | + .route("/api/time", axum::routing::get(rest::time_handler)) |
| 75 | + .route( |
| 76 | + "/mcp", |
| 77 | + axum::routing::post(streamable_http::handler).delete(streamable_http::delete_handler), |
| 78 | + ) |
| 79 | + .route("/sse", axum::routing::get(sse::handler)) |
| 80 | + .route("/messages", axum::routing::post(sse::message_handler)) |
| 81 | + .route("/message", axum::routing::post(sse::message_handler)) |
| 82 | +} |
| 83 | + |
| 84 | +async fn health_handler() -> axum::Json<serde_json::Value> { |
| 85 | + axum::Json(json!({ |
| 86 | + "status": "healthy", |
| 87 | + "server": APP_NAME, |
| 88 | + "version": APP_VERSION |
| 89 | + })) |
| 90 | +} |
| 91 | + |
| 92 | +async fn version_handler() -> axum::Json<serde_json::Value> { |
| 93 | + axum::Json(json!({ |
| 94 | + "name": APP_NAME, |
| 95 | + "version": APP_VERSION, |
| 96 | + "mcp_version": MCP_PROTOCOL_VERSION |
| 97 | + })) |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + |
| 104 | + #[tokio::test] |
| 105 | + async fn test_version_endpoint_advertises_latest_protocol() { |
| 106 | + let version = version_handler().await; |
| 107 | + assert_eq!(version.0["mcp_version"], MCP_PROTOCOL_VERSION); |
| 108 | + } |
| 109 | +} |
0 commit comments