zagens-runtime-adapters 0.7.1

Runtime tool/MCP/persist adapters for Zagens sidecar (D16 E1-a)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use std::collections::HashMap;
use std::fs;

use self::diagnostics::{mask_url_secrets, redact_body_preview};

#[test]
fn test_mcp_config_defaults() {
    let config = McpConfig::default();
    assert_eq!(config.timeouts.connect_timeout, 30);
    assert_eq!(config.timeouts.execute_timeout, 60);
    assert_eq!(config.timeouts.read_timeout, 120);
    assert!(config.servers.is_empty());
}

#[test]
fn test_mcp_config_parse() {
    let json = r#"{
        "timeouts": {
            "connect_timeout": 15,
            "execute_timeout": 90
        },
        "servers": {
            "test": {
                "command": "node",
                "args": ["server.js"],
                "env": {"FOO": "bar"}
            }
        }
    }"#;

    let config: McpConfig = serde_json::from_str(json).unwrap();
    assert_eq!(config.timeouts.connect_timeout, 15);
    assert_eq!(config.timeouts.execute_timeout, 90);
    assert_eq!(config.timeouts.read_timeout, 120); // default
    assert!(config.servers.contains_key("test"));

    let server = config.servers.get("test").unwrap();
    assert_eq!(server.command, Some("node".to_string()));
    assert_eq!(server.args, vec!["server.js"]);
    assert_eq!(server.env.get("FOO"), Some(&"bar".to_string()));
}

#[test]
fn test_mcp_config_parse_mcp_servers_alias_and_snapshot() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("mcp.json");
    fs::write(
        &path,
        r#"{
          "mcpServers": {
            "disabled": {
              "command": "node",
              "args": ["server.js"],
              "disabled": true
            }
          }
        }"#,
    )
    .unwrap();

    let cfg = load_config(&path).unwrap();
    assert!(cfg.servers.contains_key("disabled"));
    let snapshot = manager_snapshot_from_config(&path, true).unwrap();
    assert!(snapshot.restart_required);
    assert_eq!(snapshot.servers[0].name, "disabled");
    assert!(!snapshot.servers[0].enabled);
    assert_eq!(snapshot.servers[0].error.as_deref(), Some("disabled"));
}

#[test]
fn test_mcp_config_manager_actions_round_trip() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("mcp.json");

    assert_eq!(init_config(&path, false).unwrap(), McpWriteStatus::Created);
    assert_eq!(
        init_config(&path, false).unwrap(),
        McpWriteStatus::SkippedExists
    );

    add_server_config(
        &path,
        "local".to_string(),
        Some("node".to_string()),
        None,
        vec!["server.js".to_string()],
    )
    .unwrap();
    set_server_enabled(&path, "local", false).unwrap();
    let disabled = manager_snapshot_from_config(&path, true).unwrap();
    let local = disabled
        .servers
        .iter()
        .find(|server| server.name == "local")
        .unwrap();
    assert!(!local.enabled);
    assert_eq!(local.transport, "stdio");

    remove_server_config(&path, "local").unwrap();
    let removed = manager_snapshot_from_config(&path, true).unwrap();
    assert!(removed.servers.iter().all(|server| server.name != "local"));
}

#[test]
fn test_server_effective_timeouts() {
    let global = McpTimeouts::default();

    let server_with_override = McpServerConfig {
        command: Some("test".to_string()),
        args: vec![],
        env: HashMap::new(),
        url: None,
        transport: None,
        headers: HashMap::new(),
        auth: None,
        connect_timeout: Some(20),
        execute_timeout: None,
        read_timeout: Some(180),
        disabled: false,
        enabled: true,
        required: false,
        enabled_tools: Vec::new(),
        disabled_tools: Vec::new(),
    };

    assert_eq!(server_with_override.effective_connect_timeout(&global), 20);
    assert_eq!(server_with_override.effective_execute_timeout(&global), 60); // global default
    assert_eq!(server_with_override.effective_read_timeout(&global), 180);
}

#[test]
fn test_mcp_pool_is_mcp_tool() {
    assert!(McpPool::is_mcp_tool("mcp_filesystem_read"));
    assert!(McpPool::is_mcp_tool("mcp_git_status"));
    assert!(McpPool::is_mcp_tool("list_mcp_resources"));
    assert!(McpPool::is_mcp_tool("list_mcp_resource_templates"));
    assert!(McpPool::is_mcp_tool("read_mcp_resource"));
    assert!(!McpPool::is_mcp_tool("read_file"));
    assert!(!McpPool::is_mcp_tool("exec_shell"));
}

