warmplane 0.18.0

Local control plane that keeps MCP sessions warm with compact capability/resource/prompt facades.
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Rust guideline compliant 2026-08-18

//! Comprehensive end-to-end integration tests for Warmplane (`M-CANONICAL-DOCS`).
//!
//! Exercises real binary execution over stdio, live TCP daemon listeners, real-time SSE streaming,
//! OAuth2 mock provider discovery/refresh, and configuration hot-reloading.

use axum::{
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Json},
    routing::post,
    Router,
};
use futures::StreamExt;
use serde_json::{json, Value};
use std::{
    collections::{HashMap, HashSet},
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc,
    },
    time::Duration,
};
use tempfile::NamedTempFile;
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::TcpListener,
    process::Command,
    sync::RwLock,
    time::timeout,
};

use warmplane::{
    config::{save_config, McpConfig, ServerConfig},
    daemon::server::{build_router, initialize_state},
    oauth2::{DiscoveryMetadata, OAuth2ClientState, OAuth2TokenState, OAuthRegistry},
};

// ============================================================================
// Helper: read stdout until a JSON-RPC message with expected ID arrives
// ============================================================================

async fn read_jsonrpc_until_id<R: AsyncBufReadExt + Unpin>(
    reader: &mut R,
    expected_id: u64,
) -> Value {
    let mut line = String::new();
    loop {
        line.clear();
        let bytes_read = timeout(Duration::from_secs(5), reader.read_line(&mut line))
            .await
            .expect("timeout waiting for JSON-RPC message")
            .expect("stdout read error");

        if bytes_read == 0 {
            panic!("stdout closed before receiving id={}", expected_id);
        }

        let trimmed = line.trim();
        if trimmed.is_empty() || !trimmed.starts_with('{') {
            continue;
        }

        if let Ok(val) = serde_json::from_str::<Value>(trimmed) {
            if val.get("id").and_then(Value::as_u64) == Some(expected_id) {
                return val;
            }
        }
    }
}

// ============================================================================
// Test 1: Full Stdio MCP Server Facade Protocol Handshake
// ============================================================================

#[tokio::test]
async fn test_e2e_stdio_mcp_server_facade_protocol() {
    let temp_config = NamedTempFile::new().unwrap();
    let config_path = temp_config.path().to_str().unwrap().to_string();

    let initial_config = McpConfig::default();
    save_config(&config_path, &initial_config).unwrap();

    let bin_path = env!("CARGO_BIN_EXE_warmplane");
    let mut child = Command::new(bin_path)
        .arg("mcp-server")
        .arg("--config")
        .arg(&config_path)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("failed to spawn warmplane mcp-server process");

    let stdin = child.stdin.as_mut().expect("child stdin must be captured");
    let stdout = child.stdout.take().expect("child stdout must be captured");
    let mut reader = BufReader::new(stdout);

    // 1. Send initialize request
    let init_req = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-11-25",
            "capabilities": {},
            "clientInfo": {
                "name": "e2e-test-client",
                "version": "1.0.0"
            }
        }
    });

    stdin
        .write_all(format!("{}\n", serde_json::to_string(&init_req).unwrap()).as_bytes())
        .await
        .unwrap();
    stdin.flush().await.unwrap();

    let init_resp = read_jsonrpc_until_id(&mut reader, 1).await;
    assert_eq!(init_resp["id"], 1);
    assert!(init_resp["result"]["capabilities"]["tools"].is_object());

    // 2. Send initialized notification
    let initialized_notif = json!({
        "jsonrpc": "2.0",
        "method": "notifications/initialized"
    });
    stdin
        .write_all(format!("{}\n", serde_json::to_string(&initialized_notif).unwrap()).as_bytes())
        .await
        .unwrap();
    stdin.flush().await.unwrap();

    // 3. Query tools/list
    let tools_req = json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/list",
        "params": {}
    });
    stdin
        .write_all(format!("{}\n", serde_json::to_string(&tools_req).unwrap()).as_bytes())
        .await
        .unwrap();
    stdin.flush().await.unwrap();

    let tools_resp = read_jsonrpc_until_id(&mut reader, 2).await;
    assert_eq!(tools_resp["id"], 2);
    let tools = tools_resp["result"]["tools"]
        .as_array()
        .expect("tools array must exist");
    let tool_names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();

    assert!(tool_names.contains(&"capabilities_list"));
    assert!(tool_names.contains(&"capability_call"));
    assert!(tool_names.contains(&"resources_list"));
    assert!(tool_names.contains(&"prompts_list"));

    // 4. Call capabilities_list facade tool
    let call_req = json!({
        "jsonrpc": "2.0",
        "id": 3,
        "method": "tools/call",
        "params": {
            "name": "capabilities_list",
            "arguments": {}
        }
    });
    stdin
        .write_all(format!("{}\n", serde_json::to_string(&call_req).unwrap()).as_bytes())
        .await
        .unwrap();
    stdin.flush().await.unwrap();

    let call_resp = read_jsonrpc_until_id(&mut reader, 3).await;
    assert_eq!(call_resp["id"], 3);
    assert!(call_resp["result"]["content"].is_array());

    // 5. Clean termination upon closing stdin
    drop(child.stdin.take());
    let exit_status = timeout(Duration::from_secs(5), child.wait())
        .await
        .expect("timeout waiting for child process exit")
        .unwrap();

    assert!(exit_status.success());
}

