mocopr-core 0.1.0

Core types and protocol implementation for MoCoPr (Model Context Protocol)
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
//! Standard I/O transport implementation

use super::*;
use crate::error::TransportError;
use std::process::Stdio;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
use tracing::{debug, trace, warn};

/// Transport statistics
#[derive(Debug, Default)]
pub struct TransportStats {
    /// Number of messages sent
    pub messages_sent: u64,
    /// Number of messages received
    pub messages_received: u64,
    /// Number of bytes sent
    pub bytes_sent: u64,
    /// Number of bytes received
    pub bytes_received: u64,
}

/// Standard I/O transport for communicating with processes
pub struct StdioTransport {
    io: StdioIO,
    child: Option<Child>,
    stats: TransportStats,
}

/// Enum to handle different I/O types
enum StdioIO {
    /// Child process I/O
    Child {
        stdin: ChildStdin,
        stdout: BufReader<ChildStdout>,
    },
    /// Current process I/O
    Current {
        stdin: BufReader<tokio::io::Stdin>,
        stdout: tokio::io::Stdout,
    },
    /// No I/O (default state)
    None,
}

impl StdioTransport {
    /// Create a new stdio transport
    pub fn new() -> Self {
        Self {
            io: StdioIO::None,
            child: None,
            stats: TransportStats::default(),
        }
    }

    /// Create a new stdio transport from process handles
    pub fn from_process(stdin: ChildStdin, stdout: ChildStdout, child: Child) -> Self {
        Self {
            io: StdioIO::Child {
                stdin,
                stdout: BufReader::new(stdout),
            },
            child: Some(child),
            stats: TransportStats::default(),
        }
    }

    /// Create a new stdio transport by spawning a command
    pub async fn spawn<I, S>(command: &str, args: I) -> Result<Self>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<std::ffi::OsStr>,
    {
        let mut cmd = Command::new(command);
        cmd.args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = cmd.spawn().map_err(|e| {
            TransportError::ConnectionFailed(format!("Failed to spawn command '{command}': {e}"))
        })?;

        let stdin = child.stdin.take().ok_or_else(|| {
            TransportError::ConnectionFailed("Failed to get stdin handle".to_string())
        })?;

        let stdout = child.stdout.take().ok_or_else(|| {
            TransportError::ConnectionFailed("Failed to get stdout handle".to_string())
        })?;

        Ok(Self::from_process(stdin, stdout, child))
    }

    /// Use the current process's stdin/stdout
    pub fn current_process() -> Self {
        let stdin = BufReader::new(tokio::io::stdin());
        let stdout = tokio::io::stdout();

        Self {
            io: StdioIO::Current { stdin, stdout },
            child: None,
            stats: TransportStats::default(),
        }
    }

    /// Check if the transport is ready for communication
    pub fn is_ready(&self) -> bool {
        matches!(self.io, StdioIO::Child { .. } | StdioIO::Current { .. })
    }

    /// Get transport statistics
    pub fn stats(&self) -> &TransportStats {
        &self.stats
    }

    /// Kill the child process if it exists
    pub async fn kill(&mut self) -> Result<()> {
        if let Some(mut child) = self.child.take() {
            child.kill().await.map_err(|e| {
                Error::Transport(TransportError::ConnectionFailed(format!(
                    "Failed to kill child process: {e}"
                )))
            })?;
        }
        Ok(())
    }

    /// Wait for the child process to exit
    pub async fn wait(&mut self) -> Result<std::process::ExitStatus> {
        if let Some(mut child) = self.child.take() {
            child.wait().await.map_err(|e| {
                Error::Transport(TransportError::ConnectionFailed(format!(
                    "Failed to wait for child: {e}"
                )))
            })
        } else {
            Err(Error::Transport(TransportError::Closed))
        }
    }
}

#[async_trait]
impl Transport for StdioTransport {
    async fn send(&mut self, message: &str) -> Result<()> {
        trace!("Sending message via stdio: {}", message);

        match &mut self.io {
            StdioIO::Child { stdin, .. } => {
                let line = format!("{message}\n");
                stdin.write_all(line.as_bytes()).await.map_err(|e| {
                    Error::Transport(TransportError::SendFailed(format!(
                        "Failed to write to stdin: {e}"
                    )))
                })?;

                stdin.flush().await.map_err(|e| {
                    Error::Transport(TransportError::SendFailed(format!(
                        "Failed to flush stdin: {e}"
                    )))
                })?;

                self.stats.messages_sent += 1;
                self.stats.bytes_sent += line.len() as u64;

                Ok(())
            }
            StdioIO::Current { stdout, .. } => {
                let line = format!("{message}\n");
                stdout.write_all(line.as_bytes()).await.map_err(|e| {
                    Error::Transport(TransportError::SendFailed(format!(
                        "Failed to write to stdout: {e}"
                    )))
                })?;

                stdout.flush().await.map_err(|e| {
                    Error::Transport(TransportError::SendFailed(format!(
                        "Failed to flush stdout: {e}"
                    )))
                })?;

                self.stats.messages_sent += 1;
                self.stats.bytes_sent += line.len() as u64;

                Ok(())
            }
            StdioIO::None => Err(Error::Transport(TransportError::NotReady)),
        }
    }

