Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

config: string-or-vec deserializer for AI_MEMORY_ALLOWED_HOSTS

AkitaOnRails47dc57a07d42
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index a59f7645..f1dc9197 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -143,22 +143,13 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
let cancel = CancellationToken::new();
let server_clone = server.clone();
// `Host`-header allowlist for rmcp's DNS-rebinding guard.
- // Precedence: AI_MEMORY_ALLOWED_HOSTS env (comma-separated)
- // overrides config.allowed_hosts entirely; both default to
- // loopback. Matches the AI_MEMORY_AUTH_TOKEN env-overrides-
- // config pattern just below.
- let allowed_hosts: Vec<String> = std::env::var("AI_MEMORY_ALLOWED_HOSTS")
- .ok()
- .filter(|s| !s.is_empty())
- .map(|s| {
- s.split(',')
- .map(|x| x.trim().to_string())
- .filter(|x| !x.is_empty())
- .collect()
- })
- .unwrap_or_else(|| config.allowed_hosts.clone());
+ // Sourced from Config (which already handles the
+ // `AI_MEMORY_ALLOWED_HOSTS=a,b,c` env-string vs.
+ // config.toml sequence forms via the string-or-vec
+ // deserializer). Logged so operators can verify the
+ // effective list against what they intended.
info!(
- allowed_hosts = ?allowed_hosts,
+ allowed_hosts = ?config.allowed_hosts,
"MCP Host-header allowlist"
);
let mcp_service = StreamableHttpService::new(
@@ -166,7 +157,7 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
LocalSessionManager::default().into(),
StreamableHttpServerConfig::default()
.with_cancellation_token(cancel.child_token())
- .with_allowed_hosts(allowed_hosts),
+ .with_allowed_hosts(config.allowed_hosts.clone()),
);
let hooks = hook_router(HookState {
workspace_id: ws,
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index e741228c..e073559f 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -55,9 +55,36 @@ pub struct Config {
/// Default is loopback only; to expose ai-memory on a LAN
/// IP / `home.lan` / etc., add that authority here or pass it via
/// `AI_MEMORY_ALLOWED_HOSTS=host1,host2,…` at startup.
+ ///
+ /// Accepts either a TOML/JSON sequence (`["a","b"]`) or a
+ /// comma-separated string (`"a,b"`) for ergonomics — env vars
+ /// can't be sequences without ugly escaping.
+ #[serde(deserialize_with = "deserialize_string_or_vec")]
pub allowed_hosts: Vec<String>,
}
+/// Accept `Vec<String>` either as a real sequence (config.toml /
+/// JSON array) or as a comma-separated single string (env var).
+fn deserialize_string_or_vec<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
+where
+ D: serde::Deserializer<'de>,
+{
+ #[derive(Deserialize)]
+ #[serde(untagged)]
+ enum Either {
+ Single(String),
+ Many(Vec<String>),
+ }
+ Ok(match Either::deserialize(deserializer)? {
+ Either::Single(s) => s
+ .split(',')
+ .map(|x| x.trim().to_string())
+ .filter(|x| !x.is_empty())
+ .collect(),
+ Either::Many(v) => v,
+ })
+}
+
/// `[auth]` section of `config.toml`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]