micromegas/servers/mcp/
config.rs1use micromegas_tracing::prelude::*;
2use std::path::PathBuf;
3
4pub const ABSOLUTE_MAX_ROWS: usize = 10_000;
6pub const ABSOLUTE_MAX_BYTES: usize = 1024 * 1024;
7
8pub const DEFAULT_ROWS: usize = 1_000;
9pub const DEFAULT_MAX_BYTES: usize = 128 * 1024;
10pub const DEFAULT_MAX_REQUEST_BODY_BYTES: usize = 1024 * 1024;
11
12const DEFAULT_ALLOWED_HOSTS: &[&str] = &["localhost", "127.0.0.1", "::1"];
13
14#[derive(Debug, Clone)]
16pub struct McpConfig {
17 pub enabled: bool,
18 pub path: String,
20 pub flightsql_url: Option<String>,
22 pub default_rows: usize,
24 pub max_rows: usize,
26 pub max_bytes: usize,
28 pub skills_dir: Option<PathBuf>,
29 pub allowed_hosts: Vec<String>,
30 pub allowed_origins: Vec<String>,
32 pub max_request_body_bytes: usize,
33}
34
35impl Default for McpConfig {
36 fn default() -> Self {
37 Self {
38 enabled: false,
39 path: "/mcp".to_string(),
40 flightsql_url: None,
41 default_rows: DEFAULT_ROWS,
42 max_rows: ABSOLUTE_MAX_ROWS,
43 max_bytes: DEFAULT_MAX_BYTES,
44 skills_dir: None,
45 allowed_hosts: DEFAULT_ALLOWED_HOSTS
46 .iter()
47 .map(|s| s.to_string())
48 .collect(),
49 allowed_origins: vec![],
50 max_request_body_bytes: DEFAULT_MAX_REQUEST_BODY_BYTES,
51 }
52 }
53}
54
55fn parse_list(value: &str) -> Vec<String> {
56 value
57 .split(',')
58 .map(|s| s.trim())
59 .filter(|s| !s.is_empty())
60 .map(|s| s.to_string())
61 .collect()
62}
63
64fn parse_bool(value: &str) -> bool {
65 matches!(value.trim().to_ascii_lowercase().as_str(), "true" | "1")
66}
67
68impl McpConfig {
69 pub fn from_env(base_path: &str) -> Self {
71 Self::from_lookup(base_path, |key| std::env::var(key).ok())
72 }
73
74 pub fn from_lookup(base_path: &str, lookup: impl Fn(&str) -> Option<String>) -> Self {
76 let mut config = Self {
77 path: format!("{}/mcp", base_path.trim_end_matches('/')),
78 ..Self::default()
79 };
80 config.enabled = lookup("MICROMEGAS_MCP_ENABLED")
81 .map(|v| parse_bool(&v))
82 .unwrap_or(false);
83 config.flightsql_url = lookup("MICROMEGAS_MCP_FLIGHTSQL_URL").filter(|v| !v.is_empty());
84 let number = |key: &str| {
85 let raw = lookup(key)?;
86 match raw.trim().parse::<usize>() {
87 Ok(n) => Some(n),
88 Err(_) => {
89 warn!("Invalid {key} value '{raw}', using default");
90 None
91 }
92 }
93 };
94 if let Some(n) = number("MICROMEGAS_MCP_MAX_ROWS") {
95 config.max_rows = n;
96 }
97 if let Some(n) = number("MICROMEGAS_MCP_DEFAULT_ROWS") {
98 config.default_rows = n;
99 }
100 if let Some(n) = number("MICROMEGAS_MCP_MAX_BYTES") {
101 config.max_bytes = n;
102 }
103 config.skills_dir = lookup("MICROMEGAS_MCP_SKILLS_DIR")
104 .filter(|v| !v.is_empty())
105 .map(PathBuf::from);
106 if let Some(hosts) = lookup("MICROMEGAS_MCP_ALLOWED_HOSTS") {
107 config.allowed_hosts = parse_list(&hosts);
108 }
109 if let Some(origins) = lookup("MICROMEGAS_MCP_ALLOWED_ORIGINS") {
110 config.allowed_origins = parse_list(&origins);
111 }
112 config.clamp()
113 }
114
115 pub fn clamp(mut self) -> Self {
117 self.max_rows = self.max_rows.clamp(1, ABSOLUTE_MAX_ROWS);
118 self.default_rows = self.default_rows.clamp(1, self.max_rows);
119 self.max_bytes = self.max_bytes.clamp(1, ABSOLUTE_MAX_BYTES);
120 self
121 }
122
123 pub fn effective_limit(&self, requested: Option<usize>) -> usize {
125 requested
126 .unwrap_or(self.default_rows)
127 .clamp(1, self.max_rows)
128 }
129}