1use crate::mcp::{ClientFactory, Connection, Connector, McpError, Tool, ToolResult, loopback_url};
2use crate::mcp_http::BoundedHttp;
3use rmcp::{RoleClient, ServiceExt, service::RunningService};
4use std::{process::Stdio, time::Duration};
5use tokio::io::AsyncRead;
6use tokio_util::sync::CancellationToken;
7pub struct BoundedLines<Reader> {
8 inner: Reader,
9 line: usize,
10 total: usize,
11}
12impl<Reader> BoundedLines<Reader> {
13 pub fn new(inner: Reader) -> Self {
14 Self {
15 inner,
16 line: 0,
17 total: 0,
18 }
19 }
20}
21impl<Reader: AsyncRead + Unpin> AsyncRead for BoundedLines<Reader> {
22 fn poll_read(
23 self: std::pin::Pin<&mut Self>,
24 context: &mut std::task::Context<'_>,
25 output: &mut tokio::io::ReadBuf<'_>,
26 ) -> std::task::Poll<std::io::Result<()>> {
27 let this = self.get_mut();
28 let mut bytes = [0u8; 8192];
29 let length = output.remaining().min(bytes.len());
30 if length == 0 {
31 return std::task::Poll::Ready(Ok(()));
32 }
33 let mut buffer = tokio::io::ReadBuf::new(&mut bytes[..length]);
34 match std::pin::Pin::new(&mut this.inner).poll_read(context, &mut buffer) {
35 std::task::Poll::Ready(Ok(())) => {
36 for byte in buffer.filled() {
37 this.total += 1;
38 if *byte == b'\n' {
39 this.line = 0;
40 } else {
41 this.line += 1;
42 }
43 if this.line > 1048576 || this.total > 64 * 1024 * 1024 {
44 return std::task::Poll::Ready(Err(std::io::Error::other(
45 "MCP input limit",
46 )));
47 }
48 }
49 output.put_slice(buffer.filled());
50 std::task::Poll::Ready(Ok(()))
51 }
52 result => result,
53 }
54 }
55}
56pub struct SdkFactory;
57struct SdkConnection {
58 service: RunningService<RoleClient, ()>,
59 child: Option<tokio::process::Child>,
60}
61#[async_trait::async_trait]
62impl ClientFactory for SdkFactory {
63 async fn connect(
64 &self,
65 connector: &Connector,
66 cancel: &CancellationToken,
67 ) -> Result<Box<dyn Connection>, McpError> {
68 connector.validate()?;
69 if cancel.is_cancelled() {
70 return Err(McpError::Cancelled);
71 }
72 let mut child = None;
73 let service = match connector {
74 Connector::Stdio {
75 command, args, env, ..
76 } => {
77 let binary = crate::process_control::resolve_binary(command)
78 .map_err(|_| McpError::Unavailable)?;
79 let mut process = tokio::process::Command::new(binary);
80 process
81 .args(args)
82 .env_clear()
83 .envs(crate::process_control::minimal_env());
84 if let Some(env) = env {
85 process.envs(env);
86 }
87 let mut process = process
88 .stdin(Stdio::piped())
89 .stdout(Stdio::piped())
90 .stderr(Stdio::null())
91 .kill_on_drop(true)
92 .spawn()
93 .map_err(|_| McpError::Transport)?;
94 let output = process.stdout.take().ok_or(McpError::Transport)?;
95 let input = process.stdin.take().ok_or(McpError::Transport)?;
96 child = Some(process);
97 let transport = rmcp::transport::async_rw::AsyncRwTransport::new_client(
98 BoundedLines::new(output),
99 input,
100 );
101 tokio::select! {biased;_=cancel.cancelled()=>return Err(McpError::Cancelled),result=tokio::time::timeout(Duration::from_secs(15),().serve(transport))=>result.map_err(|_|McpError::Transport)?.map_err(|_|McpError::Transport)?}
102 }
103 Connector::Http { url, .. } => {
104 let url = loopback_url(url)?;
105 let addresses = [
106 std::net::SocketAddr::from(([127, 0, 0, 1], 0)),
107 std::net::SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 1], 0)),
108 ];
109 let client = reqwest_mcp::Client::builder()
110 .redirect(reqwest_mcp::redirect::Policy::none())
111 .no_proxy()
112 .resolve_to_addrs("localhost", &addresses)
113 .connect_timeout(Duration::from_secs(5))
114 .timeout(Duration::from_secs(60))
115 .build()
116 .map_err(|_| McpError::Transport)?;
117 let mut config=rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig::with_uri(url.as_str());
118 config.max_sse_event_size = 1048576;
119 config.max_concurrent_requests = 1;
120 config.channel_buffer_capacity = 16;
121 config.reinit_on_expired_session = false;
122 let transport = rmcp::transport::StreamableHttpClientTransport::with_client(
123 BoundedHttp(client.clone()),
124 config,
125 );
126 let connected = tokio::select! {biased;_=cancel.cancelled()=>return Err(McpError::Cancelled),result=tokio::time::timeout(Duration::from_secs(7),().serve(transport))=>result};
127 match connected {
128 Ok(Ok(service)) => service,
129 _ => {
130 let legacy = async {
131 let transport =
132 crate::mcp_legacy::LegacySse::open(client, url, cancel).await?;
133 ().serve(transport).await.map_err(|_| McpError::Transport)
134 };
135 tokio::select! {biased;_=cancel.cancelled()=>return Err(McpError::Cancelled),result=tokio::time::timeout(Duration::from_secs(7),legacy)=>result.map_err(|_|McpError::Transport)??}
136 }
137 }
138 }
139 };
140 Ok(Box::new(SdkConnection { service, child }))
141 }
142}
143#[async_trait::async_trait]
144impl Connection for SdkConnection {
145 async fn tools(&mut self, cancel: &CancellationToken) -> Result<Vec<Tool>, McpError> {
146 let mut tools = Vec::new();
147 let mut cursor = None;
148 let mut seen = std::collections::HashSet::new();
149 loop {
150 let params = cursor.clone().map(|cursor| {
151 let mut params = rmcp::model::PaginatedRequestParams::default();
152 params.cursor = Some(cursor);
153 params
154 });
155 let result = tokio::select! {biased;_=cancel.cancelled()=>return Err(McpError::Cancelled),result=self.service.list_tools(params)=>result.map_err(|_|McpError::Transport)?};
156 for tool in result.tools {
157 tools.push(Tool {
158 name: tool.name.into_owned(),
159 description: tool
160 .description
161 .map(|value| value.into_owned())
162 .unwrap_or_default(),
163 input_schema: serde_json::to_value(tool.input_schema)
164 .map_err(|_| McpError::Invalid)?,
165 });
166 if tools.len() > 1024 {
167 return Err(McpError::Limit);
168 }
169 }
170 cursor = result.next_cursor;
171 if let Some(cursor) = &cursor {
172 if !seen.insert(cursor.clone()) || seen.len() > 32 {
173 return Err(McpError::Limit);
174 }
175 } else {
176 break;
177 }
178 }
179 Ok(tools)
180 }
181 async fn call(
182 &mut self,
183 name: &str,
184 args: serde_json::Value,
185 cancel: &CancellationToken,
186 ) -> Result<ToolResult, McpError> {
187 let params: rmcp::model::CallToolRequestParams =
188 serde_json::from_value(serde_json::json!({"name":name,"arguments":args}))
189 .map_err(|_| McpError::Invalid)?;
190 let result = tokio::select! {biased;_=cancel.cancelled()=>return Err(McpError::Cancelled),result=self.service.call_tool(params)=>result.map_err(|_|McpError::Transport)?};
191 let value = serde_json::to_value(result).map_err(|_| McpError::Invalid)?;
192 let mut content = Vec::new();
193 let mut bytes = 0;
194 for block in value["content"].as_array().ok_or(McpError::Invalid)? {
195 if block["type"] == "text" {
196 let text = block["text"].as_str().ok_or(McpError::Invalid)?;
197 bytes += text.len();
198 if bytes > 1048576 {
199 return Err(McpError::Limit);
200 }
201 content.push(text);
202 }
203 }
204 Ok(ToolResult {
205 content: content.join("\n"),
206 is_error: value["isError"] == true,
207 })
208 }
209 async fn close(&mut self) -> Result<(), McpError> {
210 let closed = self
211 .service
212 .close_with_timeout(Duration::from_secs(3))
213 .await
214 .map_err(|_| McpError::Transport);
215 if let Some(mut child) = self.child.take() {
216 let _ = tokio::time::timeout(Duration::from_secs(2), child.kill()).await;
217 }
218 if closed?.is_none() {
219 return Err(McpError::Transport);
220 }
221 Ok(())
222 }
223}
224
225#[cfg(test)]
226mod tests {
227 use super::*;
228 use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
229
230 #[tokio::test]
231 async fn official_sdk_discovers_and_invokes_over_in_memory_stdio() {
232 let (client, server) = tokio::io::duplex(65536);
233 let server = tokio::spawn(async move {
234 let (input, mut output) = tokio::io::split(server);
235 let mut lines = BufReader::new(input).lines();
236 while let Some(line) = lines.next_line().await.unwrap() {
237 let request: serde_json::Value = serde_json::from_str(&line).unwrap();
238 let Some(id) = request.get("id") else {
239 continue;
240 };
241 let result = match request["method"].as_str().unwrap() {
242 "initialize" => {
243 serde_json::json!({"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"serverInfo":{"name":"fixture","version":"1"}})
244 }
245 "tools/list" => {
246 serde_json::json!({"tools":[{"name":"echo","description":"read echo","inputSchema":{"type":"object"}}]})
247 }
248 "tools/call" => {
249 serde_json::json!({"content":[{"type":"text","text":request["params"]["arguments"]["text"]}]})
250 }
251 other => panic!("unexpected request {other}"),
252 };
253 output
254 .write_all(
255 format!(
256 "{}\n",
257 serde_json::json!({"jsonrpc":"2.0","id":id,"result":result})
258 )
259 .as_bytes(),
260 )
261 .await
262 .unwrap();
263 }
264 });
265 let (input, output) = tokio::io::split(client);
266 let transport = rmcp::transport::async_rw::AsyncRwTransport::new_client(
267 BoundedLines::new(input),
268 output,
269 );
270 let service = tokio::time::timeout(Duration::from_secs(1), ().serve(transport))
271 .await
272 .unwrap()
273 .unwrap();
274 let mut connection = SdkConnection {
275 service,
276 child: None,
277 };
278 let cancel = CancellationToken::new();
279 assert_eq!(connection.tools(&cancel).await.unwrap()[0].name, "echo");
280 let result = connection
281 .call("echo", serde_json::json!({"text":"hello"}), &cancel)
282 .await
283 .unwrap();
284 assert_eq!(result.content, "hello");
285 connection.close().await.unwrap();
286 drop(connection);
287 tokio::time::timeout(Duration::from_secs(1), server)
288 .await
289 .unwrap()
290 .unwrap();
291 }
292}