mcp_runner/config/validator.rs
1//! Configuration validation module.
2//!
3//! This module provides functions for validating MCP Runner configurations,
4//! ensuring that configuration values are valid and consistent before they
5//! are used to create and manage server processes.
6
7use crate::config::{Config, SSEProxyConfig, ServerConfig};
8use crate::error::{Error, Result};
9use std::collections::HashMap;
10
11/// Validates a server configuration.
12///
13/// This function checks that the server configuration is valid by ensuring:
14/// - The command is not empty
15/// - Any required arguments are present
16///
17/// # Arguments
18///
19/// * `config` - The server configuration to validate
20///
21/// # Returns
22///
23/// A `Result` indicating success or failure
24pub fn validate_server_config(config: &ServerConfig) -> Result<()> {
25 // Command must not be empty
26 if config.command.is_empty() {
27 return Err(Error::ConfigValidation(
28 "Server command cannot be empty".to_string(),
29 ));
30 }
31
32 Ok(())
33}
34
35/// Validates a collection of server configurations.
36///
37/// This function validates each server configuration in the collection.
38///
39/// # Arguments
40///
41/// * `configs` - The server configurations to validate
42///
43/// # Returns
44///
45/// A `Result` indicating success or failure
46pub fn validate_server_configs(configs: &HashMap<String, ServerConfig>) -> Result<()> {
47 for (name, config) in configs {
48 validate_server_config(config).map_err(|e| {
49 Error::ConfigValidation(format!(
50 "Invalid configuration for server '{}': {}",
51 name, e
52 ))
53 })?;
54 }
55
56 Ok(())
57}
58
59/// Validates an SSE proxy configuration.
60///
61/// This function checks that the SSE proxy configuration is valid.
62/// Currently, there's no specific validation needed, but this function
63/// exists for future validation requirements.
64///
65/// # Arguments
66///
67/// * `config` - The SSE proxy configuration to validate
68///
69/// # Returns
70///
71/// A `Result` indicating success or failure
72pub fn validate_sse_proxy_config(_config: &SSEProxyConfig) -> Result<()> {
73 // Currently, there's no validation needed for SSE proxy config
74 // This function exists for future validation needs
75
76 Ok(())
77}
78
79/// Validates a complete configuration.
80///
81/// This function validates the entire configuration by checking:
82/// - Server configurations are valid
83/// - SSE proxy configuration is valid if present
84///
85/// # Arguments
86///
87/// * `config` - The configuration to validate
88///
89/// # Returns
90///
91/// A `Result` indicating success or failure
92pub fn validate_config(config: &HashMap<String, ServerConfig>) -> Result<()> {
93 validate_server_configs(config)
94}
95
96/// Validates a complete Config object
97///
98/// This validates both the server configurations and SSE proxy config if present
99///
100/// # Arguments
101///
102/// * `config` - The Config object to validate
103///
104/// # Returns
105///
106/// A `Result` indicating success or failure
107pub fn validate_full_config(config: &Config) -> Result<()> {
108 // Validate server configs
109 validate_server_configs(&config.mcp_servers)?;
110
111 // Validate SSE proxy config if present
112 if let Some(proxy_config) = &config.sse_proxy {
113 validate_sse_proxy_config(proxy_config)?;
114 }
115
116 Ok(())
117}