turbomcp_server/server/
core.rs

1//! Core MCP server implementation
2//!
3//! Contains the main McpServer struct and its core functionality including
4//! middleware building, lifecycle management, and server construction.
5
6use std::sync::Arc;
7use tracing::{info, info_span};
8
9#[cfg(feature = "http")]
10use tracing::warn;
11
12use crate::{
13    config::ServerConfig,
14    error::ServerResult,
15    lifecycle::{HealthStatus, ServerLifecycle},
16    metrics::ServerMetrics,
17    registry::HandlerRegistry,
18    routing::RequestRouter,
19    service::McpService,
20};
21
22#[cfg(feature = "middleware")]
23use crate::middleware::MiddlewareStack;
24
25use bytes::Bytes;
26use http::{Request, Response};
27use tokio::time::{Duration, sleep};
28use turbomcp_transport::Transport;
29use turbomcp_transport::core::TransportError;
30
31use super::shutdown::ShutdownHandle;
32
33/// Check if logging should be enabled for STDIO transport
34///
35/// For MCP STDIO transport compliance, logging is disabled by default since stdout
36/// must be reserved exclusively for JSON-RPC messages. This can be overridden by
37/// setting the TURBOMCP_FORCE_LOGGING environment variable.
38pub(crate) fn should_log_for_stdio() -> bool {
39    std::env::var("TURBOMCP_FORCE_LOGGING").is_ok()
40}
41
42/// Wrapper that holds router + headers and implements JsonRpcHandler
43/// This allows us to pass headers to create_context without storing them on the router.
44/// Used by both HTTP and WebSocket transports.
45#[cfg(any(feature = "http", feature = "websocket"))]
46struct HttpHandlerWithHeaders {
47    router: crate::routing::RequestRouter,
48    headers: Option<std::collections::HashMap<String, String>>,
49    transport: &'static str,
50}
51
52#[cfg(any(feature = "http", feature = "websocket"))]
53#[async_trait::async_trait]
54impl turbomcp_protocol::JsonRpcHandler for HttpHandlerWithHeaders {
55    async fn handle_request(&self, req_value: serde_json::Value) -> serde_json::Value {
56        use turbomcp_protocol::jsonrpc::JsonRpcRequest;
57
58        // Parse the request
59        let req: JsonRpcRequest = match serde_json::from_value(req_value) {
60            Ok(r) => r,
61            Err(e) => {
62                return serde_json::json!({
63                    "jsonrpc": "2.0",
64                    "error": {
65                        "code": -32700,
66                        "message": format!("Parse error: {}", e)
67                    },
68                    "id": null
69                });
70            }
71        };
72
73        // Create context with headers and transport type
74        let ctx = self
75            .router
76            .create_context(self.headers.clone(), Some(self.transport));
77
78        // Route the request
79        let response = self.router.route(req, ctx).await;
80
81        // Serialize response
82        match serde_json::to_value(&response) {
83            Ok(v) => v,
84            Err(e) => {
85                serde_json::json!({
86                    "jsonrpc": "2.0",
87                    "error": {
88                        "code": -32603,
89                        "message": format!("Internal error: failed to serialize response: {}", e)
90                    },
91                    "id": response.id
92                })
93            }
94        }
95    }
96
97    fn server_info(&self) -> turbomcp_protocol::ServerInfo {
98        self.router.server_info()
99    }
100}
101
102/// Main MCP server following the Axum/Tower Clone pattern
103///
104/// ## Sharing Pattern
105///
106/// `McpServer` implements `Clone` like Axum's `Router`. All heavy state is Arc-wrapped
107/// internally, making cloning cheap (just atomic reference count increments).
108///
109/// ```rust,no_run
110/// use turbomcp_server::ServerBuilder;
111///
112/// # async fn example() {
113/// let server = ServerBuilder::new().build();
114///
115/// // Clone for passing to functions (cheap - just Arc increments)
116/// let server1 = server.clone();
117/// let server2 = server.clone();
118///
119/// // Access config and health
120/// let config = server1.config();
121/// println!("Server: {}", config.name);
122///
123/// let health = server2.health().await;
124/// println!("Health: {:?}", health);
125/// # }
126/// ```
127///
128/// ## Architecture Notes
129///
130/// The `service` field contains `BoxCloneService` which is `Send + Clone` but NOT `Sync`.
131/// This is intentional and follows Tower's design - users clone the server instead of
132/// Arc-wrapping it.
133///
134/// **Architecture Note**: The service field provides tower::Service integration for
135/// advanced middleware patterns. The request processing pipeline currently uses the
136/// RequestRouter directly. Tower integration can be added via custom middleware layers
137/// when needed for specific use cases (e.g., custom rate limiting, advanced tracing).
138#[derive(Clone)]
139pub struct McpServer {
140    /// Server configuration (Clone-able)
141    pub(crate) config: ServerConfig,
142    /// Handler registry (Arc-wrapped for cheap cloning)
143    pub(crate) registry: Arc<HandlerRegistry>,
144    /// Request router (Arc-wrapped for cheap cloning)
145    pub(crate) router: Arc<RequestRouter>,
146    /// Tower middleware service stack (Clone but !Sync - this is the Tower pattern)
147    ///
148    /// All requests flow through this service stack, which provides:
149    /// - Timeout enforcement
150    /// - Request validation
151    /// - Authorization checks
152    /// - Rate limiting
153    /// - Audit logging
154    /// - And more middleware layers as configured
155    ///
156    /// See `server/transport.rs` for integration with transport layer.
157    pub(crate) service:
158        tower::util::BoxCloneService<Request<Bytes>, Response<Bytes>, crate::ServerError>,
159    /// Server lifecycle (Arc-wrapped for cheap cloning)
160    pub(crate) lifecycle: Arc<ServerLifecycle>,
161    /// Server metrics (Arc-wrapped for cheap cloning)
162    pub(crate) metrics: Arc<ServerMetrics>,
163}
164
165impl std::fmt::Debug for McpServer {
166    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167        f.debug_struct("McpServer")
168            .field("config", &self.config)
169            .finish()
170    }
171}
172
173impl McpServer {
174    /// Build comprehensive Tower middleware stack (transport-agnostic)
175    ///
176    /// ## Architecture
177    ///
178    /// This creates a complete Tower service stack with conditional middleware layers:
179    /// - **Timeout Layer**: Request timeout enforcement (tower_http)
180    /// - **Validation Layer**: JSON-RPC structure validation
181    /// - **Authorization Layer**: Resource access control
182    /// - **Core Service**: JSON-RPC routing and handler execution
183    ///
184    /// All middleware is composed using Tower's ServiceBuilder pattern, which provides:
185    /// - Top-to-bottom execution order
186    /// - Type-safe layer composition
187    /// - Zero-cost abstractions
188    /// - Clone-able service instances
189    ///
190    /// ## Integration
191    ///
192    /// The resulting BoxCloneService is stored in `self.service` and called from
193    /// `server/transport.rs` for every incoming request. This ensures ALL requests
194    /// flow through the complete middleware pipeline before reaching handlers.
195    ///
196    /// ## Adding Middleware
197    ///
198    /// To add new middleware, update the match arms below to include your layer.
199    /// Follow the pattern of conditional inclusion based on config flags.
200    #[cfg(feature = "middleware")]
201    fn build_middleware_stack(
202        core_service: McpService,
203        stack: MiddlewareStack,
204    ) -> tower::util::BoxCloneService<Request<Bytes>, Response<Bytes>, crate::ServerError> {
205        // COMPREHENSIVE TOWER COMPOSITION - Conditional Layer Stacking
206        //
207        // This approach builds the middleware stack incrementally, boxing at each step.
208        // While this has a small performance cost from multiple boxing operations,
209        // it provides several critical advantages:
210        //
211        // 1. **Maintainability**: No combinatorial explosion (8 match arms → simple chain)
212        // 2. **Extensibility**: Adding new middleware requires only one new block
213        // 3. **Clarity**: Each layer's purpose and configuration is explicit
214        // 4. **Type Safety**: BoxCloneService provides type erasure while preserving Clone
215        //
216        // Performance note: The boxing overhead is negligible compared to network I/O
217        // and handler execution time. Modern allocators make this essentially free.
218
219        // Start with core service as a boxed service for uniform type handling
220        let mut service: tower::util::BoxCloneService<
221            Request<Bytes>,
222            Response<Bytes>,
223            crate::ServerError,
224        > = tower::util::BoxCloneService::new(core_service);
225
226        // Authorization layer removed in 2.0.0 - handle at application layer
227
228        // Layer 2: Validation
229        // Validates request structure after auth but before processing
230        #[cfg(feature = "middleware")]
231        {
232            if let Some(validation_layer) = stack.validation_layer() {
233                service = tower::util::BoxCloneService::new(
234                    tower::ServiceBuilder::new()
235                        .layer(validation_layer)
236                        .service(service),
237                );
238            }
239        }
240
241        // Layer 3: Timeout (outermost)
242        // Applied last so it can enforce timeout on the entire request pipeline
243        #[cfg(feature = "middleware")]
244        {
245            if let Some(timeout_config) = stack.timeout_config
246                && timeout_config.enabled
247            {
248                service = tower::util::BoxCloneService::new(
249                    tower::ServiceBuilder::new()
250                        .layer(tower_http::timeout::TimeoutLayer::new(
251                            timeout_config.request_timeout,
252                        ))
253                        .service(service),
254                );
255            }
256        }
257
258        // Future middleware can be added here with similar if-let blocks:
259        // if let Some(auth_config) = stack.auth_config { ... }
260        // if let Some(audit_config) = stack.audit_config { ... }
261        // if let Some(rate_limit_config) = stack.rate_limit_config { ... }
262
263        service
264    }
265
266    /// Create a new server
267    #[must_use]
268    pub fn new(config: ServerConfig) -> Self {
269        Self::new_with_registry(config, HandlerRegistry::new())
270    }
271
272    /// Create a new server with an existing registry (used by ServerBuilder)
273    #[must_use]
274    pub(crate) fn new_with_registry(config: ServerConfig, registry: HandlerRegistry) -> Self {
275        let registry = Arc::new(registry);
276        let metrics = Arc::new(ServerMetrics::new());
277        let router = Arc::new(RequestRouter::new(
278            Arc::clone(&registry),
279            Arc::clone(&metrics),
280            config.clone(),
281        ));
282        // Build middleware stack configuration
283        #[cfg(feature = "middleware")]
284        #[cfg_attr(not(feature = "rate-limiting"), allow(unused_mut))]
285        let mut stack = crate::middleware::MiddlewareStack::new();
286
287        // Auto-install rate limiting if enabled in config
288        #[cfg(feature = "rate-limiting")]
289        if config.rate_limiting.enabled {
290            use crate::middleware::rate_limit::{RateLimitStrategy, RateLimits};
291            use std::num::NonZeroU32;
292            use std::time::Duration;
293
294            let rate_config = crate::middleware::RateLimitConfig {
295                strategy: RateLimitStrategy::Global,
296                limits: RateLimits {
297                    requests_per_period: NonZeroU32::new(
298                        config.rate_limiting.requests_per_second * 60,
299                    )
300                    .unwrap(), // Convert per-second to per-minute
301                    period: Duration::from_secs(60),
302                    burst_size: Some(NonZeroU32::new(config.rate_limiting.burst_capacity).unwrap()),
303                },
304                enabled: true,
305            };
306
307            stack = stack.with_rate_limit(rate_config);
308        }
309
310        // Create core MCP service
311        let core_service = McpService::new(
312            Arc::clone(&registry),
313            Arc::clone(&router),
314            Arc::clone(&metrics),
315        );
316
317        // COMPREHENSIVE TOWER SERVICE COMPOSITION
318        // Build the complete middleware stack with proper type erasure
319        //
320        // This service is called from server/transport.rs for EVERY incoming request:
321        // TransportMessage -> http::Request -> service.call() -> http::Response -> TransportMessage
322        //
323        // The Tower middleware stack provides:
324        // ✓ Timeout enforcement (configurable per-request)
325        // ✓ Request validation (JSON-RPC structure)
326        // ✓ Authorization checks (resource access control)
327        // ✓ Rate limiting (if enabled in config)
328        // ✓ Audit logging (configurable)
329        // ✓ And more layers as configured
330        //
331        // BoxCloneService is Clone but !Sync - this is the Tower pattern
332        #[cfg(feature = "middleware")]
333        let service = Self::build_middleware_stack(core_service, stack);
334
335        #[cfg(not(feature = "middleware"))]
336        let service = tower::util::BoxCloneService::new(core_service);
337
338        let lifecycle = Arc::new(ServerLifecycle::new());
339
340        Self {
341            config,
342            registry,
343            router,
344            service,
345            lifecycle,
346            metrics,
347        }
348    }
349
350    /// Get server configuration
351    #[must_use]
352    pub const fn config(&self) -> &ServerConfig {
353        &self.config
354    }
355
356    /// Get handler registry
357    #[must_use]
358    pub const fn registry(&self) -> &Arc<HandlerRegistry> {
359        &self.registry
360    }
361
362    /// Get request router
363    #[must_use]
364    pub const fn router(&self) -> &Arc<RequestRouter> {
365        &self.router
366    }
367
368    /// Get server lifecycle
369    #[must_use]
370    pub const fn lifecycle(&self) -> &Arc<ServerLifecycle> {
371        &self.lifecycle
372    }
373
374    /// Get server metrics
375    #[must_use]
376    pub const fn metrics(&self) -> &Arc<ServerMetrics> {
377        &self.metrics
378    }
379
380    /// Get the Tower service stack (test accessor)
381    ///
382    /// **Note**: This is primarily for integration testing. Production code should
383    /// use the transport layer which calls the service internally via
384    /// `handle_transport_message()`.
385    ///
386    /// Returns a clone of the Tower service stack, which is cheap (BoxCloneService
387    /// is designed for cloning).
388    #[doc(hidden)]
389    pub fn service(
390        &self,
391    ) -> tower::util::BoxCloneService<Request<Bytes>, Response<Bytes>, crate::ServerError> {
392        self.service.clone()
393    }
394
395    /// Get a shutdown handle for graceful server termination
396    ///
397    /// This handle enables external control over server shutdown, essential for:
398    /// - **Production deployments**: Graceful shutdown on SIGTERM/SIGINT
399    /// - **Container orchestration**: Kubernetes graceful pod termination
400    /// - **Load balancer integration**: Health check coordination
401    /// - **Multi-component systems**: Coordinated shutdown sequences
402    /// - **Maintenance operations**: Planned downtime and updates
403    ///
404    /// # Examples
405    ///
406    /// ## Basic shutdown coordination
407    /// ```no_run
408    /// # use turbomcp_server::ServerBuilder;
409    /// # #[tokio::main]
410    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
411    /// let server = ServerBuilder::new().build();
412    /// let shutdown_handle = server.shutdown_handle();
413    ///
414    /// // Coordinate with other services
415    /// tokio::spawn(async move {
416    ///     // Wait for external shutdown signal
417    ///     tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
418    ///     println!("Shutdown signal received, terminating gracefully...");
419    ///     shutdown_handle.shutdown().await;
420    /// });
421    ///
422    /// // Server will gracefully shut down when signaled
423    /// // server.run_stdio().await?;
424    /// # Ok(())
425    /// # }
426    /// ```
427    ///
428    /// ## Container/Kubernetes deployment
429    /// ```no_run
430    /// # use turbomcp_server::ServerBuilder;
431    /// # use std::sync::Arc;
432    /// # #[tokio::main]
433    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
434    /// let server = ServerBuilder::new().build();
435    /// let shutdown_handle = server.shutdown_handle();
436    /// let shutdown_handle_clone = shutdown_handle.clone();
437    ///
438    /// // Handle multiple signal types with proper platform support
439    /// tokio::spawn(async move {
440    ///     #[cfg(unix)]
441    ///     {
442    ///         use tokio::signal::unix::{signal, SignalKind};
443    ///         let mut sigterm = signal(SignalKind::terminate()).unwrap();
444    ///         tokio::select! {
445    ///             _ = tokio::signal::ctrl_c() => {
446    ///                 println!("SIGINT received");
447    ///             }
448    ///             _ = sigterm.recv() => {
449    ///                 println!("SIGTERM received");
450    ///             }
451    ///         }
452    ///     }
453    ///     #[cfg(not(unix))]
454    ///     {
455    ///         tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
456    ///         println!("SIGINT received");
457    ///     }
458    ///     shutdown_handle_clone.shutdown().await;
459    /// });
460    ///
461    /// // Server handles graceful shutdown automatically
462    /// // server.run_tcp("0.0.0.0:8080").await?;
463    /// # Ok(())
464    /// # }
465    /// ```
466    pub fn shutdown_handle(&self) -> ShutdownHandle {
467        ShutdownHandle::new(self.lifecycle.clone())
468    }
469
470    /// Run the server with STDIO transport
471    ///
472    /// # Errors
473    ///
474    /// Returns [`crate::ServerError::Transport`] if:
475    /// - STDIO transport connection fails
476    /// - Message sending/receiving fails
477    /// - Transport disconnection fails
478    #[tracing::instrument(skip(self), fields(
479        transport = "stdio",
480        service_name = %self.config.name,
481        service_version = %self.config.version
482    ))]
483    pub async fn run_stdio(mut self) -> ServerResult<()> {
484        // For STDIO transport, disable logging unless explicitly overridden
485        // STDIO stdout must be reserved exclusively for JSON-RPC messages per MCP protocol
486        if should_log_for_stdio() {
487            info!("Starting MCP server with STDIO transport");
488        }
489
490        // Start performance monitoring for STDIO server
491        let _perf_span = info_span!("server.run", transport = "stdio").entered();
492        info!("Initializing STDIO transport for MCP server");
493
494        self.lifecycle.start().await;
495
496        // BIDIRECTIONAL STDIO SETUP
497        // Create STDIO dispatcher for server-initiated requests (sampling, elicitation, roots, ping)
498        let (request_tx, request_rx) = tokio::sync::mpsc::unbounded_channel();
499
500        // Use fully-qualified path to avoid ambiguity with the turbomcp crate's runtime module
501        let dispatcher = crate::runtime::StdioDispatcher::new(request_tx);
502
503        // Configure router's bidirectional support with the STDIO dispatcher
504        // SAFETY: We have &mut self, so we can safely get mutable access to the Arc'd router
505        // This is the CRITICAL STEP that was missing - without this, all server→client requests fail
506        let router = Arc::make_mut(&mut self.router);
507        router.set_server_request_dispatcher(dispatcher.clone());
508
509        // Run STDIO with full bidirectional support (MCP 2025-06-18 compliant)
510        // This uses the bidirectional-aware runtime that handles both:
511        // - Client→Server requests (tools, resources, prompts)
512        // - Server→Client requests (sampling, elicitation, roots, ping)
513        crate::runtime::run_stdio_bidirectional(self.router.clone(), dispatcher, request_rx)
514            .await
515            .map_err(|e| crate::ServerError::Handler {
516                message: format!("STDIO bidirectional runtime failed: {}", e),
517                context: Some("run_stdio".to_string()),
518            })
519    }
520
521    /// Get health status
522    pub async fn health(&self) -> HealthStatus {
523        self.lifecycle.health().await
524    }
525
526    /// Run server with HTTP transport using default configuration
527    ///
528    /// This provides a working HTTP server with:
529    /// - Standard HTTP POST/GET/DELETE for MCP protocol at `/mcp`
530    /// - Full MCP 2025-06-18 protocol compliance
531    /// - Graceful shutdown support
532    /// - Default rate limiting (100 req/60s)
533    /// - Default security settings (localhost allowed, CORS disabled)
534    ///
535    /// For custom configuration (rate limits, security, CORS), use `run_http_with_config`.
536    ///
537    /// # Examples
538    ///
539    /// ## Basic usage with default configuration
540    /// ```no_run
541    /// use turbomcp_server::ServerBuilder;
542    ///
543    /// #[tokio::main]
544    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
545    ///     let server = ServerBuilder::new()
546    ///         .name("my-server")
547    ///         .version("1.0.0")
548    ///         .build();
549    ///
550    ///     server.run_http("127.0.0.1:3000").await?;
551    ///     Ok(())
552    /// }
553    /// ```
554    ///
555    /// ## With custom configuration
556    /// ```no_run
557    /// use turbomcp_server::ServerBuilder;
558    /// use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
559    /// use std::time::Duration;
560    ///
561    /// #[tokio::main]
562    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
563    ///     let server = ServerBuilder::new()
564    ///         .name("my-server")
565    ///         .version("1.0.0")
566    ///         .build();
567    ///
568    ///     let config = StreamableHttpConfigBuilder::new()
569    ///         .without_rate_limit()  // For benchmarking
570    ///         .allow_any_origin(true)  // Enable CORS
571    ///         .build();
572    ///
573    ///     server.run_http_with_config("127.0.0.1:3000", config).await?;
574    ///     Ok(())
575    /// }
576    /// ```
577    ///
578    /// # Errors
579    ///
580    /// Returns [`crate::ServerError::Transport`] if:
581    /// - Address resolution fails
582    /// - HTTP server fails to start
583    /// - Transport disconnection fails
584    #[cfg(feature = "http")]
585    #[tracing::instrument(skip(self), fields(
586        transport = "http",
587        service_name = %self.config.name,
588        service_version = %self.config.version,
589        addr = ?addr
590    ))]
591    pub async fn run_http<A: std::net::ToSocketAddrs + Send + std::fmt::Debug>(
592        self,
593        addr: A,
594    ) -> ServerResult<()> {
595        use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
596
597        // Build default configuration
598        let config = StreamableHttpConfigBuilder::new().build();
599
600        self.run_http_with_config(addr, config).await
601    }
602
603    /// Run server with HTTP transport and custom configuration
604    ///
605    /// This provides full control over HTTP server configuration including:
606    /// - Rate limiting (requests per time window, or disabled entirely)
607    /// - Security settings (CORS, origin validation, authentication)
608    /// - Network settings (bind address, endpoint path, keep-alive)
609    /// - Advanced settings (replay buffer size, etc.)
610    ///
611    /// # Bind Address Configuration
612    ///
613    /// **IMPORTANT**: The `addr` parameter takes precedence over `config.bind_addr`.
614    /// If they differ, a deprecation warning is logged.
615    ///
616    /// **Best Practice** (recommended for forward compatibility):
617    /// ```no_run
618    /// # use turbomcp_server::ServerBuilder;
619    /// # use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
620    /// # #[tokio::main]
621    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
622    /// let config = StreamableHttpConfigBuilder::new()
623    ///     .with_bind_address("127.0.0.1:3001")  // Set bind address in config
624    ///     .build();
625    ///
626    /// // Pass matching addr parameter (or use default "127.0.0.1:8080")
627    /// ServerBuilder::new()
628    ///     .build()
629    ///     .run_http_with_config("127.0.0.1:3001", config).await?;
630    /// # Ok(())
631    /// # }
632    /// ```
633    ///
634    /// **Deprecated** (will be removed in v3.x):
635    /// ```no_run
636    /// # use turbomcp_server::ServerBuilder;
637    /// # use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
638    /// # #[tokio::main]
639    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
640    /// // ⚠️ Avoid setting different addresses - causes deprecation warning
641    /// let config = StreamableHttpConfigBuilder::new()
642    ///     .with_bind_address("0.0.0.0:5000")  // This is ignored!
643    ///     .build();
644    ///
645    /// // The addr parameter wins (3001 is used, not 5000)
646    /// ServerBuilder::new()
647    ///     .build()
648    ///     .run_http_with_config("127.0.0.1:3001", config).await?;
649    /// # Ok(())
650    /// # }
651    /// ```
652    ///
653    /// **Future (v3.x)**: The `addr` parameter will be removed. Configure bind address
654    /// via `config.with_bind_address()` only.
655    ///
656    /// # Examples
657    ///
658    /// ## Benchmarking configuration (no rate limits)
659    /// ```no_run
660    /// use turbomcp_server::ServerBuilder;
661    /// use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
662    ///
663    /// #[tokio::main]
664    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
665    ///     let server = ServerBuilder::new()
666    ///         .name("benchmark-server")
667    ///         .version("1.0.0")
668    ///         .build();
669    ///
670    ///     let config = StreamableHttpConfigBuilder::new()
671    ///         .with_bind_address("127.0.0.1:3000")
672    ///         .without_rate_limit()  // Disable rate limiting
673    ///         .build();
674    ///
675    ///     server.run_http_with_config("127.0.0.1:3000", config).await?;
676    ///     Ok(())
677    /// }
678    /// ```
679    ///
680    /// ## Production configuration (secure, rate limited)
681    /// ```no_run
682    /// use turbomcp_server::ServerBuilder;
683    /// use turbomcp_transport::streamable_http_v2::StreamableHttpConfigBuilder;
684    /// use std::time::Duration;
685    ///
686    /// #[tokio::main]
687    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
688    ///     let server = ServerBuilder::new()
689    ///         .name("production-server")
690    ///         .version("1.0.0")
691    ///         .build();
692    ///
693    ///     let config = StreamableHttpConfigBuilder::new()
694    ///         .with_bind_address("127.0.0.1:3000")
695    ///         .with_rate_limit(1000, Duration::from_secs(60))  // 1000 req/min
696    ///         .allow_any_origin(false)  // Strict CORS
697    ///         .require_authentication(true)  // Require auth
698    ///         .build();
699    ///
700    ///     server.run_http_with_config("127.0.0.1:3000", config).await?;
701    ///     Ok(())
702    /// }
703    /// ```
704    ///
705    /// # Errors
706    ///
707    /// Returns [`crate::ServerError::Transport`] if:
708    /// - Address resolution fails
709    /// - HTTP server fails to start
710    /// - Transport disconnection fails
711    #[cfg(feature = "http")]
712    #[tracing::instrument(skip(self, config), fields(
713        transport = "http",
714        service_name = %self.config.name,
715        service_version = %self.config.version,
716        addr = ?addr
717    ))]
718    pub async fn run_http_with_config<A: std::net::ToSocketAddrs + Send + std::fmt::Debug>(
719        self,
720        addr: A,
721        mut config: turbomcp_transport::streamable_http_v2::StreamableHttpConfig,
722    ) -> ServerResult<()> {
723        use std::collections::HashMap;
724        use tokio::sync::{Mutex, RwLock};
725
726        // Sprint 2.6: Check for insecure 0.0.0.0 binding
727        crate::security_checks::check_binding_security(&addr);
728
729        info!("Starting MCP server with HTTP transport");
730
731        self.lifecycle.start().await;
732
733        // Resolve address to string
734        let socket_addr = addr
735            .to_socket_addrs()
736            .map_err(|e| crate::ServerError::configuration(format!("Invalid address: {}", e)))?
737            .next()
738            .ok_or_else(|| crate::ServerError::configuration("No address resolved"))?;
739
740        info!("Resolved address: {}", socket_addr);
741
742        // Check for conflicting bind addresses and warn about deprecation
743        let socket_addr_str = socket_addr.to_string();
744        if config.bind_addr != socket_addr_str {
745            warn!(
746                addr_parameter = %socket_addr_str,
747                config_bind_addr = %config.bind_addr,
748                "⚠️  DEPRECATION WARNING: The `addr` parameter takes precedence over `config.bind_addr`"
749            );
750            warn!(
751                "⚠️  In TurboMCP v3.x, the `addr` parameter will be removed. Please use StreamableHttpConfigBuilder::new().with_bind_address(\"{}\").build() instead",
752                socket_addr_str
753            );
754            warn!(
755                "⚠️  Avoid setting both `addr` parameter and `config.bind_addr` to prevent confusion"
756            );
757
758            // Update config to single source of truth
759            config.bind_addr = socket_addr_str.clone();
760            config.base_url = format!("http://{}", socket_addr_str);
761        }
762
763        info!(
764            config = ?config,
765            "HTTP configuration (updated with resolved address)"
766        );
767
768        // BIDIRECTIONAL HTTP SETUP
769        // Create shared state for session management and bidirectional MCP
770        let sessions = Arc::new(RwLock::new(HashMap::new()));
771        let pending_requests = Arc::new(Mutex::new(HashMap::new()));
772
773        // Share router across all sessions (routing logic and handler registry)
774        let router = self.router.clone();
775
776        // Capture server identity for MCP protocol compliance
777        let server_info = turbomcp_protocol::ServerInfo {
778            name: self.config.name.clone(),
779            version: self.config.version.clone(),
780        };
781
782        // Factory pattern: create session-specific router for each HTTP request
783        // This is the clean architecture that HTTP requires - each session gets its own
784        // bidirectional dispatcher while sharing the routing logic
785        let sessions_for_factory = Arc::clone(&sessions);
786        let pending_for_factory = Arc::clone(&pending_requests);
787        let router_for_factory = Arc::clone(&router);
788
789        // Create a wrapper that converts headers and delegates to router
790        // This is cleaner than storing headers on the router itself
791        let handler_factory =
792            move |session_id: Option<String>, headers: Option<axum::http::HeaderMap>| {
793                let session_id = session_id.unwrap_or_else(|| {
794                    let new_id = uuid::Uuid::new_v4().to_string();
795                    tracing::warn!(
796                        "⚠️ Factory generating random session ID (no session ID provided): {}",
797                        new_id
798                    );
799                    new_id
800                });
801
802                tracing::debug!("Factory creating handler for session: {}", session_id);
803
804                // Create session-specific HTTP dispatcher (now local to turbomcp-server!)
805                let dispatcher = crate::runtime::http::HttpDispatcher::new(
806                    session_id,
807                    Arc::clone(&sessions_for_factory),
808                    Arc::clone(&pending_for_factory),
809                );
810
811                // Clone the base router and configure with session-specific dispatcher
812                // CRITICAL: set_server_request_dispatcher also recreates server_to_client adapter
813                let mut session_router = (*router_for_factory).clone();
814                session_router.set_server_request_dispatcher(dispatcher);
815
816                // Convert HeaderMap to HashMap<String, String> for passing to create_context
817                let headers_map = headers.map(|header_map| {
818                    header_map
819                        .iter()
820                        .filter_map(|(name, value)| {
821                            value
822                                .to_str()
823                                .ok()
824                                .map(|v| (name.to_string(), v.to_string()))
825                        })
826                        .collect()
827                });
828
829                // Create wrapper that passes headers to create_context (HTTP transport)
830                HttpHandlerWithHeaders {
831                    router: session_router,
832                    headers: headers_map,
833                    transport: "http",
834                }
835            };
836
837        info!(
838            server_name = %server_info.name,
839            server_version = %server_info.version,
840            bind_addr = %socket_addr,
841            endpoint_path = %config.endpoint_path,
842            "HTTP server starting with full bidirectional support (elicitation, sampling, roots, ping)"
843        );
844
845        // Use factory-based HTTP server with full bidirectional support
846        use crate::runtime::http::run_http;
847        run_http(
848            handler_factory,
849            sessions,
850            pending_requests,
851            socket_addr.to_string(),
852            config.endpoint_path.clone(),
853        )
854        .await
855        .map_err(|e| {
856            tracing::error!(error = %e, "HTTP server failed");
857            crate::ServerError::handler(e.to_string())
858        })?;
859
860        info!("HTTP server shutdown complete");
861        Ok(())
862    }
863
864    /// Run server with WebSocket transport (full bidirectional support)
865    ///
866    /// This provides a simple API for WebSocket servers with sensible defaults:
867    /// - Default endpoint: `/mcp/ws`
868    /// - Full MCP 2025-06-18 compliance
869    /// - Bidirectional communication
870    /// - Elicitation support
871    /// - Session management and middleware
872    ///
873    /// For custom configuration, use `run_websocket_with_config()`.
874    ///
875    /// # Example
876    ///
877    /// ```no_run
878    /// use turbomcp_server::ServerBuilder;
879    ///
880    /// #[tokio::main]
881    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
882    ///     let server = ServerBuilder::new()
883    ///         .name("ws-server")
884    ///         .version("1.0.0")
885    ///         .build();
886    ///
887    ///     server.run_websocket("127.0.0.1:8080").await?;
888    ///     Ok(())
889    /// }
890    /// ```
891    #[cfg(feature = "websocket")]
892    #[tracing::instrument(skip(self), fields(
893        transport = "websocket",
894        service_name = %self.config.name,
895        service_version = %self.config.version,
896        addr = ?addr
897    ))]
898    pub async fn run_websocket<A: std::net::ToSocketAddrs + Send + std::fmt::Debug>(
899        self,
900        addr: A,
901    ) -> ServerResult<()> {
902        use crate::config::WebSocketServerConfig;
903
904        // Build default configuration
905        let config = WebSocketServerConfig::default();
906
907        self.run_websocket_with_config(addr, config).await
908    }
909
910    /// Run server with WebSocket transport and custom configuration
911    ///
912    /// This provides full control over WebSocket server configuration including:
913    /// - Custom endpoint path
914    /// - MCP server settings (middleware, security, etc.)
915    ///
916    /// # Example
917    ///
918    /// ```no_run
919    /// use turbomcp_server::{ServerBuilder, WebSocketServerConfig};
920    ///
921    /// #[tokio::main]
922    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
923    ///     let server = ServerBuilder::new()
924    ///         .name("custom-ws-server")
925    ///         .version("1.0.0")
926    ///         .build();
927    ///
928    ///     let config = WebSocketServerConfig {
929    ///         bind_addr: "0.0.0.0:8080".to_string(),
930    ///         endpoint_path: "/custom/ws".to_string(),
931    ///     };
932    ///
933    ///     server.run_websocket_with_config("127.0.0.1:8080", config).await?;
934    ///     Ok(())
935    /// }
936    /// ```
937    #[cfg(feature = "websocket")]
938    #[tracing::instrument(skip(self, config), fields(
939        transport = "websocket",
940        service_name = %self.config.name,
941        service_version = %self.config.version,
942        addr = ?addr
943    ))]
944    pub async fn run_websocket_with_config<A: std::net::ToSocketAddrs + Send + std::fmt::Debug>(
945        self,
946        addr: A,
947        config: crate::config::WebSocketServerConfig,
948    ) -> ServerResult<()> {
949        use axum::{Router, middleware, routing::get};
950        use turbomcp_transport::axum::{WebSocketFactoryState, websocket_handler_with_factory};
951        use turbomcp_transport::tower::SessionInfo;
952
953        // Sprint 2.6: Check for insecure 0.0.0.0 binding
954        crate::security_checks::check_binding_security(&addr);
955
956        info!("Starting MCP server with WebSocket transport");
957        info!(config = ?config, "WebSocket configuration");
958
959        self.lifecycle.start().await;
960
961        // Resolve address to string
962        let socket_addr = addr
963            .to_socket_addrs()
964            .map_err(|e| crate::ServerError::configuration(format!("Invalid address: {}", e)))?
965            .next()
966            .ok_or_else(|| crate::ServerError::configuration("No address resolved"))?;
967
968        info!("Resolved address: {}", socket_addr);
969
970        // Capture server identity for MCP protocol compliance
971        let server_info = turbomcp_protocol::ServerInfo {
972            name: self.config.name.clone(),
973            version: self.config.version.clone(),
974        };
975
976        // Router for this server (shared across all connections)
977        let router = (*self.router).clone();
978
979        // Factory: creates per-connection handler with bidirectional support
980        // This is the unified architecture - transport layer handles WebSocket mechanics,
981        // server layer provides MCP-specific handler logic
982        let handler_factory =
983            move |transport_dispatcher: turbomcp_transport::axum::WebSocketDispatcher,
984                  headers: Option<std::collections::HashMap<String, String>>| {
985                // Wrap transport dispatcher with server layer adapter
986                let server_dispatcher =
987                    crate::routing::WebSocketDispatcherAdapter::new(transport_dispatcher);
988
989                // Clone router for this connection and configure with dispatcher
990                let mut connection_router = router.clone();
991                connection_router.set_server_request_dispatcher(server_dispatcher);
992
993                // Create wrapper that passes headers to create_context (WebSocket transport)
994                // We can reuse HttpHandlerWithHeaders since it's generic
995                Arc::new(HttpHandlerWithHeaders {
996                    router: connection_router,
997                    headers,
998                    transport: "websocket",
999                }) as Arc<dyn turbomcp_protocol::JsonRpcHandler>
1000            };
1001
1002        info!(
1003            server_name = %server_info.name,
1004            server_version = %server_info.version,
1005            bind_addr = %socket_addr,
1006            endpoint_path = %config.endpoint_path,
1007            "WebSocket server starting with full bidirectional support (elicitation, sampling, roots, ping)"
1008        );
1009
1010        // Create factory state for transport layer
1011        let factory_state = WebSocketFactoryState::new(handler_factory);
1012
1013        // Session middleware to extract headers (same as HTTP transport)
1014        let session_middleware = |mut request: axum::extract::Request, next: middleware::Next| async move {
1015            let mut session = SessionInfo::new();
1016
1017            // Extract headers and store in session metadata
1018            for (name, value) in request.headers().iter() {
1019                if let Ok(value_str) = value.to_str() {
1020                    session
1021                        .metadata
1022                        .insert(name.to_string(), value_str.to_string());
1023                }
1024            }
1025
1026            // Extract specific useful headers
1027            if let Some(user_agent) = request
1028                .headers()
1029                .get("user-agent")
1030                .and_then(|v| v.to_str().ok())
1031            {
1032                session.user_agent = Some(user_agent.to_string());
1033            }
1034
1035            if let Some(remote_addr) = request
1036                .headers()
1037                .get("x-forwarded-for")
1038                .and_then(|v| v.to_str().ok())
1039            {
1040                session.remote_addr = Some(remote_addr.to_string());
1041            }
1042
1043            request.extensions_mut().insert(session);
1044            next.run(request).await
1045        };
1046
1047        // Build Axum router using transport layer
1048        let app = Router::new()
1049            .route(&config.endpoint_path, get(websocket_handler_with_factory))
1050            .with_state(factory_state)
1051            .layer(middleware::from_fn(session_middleware));
1052
1053        info!("WebSocket server bound to {}", socket_addr);
1054
1055        // Serve using Axum
1056        let listener = tokio::net::TcpListener::bind(socket_addr)
1057            .await
1058            .map_err(|e| crate::ServerError::configuration(format!("Failed to bind: {}", e)))?;
1059
1060        axum::serve(listener, app).await.map_err(|e| {
1061            tracing::error!(error = %e, "WebSocket server failed");
1062            crate::ServerError::handler(e.to_string())
1063        })?;
1064
1065        info!("WebSocket server shutdown complete");
1066        Ok(())
1067    }
1068
1069    /// Run server with TCP transport (progressive enhancement - runtime configuration)
1070    #[cfg(feature = "tcp")]
1071    #[tracing::instrument(skip(self), fields(
1072        transport = "tcp",
1073        service_name = %self.config.name,
1074        service_version = %self.config.version,
1075        addr = ?addr
1076    ))]
1077    pub async fn run_tcp<A: std::net::ToSocketAddrs + Send + std::fmt::Debug>(
1078        mut self,
1079        addr: A,
1080    ) -> ServerResult<()> {
1081        use turbomcp_transport::TcpTransport;
1082
1083        // Sprint 2.6: Check for insecure 0.0.0.0 binding
1084        crate::security_checks::check_binding_security(&addr);
1085
1086        // Start performance monitoring for TCP server
1087        let _perf_span = info_span!("server.run", transport = "tcp").entered();
1088        info!(?addr, "Starting MCP server with TCP transport");
1089
1090        self.lifecycle.start().await;
1091
1092        // Convert ToSocketAddrs to SocketAddr
1093        let socket_addr = match addr.to_socket_addrs() {
1094            Ok(mut addrs) => match addrs.next() {
1095                Some(addr) => addr,
1096                None => {
1097                    tracing::error!("No socket address resolved from provided address");
1098                    self.lifecycle.shutdown().await;
1099                    return Err(crate::ServerError::configuration("Invalid socket address"));
1100                }
1101            },
1102            Err(e) => {
1103                tracing::error!(error = %e, "Failed to resolve socket address");
1104                self.lifecycle.shutdown().await;
1105                return Err(crate::ServerError::configuration(format!(
1106                    "Address resolution failed: {e}"
1107                )));
1108            }
1109        };
1110
1111        let transport = TcpTransport::new_server(socket_addr);
1112        if let Err(e) = transport.connect().await {
1113            tracing::error!(error = %e, "Failed to connect TCP transport");
1114            self.lifecycle.shutdown().await;
1115            return Err(e.into());
1116        }
1117
1118        // BIDIRECTIONAL TCP SETUP
1119        // Create generic transport dispatcher for server-initiated requests
1120        let dispatcher = crate::runtime::TransportDispatcher::new(transport);
1121
1122        // Configure router's bidirectional support with the TCP dispatcher
1123        // This enables ctx.elicit(), ctx.create_message(), ctx.list_roots(), etc.
1124        let router = Arc::make_mut(&mut self.router);
1125        router.set_server_request_dispatcher(dispatcher.clone());
1126
1127        // Run TCP with full bidirectional support (MCP 2025-06-18 compliant)
1128        // This uses the generic bidirectional runtime that handles both:
1129        // - Client→Server requests (tools, resources, prompts)
1130        // - Server→Client requests (sampling, elicitation, roots, ping)
1131        crate::runtime::run_transport_bidirectional(self.router.clone(), dispatcher)
1132            .await
1133            .map_err(|e| crate::ServerError::Handler {
1134                message: format!("TCP bidirectional runtime failed: {}", e),
1135                context: Some("run_tcp".to_string()),
1136            })
1137    }
1138
1139    /// Run server with Unix socket transport (progressive enhancement - runtime configuration)
1140    #[cfg(all(feature = "unix", unix))]
1141    #[tracing::instrument(skip(self), fields(
1142        transport = "unix",
1143        service_name = %self.config.name,
1144        service_version = %self.config.version,
1145        path = ?path.as_ref()
1146    ))]
1147    pub async fn run_unix<P: AsRef<std::path::Path>>(mut self, path: P) -> ServerResult<()> {
1148        use std::path::PathBuf;
1149        use turbomcp_transport::UnixTransport;
1150
1151        // Start performance monitoring for Unix server
1152        let _perf_span = info_span!("server.run", transport = "unix").entered();
1153        info!(path = ?path.as_ref(), "Starting MCP server with Unix socket transport");
1154
1155        self.lifecycle.start().await;
1156
1157        let socket_path = PathBuf::from(path.as_ref());
1158        let transport = UnixTransport::new_server(socket_path);
1159        if let Err(e) = transport.connect().await {
1160            tracing::error!(error = %e, "Failed to connect Unix socket transport");
1161            self.lifecycle.shutdown().await;
1162            return Err(e.into());
1163        }
1164
1165        // BIDIRECTIONAL UNIX SOCKET SETUP
1166        // Create generic transport dispatcher for server-initiated requests
1167        let dispatcher = crate::runtime::TransportDispatcher::new(transport);
1168
1169        // Configure router's bidirectional support with the Unix socket dispatcher
1170        // This enables ctx.elicit(), ctx.create_message(), ctx.list_roots(), etc.
1171        let router = Arc::make_mut(&mut self.router);
1172        router.set_server_request_dispatcher(dispatcher.clone());
1173
1174        // Run Unix Socket with full bidirectional support (MCP 2025-06-18 compliant)
1175        // This uses the generic bidirectional runtime that handles both:
1176        // - Client→Server requests (tools, resources, prompts)
1177        // - Server→Client requests (sampling, elicitation, roots, ping)
1178        crate::runtime::run_transport_bidirectional(self.router.clone(), dispatcher)
1179            .await
1180            .map_err(|e| crate::ServerError::Handler {
1181                message: format!("Unix socket bidirectional runtime failed: {}", e),
1182                context: Some("run_unix".to_string()),
1183            })
1184    }
1185
1186    /// Generic transport runner (DRY principle)
1187    /// Used by feature-gated transport methods (http, tcp, websocket, unix)
1188    #[allow(dead_code)]
1189    #[tracing::instrument(skip(self, transport), fields(
1190        service_name = %self.config.name,
1191        service_version = %self.config.version
1192    ))]
1193    async fn run_with_transport<T: Transport>(&self, mut transport: T) -> ServerResult<()> {
1194        // Install signal handlers for graceful shutdown (Ctrl+C / SIGTERM)
1195        let lifecycle_for_sigint = self.lifecycle.clone();
1196        tokio::spawn(async move {
1197            if let Err(e) = tokio::signal::ctrl_c().await {
1198                tracing::warn!(error = %e, "Failed to install Ctrl+C handler");
1199                return;
1200            }
1201            tracing::info!("Ctrl+C received, initiating shutdown");
1202            lifecycle_for_sigint.shutdown().await;
1203        });
1204
1205        #[cfg(unix)]
1206        {
1207            let lifecycle_for_sigterm = self.lifecycle.clone();
1208            tokio::spawn(async move {
1209                use tokio::signal::unix::{SignalKind, signal};
1210                match signal(SignalKind::terminate()) {
1211                    Ok(mut sigterm) => {
1212                        sigterm.recv().await;
1213                        tracing::info!("SIGTERM received, initiating shutdown");
1214                        lifecycle_for_sigterm.shutdown().await;
1215                    }
1216                    Err(e) => tracing::warn!(error = %e, "Failed to install SIGTERM handler"),
1217                }
1218            });
1219        }
1220
1221        // Shutdown signal
1222        let mut shutdown = self.lifecycle.shutdown_signal();
1223
1224        // Main message processing loop
1225        loop {
1226            tokio::select! {
1227                _ = shutdown.recv() => {
1228                    tracing::info!("Shutdown signal received");
1229                    break;
1230                }
1231                res = transport.receive() => {
1232                    match res {
1233                        Ok(Some(message)) => {
1234                            if let Err(e) = self.handle_transport_message(&mut transport, message).await {
1235                                tracing::warn!(error = %e, "Failed to handle transport message");
1236                            }
1237                        }
1238                        Ok(None) => {
1239                            // No message available; sleep briefly to avoid busy loop
1240                            sleep(Duration::from_millis(5)).await;
1241                        }
1242                        Err(e) => {
1243                            match e {
1244                                TransportError::ReceiveFailed(msg) if msg.contains("disconnected") => {
1245                                    tracing::info!("Transport receive channel disconnected; shutting down");
1246                                    break;
1247                                }
1248                                _ => {
1249                                    tracing::error!(error = %e, "Transport receive failed");
1250                                    // Backoff on errors
1251                                    sleep(Duration::from_millis(50)).await;
1252                                }
1253                            }
1254                        }
1255                    }
1256                }
1257            }
1258        }
1259
1260        // Disconnect transport
1261        if let Err(e) = transport.disconnect().await {
1262            tracing::warn!(error = %e, "Error while disconnecting transport");
1263        }
1264
1265        tracing::info!("Server shutdown complete");
1266        Ok(())
1267    }
1268}
1269
1270// Compile-time assertion that McpServer is Send + Clone (Tower pattern)
1271// Note: McpServer is Clone but NOT Sync (due to BoxCloneService being !Sync)
1272// This is intentional and follows the Axum/Tower design pattern
1273#[allow(dead_code)]
1274const _: () = {
1275    const fn assert_send_clone<T: Send + Clone>() {}
1276    const fn check() {
1277        assert_send_clone::<crate::server::core::McpServer>();
1278    }
1279};