// ============================================================================
// Test 2: Real-Time SSE Event Streaming & Live Dynamic Server Mount/Unmount
// ============================================================================

#[tokio::test]
async fn test_e2e_realtime_sse_event_streaming() {
    let temp_config = NamedTempFile::new().unwrap();
    let config_path = temp_config.path().to_str().unwrap().to_string();

    let initial_config = McpConfig::default();
    save_config(&config_path, &initial_config).unwrap();

    let state = initialize_state(initial_config, &config_path)
        .await
        .unwrap();
    let app = build_router(state.clone());

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();

    let server_task = tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    let client = reqwest::Client::new();
    let sse_url = format!("http://127.0.0.1:{}/v1/resources/updates", port);

    let sse_res = client
        .get(&sse_url)
        .send()
        .await
        .expect("failed to connect to SSE stream endpoint");

    assert_eq!(sse_res.status(), StatusCode::OK);
    let mut stream = sse_res.bytes_stream();

    // Dynamically mount an upstream server via REST API
    let mount_url = format!("http://127.0.0.1:{}/v1/config/servers", port);
    let mount_payload = json!({
        "name": "dynamic_test_srv",
        "server": {
            "command": "echo",
            "args": ["running"]
        }
    });

    let mount_res = client
        .post(&mount_url)
        .json(&mount_payload)
        .send()
        .await
        .unwrap();

    assert_eq!(mount_res.status(), StatusCode::OK);

    // Read the SSE event stream for the dynamic mount notification
    let mut received_event = false;
    let read_future = async {
        while let Some(chunk_res) = stream.next().await {
            if let Ok(bytes) = chunk_res {
                let text = String::from_utf8_lossy(&bytes);
                if text.contains("server://dynamic_test_srv") || text.contains("resource_update") {
                    received_event = true;
                    break;
                }
            }
        }
    };

    timeout(Duration::from_secs(5), read_future)
        .await
        .expect("timeout waiting for real-time SSE event");

    assert!(
        received_event,
        "SSE stream must yield live mount notification"
    );

    // Unmount server via REST API
    let unmount_url = format!(
        "http://127.0.0.1:{}/v1/config/servers/dynamic_test_srv",
        port
    );
    let unmount_res = client.delete(&unmount_url).send().await.unwrap();
    assert_eq!(unmount_res.status(), StatusCode::OK);

    server_task.abort();
}

// ============================================================================
// Test 3: Configuration Hot-Reloading E2E
// ============================================================================

