mcp-cpp-server 0.2.2

A high-performance Model Context Protocol (MCP) server for C++ code analysis using clangd LSP integration
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Process management layer
//!
//! Handles external process lifecycle and stderr monitoring,
//! completely separate from transport concerns.

use crate::io::transport::{StdioTransport, Transport};
use async_trait::async_trait;
use std::io;
use std::path::PathBuf;
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;
use tracing::{error, info, trace};
// warn! is used in Windows-specific code blocks
#[cfg(windows)]
use tracing::warn;

// ============================================================================
// Process State Management
// ============================================================================

/// How to stop a process
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

pub enum StopMode {
    /// Try graceful shutdown first (SIGTERM), then force kill if needed
    Graceful,
    /// Force kill immediately (SIGKILL)
    #[allow(dead_code)]
    Force,
}

/// Process lifecycle states
#[derive(Debug, Clone, PartialEq, Eq)]

pub enum ProcessState {
    /// Process has not been started yet
    NotStarted,
    /// Process is currently running
    Running { pid: u32 },
    /// Process has been stopped (either gracefully or forcefully)
    Stopped,
}

impl ProcessState {
    /// Get the process ID if the process is running
    pub fn pid(&self) -> Option<u32> {
        match self {
            ProcessState::Running { pid } => Some(*pid),
            _ => None,
        }
    }

    /// Check if the process is currently running
    pub fn is_running(&self) -> bool {
        matches!(self, ProcessState::Running { .. })
    }
}

// ============================================================================
// Process Exit Events
// ============================================================================

/// Event fired when process exits unexpectedly
#[derive(Debug, Clone)]

pub struct ProcessExitEvent {}

// ============================================================================
// Process Restart Handler Trait
// ============================================================================

/// Trait for handling process exit events
#[async_trait]

pub trait ProcessExitHandler: Send + Sync {
    /// Called when process exits unexpectedly
    async fn on_process_exit(&self, event: ProcessExitEvent);
}

// ============================================================================
// Stderr Monitoring Trait
// ============================================================================

/// Trait for monitoring stderr output from external processes
pub trait StderrMonitor: Send + Sync {
    /// Install a handler for stderr lines
    ///
    /// The handler will be called for each line received from stderr.
    /// Only one handler can be active at a time - installing a new handler
    /// will replace the previous one.
    ///
    /// Note: Monitoring starts automatically when the process starts if a handler is installed.
    fn on_stderr_line<F>(&mut self, handler: F)
    where
        F: Fn(String) + Send + Sync + 'static;
}

// ============================================================================
// Process Management
// ============================================================================

/// Error types for process management
#[derive(Debug, thiserror::Error)]

pub enum ProcessError {
    #[error("I/O error: {0}")]
    Io(#[from] io::Error),

    #[error("Process not started")]
    NotStarted,

    #[error("Process already started")]
    AlreadyStarted,

    #[error("Stdin not available")]
    StdinNotAvailable,

    #[error("Stdout not available")]
    StdoutNotAvailable,

    #[error("Stderr not available")]
    StderrNotAvailable,
}

/// Trait for managing external process lifecycle
#[async_trait]

pub trait ProcessManager: Send + Sync {
    type Error: std::error::Error + Send + Sync + 'static;

    /// Start the external process
    async fn start(&mut self) -> Result<(), Self::Error>;

    /// Stop the external process
    async fn stop(&mut self, mode: StopMode) -> Result<(), Self::Error>;

    /// Check if the process is currently running
    fn is_running(&self) -> bool;

    /// Create a stdio transport for communicating with the process
    /// This consumes the stdin/stdout from the process
    fn create_stdio_transport(&mut self) -> Result<StdioTransport, Self::Error>;

    /// Synchronous force kill for Drop trait implementations
    ///
    /// This is a simplified version of stop() that skips async transport cleanup
    /// and directly kills the process. Intended for use in Drop implementations.
    fn kill_sync(&mut self);
}

/// Manages child processes spawned via Command
pub struct ChildProcessManager {
    /// Command to execute
    command: String,

