1use crate::errors::{MCSError, Result};
2use crate::Transport;
3use std::sync::Arc;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum Durability {
18 Async,
19 Sync,
20}
21
22impl Durability {
23 pub const fn is_sync(self) -> bool {
24 matches!(self, Durability::Sync)
25 }
26}
27
28impl std::str::FromStr for Durability {
29 type Err = String;
30 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
31 match s {
32 "async" | "Async" => Ok(Durability::Async),
33 "sync" | "Sync" => Ok(Durability::Sync),
34 _ => Err(format!("unknown durability '{s}'; expected 'async' or 'sync'")),
35 }
36 }
37}
38
39#[derive(Debug, Clone)]
40pub struct Config {
41 pub memory_file_path: String,
42 pub transport: Transport,
43 pub bind_addr: String,
44 pub durability: Durability,
45 pub auth_token: Option<Arc<str>>,
49 pub mmap_size: i64,
50 pub lru_cache_size: usize,
51 pub read_pool_size: usize,
53}
54
55impl Config {
56 pub fn from_args(args: &super::Args) -> Result<Self> {
57 let memory_file_path = args
58 .memory_file
59 .clone()
60 .or_else(|| std::env::var("MEMORY_FILE_PATH").ok())
61 .unwrap_or_else(|| "memory.mcpmem".to_string());
62
63 let auth_token: Option<Arc<str>> = if let Some(t) = args.auth_token.clone() {
67 Some(Arc::from(t.as_str()))
68 } else if let Some(path) = args.auth_token_file.clone() {
69 let contents = std::fs::read_to_string(&path).map_err(|e| {
70 MCSError::InvalidParams(format!("failed to read --auth-token-file '{path}': {e}"))
71 })?;
72 let token = contents.trim();
73 if token.is_empty() {
74 return Err(MCSError::InvalidParams(format!(
75 "--auth-token-file '{path}' is empty; refusing to start with auth disabled"
76 )));
77 }
78 Some(Arc::from(token))
79 } else {
80 std::env::var("MCP_MEMORY_AUTH_TOKEN")
81 .ok()
82 .filter(|t| !t.is_empty())
83 .map(|t| Arc::from(t.as_str()))
84 };
85
86 let durability = if let Ok(env) = std::env::var("MCP_MEMORY_DURABILITY") {
87 env.parse().unwrap_or_else(|e| {
88 tracing::warn!("MCP_MEMORY_DURABILITY parse failed: {e}; falling back to Async");
89 Durability::Async
90 })
91 } else {
92 Durability::Async
93 };
94
95 Ok(Config {
96 memory_file_path,
97 transport: args.transport,
98 bind_addr: args.bind.clone(),
99 durability,
100 auth_token,
101 mmap_size: args.mmap_size,
102 lru_cache_size: args.lru_cache_size,
103 read_pool_size: args.read_pool_size.max(1),
104 })
105 }
106}
107
108impl Default for Config {
109 fn default() -> Self {
110 Self {
111 memory_file_path: "memory.mcpmem".to_string(),
112 transport: Transport::Stdio,
113 bind_addr: "127.0.0.1:8080".to_string(),
114 durability: Durability::Async,
115 auth_token: None,
116 mmap_size: 268435456,
117 lru_cache_size: 10000,
118 read_pool_size: 4,
119 }
120 }
121}
122
123