agent-graph-mcp 0.3.0

Run 9 agents at once — MCP server for graph-orchestrated LLM workflows with parallel fan-out (up to 16 nodes), checkpoint/resume, HITL approvals, HMAC receipts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Strict CLI argument parsing for agent-graph-mcp.
//!
//! Replaces the silent `_ => {}` pattern that allowed typos like `--dat-dir`
//! to launch with unsafe defaults. Every unknown flag, missing value, or
//! malformed URL must exit nonzero before MCP transport starts.

use std::path::PathBuf;

use crate::spec::{validate_max_graphs, DEFAULT_MAX_GRAPHS};

/// Parsed CLI configuration.
#[derive(Debug, Clone)]
pub struct CliConfig {
    pub base_url: String,
    pub default_model: String,
    pub data_dir: Option<PathBuf>,
    pub integrity_key_path: Option<PathBuf>,
    pub require_integrity_key: bool,
    pub ephemeral: bool,
    pub max_graphs: usize,
}

impl Default for CliConfig {
    fn default() -> Self {
        Self {
            base_url: "http://127.0.0.1:11434".to_string(),
            default_model: "glm-5.2:cloud".to_string(),
            data_dir: None,
            integrity_key_path: None,
            require_integrity_key: false,
            ephemeral: false,
            max_graphs: DEFAULT_MAX_GRAPHS,
        }
    }
}

/// Typed CLI parse error. The caller must exit nonzero and must not start
/// MCP transport.
#[derive(Debug, Clone)]
pub struct CliError {
    pub message: String,
    pub exit_code: i32,
}

impl CliError {
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            exit_code: 2,
        }
    }
}

impl std::fmt::Display for CliError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for CliError {}

#[derive(Debug, Clone)]
pub struct ProxyConfig {
    pub socket: PathBuf,
    pub timeout_ms: u64,
}
pub fn parse_proxy_args(args: &[String]) -> Result<ProxyConfig, CliError> {
    let mut socket = std::env::var_os("XDG_RUNTIME_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("/tmp"))
        .join("agent-graph/mcp.sock");
    let mut timeout_ms = 2000;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--help" => {
                return Err(CliError {
                    message: "agent-graph-mcp [--socket PATH] [--connect-timeout-ms N]".into(),
                    exit_code: 0,
                });
            }
            "--version" => {
                return Err(CliError {
                    message: env!("CARGO_PKG_VERSION").into(),
                    exit_code: 0,
                });
            }
            "--socket" => {
                i += 1;
                socket = PathBuf::from(
                    args.get(i)
                        .ok_or_else(|| CliError::new("--socket requires a value"))?,
                );
            }
            "--connect-timeout-ms" => {
                i += 1;
                timeout_ms = args
                    .get(i)
                    .ok_or_else(|| CliError::new("--connect-timeout-ms requires a value"))?
                    .parse()
                    .map_err(|_| CliError::new("invalid timeout"))?;
            }
            "--data-dir" | "--integrity-key" | "--base-url" | "--model" | "--max-graphs" => {
                return Err(CliError::new("LEGACY_DIRECT_DURABLE_UNSUPPORTED"));
            }
            other => return Err(CliError::new(format!("unknown argument: '{other}'"))),
        }
        i += 1;
    }
    Ok(ProxyConfig { socket, timeout_ms })
}