    /// Command arguments
    args: Vec<String>,

    /// Working directory for the process (optional)
    working_directory: Option<PathBuf>,

    /// Thread-safe process state
    state: Arc<Mutex<ProcessState>>,

    /// Stdio transport (created when process starts)
    stdio_transport: Option<StdioTransport>,

    /// Stderr handler
    stderr_handler: Option<Box<dyn Fn(String) + Send + Sync>>,

    /// Stderr monitoring task handle
    stderr_task: Option<JoinHandle<()>>,

    /// Process wait task handle (waits for child to exit)
    wait_task: Option<JoinHandle<()>>,

    /// Process exit event handler
    exit_handler: Option<Arc<dyn ProcessExitHandler>>,
}

impl ChildProcessManager {
    /// Create a new child process manager
    ///
    /// # Arguments
    /// * `command` - The command to execute
    /// * `args` - Command line arguments
    /// * `working_dir` - Optional working directory for the process
    pub fn new(command: String, args: Vec<String>, working_dir: Option<PathBuf>) -> Self {
        Self {
            command,
            args,
            working_directory: working_dir,
            state: Arc::new(Mutex::new(ProcessState::NotStarted)),
            stdio_transport: None,
            stderr_handler: None,
            stderr_task: None,
            wait_task: None,
            exit_handler: None,
        }
    }

    /// Get current process state (thread-safe)
    pub fn get_state(&self) -> ProcessState {
        // Intentional .unwrap() - poisoned mutex indicates serious bug, panic is appropriate
        self.state.lock().unwrap().clone()
    }

    /// Spawn the stderr monitoring task with a provided stderr pipe
    ///
    /// Always drains stderr to prevent child process from blocking.
    /// If a handler is installed, lines are forwarded to it.
    async fn spawn_stderr_monitor_with_pipe(
        &mut self,
        stderr: tokio::process::ChildStderr,
    ) -> Result<(), ProcessError> {
        // Only start if we don't already have a task running
        if self.stderr_task.is_some() {
            return Ok(());
        }

        // Move handler into task (take ownership, no cloning needed)
        let handler = self.stderr_handler.take();

        let task = tokio::spawn(async move {
            let mut reader = BufReader::new(stderr);
            let mut line = String::new();

            trace!(
                "ChildProcessManager: Starting stderr monitoring (handler: {})",
                if handler.is_some() {
                    "installed"
                } else {
                    "draining only"
                }
            );

            loop {
                line.clear();
                match reader.read_line(&mut line).await {
                    Ok(0) => {
                        // EOF reached
                        trace!("ChildProcessManager: stderr EOF reached");
                        break;
                    }
                    Ok(_) => {
                        let line_content = line.trim().to_string();
                        if !line_content.is_empty() {
                            if let Some(ref handler) = handler {
                                // Handler installed - forward the line (direct Box call, no Arc deref)
                                trace!("ChildProcessManager: stderr line: {}", line_content);
                                handler(line_content);
                            } else {
                                // No handler - just drain (optionally trace at debug level)
                                trace!("ChildProcessManager: stderr drained: {}", line_content);
                            }
                        }
                    }
                    Err(e) => {
                        error!("Failed to read from stderr: {}", e);
                        break;
                    }
                }
            }

            trace!("ChildProcessManager: stderr monitoring finished");
        });

        self.stderr_task = Some(task);
        Ok(())
    }