    async fn receive(&mut self) -> Result<Option<String>> {
        trace!("Receiving message via stdio");

        match &mut self.io {
            StdioIO::Child { stdout, .. } => {
                let mut line = String::new();
                match stdout.read_line(&mut line).await {
                    Ok(0) => {
                        // EOF - connection closed
                        Ok(None)
                    }
                    Ok(_) => {
                        // Remove trailing newline
                        if line.ends_with('\n') {
                            line.pop();
                            if line.ends_with('\r') {
                                line.pop();
                            }
                        }

                        self.stats.messages_received += 1;
                        self.stats.bytes_received += line.len() as u64;

                        trace!("Received message: {}", line);
                        Ok(Some(line))
                    }
                    Err(e) => {
                        warn!("Failed to read from stdout: {}", e);
                        Err(Error::Transport(TransportError::ReceiveFailed(format!(
                            "Failed to read from stdout: {e}"
                        ))))
                    }
                }
            }
            StdioIO::Current { stdin, .. } => {
                let mut line = String::new();
                match stdin.read_line(&mut line).await {
                    Ok(0) => {
                        // EOF - connection closed
                        Ok(None)
                    }
                    Ok(_) => {
                        // Remove trailing newline
                        if line.ends_with('\n') {
                            line.pop();
                            if line.ends_with('\r') {
                                line.pop();
                            }
                        }

                        self.stats.messages_received += 1;
                        self.stats.bytes_received += line.len() as u64;

                        trace!("Received message: {}", line);
                        Ok(Some(line))
                    }
                    Err(e) => {
                        warn!("Failed to read from stdin: {}", e);
                        Err(Error::Transport(TransportError::ReceiveFailed(format!(
                            "Failed to read from stdin: {e}"
                        ))))
                    }
                }
            }
            StdioIO::None => Err(Error::Transport(TransportError::NotReady)),
        }
    }

    async fn close(&mut self) -> Result<()> {
        debug!("Closing stdio transport");

        // Close I/O based on the type
        match std::mem::replace(&mut self.io, StdioIO::None) {
            StdioIO::Child { mut stdin, .. } => {
                let _ = stdin.shutdown().await;
            }
            StdioIO::Current { stdout, .. } => {
                // stdout will be closed when dropped
                drop(stdout);
            }
            StdioIO::None => {}
        }

        // Wait for child process to exit
        if let Some(mut child) = self.child.take() {
            tokio::spawn(async move {
                let _ = child.wait().await;
            });
        }

        Ok(())
    }

    fn transport_type(&self) -> &'static str {
        "stdio"
    }

    fn is_connected(&self) -> bool {
        self.is_ready() && self.child.is_some()
    }
}

impl Default for StdioTransport {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[tokio::test]
    async fn test_stdio_transport_creation() {
        let transport = StdioTransport::new();
        assert!(!transport.is_ready());
        assert_eq!(transport.transport_type(), "stdio");
    }

    #[tokio::test]
    async fn test_stdio_transport_stats() {
        let transport = StdioTransport::new();
        let stats = transport.stats();
        assert_eq!(stats.messages_sent, 0);
        assert_eq!(stats.messages_received, 0);
        assert_eq!(stats.bytes_sent, 0);
        assert_eq!(stats.bytes_received, 0);
    }

    #[tokio::test]
    async fn test_stdio_transport_message_serialization() {
        let _transport = StdioTransport::new();

        // Test valid JSON
        let valid_message = json!({
            "jsonrpc": "2.0",
            "method": "test",
            "id": 1
        });

        // We can't actually send/receive without a running process,
        // but we can test the structure
        assert!(valid_message.is_object());
        assert_eq!(valid_message["jsonrpc"], "2.0");
    }

    #[tokio::test]
    async fn test_stdio_transport_error_conditions() {
        let transport = StdioTransport::new();

        // Test that transport is not ready initially
        assert!(!transport.is_ready());

        // Test stats on unconnected transport
        let stats = transport.stats();
        assert_eq!(stats.messages_sent, 0);
        assert_eq!(stats.messages_received, 0);
    }

    #[tokio::test]
    async fn test_transport_stats_fields() {
        let stats = TransportStats {
            messages_sent: 10,
            messages_received: 15,
            bytes_sent: 1024,
            bytes_received: 2048,
        };

        assert_eq!(stats.messages_sent, 10);
        assert_eq!(stats.messages_received, 15);
        assert_eq!(stats.bytes_sent, 1024);
        assert_eq!(stats.bytes_received, 2048);
    }

    #[tokio::test]
    async fn test_transport_factory() {
        // Test stdio transport creation
        let transport =
            super::super::TransportFactory::create(super::super::TransportConfig::Stdio).await;
        assert!(transport.is_ok());

        let transport = transport.unwrap();
        assert_eq!(transport.transport_type(), "stdio");
    }

    #[tokio::test]
    async fn test_transport_spawn_invalid_command() {
        // Test spawning an invalid command
        let result = StdioTransport::spawn("nonexistent_command_12345", Vec::<String>::new()).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_transport_config_types() {
        // Test different transport config types
        let stdio_config = super::super::TransportConfig::Stdio;
        let ws_config = super::super::TransportConfig::WebSocket {
            url: "ws://localhost:8080".to_string(),
        };
        let http_config = super::super::TransportConfig::Http {
            url: "http://localhost:8080".to_string(),
        };

        // Just verify they can be created - actual connection testing would require running servers
        assert!(matches!(stdio_config, super::super::TransportConfig::Stdio));
        assert!(matches!(
            ws_config,
            super::super::TransportConfig::WebSocket { .. }
        ));
        assert!(matches!(
            http_config,
            super::super::TransportConfig::Http { .. }
        ));
    }
}