#[test]
fn test_format_tool_result_text() {
    let result = serde_json::json!({
        "content": [
            {"type": "text", "text": "Hello, world!"}
        ]
    });
    assert_eq!(format_tool_result(&result), "Hello, world!");
}

#[test]
fn test_format_tool_result_error() {
    let result = serde_json::json!({
        "isError": true,
        "content": [
            {"type": "text", "text": "Something went wrong"}
        ]
    });
    assert_eq!(format_tool_result(&result), "Error: Something went wrong");
}

#[test]
fn test_format_tool_result_multiple_content() {
    let result = serde_json::json!({
        "content": [
            {"type": "text", "text": "Line 1"},
            {"type": "text", "text": "Line 2"},
            {"type": "image", "data": "base64..."}
        ]
    });
    let formatted = format_tool_result(&result);
    assert!(formatted.contains("Line 1"));
    assert!(formatted.contains("Line 2"));
    assert!(formatted.contains("[image content]"));
}

#[test]
fn test_is_tool_error_flag() {
    let ok = serde_json::json!({ "content": [{"type": "text", "text": "done"}] });
    assert!(!is_tool_error(&ok));

    let err = serde_json::json!({
        "isError": true,
        "content": [{"type": "text", "text": "boom"}]
    });
    assert!(is_tool_error(&err));
}

#[test]
fn test_extract_tool_content_drops_envelope() {
    // Only the text blocks survive — the isError/meta envelope is stripped.
    let err = serde_json::json!({
        "isError": true,
        "content": [{"type": "text", "text": "boom"}],
        "meta": {"trace": "abc"}
    });
    assert_eq!(extract_tool_content(&err), "boom");
}

#[test]
fn test_extract_tool_content_non_text_block() {
    let result = serde_json::json!({
        "content": [
            {"type": "image", "data": "base64..."},
            {"type": "text", "text": "caption"}
        ]
    });
    assert_eq!(extract_tool_content(&result), "[image content]\ncaption");
}

#[test]
fn test_parse_prefixed_name_underscore_server() {
    let config: McpConfig = serde_json::from_str(
        r#"{
            "servers": {
                "github_mcp": { "command": "node" },
                "git": { "command": "node" }
            }
        }"#,
    )
    .unwrap();
    let pool = McpPool::new(config);

    // Server name with an underscore must not be split at the first `_`.
    assert_eq!(
        pool.parse_prefixed_name("mcp_github_mcp_search").unwrap(),
        ("github_mcp", "search")
    );
    // A shorter, overlapping prefix still resolves to the right server.
    assert_eq!(
        pool.parse_prefixed_name("mcp_git_status").unwrap(),
        ("git", "status")
    );
    // Tool names may also contain underscores.
    assert_eq!(
        pool.parse_prefixed_name("mcp_github_mcp_create_issue")
            .unwrap(),
        ("github_mcp", "create_issue")
    );
}

#[test]
fn test_parse_prefixed_name_unknown_server_falls_back() {
    let pool = McpPool::new(McpConfig::default());
    // Server not in config: fall back to first-underscore split.
    assert_eq!(
        pool.parse_prefixed_name("mcp_unknown_tool").unwrap(),
        ("unknown", "tool")
    );
    assert!(pool.parse_prefixed_name("notmcp_x").is_err());
    assert!(pool.parse_prefixed_name("mcp_noseparator").is_err());
}

#[test]
fn test_transport_kind_inference() {
    let cfg: McpConfig = serde_json::from_str(
        r#"{
            "servers": {
                "local":  { "command": "node" },
                "legacy": { "url": "https://example.com/sse" }
            }
        }"#,
    )
    .unwrap();
    assert_eq!(
        cfg.servers["local"].transport_kind().unwrap(),
        McpTransportKind::Stdio
    );
    // url without explicit transport stays SSE for backward compatibility.
    assert_eq!(
        cfg.servers["legacy"].transport_kind().unwrap(),
        McpTransportKind::Sse
    );
}

