-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathstdio.rs
278 lines (242 loc) · 9.18 KB
/
stdio.rs
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
use std::collections::HashMap;
use std::sync::Arc;
use tokio::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command};
use async_trait::async_trait;
use mcp_spec::protocol::JsonRpcMessage;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::sync::{mpsc, Mutex};
use super::{send_message, Error, PendingRequests, Transport, TransportHandle, TransportMessage};
/// A `StdioTransport` uses a child process's stdin/stdout as a communication channel.
///
/// It uses channels for message passing and handles responses asynchronously through a background task.
pub struct StdioActor {
receiver: mpsc::Receiver<TransportMessage>,
pending_requests: Arc<PendingRequests>,
_process: Child, // we store the process to keep it alive
error_sender: mpsc::Sender<Error>,
stdin: ChildStdin,
stdout: ChildStdout,
stderr: ChildStderr,
}
impl StdioActor {
pub async fn run(mut self) {
use tokio::pin;
let incoming = Self::handle_incoming_messages(self.stdout, self.pending_requests.clone());
let outgoing = Self::handle_outgoing_messages(
self.receiver,
self.stdin,
self.pending_requests.clone(),
);
// take ownership of futures for tokio::select
pin!(incoming);
pin!(outgoing);
// Use select! to wait for either I/O completion or process exit
tokio::select! {
result = &mut incoming => {
tracing::debug!("Stdin handler completed: {:?}", result);
}
result = &mut outgoing => {
tracing::debug!("Stdout handler completed: {:?}", result);
}
// capture the status so we don't need to wait for a timeout
status = self._process.wait() => {
tracing::debug!("Process exited with status: {:?}", status);
}
}
// Then always try to read stderr before cleaning up
let mut stderr_buffer = Vec::new();
if let Ok(bytes) = self.stderr.read_to_end(&mut stderr_buffer).await {
let err_msg = if bytes > 0 {
String::from_utf8_lossy(&stderr_buffer).to_string()
} else {
"Process ended unexpectedly".to_string()
};
tracing::info!("Process stderr: {}", err_msg);
let _ = self
.error_sender
.send(Error::StdioProcessError(err_msg))
.await;
}
// Clean up regardless of which path we took
self.pending_requests.clear().await;
}
async fn handle_incoming_messages(stdout: ChildStdout, pending_requests: Arc<PendingRequests>) {
let mut reader = BufReader::new(stdout);
let mut line = String::new();
loop {
match reader.read_line(&mut line).await {
Ok(0) => {
tracing::error!("Child process ended (EOF on stdout)");
break;
} // EOF
Ok(_) => {
if let Ok(message) = serde_json::from_str::<JsonRpcMessage>(&line) {
tracing::debug!(
message = ?message,
"Received incoming message"
);
match &message {
JsonRpcMessage::Response(response) => {
if let Some(id) = &response.id {
pending_requests.respond(&id.to_string(), Ok(message)).await;
}
}
JsonRpcMessage::Error(error) => {
if let Some(id) = &error.id {
pending_requests.respond(&id.to_string(), Ok(message)).await;
}
}
_ => {} // TODO: Handle other variants (Request, etc.)
}
}
line.clear();
}
Err(e) => {
tracing::error!(error = ?e, "Error reading line");
break;
}
}
}
}
async fn handle_outgoing_messages(
mut receiver: mpsc::Receiver<TransportMessage>,
mut stdin: ChildStdin,
pending_requests: Arc<PendingRequests>,
) {
while let Some(mut transport_msg) = receiver.recv().await {
let message_str = match serde_json::to_string(&transport_msg.message) {
Ok(s) => s,
Err(e) => {
if let Some(tx) = transport_msg.response_tx.take() {
let _ = tx.send(Err(Error::Serialization(e)));
}
continue;
}
};
tracing::debug!(message = ?transport_msg.message, "Sending outgoing message");
if let Some(response_tx) = transport_msg.response_tx.take() {
if let JsonRpcMessage::Request(request) = &transport_msg.message {
if let Some(id) = &request.id {
pending_requests.insert(id.to_string(), response_tx).await;
}
}
}
if let Err(e) = stdin
.write_all(format!("{}\n", message_str).as_bytes())
.await
{
tracing::error!(error = ?e, "Error writing message to child process");
pending_requests.clear().await;
break;
}
if let Err(e) = stdin.flush().await {
tracing::error!(error = ?e, "Error flushing message to child process");
pending_requests.clear().await;
break;
}
}
}
}
#[derive(Clone)]
pub struct StdioTransportHandle {
sender: mpsc::Sender<TransportMessage>,
error_receiver: Arc<Mutex<mpsc::Receiver<Error>>>,
}
#[async_trait::async_trait]
impl TransportHandle for StdioTransportHandle {
async fn send(&self, message: JsonRpcMessage) -> Result<JsonRpcMessage, Error> {
let result = send_message(&self.sender, message).await;
// Check for any pending errors even if send is successful
self.check_for_errors().await?;
result
}
}
impl StdioTransportHandle {
/// Check if there are any process errors
pub async fn check_for_errors(&self) -> Result<(), Error> {
match self.error_receiver.lock().await.try_recv() {
Ok(error) => {
tracing::debug!("Found error: {:?}", error);
Err(error)
}
Err(_) => Ok(()),
}
}
}
pub struct StdioTransport {
command: String,
args: Vec<String>,
env: HashMap<String, String>,
}
impl StdioTransport {
pub fn new<S: Into<String>>(
command: S,
args: Vec<String>,
env: HashMap<String, String>,
) -> Self {
Self {
command: command.into(),
args,
env,
}
}
async fn spawn_process(&self) -> Result<(Child, ChildStdin, ChildStdout, ChildStderr), Error> {
let mut command = Command::new(&self.command);
command
.envs(&self.env)
.args(&self.args)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.kill_on_drop(true);
// Set process group only on Unix systems
#[cfg(unix)]
command.process_group(0); // don't inherit signal handling from parent process
// Hide console window on Windows
#[cfg(windows)]
command.creation_flags(0x08000000); // CREATE_NO_WINDOW flag
let mut process = command
.spawn()
.map_err(|e| Error::StdioProcessError(e.to_string()))?;
let stdin = process
.stdin
.take()
.ok_or_else(|| Error::StdioProcessError("Failed to get stdin".into()))?;
let stdout = process
.stdout
.take()
.ok_or_else(|| Error::StdioProcessError("Failed to get stdout".into()))?;
let stderr = process
.stderr
.take()
.ok_or_else(|| Error::StdioProcessError("Failed to get stderr".into()))?;
Ok((process, stdin, stdout, stderr))
}
}
#[async_trait]
impl Transport for StdioTransport {
type Handle = StdioTransportHandle;
async fn start(&self) -> Result<Self::Handle, Error> {
let (process, stdin, stdout, stderr) = self.spawn_process().await?;
let (message_tx, message_rx) = mpsc::channel(32);
let (error_tx, error_rx) = mpsc::channel(1);
let actor = StdioActor {
receiver: message_rx,
pending_requests: Arc::new(PendingRequests::new()),
_process: process,
error_sender: error_tx,
stdin,
stdout,
stderr,
};
tokio::spawn(actor.run());
let handle = StdioTransportHandle {
sender: message_tx,
error_receiver: Arc::new(Mutex::new(error_rx)),
};
Ok(handle)
}
async fn close(&self) -> Result<(), Error> {
Ok(())
}
}