Skip to main content

mcp_context_server/
config.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4/// Default timeout for tool operations (30 seconds).
5const DEFAULT_TOOL_TIMEOUT_SECS: u64 = 30;
6
7/// Server configuration loaded from environment variables.
8#[derive(Debug, Clone)]
9pub struct ServerConfig {
10    pub cache_root: PathBuf,
11    pub tool_timeout: Duration,
12}
13
14impl ServerConfig {
15    /// Load configuration from environment.
16    ///
17    /// - `CONTEXT_CACHE_ROOT` (required) — root directory for caches
18    /// - `CONTEXT_TOOL_TIMEOUT_SECS` (optional, default 30) — max seconds per tool call
19    pub fn from_env() -> Result<Self, String> {
20        let cache_root = std::env::var("CONTEXT_CACHE_ROOT")
21            .map(PathBuf::from)
22            .map_err(|_| "CONTEXT_CACHE_ROOT environment variable is not set".to_string())?;
23
24        let tool_timeout_secs = match std::env::var("CONTEXT_TOOL_TIMEOUT_SECS") {
25            Ok(val) => val
26                .parse::<u64>()
27                .map_err(|_| "CONTEXT_TOOL_TIMEOUT_SECS must be a positive integer".to_string())?,
28            Err(_) => DEFAULT_TOOL_TIMEOUT_SECS,
29        };
30
31        Ok(Self {
32            cache_root,
33            tool_timeout: Duration::from_secs(tool_timeout_secs),
34        })
35    }
36}