rust_mcp_sdk/mcp_http/
app_state.rs

1use crate::mcp_traits::mcp_handler::McpServerHandler;
2use crate::session_store::SessionStore;
3use crate::{id_generator::FastIdGenerator, mcp_traits::IdGenerator, schema::InitializeResult};
4use rust_mcp_transport::event_store::EventStore;
5use rust_mcp_transport::{SessionId, TransportOptions};
6use std::{sync::Arc, time::Duration};
7
8/// Application state struct for the Hyper ser
9///
10/// Holds shared, thread-safe references to session storage, ID generator,
11/// server details, handler, ping interval, and transport options.
12#[derive(Clone)]
13pub struct McpAppState {
14    pub session_store: Arc<dyn SessionStore>,
15    pub id_generator: Arc<dyn IdGenerator<SessionId>>,
16    pub stream_id_gen: Arc<FastIdGenerator>,
17    pub server_details: Arc<InitializeResult>,
18    pub handler: Arc<dyn McpServerHandler>,
19    pub ping_interval: Duration,
20    pub transport_options: Arc<TransportOptions>,
21    pub enable_json_response: bool,
22    /// List of allowed host header values for DNS rebinding protection.
23    /// If not specified, host validation is disabled.
24    pub allowed_hosts: Option<Vec<String>>,
25    /// List of allowed origin header values for DNS rebinding protection.
26    /// If not specified, origin validation is disabled.
27    pub allowed_origins: Option<Vec<String>>,
28    /// Enable DNS rebinding protection (requires allowedHosts and/or allowedOrigins to be configured).
29    /// Default is false for backwards compatibility.
30    pub dns_rebinding_protection: bool,
31    /// Event store for resumability support
32    /// If provided, resumability will be enabled, allowing clients to reconnect and resume messages
33    pub event_store: Option<Arc<dyn EventStore>>,
34}
35
36impl McpAppState {
37    pub fn needs_dns_protection(&self) -> bool {
38        self.dns_rebinding_protection
39            && (self.allowed_hosts.is_some() || self.allowed_origins.is_some())
40    }
41}