    /// Spawn the wait task that monitors child process exit
    async fn spawn_wait_task(&mut self, mut child: Child) -> Result<(), ProcessError> {
        let current_pid = self.get_state().pid();
        let exit_handler = self.exit_handler.clone();
        let state = Arc::clone(&self.state);

        let task = tokio::spawn(async move {
            trace!(
                "ChildProcessManager: Starting wait task for PID {:?}",
                current_pid
            );

            // Wait for the child process to exit
            match child.wait().await {
                Ok(exit_status) => {
                    info!(
                        "Process PID {:?} exited with status: {}",
                        current_pid, exit_status
                    );

                    // Transition state to Stopped
                    if let Ok(mut process_state) = state.lock() {
                        *process_state = ProcessState::Stopped;
                    }

                    // Fire exit event if handler is present
                    if let Some(handler) = &exit_handler {
                        let event = ProcessExitEvent {};

                        handler.on_process_exit(event).await;
                    }
                }
                Err(e) => {
                    error!("Error waiting for child process: {}", e);

                    // Transition state to Stopped even on error
                    if let Ok(mut process_state) = state.lock() {
                        *process_state = ProcessState::Stopped;
                    }

                    // Fire exit event for error case too
                    if let Some(handler) = &exit_handler {
                        let event = ProcessExitEvent {};

                        handler.on_process_exit(event).await;
                    }
                }
            }

            trace!(
                "ChildProcessManager: Wait task finished for PID {:?}",
                current_pid
            );
        });

        self.wait_task = Some(task);
        Ok(())
    }
}

#[async_trait]
impl ProcessManager for ChildProcessManager {
    type Error = ProcessError;