#[test]
fn test_transport_kind_explicit_and_alias() {
    let cfg: McpConfig = serde_json::from_str(
        r#"{
            "servers": {
                "stream": { "url": "https://example.com/mcp", "transport": "http" },
                "aliased": { "url": "https://example.com/mcp", "type": "streamable-http" },
                "forced_sse": { "url": "https://example.com/mcp", "type": "sse" }
            }
        }"#,
    )
    .unwrap();
    assert_eq!(
        cfg.servers["stream"].transport_kind().unwrap(),
        McpTransportKind::Http
    );
    assert_eq!(
        cfg.servers["aliased"].transport_kind().unwrap(),
        McpTransportKind::Http
    );
    assert_eq!(
        cfg.servers["forced_sse"].transport_kind().unwrap(),
        McpTransportKind::Sse
    );
}

#[tokio::test]
async fn test_mcp_pool_reload_config_diff() {
    let cfg_a: McpConfig = serde_json::from_str(
        r#"{
            "servers": {
                "alpha": { "command": "node", "args": ["a.js"] },
                "beta": { "command": "node", "args": ["b.js"] }
            }
        }"#,
    )
    .unwrap();
    let mut pool = McpPool::new(cfg_a);

    let cfg_b: McpConfig = serde_json::from_str(
        r#"{
            "servers": {
                "alpha": { "command": "node", "args": ["a.v2.js"] },
                "gamma": { "command": "node", "args": ["g.js"] }
            }
        }"#,
    )
    .unwrap();
    let report = pool.reload_config(cfg_b, false).await;
    assert_eq!(report.removed, vec!["beta".to_string()]);
    assert!(report.updated.contains(&"alpha".to_string()));
    assert!(pool.config().servers.contains_key("gamma"));
    assert!(!pool.config().servers.contains_key("beta"));
}

#[test]
fn test_merge_preserved_secrets_on_save() {
    let old = McpServerConfig {
        command: None,
        args: vec![],
        env: HashMap::new(),
        url: Some("https://example.com/mcp".to_string()),
        transport: Some("http".to_string()),
        headers: HashMap::new(),
        auth: Some(McpAuthConfig {
            auth_type: Some("bearer".to_string()),
            token: Some("secret-token".to_string()),
            header: None,
            api_key: None,
        }),
        connect_timeout: None,
        execute_timeout: None,
        read_timeout: None,
        disabled: false,
        enabled: true,
        required: false,
        enabled_tools: vec![],
        disabled_tools: vec![],
    };
    let mut new = old.redacted_for_display();
    assert!(new.auth.as_ref().unwrap().token.is_none());
    merge_preserved_secrets(&mut new, &old);
    assert_eq!(new.auth.as_ref().unwrap().token.as_deref(), Some("secret-token"));
}

#[test]
fn test_redacted_display_keeps_env_placeholder() {
    let cfg = McpServerConfig {
        command: None,
        args: vec![],
        env: HashMap::new(),
        url: Some("https://example.com/mcp".to_string()),
        transport: None,
        headers: HashMap::from([(
            "Authorization".to_string(),
            "Bearer ${MCP_TOKEN}".to_string(),
        )]),
        auth: None,
        connect_timeout: None,
        execute_timeout: None,
        read_timeout: None,
        disabled: false,
        enabled: true,
        required: false,
        enabled_tools: vec![],
        disabled_tools: vec![],
    };
    let redacted = cfg.redacted_for_display();
    assert_eq!(
        redacted.headers.get("Authorization").map(String::as_str),
        Some("Bearer ${MCP_TOKEN}")
    );
}

#[test]
fn test_transport_kind_invalid() {
    // Unknown transport label.
    let bad: McpConfig = serde_json::from_str(
        r#"{ "servers": { "x": { "url": "https://e.com", "transport": "carrier-pigeon" } } }"#,
    )
    .unwrap();
    assert!(bad.servers["x"].transport_kind().is_err());

    // http transport without a url.
    let no_url: McpConfig = serde_json::from_str(
        r#"{ "servers": { "x": { "command": "node", "transport": "http" } } }"#,
    )
    .unwrap();
    assert!(no_url.servers["x"].transport_kind().is_err());
}

#[test]
fn test_streamable_http_sse_body_parsing() {
    let client = reqwest::Client::new();
    let mut transport =
        self::transport::StreamableHttpTransport::new(client, "https://example.com/mcp".to_string());
    transport.enqueue_sse_body(
        "event: message\ndata: {\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"ok\":true}}\n\n\
         event: message\ndata: {\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\"}\n\n",
    );
    let first = transport.inbox.pop_front().unwrap();
    assert_eq!(first["id"], 1);
    assert_eq!(first["result"]["ok"], true);
    let second = transport.inbox.pop_front().unwrap();
    assert_eq!(second["method"], "notifications/progress");
}

