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
use super::{RequestHandler, Transport};
use crate::error::FastMCPError;
use crate::mcp::types::{JsonRpcError, JsonRpcMessage, RequestId};
use async_trait::async_trait;
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tracing::{error, info};
/// Stdio-based transport for line-delimited JSON-RPC over stdin/stdout.
pub struct StdioTransport {}
impl StdioTransport {
pub fn new() -> Self {
Self {}
}
/// Internal method to handle streams, useful for testing
pub async fn run_loop<R, W>(
&self,
reader: R,
mut writer: W,
handler: Arc<dyn RequestHandler>,
) -> Result<(), FastMCPError>
where
R: tokio::io::AsyncRead + Unpin + Send,
W: tokio::io::AsyncWrite + Unpin + Send,
{
let mut reader = BufReader::new(reader);
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line).await {
Ok(0) => {
info!("EOF on input, shutting down");
break;
}
Ok(_) => {
// Parse line as JSON
match serde_json::from_str::<JsonRpcMessage>(&line) {
Ok(message) => {
info!("Received message: {:?}", message);
match message {
JsonRpcMessage::Request(req) => {
// Dispatch to handler
let id = req.id.clone();
match handler.handle_request(req).await {
Ok(resp) => {
let resp_str = serde_json::to_string(
&JsonRpcMessage::Response(resp),
)
.map_err(FastMCPError::Json)?;
writer
.write_all(resp_str.as_bytes())
.await
.map_err(FastMCPError::StdIo)?;
writer
.write_all(b"\n")
.await
.map_err(FastMCPError::StdIo)?;
}
Err(e) => {
error!("Handler error: {}", e);
// Send generic JSON-RPC error response.
let err_resp = JsonRpcError::new(
id,
-32603, // Internal error
&format!("Internal error: {}", e),
None,
);
let resp_str = serde_json::to_string(
&JsonRpcMessage::Error(err_resp),
)
.map_err(FastMCPError::Json)?;
writer
.write_all(resp_str.as_bytes())
.await
.map_err(FastMCPError::StdIo)?;
writer
.write_all(b"\n")
.await
.map_err(FastMCPError::StdIo)?;
}
}
}
JsonRpcMessage::Notification(notif) => {
if let Err(e) = handler.handle_notification(notif).await {
error!("Notification handler error: {}", e);
}
}
_ => {
// Responses?
// For simple server, we might ignore unless we are acting as client too (loopback).
}
}
writer.flush().await.map_err(FastMCPError::StdIo)?;
}
Err(e) => {
error!("Failed to parse JSON: {}", e);
let err_resp =
JsonRpcError::new(RequestId::Int(0), -32700, "Parse error", None);
let resp_str = serde_json::to_string(&JsonRpcMessage::Error(err_resp))
.map_err(FastMCPError::Json)?;
writer
.write_all(resp_str.as_bytes())
.await
.map_err(FastMCPError::StdIo)?;
writer.write_all(b"\n").await.map_err(FastMCPError::StdIo)?;
writer.flush().await.map_err(FastMCPError::StdIo)?;
}
}
}
Err(e) => {
error!("Error reading input: {}", e);
return Err(FastMCPError::StdIo(e));
}
}
}
Ok(())
}
}
impl Default for StdioTransport {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Transport for StdioTransport {
async fn start(
&self,
handler: Arc<dyn RequestHandler>,
outbound_rx: Option<tokio::sync::broadcast::Receiver<JsonRpcMessage>>,
) -> Result<(), FastMCPError> {
info!("Starting Stdio transport");
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
if let Some(rx) = outbound_rx {
Self::run_loop_with_notifications(tokio::io::BufReader::new(stdin), stdout, handler, rx)
.await
} else {
self.run_loop(stdin, stdout, handler).await
}
}
}
impl StdioTransport {
async fn run_loop_with_notifications<R, W>(
reader: R,
mut writer: W,
handler: Arc<dyn RequestHandler>,
mut rx: tokio::sync::broadcast::Receiver<JsonRpcMessage>,
) -> Result<(), FastMCPError>
where
R: tokio::io::AsyncRead + Unpin + Send,
W: tokio::io::AsyncWrite + Unpin + Send,
{
let mut reader = tokio::io::BufReader::new(reader);
let mut line = String::new();
loop {
tokio::select! {
res = reader.read_line(&mut line) => {
match res {
Ok(0) => {
info!("EOF on input, shutting down");
break;
}
Ok(_) => {
match serde_json::from_str::<JsonRpcMessage>(&line) {
Ok(message) => {
match message {
JsonRpcMessage::Request(req) => {
let id = req.id.clone();
match handler.handle_request(req).await {
Ok(resp) => {
let resp_str = serde_json::to_string(&JsonRpcMessage::Response(resp)).map_err(FastMCPError::Json)?;
writer.write_all(resp_str.as_bytes()).await.map_err(FastMCPError::StdIo)?;
writer.write_all(b"\n").await.map_err(FastMCPError::StdIo)?;
},
Err(e) => {
let err_resp = JsonRpcError::new(id, -32603, &format!("Internal error: {}", e), None);
let resp_str = serde_json::to_string(&JsonRpcMessage::Error(err_resp)).map_err(FastMCPError::Json)?;
writer.write_all(resp_str.as_bytes()).await.map_err(FastMCPError::StdIo)?;
writer.write_all(b"\n").await.map_err(FastMCPError::StdIo)?;
}
}
},
JsonRpcMessage::Notification(notif) => {
let _ = handler.handle_notification(notif).await;
},
_ => {}
}
writer.flush().await.map_err(FastMCPError::StdIo)?;
}
Err(e) => {
error!("Failed to parse message: {}", e);
}
}
line.clear();
}
Err(e) => {
error!("Error reading line: {}", e);
break;
}
}
}
msg = rx.recv() => {
match msg {
Ok(outgoing_msg) => {
let resp_str = serde_json::to_string(&outgoing_msg).map_err(FastMCPError::Json)?;
writer.write_all(resp_str.as_bytes()).await.map_err(FastMCPError::StdIo)?;
writer.write_all(b"\n").await.map_err(FastMCPError::StdIo)?;
writer.flush().await.map_err(FastMCPError::StdIo)?;
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
break;
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
tracing::warn!("Skipped {} lagged messages", n);
}
}
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::server::transport::{JsonRpcRequest, JsonRpcResponse, RequestHandler};
use serde_json::{Value, json};
use std::io::Cursor;
struct MockHandler;
#[async_trait]
impl RequestHandler for MockHandler {
async fn handle_request(
&self,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse, FastMCPError> {
if request.method == "ping" {
Ok(JsonRpcResponse::new(request.id, Value::Null))
} else {
Err(FastMCPError::InvalidRequest("Method not found".to_string()))
}
}
async fn handle_notification(
&self,
_notification: crate::mcp::types::JsonRpcNotification,
) -> Result<(), FastMCPError> {
Ok(())
}
}
#[tokio::test]
async fn test_stdio_ping() {
let transport = StdioTransport::new();
// Input: valid ping request
let input_data = r#"{"jsonrpc": "2.0", "method": "ping", "id": 1}"#;
// Need newline
let input = Cursor::new(format!("{}\n", input_data));
let mut output = Vec::new();
let handler = Arc::new(MockHandler);
transport
.run_loop(input, &mut output, handler)
.await
.unwrap();
let output_str = String::from_utf8(output).unwrap();
let resp: JsonRpcMessage = serde_json::from_str(&output_str).unwrap();
if let JsonRpcMessage::Response(r) = resp {
assert_eq!(r.id, RequestId::Int(1));
} else {
panic!("Expected Response");
}
}
#[tokio::test]
async fn test_stdio_notifications() {
let (tx, rx) = tokio::sync::broadcast::channel(10);
let handler = Arc::new(MockHandler);
// Uses a duplex stream to simulate Stdin that stays open
let (client_read, client_write) = tokio::io::duplex(1024);
let mut output = Vec::new();
// Spawn transport loop
let handle = tokio::spawn(async move {
StdioTransport::run_loop_with_notifications(client_read, &mut output, handler, rx)
.await
.unwrap();
output
});
// Send notification
let notif_msg = JsonRpcMessage::Notification(crate::mcp::types::JsonRpcNotification {
jsonrpc: "2.0".to_string(),
method: "test/method".to_string(),
params: Some(json!({"foo": "bar"})),
});
tx.send(notif_msg).unwrap();
// Allow some time for processing
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
// Drop client_write to signal EOF to reader
drop(client_write);
// Drop sender to signal closed (though loop likely breaks on EOF first)
drop(tx);
let output = handle.await.unwrap();
let output_str = String::from_utf8(output).unwrap();
// Verify output contains the notification
assert!(output_str.contains("test/method"));
assert!(output_str.contains("jsonrpc"));
}
}