| 65 | #[async_trait] |
| 66 | impl McpTransport for StdioTransport { |
| 67 | async fn send(&self, request: &JsonRpcRequest) -> Result<(), McpClientError> { |
| 68 | let mut stdin_guard = self.stdin.lock().await; |
| 69 | let stdin = stdin_guard |
| 70 | .as_mut() |
| 71 | .ok_or_else(|| McpClientError::TransportError("Process not running".to_string()))?; |
| 72 | |
| 73 | let content = serde_json::to_string(request).map_err(|e| { |
| 74 | McpClientError::ProtocolError(format!("Failed to serialize request: {}", e)) |
| 75 | })?; |
| 76 | |
| 77 | let message = format!("Content-Length: {}\r\n\r\n{}", content.len(), content); |
| 78 | |
| 79 | stdin |
| 80 | .write_all(message.as_bytes()) |
| 81 | .await |
| 82 | .map_err(|e| McpClientError::TransportError(format!("Failed to write: {}", e)))?; |
| 83 | |
| 84 | stdin |
| 85 | .flush() |
| 86 | .await |
| 87 | .map_err(|e| McpClientError::TransportError(format!("Failed to flush: {}", e)))?; |
| 88 | |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | async fn receive(&self) -> Result<Option<JsonRpcMessage>, McpClientError> { |
| 93 | let mut process_guard = self.process.lock().await; |