mcpkit_transport/http/
config.rs1use std::time::Duration;
4
5pub const MCP_PROTOCOL_VERSION: &str = "2025-11-25";
9
10pub const MCP_PROTOCOL_VERSION_HEADER: &str = "mcp-protocol-version";
15
16pub const MCP_SESSION_ID_HEADER: &str = "mcp-session-id";
21
22pub const DEFAULT_MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
24
25#[derive(Debug, Clone)]
27pub struct HttpTransportConfig {
28 pub base_url: String,
30 pub session_id: Option<String>,
32 pub connect_timeout: Duration,
34 pub request_timeout: Duration,
36 pub auto_reconnect: bool,
38 pub max_reconnect_attempts: u32,
40 pub headers: Vec<(String, String)>,
42 pub protocol_version: String,
44 pub max_message_size: usize,
46}
47
48impl HttpTransportConfig {
49 #[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 #[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 #[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 #[must_use]
81 pub const fn with_connect_timeout(mut self, timeout: Duration) -> Self {
82 self.connect_timeout = timeout;
83 self
84 }
85
86 #[must_use]
88 pub const fn with_request_timeout(mut self, timeout: Duration) -> Self {
89 self.request_timeout = timeout;
90 self
91 }
92
93 #[must_use]
95 pub const fn without_auto_reconnect(mut self) -> Self {
96 self.auto_reconnect = false;
97 self
98 }
99
100 #[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 #[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 #[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#[derive(Debug, Default)]
130pub struct HttpTransportBuilder {
131 pub(crate) config: HttpTransportConfig,
133}
134
135impl HttpTransportBuilder {
136 #[must_use]
138 pub fn new(base_url: impl Into<String>) -> Self {
139 Self {
140 config: HttpTransportConfig::new(base_url),
141 }
142 }
143
144 #[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 #[must_use]
153 pub const fn connect_timeout(mut self, timeout: Duration) -> Self {
154 self.config.connect_timeout = timeout;
155 self
156 }
157
158 #[must_use]
160 pub const fn request_timeout(mut self, timeout: Duration) -> Self {
161 self.config.request_timeout = timeout;
162 self
163 }
164
165 #[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 #[must_use]
174 pub const fn no_auto_reconnect(mut self) -> Self {
175 self.config.auto_reconnect = false;
176 self
177 }
178}