///
/// Strict rules:
/// - Unknown flags are rejected.
/// - Flags requiring a value must have one.
/// - `--data-dir` implies durable mode; `--ephemeral` is explicit memory-only.
/// - `--require-integrity-key` without a readable key path is rejected at
///   parse time if the path is provided here; otherwise the caller validates
///   file existence/length after parsing.
/// - Provider URLs must be http or https.
pub fn parse_args(args: &[String]) -> Result<CliConfig, CliError> {
    let mut config = CliConfig::default();
    let mut iter = args.iter().peekable();

    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "--base-url" => {
                let value = iter
                    .next()
                    .ok_or_else(|| CliError::new("--base-url requires a value"))?;
                validate_url(value)?;
                config.base_url = value.clone();
            }
            "--model" => {
                let value = iter
                    .next()
                    .ok_or_else(|| CliError::new("--model requires a value"))?;
                if value.is_empty() {
                    return Err(CliError::new("--model value must not be empty"));
                }
                config.default_model = value.clone();
            }
            "--data-dir" => {
                let value = iter
                    .next()
                    .ok_or_else(|| CliError::new("--data-dir requires a value"))?;
                if value.is_empty() {
                    return Err(CliError::new("--data-dir value must not be empty"));
                }
                config.data_dir = Some(PathBuf::from(value));
            }
            "--integrity-key" => {
                let value = iter
                    .next()
                    .ok_or_else(|| CliError::new("--integrity-key requires a value"))?;
                if value.is_empty() {
                    return Err(CliError::new("--integrity-key value must not be empty"));
                }
                config.integrity_key_path = Some(PathBuf::from(value));
            }
            "--require-integrity-key" => {
                config.require_integrity_key = true;
            }
            "--ephemeral" => {
                config.ephemeral = true;
            }
            "--max-graphs" => {
                let value = iter
                    .next()
                    .ok_or_else(|| CliError::new("--max-graphs requires a value"))?;
                let max_graphs = value
                    .parse::<usize>()
                    .map_err(|_| CliError::new("--max-graphs must be an integer"))?;
                config.max_graphs = validate_max_graphs(max_graphs).map_err(CliError::new)?;
            }
            "--help" => {
                eprintln!("agent-graph-mcp [OPTIONS]");
                eprintln!();
                eprintln!("Options:");
                eprintln!(
                    "  --base-url <url>         Provider URL (default: http://127.0.0.1:11434)"
                );
                eprintln!(
                    "  --model <name>           Default model for LLM nodes (default: glm-5.2:cloud)"
                );
                eprintln!("  --data-dir <path>        Persistent storage directory");
                eprintln!("  --integrity-key <path>   Integrity key file for durable mode");
                eprintln!(
                    "  --require-integrity-key  Fail startup if integrity key is missing/unreadable"
                );
                eprintln!("  --ephemeral              Explicit in-memory mode (no persistence)");
                eprintln!(
                    "  --max-graphs <n>         Registered graph capacity (1-1024; default: 64)"
                );
                eprintln!("  --help                   Show this help message");
                return Err(CliError {
                    message: String::new(),
                    exit_code: 0,
                });
            }
            _ => {
                return Err(CliError::new(format!(
                    "unknown argument: '{arg}' — use --help for usage"
                )));
            }
        }
    }

    // Cross-flag validation
    if config.ephemeral && config.data_dir.is_some() {
        return Err(CliError::new(
            "--ephemeral and --data-dir are mutually exclusive",
        ));
    }

    if config.require_integrity_key && config.data_dir.is_none() {
        return Err(CliError::new(
            "--require-integrity-key requires --data-dir (durable mode)",
        ));
    }

    Ok(config)
}

