mcp-probe-core 0.3.0

Core MCP (Model Context Protocol) types, traits, and transport implementations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Transport configuration system for MCP clients.
//!
//! This module provides a type-safe configuration system for all MCP transport types.
//! Configurations can be created programmatically or loaded from files.
//!
//! # Examples
//!
//! ```rust
//! use mcp_probe_core::transport::{TransportConfig, StdioConfig, HttpSseConfig};
//! use std::time::Duration;
//!
//! // Stdio transport configuration
//! let stdio_config = TransportConfig::Stdio(StdioConfig {
//!     command: "python".to_string(),
//!     args: vec!["server.py".to_string()],
//!     working_dir: Some("/path/to/server".to_string()),
//!     timeout: Duration::from_secs(30),
//!     environment: Default::default(),
//! });
//!
//! // HTTP+SSE transport configuration  
//! let http_config = TransportConfig::HttpSse(HttpSseConfig {
//!     base_url: "https://api.example.com/mcp".parse().unwrap(),
//!     timeout: Duration::from_secs(60),
//!     headers: Default::default(),
//!     auth: None,
//! });
//! ```

use crate::error::{ConfigError, McpResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use url::Url;

/// Transport configuration enum supporting all MCP transport types.
///
/// This enum provides type-safe configuration for different transport mechanisms,
/// ensuring that each transport gets the configuration parameters it needs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TransportConfig {
    /// Local process communication via stdio
    Stdio(StdioConfig),

    /// Remote HTTP server with Server-Sent Events
    HttpSse(HttpSseConfig),

    /// Full-duplex HTTP streaming
    HttpStream(HttpStreamConfig),
}

impl TransportConfig {
    /// Create a new stdio transport configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::transport::TransportConfig;
    ///
    /// let config = TransportConfig::stdio("python", &["server.py"]);
    /// ```
    pub fn stdio(command: impl Into<String>, args: &[impl ToString]) -> Self {
        Self::Stdio(StdioConfig {
            command: command.into(),
            args: args.iter().map(|s| s.to_string()).collect(),
            working_dir: None,
            timeout: Duration::from_secs(30),
            environment: HashMap::new(),
        })
    }

    /// Create a new HTTP+SSE transport configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::transport::TransportConfig;
    ///
    /// let config = TransportConfig::http_sse("https://api.example.com/mcp").unwrap();
    /// ```
    pub fn http_sse(base_url: impl AsRef<str>) -> McpResult<Self> {
        let url = base_url
            .as_ref()
            .parse()
            .map_err(|e| ConfigError::InvalidValue {
                parameter: "base_url".to_string(),
                value: base_url.as_ref().to_string(),
                reason: format!("Invalid URL: {}", e),
            })?;

        Ok(Self::HttpSse(HttpSseConfig {
            base_url: url,
            timeout: Duration::from_secs(60),
            headers: HashMap::new(),
            auth: None,
        }))
    }

    /// Create a new HTTP streaming transport configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::transport::TransportConfig;
    ///
    /// let config = TransportConfig::http_stream("https://stream.example.com/mcp").unwrap();
    /// ```
    pub fn http_stream(base_url: impl AsRef<str>) -> McpResult<Self> {
        let url = base_url
            .as_ref()
            .parse()
            .map_err(|e| ConfigError::InvalidValue {
                parameter: "base_url".to_string(),
                value: base_url.as_ref().to_string(),
                reason: format!("Invalid URL: {}", e),
            })?;

        Ok(Self::HttpStream(HttpStreamConfig {
            base_url: url,
            timeout: Duration::from_secs(300),
            headers: HashMap::new(),
            auth: None,
            compression: true,
            flow_control_window: 65536,
        }))
    }

