mcp_core/server/
config.rs

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
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::{prompts::Prompt, tools::ToolType};

// Server Configuration
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerConfig {
    pub server: ServerSettings,
    pub resources: ResourceSettings,
    pub security: SecuritySettings,
    pub logging: LoggingSettings,
    pub tool_settings: ToolSettings,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<ToolType>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub prompts: Vec<Prompt>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerSettings {
    pub name: String,
    pub version: String,
    pub transport: TransportType,
    pub host: String,
    pub port: u16,
    pub max_connections: usize,
    pub timeout_ms: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceSettings {
    pub root_path: PathBuf,
    pub allowed_schemes: Vec<String>,
    pub max_file_size: usize,
    pub enable_templates: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SecuritySettings {
    pub enable_auth: bool,
    pub token_secret: Option<String>,
    pub rate_limit: RateLimitSettings,
    pub allowed_origins: Vec<String>,
}

impl Default for SecuritySettings {
    fn default() -> Self {
        SecuritySettings {
            enable_auth: false,
            token_secret: None,
            rate_limit: RateLimitSettings {
                requests_per_minute: 60,
                burst_size: 10,
            },
            allowed_origins: vec!["*".to_string()],
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RateLimitSettings {
    pub requests_per_minute: u32,
    pub burst_size: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoggingSettings {
    pub level: String,
    pub file: Option<PathBuf>,
    pub format: LogFormat,
}

impl Default for LoggingSettings {
    fn default() -> Self {
        LoggingSettings {
            level: "info".to_string(),
            file: None,
            format: LogFormat::Pretty,
        }
    }
}

// Add new tool settings struct
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolSettings {
    pub enabled: bool,
    pub require_confirmation: bool,
    pub allowed_tools: Vec<String>,
    pub max_execution_time_ms: u64,
    pub rate_limit: RateLimitSettings,
}

impl Default for ToolSettings {
    fn default() -> Self {
        ToolSettings {
            enabled: true,
            require_confirmation: true,
            allowed_tools: vec!["*".to_string()], // Allow all tools by default
            max_execution_time_ms: 30000,         // 30 seconds
            rate_limit: RateLimitSettings {
                requests_per_minute: 30,
                burst_size: 5,
            },
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TransportType {
    Stdio,
    Sse,
    WebSocket,
    Unix,
}

impl From<&str> for TransportType {
    fn from(s: &str) -> Self {
        match s {
            "stdio" => TransportType::Stdio,
            "sse" => TransportType::Sse,
            "ws" => TransportType::WebSocket,
            "unix" => TransportType::Unix,
            _ => TransportType::Stdio,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
    Json,
    Pretty,
    Compact,
}

impl Default for ServerConfig {
    fn default() -> Self {
        ServerConfig {
            server: ServerSettings {
                name: "mcp-server".to_string(),
                version: env!("CARGO_PKG_VERSION").to_string(),
                transport: TransportType::Stdio,
                host: "127.0.0.1".to_string(),
                port: 3000,
                max_connections: 100,
                timeout_ms: 30000,
            },
            resources: ResourceSettings {
                root_path: PathBuf::from("./resources"),
                allowed_schemes: vec!["file".to_string()],
                max_file_size: 10 * 1024 * 1024, // 10MB
                enable_templates: true,
            },
            security: SecuritySettings {
                enable_auth: false,
                token_secret: None,
                rate_limit: RateLimitSettings {
                    requests_per_minute: 60,
                    burst_size: 10,
                },
                allowed_origins: vec!["*".to_string()],
            },
            logging: LoggingSettings {
                level: "info".to_string(),
                file: None,
                format: LogFormat::Pretty,
            },
            tool_settings: ToolSettings {
                enabled: true,
                require_confirmation: true,
                allowed_tools: vec!["*".to_string()], // Allow all tools by default
                max_execution_time_ms: 30000,         // 30 seconds
                rate_limit: RateLimitSettings {
                    requests_per_minute: 30,
                    burst_size: 5,
                },
            },
            tools: vec![],
            prompts: vec![],
        }
    }
}