#[tokio::test]
async fn test_e2e_config_file_hot_reload() {
    let temp_config = NamedTempFile::new().unwrap();
    let config_path = temp_config.path().to_str().unwrap().to_string();

    let mut mcp_servers = HashMap::new();
    mcp_servers.insert(
        "server_alpha".to_string(),
        ServerConfig {
            command: Some("echo".to_string()),
            args: vec!["alpha".to_string()],
            env: HashMap::new(),
            url: None,
            auth: None,
            protocol_version: None,
            allow_stateless: None,
            headers: HashMap::new(),
            resilience: None,
        },
    );

    let initial_config = McpConfig {
        mcp_servers,
        ..Default::default()
    };
    save_config(&config_path, &initial_config).unwrap();

    let state = initialize_state(initial_config, &config_path)
        .await
        .unwrap();
    let app = build_router(state.clone());

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();

    let server_task = tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    let client = reqwest::Client::new();

    // Verify initial config reports server_alpha
    let config_res = client
        .get(format!("http://127.0.0.1:{}/v1/config", port))
        .send()
        .await
        .unwrap();
    let config_json: Value = config_res.json().await.unwrap();
    assert!(config_json["config"]["mcpServers"]["server_alpha"].is_object());

    // Update config on disk: replace server_alpha with server_beta
    let mut updated_servers = HashMap::new();
    updated_servers.insert(
        "server_beta".to_string(),
        ServerConfig {
            command: Some("echo".to_string()),
            args: vec!["beta".to_string()],
            env: HashMap::new(),
            url: None,
            auth: None,
            protocol_version: None,
            allow_stateless: None,
            headers: HashMap::new(),
            resilience: None,
        },
    );

    let updated_config = McpConfig {
        mcp_servers: updated_servers,
        ..Default::default()
    };
    save_config(&config_path, &updated_config).unwrap();

    // Trigger dynamic reload
    let reload_res = client
        .post(format!("http://127.0.0.1:{}/v1/config/reload", port))
        .send()
        .await
        .unwrap();

    assert_eq!(reload_res.status(), StatusCode::OK);
    let reload_json: Value = reload_res.json().await.unwrap();
    assert!(reload_json["mounted"]
        .as_array()
        .unwrap()
        .contains(&json!("server_beta")));
    assert!(reload_json["unmounted"]
        .as_array()
        .unwrap()
        .contains(&json!("server_alpha")));

    // Verify updated state reflection
    let config_res2 = client
        .get(format!("http://127.0.0.1:{}/v1/config", port))
        .send()
        .await
        .unwrap();
    let config_json2: Value = config_res2.json().await.unwrap();
    assert!(config_json2["config"]["mcpServers"]["server_beta"].is_object());
    assert!(config_json2["config"]["mcpServers"]["server_alpha"].is_null());

    server_task.abort();
}

// ============================================================================
// Test 4: OAuth2 Authorization Server Mock Round-Trip & Silent Token Refresh
// ============================================================================

#[derive(Clone, Default)]
struct MockOAuthState {
    refresh_calls: Arc<AtomicU32>,
    data_calls: Arc<AtomicU32>,
}