    /// Get a human-readable name for this transport type.
    pub fn transport_type(&self) -> &'static str {
        match self {
            Self::Stdio(_) => "stdio",
            Self::HttpSse(_) => "http-sse",
            Self::HttpStream(_) => "http-stream",
        }
    }

    /// Validate the configuration and return any errors.
    pub fn validate(&self) -> McpResult<()> {
        match self {
            Self::Stdio(config) => config.validate(),
            Self::HttpSse(config) => config.validate(),
            Self::HttpStream(config) => config.validate(),
        }
    }

    /// Load configuration from a file.
    ///
    /// Supports JSON, YAML, and TOML formats based on file extension.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::transport::TransportConfig;
    ///
    /// let config = TransportConfig::from_file("config.json")?;
    /// # Ok::<(), mcp_probe_core::error::McpError>(())
    /// ```
    pub fn from_file(path: impl AsRef<std::path::Path>) -> McpResult<Self> {
        let path = path.as_ref();
        let content = std::fs::read_to_string(path).map_err(|_e| ConfigError::FileNotFound {
            path: path.display().to_string(),
        })?;

        let config: Self = match path.extension().and_then(|ext| ext.to_str()) {
            Some("json") => {
                serde_json::from_str(&content).map_err(|e| ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: e.to_string(),
                })?
            }
            Some("yaml") | Some("yml") => {
                serde_yaml::from_str(&content).map_err(|e| ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: e.to_string(),
                })?
            }
            Some("toml") => toml::from_str(&content).map_err(|e| ConfigError::InvalidFormat {
                path: path.display().to_string(),
                reason: e.to_string(),
            })?,
            _ => {
                return Err(ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: "Unsupported file format. Use .json, .yaml, or .toml".to_string(),
                }
                .into())
            }
        };

        config.validate()?;
        Ok(config)
    }

    /// Save configuration to a file.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::transport::TransportConfig;
    ///
    /// let config = TransportConfig::stdio("python", &["server.py"]);
    /// config.to_file("config.json")?;
    /// # Ok::<(), mcp_probe_core::error::McpError>(())
    /// ```
    pub fn to_file(&self, path: impl AsRef<std::path::Path>) -> McpResult<()> {
        let path = path.as_ref();
        let content = match path.extension().and_then(|ext| ext.to_str()) {
            Some("json") => {
                serde_json::to_string_pretty(self).map_err(|e| ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: e.to_string(),
                })?
            }
            Some("yaml") | Some("yml") => {
                serde_yaml::to_string(self).map_err(|e| ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: e.to_string(),
                })?
            }
            Some("toml") => toml::to_string(self).map_err(|e| ConfigError::InvalidFormat {
                path: path.display().to_string(),
                reason: e.to_string(),
            })?,
            _ => {
                return Err(ConfigError::InvalidFormat {
                    path: path.display().to_string(),
                    reason: "Unsupported file format. Use .json, .yaml, or .toml".to_string(),
                }
                .into())
            }
        };

        std::fs::write(path, content)?;

        Ok(())
    }
}

/// Configuration for stdio (local process) transport.
///
/// This transport spawns a local process and communicates via stdin/stdout.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StdioConfig {
    /// Command to execute (e.g., "python", "/usr/bin/node")
    pub command: String,

    /// Arguments to pass to the command
    pub args: Vec<String>,

    /// Working directory for the process (optional)
    pub working_dir: Option<String>,

    /// Timeout for process operations
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,

    /// Environment variables to set for the process
    pub environment: HashMap<String, String>,
}

impl StdioConfig {
    /// Create a new stdio configuration.
    pub fn new(command: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            args: Vec::new(),
            working_dir: None,
            timeout: Duration::from_secs(30),
            environment: HashMap::new(),
        }
    }

    /// Add an argument to the command.
    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Add multiple arguments to the command.
    pub fn args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.args.extend(args.into_iter().map(|s| s.into()));
        self
    }

    /// Set the working directory.
    pub fn working_dir(mut self, dir: impl Into<String>) -> Self {
        self.working_dir = Some(dir.into());
        self
    }

    /// Set the timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Add an environment variable.
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.environment.insert(key.into(), value.into());
        self
    }

    /// Validate the stdio configuration.
    pub fn validate(&self) -> McpResult<()> {
        if self.command.is_empty() {
            return Err(ConfigError::MissingParameter {
                parameter: "command".to_string(),
            }
            .into());
        }

        if let Some(ref dir) = self.working_dir {
            if !PathBuf::from(dir).exists() {
                return Err(ConfigError::InvalidValue {
                    parameter: "working_dir".to_string(),
                    value: dir.clone(),
                    reason: "Directory does not exist".to_string(),
                }
                .into());
            }
        }

        Ok(())
    }
}

/// Configuration for HTTP+SSE (Server-Sent Events) transport.
///
/// This transport uses HTTP requests for client-to-server communication
/// and Server-Sent Events for server-to-client communication.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpSseConfig {
    /// Base URL for the MCP server
    pub base_url: Url,

    /// Timeout for HTTP requests
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,

    /// Additional HTTP headers to include
    pub headers: HashMap<String, String>,

    /// Authentication configuration
    pub auth: Option<AuthConfig>,
}

impl HttpSseConfig {
    /// Create a new HTTP+SSE configuration.
    pub fn new(base_url: Url) -> Self {
        Self {
            base_url,
            timeout: Duration::from_secs(60),
            headers: HashMap::new(),
            auth: None,
        }
    }

    /// Set the timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Add an HTTP header.
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Set authentication configuration.
    pub fn auth(mut self, auth: AuthConfig) -> Self {
        self.auth = Some(auth);
        self
    }

    /// Validate the HTTP+SSE configuration.
    pub fn validate(&self) -> McpResult<()> {
        if self.base_url.scheme() != "http" && self.base_url.scheme() != "https" {
            return Err(ConfigError::InvalidValue {
                parameter: "base_url".to_string(),
                value: self.base_url.to_string(),
                reason: "URL must use http or https scheme".to_string(),
            }
            .into());
        }

        if let Some(ref auth) = self.auth {
            auth.validate()?;
        }

        Ok(())
    }
}

