mcpmesh_node/config.rs
1//! The `config.toml` model. Every table and key here is real, implemented surface —
2//! docs/config.md is the operator-facing reference for all of it.
3use figment::{
4 Figment,
5 providers::{Format, Toml},
6};
7use serde::Deserialize;
8use std::collections::BTreeMap;
9use std::path::PathBuf;
10
11#[derive(Debug, Default, Deserialize)]
12#[serde(default)]
13pub struct Config {
14 pub identity: IdentityCfg,
15 pub network: NetworkCfg,
16 pub limits: LimitsCfg,
17 /// Roster-mode `[roster]` tunables: the degraded-expiry grace window, the roster URL +
18 /// poll interval, and the freshness bound — one `RosterState` machine consumes them all.
19 pub roster: RosterCfg,
20 /// `[services.<name>]` registry — each entry is a served MCP server plus its allow
21 /// list. Peers do NOT live in config; they live in the daemon's state store, so
22 /// there is no `[peers]` table here.
23 pub services: std::collections::BTreeMap<String, ServiceCfg>,
24}
25
26/// A `[services.<name>]` entry: exactly one backend kind (`run` xor `socket`) plus the
27/// nicknames/groups admitted to it. The xor is validated at access time via
28/// [`ServiceCfg::backend_result`] rather than at parse time, so a malformed entry is a
29/// per-service error, not a whole-config load failure.
30#[derive(Debug, Default, Deserialize)]
31#[serde(default)]
32pub struct ServiceCfg {
33 /// `run`: spawn this command per session (a stdio MCP server).
34 pub run: Option<Vec<String>>,
35 /// `socket`: dial this local UDS (an already-running MCP server).
36 pub socket: Option<String>,
37 /// STABLE principals admitted to this service (b64u:/eid:/roster names, #38 — never display nicknames).
38 pub allow: Vec<String>,
39 /// Per-service env vars for a `run` backend (#51). The `MCPMESH_PEER_*` identity vars win
40 /// over these. Ignored for a `socket` backend. Default empty.
41 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
42 pub env: BTreeMap<String, String>,
43 /// Working directory for a `run` backend (#51). Default: inherit the daemon's cwd.
44 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub cwd: Option<String>,
46}
47
48/// The resolved backend kind of a [`ServiceCfg`], borrowing the config as slices (no
49/// clone). `&[String]`/`&str` rather than `&Vec`/`&String` — idiomatic and gives the
50/// daemon's backend builders the most flexible borrow.
51#[derive(Debug)]
52pub enum Backend<'a> {
53 Run(&'a [String]),
54 Socket(&'a str),
55}
56
57impl ServiceCfg {
58 /// Resolve the backend, enforcing exactly-one-of `run`/`socket`. Both or neither is an
59 /// error — surfaced to the operator, never a silent default.
60 #[allow(dead_code)] // consumed by the daemon service wiring
61 pub fn backend_result(&self) -> Result<Backend<'_>, String> {
62 match (&self.run, &self.socket) {
63 (Some(cmd), None) => Ok(Backend::Run(cmd.as_slice())),
64 (None, Some(p)) => Ok(Backend::Socket(p.as_str())),
65 (Some(_), Some(_)) => Err("service has both run and socket".into()),
66 (None, None) => Err("service has neither run nor socket".into()),
67 }
68 }
69}
70
71#[derive(Debug, Default, Deserialize)]
72#[serde(default)]
73pub struct IdentityCfg {
74 pub device_key: Option<PathBuf>, // None → paths::default_device_key_path()
75 /// This device's suggested name for itself, carried in a minted pairing invite.
76 /// `None` → the daemon defaults to a short fingerprint of the endpoint id.
77 /// Additive (`#[serde(default)]` at the struct level).
78 pub nickname: Option<String>,
79 /// Roster mode: the org id this node joined (pinned at install/join).
80 pub org_id: Option<String>,
81 /// Roster mode: the pinned org-root public key, `b64u:`. The single trust anchor
82 /// roster signatures verify against. Pinned on first roster install / `join`.
83 pub org_root_pk: Option<String>,
84 /// Roster mode: this node's stable user_id in the org. Pinned at `join` (proposed)
85 /// and reconciled to the roster's authoritative value once installed.
86 pub user_id: Option<String>,
87 /// Roster mode: path to this person's user key. Minted by `join`; binds this
88 /// person's devices. `None` → paths::default_user_key_path() when needed.
89 pub user_key: Option<PathBuf>,
90}
91
92/// `[network]`. The knobs are exactly what `daemon::net_plan` implements —
93/// no aspirational surface:
94/// - `relay_mode = "default" | "custom" | "disabled"`. `"custom"` requires `relay_urls`
95/// (self-hosted iroh relays); `"disabled"` is the HERMETIC mode — no relay AND no
96/// discovery (localhost/tests).
97/// - `discovery_mode = "default" | "custom"`. `"custom"` requires `discovery_urls` —
98/// self-hosted pkarr relay URLs (e.g. an iroh-dns-server), used for BOTH publishing and
99/// resolving peer addresses in place of n0's DNS/pkarr. Ignored (off) when
100/// `relay_mode = "disabled"`.
101///
102/// Unknown modes or a `custom` without URLs are startup ERRORS (`net_plan`), never a silent
103/// fallback — a metadata-privacy knob must not quietly revert to public infrastructure.
104#[derive(Debug, Clone, Deserialize)]
105#[serde(default)]
106pub struct NetworkCfg {
107 pub relay_mode: String,
108 /// Self-hosted relay URLs, required when `relay_mode = "custom"`.
109 pub relay_urls: Vec<String>,
110 pub discovery_mode: String,
111 /// Self-hosted pkarr relay URLs, required when `discovery_mode = "custom"`.
112 pub discovery_urls: Vec<String>,
113 /// TESTING ONLY (#116): force application data over the RELAY even when a direct path exists.
114 ///
115 /// Requires the `unstable-relay-only` cargo feature. Without it this field still PARSES — a
116 /// config must stay portable between a test build and a production one — but is ignored with a
117 /// `warn!`. It is never a startup error: a testing switch must not brick a node, and it must
118 /// never be ignored SILENTLY, because believing you tested the relay when you did not is the
119 /// exact failure #116 reports.
120 ///
121 /// Selects the relay path; it does NOT prevent hole-punching (that is socket-level behaviour a
122 /// `PathSelector` cannot reach). A direct path may still form — it simply never carries data,
123 /// and `status` reports `relay` because #64 derives the path from `is_selected()`.
124 pub relay_only: bool,
125}
126impl Default for NetworkCfg {
127 fn default() -> Self {
128 Self {
129 relay_mode: "default".into(),
130 relay_urls: Vec::new(),
131 discovery_mode: "default".into(),
132 discovery_urls: Vec::new(),
133 relay_only: false,
134 }
135 }
136}
137
138/// `[limits]`. NOTE — the frame cap is deliberately NOT here: the 16 MiB `max_frame`
139/// default is a fixed CONSTANT at each wire (`mcpmesh_net::endpoint` for the mesh,
140/// `ipc::MAX_FRAME_BYTES` for the control socket, `backends::MAX_FRAME_BYTES` for local MCP
141/// servers), not a config tunable. A `max_frame` config field existed historically but was never
142/// threaded into any `FrameReader` (dead surface); threading it into the mesh path would widen
143/// `mcpmesh-net`'s public API for no demonstrated need, so the field was removed instead (serde
144/// ignores an unknown `max_frame` key in existing configs).
145#[derive(Debug, Deserialize)]
146#[serde(default)]
147pub struct LimitsCfg {
148 pub rate_limit_per_min: u32,
149 pub max_inflight: u32,
150 pub max_sessions: u32,
151 /// Per-authenticated-endpoint app-blob BYTE budget, bytes per minute (#84a).
152 ///
153 /// **0 = unlimited, and that is the default**, so an existing deployment is unchanged on
154 /// upgrade. The pre-existing blob limiter counts CONNECTIONS, which cannot see one granted
155 /// peer re-pulling a 4 GB blob on each of 60 connections a minute; this bounds the bytes.
156 ///
157 /// A peer that exceeds it gets its transfer ABORTED (retryable), not paced — pacing holds the
158 /// request open and turns a bandwidth problem into an unbounded-concurrency one.
159 ///
160 /// **Use 0 or at least 32768** (two chunks); a value in `1..32768` is FLOORED to 32768.
161 ///
162 /// Admission reserves one chunk before any bytes and the transfer then meters its own chunks,
163 /// so a sub-floor budget does not fail closed — it silently caps every servable blob at
164 /// roughly `budget - 16384` bytes and truncates anything larger. Measured: 20480 serves a
165 /// 4 KiB blob and nothing bigger. Two earlier drafts of this comment got that wrong, first
166 /// recommending the bricking value and then claiming it failed closed.
167 ///
168 /// Requires a restart: the limiter and the provider's event mask are both built once at boot.
169 pub blob_bytes_per_min: u64,
170}
171impl Default for LimitsCfg {
172 fn default() -> Self {
173 Self {
174 rate_limit_per_min: 120,
175 max_inflight: 16,
176 max_sessions: 4,
177 blob_bytes_per_min: 0, // unlimited: opt-in, no behaviour change on upgrade
178 }
179 }
180}
181
182/// The default degraded-expiry grace window (`[roster].grace_period` default "72h").
183/// A stale roster keeps serving for this window past `expires_at` (with a warning) before it
184/// stops granting roster identity. Kept here so [`RosterCfg::default`] and the parse fallback
185/// share one source; the gate mirrors it as `roster::gate::DEFAULT_GRACE_SECS`.
186const DEFAULT_GRACE_SECS: i64 = 72 * 3600;
187
188/// The default freshness bound (`[roster].max_staleness`, default "24h" = 86400s). A roster
189/// this node has not re-confirmed current within this window degrades on the SAME `RosterState`
190/// machine as expiry (warnings within `grace`, then serving stops) — bounding adversarial staleness at
191/// `max_staleness + grace` independent of `expires_at`. Shared by [`RosterCfg::default`] + the parse
192/// fallback.
193const DEFAULT_MAX_STALENESS_SECS: i64 = 24 * 3600;
194
195/// The `[roster]` config table. `grace_period` is the degraded-expiry grace window — how
196/// long a roster past `expires_at` keeps serving (degraded, warning) before it stops. Additive
197/// (`#[serde(default)]`): a config with no `[roster]` table gets the 72h default.
198#[derive(Debug, Deserialize)]
199#[serde(default)]
200pub struct RosterCfg {
201 /// Degraded-expiry grace window: `"72h"` / `"24h"` / plain seconds (default "72h").
202 pub grace_period: String,
203 /// The pinned roster URL for the HTTPS poll. Operator-managed static hosting; also how a
204 /// joiner bootstraps its FIRST roster. `None` → no URL poll (manual installs only).
205 /// Additive (`#[serde(default)]`): a config with no `url` key gets `None`.
206 pub url: Option<String>,
207 /// How often to poll `url` (default "1h"). Total-parse like `grace_period` — an
208 /// unparseable value falls back to the hourly default rather than disabling the poll.
209 pub poll_interval: String,
210 /// The freshness bound (default "24h"): how long this node may go without re-confirming
211 /// the installed roster current (via a TLS URL poll ≥ installed, a gossip install, or a
212 /// manual install) before it degrades on the SAME `RosterState` machine as expiry. Total-parse
213 /// like `grace_period` (an unparseable value falls back to the 24h default — a typo never disables
214 /// the bound). Additive (`#[serde(default)]`): a config with no `max_staleness` key gets 24h.
215 pub max_staleness: String,
216}
217impl Default for RosterCfg {
218 fn default() -> Self {
219 Self {
220 grace_period: "72h".into(),
221 url: None,
222 poll_interval: "1h".into(),
223 max_staleness: "24h".into(),
224 }
225 }
226}
227
228impl RosterCfg {
229 /// The grace window in SECONDS. An absent or unparseable `grace_period` falls back to the 72h
230 /// default rather than erroring — an operator typo must never disable degraded serving, and a
231 /// grace window is advisory, not a security bound (revocation is enforced regardless of
232 /// degraded state).
233 ///
234 /// Two paths degrade on the ONE `RosterState` machine (`RosterView::state`, Approved →
235 /// DegradedGrace → DegradedStopped): expiry (`expires_at` + THIS grace window) and freshness
236 /// (`last_confirmed` + `max_staleness`). Once DegradedStopped, the gate stops granting roster
237 /// identity (fail-closed — revocation is still enforced); within grace, serving continues
238 /// with a warning (`daemon::warn_if_degraded_grace`).
239 pub fn grace_seconds(&self) -> i64 {
240 parse_duration(&self.grace_period).unwrap_or(DEFAULT_GRACE_SECS)
241 }
242
243 /// The URL poll interval in SECONDS (default 3600). Like [`grace_seconds`](Self::grace_seconds)
244 /// it is TOTAL — an absent/unparseable value falls back to the hourly default rather than
245 /// erroring, so an operator typo slows the poll to hourly instead of disabling freshness.
246 pub fn poll_interval_seconds(&self) -> i64 {
247 parse_duration(&self.poll_interval).unwrap_or(3600)
248 }
249
250 /// The freshness bound in SECONDS (default 86400 = 24h). Like [`grace_seconds`](Self::grace_seconds)
251 /// it is TOTAL — an absent/unparseable value falls back to the 24h default rather than erroring, so
252 /// an operator typo tightens/loosens to 24h instead of disabling the freshness bound.
253 pub fn max_staleness_seconds(&self) -> i64 {
254 parse_duration(&self.max_staleness).unwrap_or(DEFAULT_MAX_STALENESS_SECS)
255 }
256}
257
258/// Parse a duration string to SECONDS: a `d`/`h`/`m`/`s` suffix (days/hours/minutes/seconds) or a
259/// bare number (seconds). Trim + suffix-strip + checked multiply; rejects a
260/// negative/overflowing/garbage value as `Err` (the caller supplies the
261/// default). `u64` parse then a checked `i64` conversion: a negative grace is meaningless, so `-1`
262/// fails the `u64` parse and falls back to the default rather than becoming a negative window.
263// Reached only by the accessors above and the `org create --expires` porcelain
264// (`enrollcmd`, the operator-managed validity window — now across the crate seam, hence
265// `pub`; still `#[doc(hidden)]` at the module level). Pure parser — no state.
266pub fn parse_duration(s: &str) -> Result<i64, String> {
267 let s = s.trim();
268 let (num, mult) = if let Some(n) = s.strip_suffix('d') {
269 (n, 24 * 3600)
270 } else if let Some(n) = s.strip_suffix('h') {
271 (n, 3600)
272 } else if let Some(n) = s.strip_suffix('m') {
273 (n, 60)
274 } else if let Some(n) = s.strip_suffix('s') {
275 (n, 1)
276 } else {
277 (s, 1)
278 };
279 num.trim()
280 .parse::<u64>()
281 .ok()
282 .and_then(|v| v.checked_mul(mult))
283 .and_then(|v| i64::try_from(v).ok())
284 .ok_or_else(|| format!("unparseable duration: {s}"))
285}
286
287// figment::Error is ~208 bytes; boxing it would churn the API for a cold path.
288#[allow(clippy::result_large_err)]
289impl Config {
290 #[allow(dead_code)] // exercised by unit tests; config-string entry point for later tooling
291 pub fn from_toml_str(s: &str) -> Result<Self, figment::Error> {
292 Figment::new().merge(Toml::string(s)).extract()
293 }
294
295 /// Missing file → defaults (first run); malformed file → Err.
296 /// Callers must surface the Err — swallowing it silently reverts user choices.
297 pub fn load(path: &std::path::Path) -> Result<Self, figment::Error> {
298 Figment::new().merge(Toml::file(path)).extract()
299 }
300}
301
302#[cfg(test)]
303mod tests {
304 use super::*;
305
306 #[test]
307 fn empty_file_yields_spec_defaults() {
308 let c = Config::from_toml_str("").unwrap();
309 assert_eq!(c.network.relay_mode, "default");
310 assert_eq!(c.network.discovery_mode, "default");
311 assert_eq!(c.limits.rate_limit_per_min, 120);
312 assert_eq!(c.limits.max_inflight, 16);
313 assert_eq!(c.limits.max_sessions, 4);
314 }
315
316 #[test]
317 fn values_override_defaults() {
318 let c = Config::from_toml_str(
319 "[network]\nrelay_mode = \"disabled\"\n[limits]\nrate_limit_per_min = 60\n",
320 )
321 .unwrap();
322 assert_eq!(c.network.relay_mode, "disabled");
323 assert_eq!(c.limits.rate_limit_per_min, 60);
324 assert_eq!(c.limits.max_inflight, 16);
325 }
326
327 /// A legacy config carrying the removed `max_frame` key still loads (serde ignores unknown
328 /// fields) — the frame cap is a fixed constant now, not a tunable (see the `LimitsCfg` doc).
329 #[test]
330 fn legacy_max_frame_key_is_ignored_not_an_error() {
331 let c =
332 Config::from_toml_str("[limits]\nmax_frame = \"1MiB\"\nmax_sessions = 2\n").unwrap();
333 assert_eq!(c.limits.max_sessions, 2);
334 }
335
336 /// The self-hosting knobs parse: `custom` modes with their URL lists. (Validation —
337 /// custom-without-urls, unknown modes — lives in `daemon::net_plan`, tested there.)
338 #[test]
339 fn network_relay_and_discovery_urls_parse() {
340 let c = Config::from_toml_str(
341 "[network]\nrelay_mode = \"custom\"\nrelay_urls = [\"https://relay.acme.com\"]\n\
342 discovery_mode = \"custom\"\ndiscovery_urls = [\"https://dns.acme.com/pkarr\"]\n",
343 )
344 .unwrap();
345 assert_eq!(c.network.relay_mode, "custom");
346 assert_eq!(
347 c.network.relay_urls,
348 vec!["https://relay.acme.com".to_string()]
349 );
350 assert_eq!(c.network.discovery_mode, "custom");
351 assert_eq!(
352 c.network.discovery_urls,
353 vec!["https://dns.acme.com/pkarr".to_string()]
354 );
355 // Absent → empty lists (the defaults need no URLs).
356 let c = Config::from_toml_str("").unwrap();
357 assert!(c.network.relay_urls.is_empty() && c.network.discovery_urls.is_empty());
358 }
359
360 #[test]
361 fn missing_file_loads_defaults() {
362 let dir = tempfile::tempdir().unwrap();
363 let c = Config::load(&dir.path().join("nope.toml")).unwrap();
364 assert_eq!(c.network.relay_mode, "default");
365 }
366
367 #[test]
368 fn roster_url_and_poll_interval_parse_with_defaults() {
369 // No [roster] table → url None, poll 1h default.
370 let c = Config::from_toml_str("").unwrap();
371 assert!(c.roster.url.is_none());
372 assert_eq!(c.roster.poll_interval_seconds(), 3600);
373 // A configured url + poll interval.
374 let c = Config::from_toml_str(
375 "[roster]\nurl = \"https://intranet.acme.com/roster.json\"\npoll_interval = \"30m\"\n",
376 )
377 .unwrap();
378 assert_eq!(
379 c.roster.url.as_deref(),
380 Some("https://intranet.acme.com/roster.json")
381 );
382 assert_eq!(c.roster.poll_interval_seconds(), 30 * 60);
383 // An unparseable poll_interval falls back to the hourly default (never disables the poll).
384 let c = Config::from_toml_str("[roster]\npoll_interval = \"never\"\n").unwrap();
385 assert_eq!(c.roster.poll_interval_seconds(), 3600);
386 // The url is additive: setting only grace_period keeps url None + the default poll.
387 let c = Config::from_toml_str("[roster]\ngrace_period = \"24h\"\n").unwrap();
388 assert!(c.roster.url.is_none());
389 assert_eq!(c.roster.poll_interval_seconds(), 3600);
390 }
391
392 #[test]
393 fn roster_max_staleness_defaults_to_24h_and_parses() {
394 // No [roster] table → the 24h freshness bound (the default).
395 let c = Config::from_toml_str("").unwrap();
396 assert_eq!(c.roster.max_staleness_seconds(), 24 * 3600);
397 // A configured value parses (units, like grace_period).
398 let c = Config::from_toml_str("[roster]\nmax_staleness = \"6h\"\n").unwrap();
399 assert_eq!(c.roster.max_staleness_seconds(), 6 * 3600);
400 // An unparseable value falls back to the 24h default (never disables the freshness bound).
401 let c = Config::from_toml_str("[roster]\nmax_staleness = \"forever\"\n").unwrap();
402 assert_eq!(c.roster.max_staleness_seconds(), 24 * 3600);
403 // Additive: setting only grace_period keeps the 24h max_staleness default.
404 let c = Config::from_toml_str("[roster]\ngrace_period = \"48h\"\n").unwrap();
405 assert_eq!(c.roster.max_staleness_seconds(), 24 * 3600);
406 }
407
408 #[test]
409 fn roster_grace_defaults_to_72h_and_parses_units() {
410 // Absent `[roster]` → the 72h default.
411 let c = Config::from_toml_str("").unwrap();
412 assert_eq!(c.roster.grace_seconds(), 72 * 3600);
413 // Hours / days / minutes / seconds / bare-seconds all resolve to seconds.
414 for (body, want) in [
415 ("[roster]\ngrace_period = \"24h\"\n", 24 * 3600),
416 ("[roster]\ngrace_period = \"72h\"\n", 72 * 3600),
417 ("[roster]\ngrace_period = \"1d\"\n", 24 * 3600),
418 ("[roster]\ngrace_period = \"30m\"\n", 30 * 60),
419 ("[roster]\ngrace_period = \"90s\"\n", 90),
420 ("[roster]\ngrace_period = \"3600\"\n", 3600), // bare seconds
421 ] {
422 assert_eq!(
423 Config::from_toml_str(body).unwrap().roster.grace_seconds(),
424 want,
425 "{body}"
426 );
427 }
428 }
429
430 #[test]
431 fn roster_grace_unparseable_or_negative_falls_back_to_default() {
432 // A garbage / negative / overflowing grace never disables degraded serving — it defaults.
433 for body in [
434 "[roster]\ngrace_period = \"seventy-two hours\"\n",
435 "[roster]\ngrace_period = \"-5h\"\n",
436 "[roster]\ngrace_period = \"18446744073709551615d\"\n", // overflows the checked_mul
437 "[roster]\ngrace_period = \"\"\n",
438 ] {
439 assert_eq!(
440 Config::from_toml_str(body).unwrap().roster.grace_seconds(),
441 72 * 3600,
442 "{body}"
443 );
444 }
445 }
446
447 #[test]
448 fn services_parse_run_and_socket() {
449 let c = Config::from_toml_str(concat!(
450 "[services.notes]\nrun = [\"npx\", \"server\"]\nallow = [\"bob\"]\n",
451 "[services.kb]\nsocket = \"/run/kb.sock\"\nallow = [\"team-eng\"]\n",
452 ))
453 .unwrap();
454 let notes = c.services.get("notes").unwrap();
455 assert!(
456 matches!(notes.backend_result(), Ok(Backend::Run(cmd)) if cmd == &["npx".to_string(), "server".to_string()][..])
457 );
458 assert_eq!(notes.allow, vec!["bob".to_string()]);
459 assert!(
460 matches!(c.services.get("kb").unwrap().backend_result(), Ok(Backend::Socket(p)) if p == "/run/kb.sock")
461 );
462 }
463
464 #[test]
465 fn service_with_both_run_and_socket_is_an_error() {
466 let e = Config::from_toml_str("[services.x]\nrun=[\"a\"]\nsocket=\"/s\"\nallow=[]\n");
467 // exactly one backend kind is required — validate at access time.
468 assert!(
469 e.unwrap()
470 .services
471 .get("x")
472 .unwrap()
473 .backend_result()
474 .is_err()
475 );
476 }
477
478 #[test]
479 fn identity_reads_user_id_and_user_key() {
480 let toml = "[identity]\n\
481 org_id = \"acme\"\n\
482 org_root_pk = \"b64u:AAAA\"\n\
483 user_id = \"alice\"\n\
484 user_key = \"/home/alice/.config/mcpmesh/user.key\"\n";
485 let cfg: Config = toml::from_str(toml).unwrap();
486 assert_eq!(cfg.identity.user_id.as_deref(), Some("alice"));
487 assert_eq!(
488 cfg.identity.user_key.as_deref(),
489 Some(std::path::Path::new("/home/alice/.config/mcpmesh/user.key"))
490 );
491 // Absent → None (pure-pairing / operator-only node).
492 let bare: Config = toml::from_str("[identity]\n").unwrap();
493 assert!(bare.identity.user_id.is_none() && bare.identity.user_key.is_none());
494 }
495}