/// Validate that a URL has http or https scheme and a non-empty host.
fn validate_url(url: &str) -> Result<(), CliError> {
    let (scheme, rest) = url
        .split_once("://")
        .ok_or_else(|| CliError::new(format!("invalid URL (no scheme): {url}")))?;

    if scheme != "http" && scheme != "https" {
        return Err(CliError::new(format!(
            "unsupported URL scheme '{scheme}': only http and https are allowed"
        )));
    }

    let authority = rest.split_once('/').map(|(auth, _)| auth).unwrap_or(rest);
    let host = authority
        .rsplit_once('@')
        .map(|(_, host)| host)
        .unwrap_or(authority);

    if host.is_empty() {
        return Err(CliError::new(format!("invalid URL (empty host): {url}")));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn args(flags: &[&str]) -> Vec<String> {
        flags.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn test_unknown_flag_rejected() {
        let result = parse_args(&args(&["--dat-dir", "/secure/path"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("unknown argument"));
    }

    #[test]
    fn test_missing_value_for_base_url() {
        let result = parse_args(&args(&["--base-url"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("requires a value"));
    }

    #[test]
    fn test_missing_value_for_model() {
        let result = parse_args(&args(&["--model"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("requires a value"));
    }

    #[test]
    fn test_missing_value_for_data_dir() {
        let result = parse_args(&args(&["--data-dir"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("requires a value"));
    }

    #[test]
    fn test_empty_model_value_rejected() {
        let result = parse_args(&args(&["--model", ""]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("must not be empty"));
    }

    #[test]
    fn test_empty_data_dir_value_rejected() {
        let result = parse_args(&args(&["--data-dir", ""]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("must not be empty"));
    }

    #[test]
    fn test_non_http_url_rejected() {
        let result = parse_args(&args(&["--base-url", "ftp://example.com"]));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .message
            .contains("unsupported URL scheme"));
    }

    #[test]
    fn test_no_scheme_url_rejected() {
        let result = parse_args(&args(&["--base-url", "example.com"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("no scheme"));
    }

    #[test]
    fn test_empty_host_url_rejected() {
        let result = parse_args(&args(&["--base-url", "http://"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("empty host"));
    }

    #[test]
    fn test_valid_http_url_accepted() {
        let result = parse_args(&args(&["--base-url", "http://127.0.0.1:11434"]));
        assert!(result.is_ok());
        assert_eq!(result.unwrap().base_url, "http://127.0.0.1:11434");
    }

    #[test]
    fn test_valid_https_url_accepted() {
        let result = parse_args(&args(&["--base-url", "https://api.openai.com"]));
        assert!(result.is_ok());
        assert_eq!(result.unwrap().base_url, "https://api.openai.com");
    }

    #[test]
    fn test_url_with_credentials_stripped_in_validation() {
        let result = parse_args(&args(&["--base-url", "https://user:pass@host.com"]));
        assert!(result.is_ok()); // validation passes; runtime redaction is separate
    }

    #[test]
    fn test_ephemeral_and_data_dir_mutually_exclusive() {
        let result = parse_args(&args(&["--ephemeral", "--data-dir", "/tmp/test"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("mutually exclusive"));
    }

    #[test]
    fn test_require_integrity_key_without_data_dir_rejected() {
        let result = parse_args(&args(&["--require-integrity-key"]));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("requires --data-dir"));
    }

    #[test]
    fn test_require_integrity_key_with_data_dir_accepted() {
        let result = parse_args(&args(&[
            "--require-integrity-key",
            "--data-dir",
            "/tmp/test",
            "--integrity-key",
            "/tmp/key",
        ]));
        assert!(result.is_ok());
        assert!(result.unwrap().require_integrity_key);
    }

    #[test]
    fn test_help_returns_exit_zero() {
        let result = parse_args(&args(&["--help"]));
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().exit_code, 0);
    }

    #[test]
    fn test_default_config_when_no_args() {
        let result = parse_args(&[]);
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.base_url, "http://127.0.0.1:11434");
        assert_eq!(config.default_model, "glm-5.2:cloud");
        assert!(config.data_dir.is_none());
        assert!(!config.ephemeral);
    }

    #[test]
    fn test_ephemeral_alone_accepted() {
        let result = parse_args(&args(&["--ephemeral"]));
        assert!(result.is_ok());
        assert!(result.unwrap().ephemeral);
    }

    #[test]
    fn test_integrity_key_path_recorded() {
        let result = parse_args(&args(&[
            "--data-dir",
            "/tmp/test",
            "--integrity-key",
            "/tmp/my-key",
        ]));
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(
            config.integrity_key_path,
            Some(PathBuf::from("/tmp/my-key"))
        );
    }

    #[test]
    fn test_max_graphs_records_configured_value() {
        let config = parse_args(&args(&["--max-graphs", "256"])).expect("valid limit");
        assert_eq!(config.max_graphs, 256);
    }

    #[test]
    fn test_zero_max_graphs_is_rejected() {
        let error = parse_args(&args(&["--max-graphs", "0"])).expect_err("zero is invalid");
        assert!(error.message.contains("between 1 and"));
    }

    #[test]
    fn test_excessive_max_graphs_is_rejected() {
        let error = parse_args(&args(&["--max-graphs", "1025"])).expect_err("hard cap");
        assert!(error.message.contains("between 1 and"));
    }
}