    async fn start(&mut self) -> Result<(), Self::Error> {
        // Simple check - don't start if already running
        if self.is_running() {
            return Err(ProcessError::AlreadyStarted);
        }

        info!("Starting process: {} {:?}", self.command, self.args);

        let mut command_builder = Command::new(&self.command);
        command_builder
            .args(&self.args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Set working directory if specified
        if let Some(working_dir) = &self.working_directory {
            command_builder.current_dir(working_dir);
        }

        let mut child = command_builder.spawn()?;

        let pid = child.id();
        info!("Process started with PID: {:?}", pid);

        // Update state to Running with PID
        if let Some(pid) = pid {
            // Intentional .unwrap() - poisoned mutex indicates serious bug, panic is appropriate
            *self.state.lock().unwrap() = ProcessState::Running { pid };
        } else {
            return Err(ProcessError::Io(std::io::Error::other(
                "Failed to get process ID",
            )));
        }

        // Extract stdio streams immediately before moving child to wait task
        let stdin = child.stdin.take().ok_or(ProcessError::StdinNotAvailable)?;
        let stdout = child
            .stdout
            .take()
            .ok_or(ProcessError::StdoutNotAvailable)?;
        let stderr = child
            .stderr
            .take()
            .ok_or(ProcessError::StderrNotAvailable)?;

        // Create and store stdio transport
        self.stdio_transport = Some(StdioTransport::new(stdin, stdout));

        // Always start stderr monitoring to prevent clangd from blocking
        // Handler is optional - if not installed, lines are just drained
        self.spawn_stderr_monitor_with_pipe(stderr).await?;

        // Start wait task with the child process (this consumes the child)
        self.spawn_wait_task(child).await?;

        Ok(())
    }

    async fn stop(&mut self, mode: StopMode) -> Result<(), Self::Error> {
        // Simply check if we have a running process
        let pid = match self.get_state().pid() {
            Some(pid) => pid,
            None => return Err(ProcessError::NotStarted),
        };

        match mode {
            StopMode::Graceful => info!("Gracefully stopping process with PID: {}", pid),
            StopMode::Force => info!("Force killing process with PID: {}", pid),
        }

        // Close stdio transport first (may trigger graceful shutdown)
        if let Some(mut transport) = self.stdio_transport.take() {
            let _ = transport.close().await; // Ignore errors during shutdown
        }

        // Send termination signal to process
        #[cfg(unix)]
        {
            unsafe {
                match mode {
                    StopMode::Graceful => {
                        // Send SIGTERM for graceful shutdown
                        if libc::kill(pid as libc::pid_t, libc::SIGTERM) == 0 {
                            info!("Sent SIGTERM to process {}", pid);
                        }
                        // Don't wait here - let the wait task detect exit naturally
                        // If process doesn't exit within reasonable time, orchestrator can call stop(Force)
                    }
                    StopMode::Force => {
                        // Force kill immediately
                        libc::kill(pid as libc::pid_t, libc::SIGKILL);
                        info!("Sent SIGKILL to process {}", pid);
                    }
                }
            }
        }
        #[cfg(not(unix))]
        {
            // On Windows, this is more complex - would need different approach
            warn!("Windows process termination not fully implemented");
        }

        // Stop stderr monitoring task
        if let Some(task) = self.stderr_task.take() {
            task.abort();
        }

        // Update state immediately for API consistency
        // The wait task will also update state when it detects the actual exit
        // Intentional .unwrap() - poisoned mutex indicates serious bug, panic is appropriate
        *self.state.lock().unwrap() = ProcessState::Stopped;

        Ok(())
    }

    fn is_running(&self) -> bool {
        self.get_state().is_running()
    }

    fn create_stdio_transport(&mut self) -> Result<StdioTransport, Self::Error> {
        self.stdio_transport.take().ok_or(ProcessError::NotStarted)
    }

    fn kill_sync(&mut self) {
        let pid = match self.get_state().pid() {
            Some(pid) => pid,
            None => return, // Already stopped
        };

        info!("Synchronously force killing process with PID: {}", pid);

        // Skip transport closure (async) - just kill the process directly
        #[cfg(unix)]
        {
            unsafe {
                libc::kill(pid as libc::pid_t, libc::SIGKILL);
                info!("Sent SIGKILL to process {}", pid);
            }
        }

        #[cfg(not(unix))]
        {
            warn!("Windows sync process kill not implemented - process may remain");
        }

        // Stop stderr monitoring task
        if let Some(task) = self.stderr_task.take() {
            task.abort();
        }

        // Update state
        // Intentional .unwrap() - poisoned mutex indicates serious bug, panic is appropriate
        *self.state.lock().unwrap() = ProcessState::Stopped;

        // Note: Transport cleanup will happen when Drop is called on transport
        // Wait task will detect process exit and clean up naturally
    }
}

impl StderrMonitor for ChildProcessManager {
    fn on_stderr_line<F>(&mut self, handler: F)
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        self.stderr_handler = Some(Box::new(handler));
    }
}

// ============================================================================
// Mock Process Manager (for testing)
// ============================================================================

/// Mock process manager for testing
#[cfg(test)]
#[allow(dead_code)]
pub struct MockProcessManager {
    running: bool,
    process_id: Option<u32>,
}

#[cfg(test)]
#[allow(dead_code)]
impl MockProcessManager {
    /// Create a new mock process manager
    pub fn new() -> Self {
        Self {
            running: false,
            process_id: None,
        }
    }
}

#[cfg(test)]
impl Default for MockProcessManager {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
#[async_trait]
impl ProcessManager for MockProcessManager {
    type Error = ProcessError;

    async fn start(&mut self) -> Result<(), Self::Error> {
        if self.running {
            return Err(ProcessError::AlreadyStarted);
        }
        self.running = true;
        self.process_id = Some(12345); // Mock PID
        Ok(())
    }

    async fn stop(&mut self, _mode: StopMode) -> Result<(), Self::Error> {
        self.running = false;
        self.process_id = None;
        Ok(())
    }

    fn is_running(&self) -> bool {
        self.running
    }

    fn create_stdio_transport(&mut self) -> Result<StdioTransport, Self::Error> {
        // For testing purposes, we can't create a real StdioTransport
        // In practice, tests would use MockTransport directly
        Err(ProcessError::NotStarted)
    }