#[test]
fn test_streamable_http_json_batch_parsing() {
    let client = reqwest::Client::new();
    let mut transport =
        self::transport::StreamableHttpTransport::new(client, "https://example.com/mcp".to_string());
    transport.enqueue_json_body(r#"[{"id":1,"result":1},{"id":2,"result":2}]"#);
    assert_eq!(transport.inbox.len(), 2);
    assert_eq!(transport.inbox[0]["id"], 1);
    assert_eq!(transport.inbox[1]["id"], 2);
}

#[tokio::test]
async fn test_mcp_pool_empty_config() {
    let pool = McpPool::new(McpConfig::default());
    assert!(pool.server_names().is_empty());
    assert!(pool.all_tools().is_empty());
}

#[test]
fn mask_url_secrets_strips_userinfo() {
    let masked = mask_url_secrets("https://user:s3cret@host.example/api?foo=bar");
    assert!(masked.contains("***"), "expected masked userinfo: {masked}");
    assert!(!masked.contains("s3cret"), "secret leaked: {masked}");
    assert!(masked.contains("host.example"), "host preserved: {masked}");
}

#[test]
fn mask_url_secrets_passes_through_clean_url() {
    assert_eq!(
        mask_url_secrets("https://api.example.com/mcp"),
        "https://api.example.com/mcp"
    );
}

#[test]
fn redact_body_preview_masks_bearer_token() {
    let redacted = redact_body_preview("Authorization: Bearer abc.def.ghi end");
    assert!(redacted.contains("Bearer ***"), "redacted: {redacted}");
    assert!(!redacted.contains("abc.def.ghi"), "leaked: {redacted}");
}

#[test]
fn redact_body_preview_masks_api_key_param() {
    let redacted = redact_body_preview("error message api_key=sk-12345&other=val");
    assert!(redacted.contains("api_key=***"), "redacted: {redacted}");
    assert!(!redacted.contains("sk-12345"), "leaked: {redacted}");
    assert!(
        redacted.contains("other=val"),
        "non-secret preserved: {redacted}"
    );
}

/// #420: `StdioTransport::shutdown` reaps the child process by sending
/// SIGTERM and giving it a brief grace period before drop fires SIGKILL.
/// The test spawns `cat` (which exits immediately on stdin EOF / SIGTERM)
/// and verifies the transport tears down cleanly. Unix-only because
/// SIGTERM doesn't exist on Windows; on Windows the test would just
/// duplicate the kill_on_drop path.
#[cfg(unix)]
#[tokio::test]
async fn stdio_transport_shutdown_terminates_child() {
    use std::time::Duration;

    use self::transport::{STDIO_SHUTDOWN_GRACE, StdioTransport};

    use tokio::process::Command as TokioCommand;
    let mut cmd = TokioCommand::new("cat");
    cmd.stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .kill_on_drop(true);
    let mut child = cmd.spawn().expect("spawn cat");
    let pid = child.id().expect("child pid");
    let stdin = child.stdin.take().expect("child stdin");
    let stdout = child.stdout.take().expect("child stdout");
    let mut transport = StdioTransport {
        child,
        stdin,
        reader: tokio::io::BufReader::new(stdout),
    };

    // shutdown() should send SIGTERM and complete within the grace window.
    let start = std::time::Instant::now();
    transport.shutdown().await;
    let elapsed = start.elapsed();
    assert!(
        elapsed < STDIO_SHUTDOWN_GRACE + Duration::from_millis(500),
        "shutdown blocked beyond grace window: {elapsed:?}"
    );

    // The child should be reaped — kill(pid, 0) returning ESRCH means
    // the pid is gone. If it's still alive, kill(0) returns 0, which
    // means our shutdown didn't terminate it.
    // SAFETY: pid was just collected from a tokio Child we spawned.
    // libc::kill with signal 0 only checks pid existence and is
    // async-signal-safe.
    let still_alive = unsafe { libc::kill(pid as i32, 0) } == 0;
    assert!(
        !still_alive,
        "child {pid} survived StdioTransport::shutdown — SIGTERM not delivered"
    );
}