aptu-coder 0.14.2

MCP server for multi-language code structure analysis
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
// SPDX-FileCopyrightText: 2026 aptu-coder contributors
// SPDX-License-Identifier: Apache-2.0

use std::sync::{Arc, Mutex, OnceLock};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::Mutex as TokioMutex;
use tracing_subscriber::filter::LevelFilter;

/// Serializes tests that mutate process-global env vars to prevent parallel pollution.
/// Uses `unwrap_or_else` to recover from mutex poison caused by panicking tests.
fn env_var_lock() -> std::sync::MutexGuard<'static, ()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    let m = LOCK.get_or_init(|| Mutex::new(()));
    m.lock().unwrap_or_else(|e| e.into_inner())
}

fn make_test_analyzer() -> aptu_coder::CodeAnalyzer {
    let peer = Arc::new(TokioMutex::new(None));
    let log_level_filter = Arc::new(Mutex::new(LevelFilter::INFO));
    let (_tx, rx) = tokio::sync::mpsc::unbounded_channel::<aptu_coder::logging::LogEvent>();
    let (metrics_tx, _metrics_rx) = tokio::sync::mpsc::unbounded_channel();
    aptu_coder::CodeAnalyzer::new(
        peer,
        log_level_filter,
        rx,
        aptu_coder::metrics::MetricsSender(metrics_tx),
    )
}

async fn call_tools_list_with_profile(profile: Option<&str>) -> serde_json::Value {
    let analyzer = make_test_analyzer();
    let (client, server) = tokio::io::duplex(65536);

    // Spawn the analyzer server on the server half
    let mut server_handle = tokio::spawn(async move {
        let (server_rx, server_tx) = tokio::io::split(server);
        if let Ok(service) = rmcp::serve_server(analyzer, (server_rx, server_tx)).await {
            let _ = service.waiting().await;
        }
    });

    let (client_rx, mut client_tx) = tokio::io::split(client);
    let mut reader = BufReader::new(client_rx).lines();

    // Step 1: Send initialize request with optional profile in _meta
    let mut init_params = serde_json::json!({
        "protocolVersion": "2025-11-25",
        "capabilities": {},
        "clientInfo": {"name": "test-client", "version": "0.1.0"}
    });

    if let Some(p) = profile {
        init_params["_meta"] = serde_json::json!({
            "io.clouatre-labs/profile": p
        });
    }

    let init = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": init_params
    })
    .to_string()
        + "\n";
    client_tx.write_all(init.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 2: Read initialize response (discard)
    let _resp = reader.next_line().await.unwrap().unwrap();

    // Step 3: Send initialized notification (no id)
    let notif = serde_json::json!({
        "jsonrpc": "2.0",
        "method": "notifications/initialized",
        "params": {}
    })
    .to_string()
        + "\n";
    client_tx.write_all(notif.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 4: Send tools/list request
    let list_req = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/list",
        "params": {}
    })
    .to_string()
        + "\n";
    client_tx.write_all(list_req.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 5: Race response loop against server handle to surface server panics
    tokio::select! {
        result = async {
            loop {
                let line = reader.next_line().await.unwrap().unwrap();
                let v: serde_json::Value = serde_json::from_str(&line).unwrap();
                if v.get("id") == Some(&serde_json::json!(2)) {
                    return v;
                }
            }
        } => {
            server_handle.abort();
            result
        }
        outcome = &mut server_handle => {
            match outcome {
                Ok(_) => panic!("server task exited unexpectedly before tools/list response"),
                Err(e) => panic!("server task panicked: {e}"),
            }
        }
    }
}