    fn kill_sync(&mut self) {
        // Mock implementation - simply reset state like stop()
        self.running = false;
        self.process_id = None;
    }
}

#[cfg(test)]
impl StderrMonitor for MockProcessManager {
    fn on_stderr_line<F>(&mut self, _callback: F)
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        // Mock implementation - no-op since we don't have real stderr in tests
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    #[tokio::test]
    async fn test_child_process_manager_lifecycle() {
        let mut manager =
            ChildProcessManager::new("echo".to_string(), vec!["hello".to_string()], None);

        assert!(!manager.is_running());

        // Start process
        manager.start().await.unwrap();

        assert!(manager.is_running());

        // Stop process
        manager.stop(StopMode::Graceful).await.unwrap();

        assert!(!manager.is_running());
    }

    #[tokio::test]
    async fn test_stderr_monitoring() {
        let mut manager = ChildProcessManager::new(
            "sh".to_string(),
            vec![
                "-c".to_string(),
                "echo 'error message' >&2; sleep 1".to_string(),
            ],
            None,
        );

        let stderr_lines = Arc::new(Mutex::new(Vec::<String>::new()));
        let stderr_lines_clone = Arc::clone(&stderr_lines);

        manager.on_stderr_line(move |line| {
            if let Ok(mut lines) = stderr_lines_clone.lock() {
                lines.push(line);
            }
        });

        manager.start().await.unwrap();

        // Wait a bit for stderr to be captured
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        manager.stop(StopMode::Graceful).await.unwrap();

        let lines = stderr_lines.lock().unwrap();
        assert!(!lines.is_empty());
        assert_eq!(lines[0], "error message");
    }

    #[tokio::test]
    async fn test_process_state_transitions() {
        let mut manager =
            ChildProcessManager::new("echo".to_string(), vec!["hello".to_string()], None);

        // Initial state should be NotStarted
        assert_eq!(manager.get_state(), ProcessState::NotStarted);
        assert!(!manager.is_running());

        // Start process - should transition to Running
        manager.start().await.unwrap();
        let running_state = manager.get_state();
        assert!(matches!(running_state, ProcessState::Running { .. }));
        assert!(manager.is_running());

        // Stop process - should transition to Stopped
        manager.stop(StopMode::Graceful).await.unwrap();
        assert_eq!(manager.get_state(), ProcessState::Stopped);
        assert!(!manager.is_running());
    }

    #[tokio::test]
    async fn test_invalid_operations() {
        let mut manager =
            ChildProcessManager::new("echo".to_string(), vec!["hello".to_string()], None);

        // Cannot stop when not started
        let result = manager.stop(StopMode::Graceful).await;
        assert!(matches!(result, Err(ProcessError::NotStarted)));

        // Start process
        manager.start().await.unwrap();

        // Cannot start when already running
        let result = manager.start().await;
        assert!(matches!(result, Err(ProcessError::AlreadyStarted)));

        // Stop process
        manager.stop(StopMode::Graceful).await.unwrap();

        // Can stop again - just returns NotStarted error (simple behavior)
        let result = manager.stop(StopMode::Graceful).await;
        assert!(matches!(result, Err(ProcessError::NotStarted)));
    }

    #[tokio::test]
    async fn test_create_transport_simple() {
        let mut manager =
            ChildProcessManager::new("echo".to_string(), vec!["hello".to_string()], None);

        // Cannot create transport when not started
        let result = manager.create_stdio_transport();
        assert!(matches!(result, Err(ProcessError::NotStarted)));

        // Start process
        manager.start().await.unwrap();

        // Should be able to create transport when running (consumes it)
        let _transport = manager.create_stdio_transport().unwrap();

        // Transport is consumed, so second call fails
        let result = manager.create_stdio_transport();
        assert!(matches!(result, Err(ProcessError::NotStarted)));
    }

    #[test]
    fn test_process_state_methods() {
        let not_started = ProcessState::NotStarted;
        assert!(!not_started.is_running());
        assert!(not_started.pid().is_none());

        let running = ProcessState::Running { pid: 12345 };
        assert!(running.is_running());
        assert_eq!(running.pid(), Some(12345));

        let stopped = ProcessState::Stopped;
        assert!(!stopped.is_running());
        assert!(stopped.pid().is_none());
    }
}