/// Configuration for HTTP streaming transport.
///
/// This transport uses full-duplex HTTP streaming for bidirectional communication.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpStreamConfig {
    /// Base URL for the MCP server
    pub base_url: Url,

    /// Timeout for streaming operations
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,

    /// Additional HTTP headers to include
    pub headers: HashMap<String, String>,

    /// Authentication configuration
    pub auth: Option<AuthConfig>,

    /// Enable compression for the stream
    pub compression: bool,

    /// Flow control window size
    pub flow_control_window: u32,
}

impl HttpStreamConfig {
    /// Create a new HTTP streaming configuration.
    pub fn new(base_url: Url) -> Self {
        Self {
            base_url,
            timeout: Duration::from_secs(300),
            headers: HashMap::new(),
            auth: None,
            compression: true,
            flow_control_window: 65536,
        }
    }

    /// Set the timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Add an HTTP header.
    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Set authentication configuration.
    pub fn auth(mut self, auth: AuthConfig) -> Self {
        self.auth = Some(auth);
        self
    }

    /// Enable or disable compression.
    pub fn compression(mut self, enabled: bool) -> Self {
        self.compression = enabled;
        self
    }

    /// Set the flow control window size.
    pub fn flow_control_window(mut self, size: u32) -> Self {
        self.flow_control_window = size;
        self
    }

    /// Validate the HTTP streaming configuration.
    pub fn validate(&self) -> McpResult<()> {
        if self.base_url.scheme() != "http" && self.base_url.scheme() != "https" {
            return Err(ConfigError::InvalidValue {
                parameter: "base_url".to_string(),
                value: self.base_url.to_string(),
                reason: "URL must use http or https scheme".to_string(),
            }
            .into());
        }

        if self.flow_control_window == 0 {
            return Err(ConfigError::InvalidValue {
                parameter: "flow_control_window".to_string(),
                value: self.flow_control_window.to_string(),
                reason: "Flow control window must be greater than 0".to_string(),
            }
            .into());
        }

        if let Some(ref auth) = self.auth {
            auth.validate()?;
        }

        Ok(())
    }
}

/// Authentication configuration for HTTP-based transports.
///
/// Supports various authentication schemes including basic auth,
/// bearer tokens, and OAuth 2.0.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum AuthConfig {
    /// HTTP Basic Authentication
    Basic { username: String, password: String },

    /// Bearer token authentication
    Bearer { token: String },

    /// OAuth 2.0 authentication
    OAuth {
        client_id: String,
        client_secret: String,
        token_url: Url,
        scope: Option<String>,
    },

    /// Custom header-based authentication
    Header { name: String, value: String },
}

impl AuthConfig {
    /// Create a new basic authentication configuration.
    pub fn basic(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self::Basic {
            username: username.into(),
            password: password.into(),
        }
    }

    /// Create a new bearer token authentication configuration.
    pub fn bearer(token: impl Into<String>) -> Self {
        Self::Bearer {
            token: token.into(),
        }
    }

    /// Create a new OAuth 2.0 authentication configuration.
    pub fn oauth(
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        token_url: Url,
        scope: Option<String>,
    ) -> Self {
        Self::OAuth {
            client_id: client_id.into(),
            client_secret: client_secret.into(),
            token_url,
            scope,
        }
    }

    /// Create a new custom header authentication configuration.
    pub fn header(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self::Header {
            name: name.into(),
            value: value.into(),
        }
    }

    /// Validate the authentication configuration.
    pub fn validate(&self) -> McpResult<()> {
        match self {
            Self::Basic { username, password } => {
                if username.is_empty() || password.is_empty() {
                    return Err(ConfigError::InvalidValue {
                        parameter: "auth".to_string(),
                        value: "basic".to_string(),
                        reason: "Username and password cannot be empty".to_string(),
                    }
                    .into());
                }
            }
            Self::Bearer { token } => {
                if token.is_empty() {
                    return Err(ConfigError::InvalidValue {
                        parameter: "auth".to_string(),
                        value: "bearer".to_string(),
                        reason: "Token cannot be empty".to_string(),
                    }
                    .into());
                }
            }
            Self::OAuth {
                client_id,
                client_secret,
                token_url,
                ..
            } => {
                if client_id.is_empty() || client_secret.is_empty() {
                    return Err(ConfigError::InvalidValue {
                        parameter: "auth".to_string(),
                        value: "oauth".to_string(),
                        reason: "Client ID and secret cannot be empty".to_string(),
                    }
                    .into());
                }
                if token_url.scheme() != "https" {
                    return Err(ConfigError::InvalidValue {
                        parameter: "token_url".to_string(),
                        value: token_url.to_string(),
                        reason: "OAuth token URL must use HTTPS".to_string(),
                    }
                    .into());
                }
            }
            Self::Header { name, value } => {
                if name.is_empty() || value.is_empty() {
                    return Err(ConfigError::InvalidValue {
                        parameter: "auth".to_string(),
                        value: "header".to_string(),
                        reason: "Header name and value cannot be empty".to_string(),
                    }
                    .into());
                }
            }
        }
        Ok(())
    }
}