mcpkit_transport/http/
config.rs

1//! HTTP transport configuration types and constants.
2
3use std::time::Duration;
4
5/// MCP Protocol version for the HTTP transport.
6///
7/// This matches `ProtocolVersion::LATEST` from mcpkit-core.
8pub const MCP_PROTOCOL_VERSION: &str = "2025-11-25";
9
10/// Header name for MCP protocol version.
11///
12/// Note: HTTP/2 requires lowercase header names. HTTP/1.1 headers are
13/// case-insensitive, so lowercase works universally.
14pub const MCP_PROTOCOL_VERSION_HEADER: &str = "mcp-protocol-version";
15
16/// Header name for MCP session ID.
17///
18/// Note: HTTP/2 requires lowercase header names. HTTP/1.1 headers are
19/// case-insensitive, so lowercase works universally.
20pub const MCP_SESSION_ID_HEADER: &str = "mcp-session-id";
21
22/// Default maximum message size (16 MB).
23pub const DEFAULT_MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
24
25/// Configuration for HTTP transport.
26#[derive(Debug, Clone)]
27pub struct HttpTransportConfig {
28    /// Base URL for the MCP endpoint.
29    pub base_url: String,
30    /// Optional session ID for resuming sessions.
31    pub session_id: Option<String>,
32    /// Connection timeout.
33    pub connect_timeout: Duration,
34    /// Request timeout.
35    pub request_timeout: Duration,
36    /// Whether to enable automatic SSE reconnection.
37    pub auto_reconnect: bool,
38    /// Maximum reconnection attempts.
39    pub max_reconnect_attempts: u32,
40    /// Custom headers to include in requests.
41    pub headers: Vec<(String, String)>,
42    /// Protocol version to use.
43    pub protocol_version: String,
44    /// Maximum message size in bytes.
45    pub max_message_size: usize,
46}
47
48impl HttpTransportConfig {
49    /// Create a new HTTP transport configuration.
50    #[must_use]
51    pub fn new(base_url: impl Into<String>) -> Self {
52        Self {
53            base_url: base_url.into(),
54            session_id: None,
55            connect_timeout: Duration::from_secs(30),
56            request_timeout: Duration::from_secs(60),
57            auto_reconnect: true,
58            max_reconnect_attempts: 5,
59            headers: Vec::new(),
60            protocol_version: MCP_PROTOCOL_VERSION.to_string(),
61            max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
62        }
63    }
64
65    /// Set the maximum message size.
66    #[must_use]
67    pub const fn with_max_message_size(mut self, size: usize) -> Self {
68        self.max_message_size = size;
69        self
70    }
71
72    /// Set the session ID for resuming a session.
73    #[must_use]
74    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
75        self.session_id = Some(session_id.into());
76        self
77    }
78
79    /// Set the connection timeout.
80    #[must_use]
81    pub const fn with_connect_timeout(mut self, timeout: Duration) -> Self {
82        self.connect_timeout = timeout;
83        self
84    }
85
86    /// Set the request timeout.
87    #[must_use]
88    pub const fn with_request_timeout(mut self, timeout: Duration) -> Self {
89        self.request_timeout = timeout;
90        self
91    }
92
93    /// Disable automatic reconnection.
94    #[must_use]
95    pub const fn without_auto_reconnect(mut self) -> Self {
96        self.auto_reconnect = false;
97        self
98    }
99
100    /// Set maximum reconnection attempts.
101    #[must_use]
102    pub const fn with_max_reconnect_attempts(mut self, attempts: u32) -> Self {
103        self.max_reconnect_attempts = attempts;
104        self
105    }
106
107    /// Add a custom header.
108    #[must_use]
109    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
110        self.headers.push((name.into(), value.into()));
111        self
112    }
113
114    /// Set the protocol version.
115    #[must_use]
116    pub fn with_protocol_version(mut self, version: impl Into<String>) -> Self {
117        self.protocol_version = version.into();
118        self
119    }
120}
121
122impl Default for HttpTransportConfig {
123    fn default() -> Self {
124        Self::new("http://localhost:8080/mcp")
125    }
126}
127
128/// Builder for HTTP transport.
129#[derive(Debug, Default)]
130pub struct HttpTransportBuilder {
131    /// The configuration being built.
132    pub(crate) config: HttpTransportConfig,
133}
134
135impl HttpTransportBuilder {
136    /// Create a new builder with the given base URL.
137    #[must_use]
138    pub fn new(base_url: impl Into<String>) -> Self {
139        Self {
140            config: HttpTransportConfig::new(base_url),
141        }
142    }
143
144    /// Set the session ID.
145    #[must_use]
146    pub fn session_id(mut self, session_id: impl Into<String>) -> Self {
147        self.config.session_id = Some(session_id.into());
148        self
149    }
150
151    /// Set the connection timeout.
152    #[must_use]
153    pub const fn connect_timeout(mut self, timeout: Duration) -> Self {
154        self.config.connect_timeout = timeout;
155        self
156    }
157
158    /// Set the request timeout.
159    #[must_use]
160    pub const fn request_timeout(mut self, timeout: Duration) -> Self {
161        self.config.request_timeout = timeout;
162        self
163    }
164
165    /// Add a custom header.
166    #[must_use]
167    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
168        self.config.headers.push((name.into(), value.into()));
169        self
170    }
171
172    /// Disable automatic reconnection.
173    #[must_use]
174    pub const fn no_auto_reconnect(mut self) -> Self {
175        self.config.auto_reconnect = false;
176        self
177    }
178}