#[tokio::test]
async fn test_e2e_oauth2_mock_provider_flow_and_refresh() {
    let mock_state = MockOAuthState::default();

    // 1. Spin up mock OAuth2 upstream server with 401 step-up and token refresh
    let mock_app = Router::new()
        .route(
            "/token",
            post({
                let state_clone = mock_state.clone();
                move |_body: axum::body::Bytes| {
                    let state = state_clone.clone();
                    async move {
                        state.refresh_calls.fetch_add(1, Ordering::SeqCst);
                        Json(json!({
                            "access_token": "fresh_access_token_777",
                            "token_type": "Bearer",
                            "expires_in": 3600,
                            "refresh_token": "fresh_refresh_token_888",
                            "scope": "read write"
                        }))
                    }
                }
            }),
        )
        .route(
            "/api/data",
            post({
                let state_clone = mock_state.clone();
                move |headers: HeaderMap| {
                    let state = state_clone.clone();
                    async move {
                        state.data_calls.fetch_add(1, Ordering::SeqCst);
                        let auth = headers
                            .get("authorization")
                            .and_then(|h| h.to_str().ok())
                            .unwrap_or("");

                        // Reject initial expired token with 401, accept refreshed token
                        if auth == "Bearer fresh_access_token_777" {
                            (
                                StatusCode::OK,
                                Json(json!({"status": "success", "content": "protected_payload"})),
                            )
                                .into_response()
                        } else {
                            (
                                StatusCode::UNAUTHORIZED,
                                Json(json!({"error": "invalid_token", "error_description": "Token expired"})),
                            )
                                .into_response()
                        }
                    }
                }
            }),
        )
        .with_state(mock_state.clone());

    let upstream_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let upstream_port = upstream_listener.local_addr().unwrap().port();
    let upstream_task = tokio::spawn(async move {
        axum::serve(upstream_listener, mock_app).await.unwrap();
    });

    let upstream_base_url = format!("http://127.0.0.1:{}", upstream_port);

    // 2. Set up Warmplane OAuthRegistry with an expired token
    let temp_token_file = NamedTempFile::new().unwrap();
    let token_storage_path = temp_token_file.path().to_str().unwrap().to_string();

    let registry = OAuthRegistry::open_or_create(&token_storage_path);

    let mut scopes = HashSet::new();
    scopes.insert("read".to_string());

    let expired_token = OAuth2TokenState {
        access_token: "expired_token_111".to_string(),
        refresh_token: Some("valid_refresh_token_222".to_string()),
        scopes: scopes.clone(),
    };

    let client_state = OAuth2ClientState {
        server_id: "mock_oauth_srv".to_string(),
        client_id: "warmplane_client".to_string(),
        _authorization_server_url: upstream_base_url.clone(),
        scopes: Arc::new(RwLock::new(scopes)),
        token_state: Arc::new(RwLock::new(Some(expired_token.clone()))),
        discovery: DiscoveryMetadata {
            authorization_endpoint: format!("{}/authorize", upstream_base_url),
            token_endpoint: format!("{}/token", upstream_base_url),
            issuer: upstream_base_url.clone(),
        },
        client_metadata_url: None,
        remote_base_url: upstream_base_url.clone(),
    };

    {
        let mut clients_guard = registry.clients.write().await;
        clients_guard.insert("mock_oauth_srv".to_string(), client_state);
    }

    // Start OAuth proxy server
    let proxy_port = warmplane::oauth2::start_oauth_proxy_server(registry.clone())
        .await
        .expect("OAuth proxy server must start");

    // 3. Send request through the Warmplane OAuth proxy
    let proxy_url = format!(
        "http://127.0.0.1:{}/proxy/mock_oauth_srv/api/data",
        proxy_port
    );
    let http_client = reqwest::Client::new();

    let proxy_resp = http_client
        .post(&proxy_url)
        .json(&json!({"query": "get_data"}))
        .send()
        .await
        .expect("Proxy request must succeed");

    assert_eq!(proxy_resp.status(), StatusCode::OK);
    let body: Value = proxy_resp.json().await.unwrap();
    assert_eq!(body["status"], "success");
    assert_eq!(body["content"], "protected_payload");

    // Verify token was refreshed and updated in storage
    assert_eq!(mock_state.refresh_calls.load(Ordering::SeqCst), 1);
    assert_eq!(mock_state.data_calls.load(Ordering::SeqCst), 2); // 1st attempt (401) + 2nd attempt (200)

    let persisted = registry
        .get_saved_token("mock_oauth_srv")
        .await
        .expect("refreshed token must be persisted");
    assert_eq!(persisted.access_token, "fresh_access_token_777");

    upstream_task.abort();
}

// ============================================================================
// Test 5: Supervisor Crash Detection and Degraded Status Recording
// ============================================================================

#[tokio::test]
async fn test_e2e_supervisor_crash_detection_and_degraded_status() {
    let temp_config = NamedTempFile::new().unwrap();
    let config_path = temp_config.path().to_str().unwrap().to_string();

    let mut mcp_servers = HashMap::new();
    // Server with a command that immediately fails / terminates
    mcp_servers.insert(
        "failing_server".to_string(),
        ServerConfig {
            command: Some("false".to_string()),
            args: vec![],
            env: HashMap::new(),
            url: None,
            auth: None,
            protocol_version: None,
            allow_stateless: None,
            headers: HashMap::new(),
            resilience: None,
        },
    );

    let initial_config = McpConfig {
        mcp_servers,
        ..Default::default()
    };
    save_config(&config_path, &initial_config).unwrap();

    let state = initialize_state(initial_config, &config_path)
        .await
        .unwrap();

    // Give the background supervisor loop time to record failure
    tokio::time::sleep(Duration::from_millis(150)).await;

    let statuses = state.server_statuses.read().await;
    let status = statuses
        .get("failing_server")
        .expect("server status must be recorded for failing_server");

    assert_eq!(status["status"], "degraded");
    assert!(status["error"].is_string());
}