async fn call_tool_with_profile(profile: Option<&str>, tool_name: &str) -> serde_json::Value {
    let analyzer = make_test_analyzer();
    let (client, server) = tokio::io::duplex(65536);

    // Spawn the analyzer server on the server half
    let mut server_handle = tokio::spawn(async move {
        let (server_rx, server_tx) = tokio::io::split(server);
        if let Ok(service) = rmcp::serve_server(analyzer, (server_rx, server_tx)).await {
            let _ = service.waiting().await;
        }
    });

    let (client_rx, mut client_tx) = tokio::io::split(client);
    let mut reader = BufReader::new(client_rx).lines();

    // Step 1: Send initialize request with optional profile in _meta
    let mut init_params = serde_json::json!({
        "protocolVersion": "2025-11-25",
        "capabilities": {},
        "clientInfo": {"name": "test-client", "version": "0.1.0"}
    });

    if let Some(p) = profile {
        init_params["_meta"] = serde_json::json!({
            "io.clouatre-labs/profile": p
        });
    }

    let init = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": init_params
    })
    .to_string()
        + "\n";
    client_tx.write_all(init.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 2: Read initialize response (discard)
    let _resp = reader.next_line().await.unwrap().unwrap();

    // Step 3: Send initialized notification (no id)
    let notif = serde_json::json!({
        "jsonrpc": "2.0",
        "method": "notifications/initialized",
        "params": {}
    })
    .to_string()
        + "\n";
    client_tx.write_all(notif.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 4: Send tools/call request
    let call = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {
            "name": tool_name,
            "arguments": {}
        }
    })
    .to_string()
        + "\n";
    client_tx.write_all(call.as_bytes()).await.unwrap();
    client_tx.flush().await.unwrap();

    // Step 5: Race response loop against server handle to surface server panics
    tokio::select! {
        result = async {
            loop {
                let line = reader.next_line().await.unwrap().unwrap();
                let v: serde_json::Value = serde_json::from_str(&line).unwrap();
                if v.get("id") == Some(&serde_json::json!(2)) {
                    return v;
                }
            }
        } => {
            server_handle.abort();
            result
        }
        outcome = &mut server_handle => {
            match outcome {
                Ok(_) => panic!("server task exited unexpectedly before tool response"),
                Err(e) => panic!("server task panicked: {e}"),
            }
        }
    }
}

#[tokio::test]
async fn test_edit_profile_tool_count() {
    let _guard = env_var_lock();
    // Arrange: initialize with edit profile
    let resp = call_tools_list_with_profile(Some("edit")).await;

    // Act: extract tool count from response
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);
    let tool_names: Vec<String> = tools
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|t| t["name"].as_str().map(|s| s.to_string()))
        .collect();

    // Assert: edit profile should have exactly 3 tools
    assert_eq!(
        tool_count, 3,
        "edit profile should enable exactly 3 tools, got: {:?}",
        tool_names
    );

    // Verify the correct tools are present
    assert!(
        tool_names.contains(&"edit_replace".to_string()),
        "edit profile should include edit_replace"
    );
    assert!(
        tool_names.contains(&"edit_overwrite".to_string()),
        "edit profile should include edit_overwrite"
    );
    assert!(
        tool_names.contains(&"exec_command".to_string()),
        "edit profile should include exec_command"
    );
}

#[tokio::test]
async fn test_analyze_profile_tool_count() {
    let _guard = env_var_lock();
    // Arrange: initialize with analyze profile
    let resp = call_tools_list_with_profile(Some("analyze")).await;

    // Act: extract tool count from response
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);
    let tool_names: Vec<String> = tools
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|t| t["name"].as_str().map(|s| s.to_string()))
        .collect();

    // Assert: analyze profile should have exactly 5 tools
    assert_eq!(
        tool_count, 5,
        "analyze profile should enable exactly 5 tools, got: {:?}",
        tool_names
    );

    // Verify the correct tools are present
    assert!(
        tool_names.contains(&"analyze_directory".to_string()),
        "analyze profile should include analyze_directory"
    );
    assert!(
        tool_names.contains(&"analyze_file".to_string()),
        "analyze profile should include analyze_file"
    );
    assert!(
        tool_names.contains(&"analyze_module".to_string()),
        "analyze profile should include analyze_module"
    );
    assert!(
        tool_names.contains(&"analyze_symbol".to_string()),
        "analyze profile should include analyze_symbol"
    );
    assert!(
        tool_names.contains(&"exec_command".to_string()),
        "analyze profile should include exec_command"
    );
}

#[tokio::test]
async fn test_no_profile_tool_count() {
    let _guard = env_var_lock();
    // Arrange: initialize with no profile metadata; env var must be absent.
    unsafe {
        std::env::remove_var("APTU_CODER_PROFILE");
    }
    let resp = call_tools_list_with_profile(None).await;

    // Act: extract tool count from response
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);

    // Assert: no profile should enable all 7 tools (default behavior)
    assert_eq!(
        tool_count, 7,
        "no profile should enable all 7 tools (default behavior), got: {}",
        tool_count
    );
}

#[tokio::test]
async fn test_unknown_profile_tool_count() {
    let _guard = env_var_lock();
    // Arrange: initialize with unknown profile string; env var must be absent.
    unsafe {
        std::env::remove_var("APTU_CODER_PROFILE");
    }
    let resp = call_tools_list_with_profile(Some("unknown_profile")).await;

    // Act: extract tool count from response
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);

    // Assert: unknown profile should enable all 7 tools (lenient fallback)
    assert_eq!(
        tool_count, 7,
        "unknown profile should enable all 7 tools (lenient fallback), got: {}",
        tool_count
    );
}

#[tokio::test]
async fn test_disabled_tool_returns_invalid_params() {
    let _guard = env_var_lock();
    // Arrange: initialize with edit profile and try to call a disabled tool (analyze_directory)
    let resp = call_tool_with_profile(Some("edit"), "analyze_directory").await;

    // Act: extract error code from response
    let error_code = resp["error"]["code"].as_i64();

    // Assert: calling a disabled tool should return INVALID_PARAMS (-32602)
    assert_eq!(
        error_code,
        Some(-32602),
        "calling a disabled tool should return INVALID_PARAMS (-32602), got: {:?}",
        resp
    );
}

#[tokio::test]
async fn test_profile_env_var_fallback() {
    // Serialize against other env-var-mutating tests.
    let _guard = env_var_lock();

    // Arrange: set APTU_CODER_PROFILE env var to "edit", initialize with no _meta.
    unsafe {
        std::env::set_var("APTU_CODER_PROFILE", "edit");
    }

    let resp = call_tools_list_with_profile(None).await;

    // Cleanup before any assertion so panics cannot leave the env var set.
    unsafe {
        std::env::remove_var("APTU_CODER_PROFILE");
    }

    // Act: extract tool count from response.
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);
    let tool_names: Vec<String> = tools
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|t| t["name"].as_str().map(|s| s.to_string()))
        .collect();

    // Assert: env var profile should be applied when _meta is absent.
    assert_eq!(
        tool_count, 3,
        "env var profile (edit) should enable exactly 3 tools, got: {:?}",
        tool_names
    );
    assert!(
        tool_names.contains(&"edit_replace".to_string()),
        "env var profile should include edit_replace"
    );
}

#[tokio::test]
async fn test_profile_env_var_ignored_when_meta_present() {
    // Serialize against other env-var-mutating tests.
    let _guard = env_var_lock();

    // Arrange: set APTU_CODER_PROFILE=edit but initialize with _meta "analyze".
    // _meta must win.
    unsafe {
        std::env::set_var("APTU_CODER_PROFILE", "edit");
    }

    let resp = call_tools_list_with_profile(Some("analyze")).await;

    // Cleanup before any assertion.
    unsafe {
        std::env::remove_var("APTU_CODER_PROFILE");
    }

    // Act: extract tool count from response.
    let tools = &resp["result"]["tools"];
    let tool_count = tools.as_array().map(|a| a.len()).unwrap_or(0);
    let tool_names: Vec<String> = tools
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|t| t["name"].as_str().map(|s| s.to_string()))
        .collect();

    // Assert: _meta profile (analyze) should override the env var (edit).
    // analyze profile enables 5 tools.
    assert_eq!(
        tool_count, 5,
        "_meta profile (analyze) should take precedence over env var, got: {:?}",
        tool_names
    );
    assert!(
        tool_names.contains(&"analyze_directory".to_string()),
        "_meta profile should include analyze_directory"
    );
}