radkit 0.0.5

Rust AI Agent Development Kit
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::errors::{AgentError, AgentResult};
use dashmap::DashMap;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use rmcp::{
    model::{
        ClientCapabilities, ClientInfo, Implementation, InitializeRequestParam, ProtocolVersion,
    },
    transport::{
        streamable_http_client::StreamableHttpClientTransportConfig, ConfigureCommandExt,
        StreamableHttpClientTransport, TokioChildProcess,
    },
    ServiceExt,
};
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use tokio::process::Command;
use tracing::{debug, info, warn};

/// Type alias for the MCP client - based on rmcp examples
pub type MCPClient =
    rmcp::service::RunningService<rmcp::service::RoleClient, InitializeRequestParam>;

/// Connection parameters for MCP servers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MCPConnectionParams {
    /// Connect to a local MCP server via stdio
    Stdio {
        command: String,
        args: Vec<String>,
        #[serde(default)]
        env: HashMap<String, String>,
        #[serde(default = "default_timeout")]
        timeout: Duration,
    },
    /// Connect to an MCP server via HTTP with SSE support
    Http {
        url: String,
        #[serde(default)]
        headers: HashMap<String, String>,
        #[serde(default = "default_timeout")]
        timeout: Duration,
    },
}

const fn default_timeout() -> Duration {
    Duration::from_secs(5)
}

/// Manages MCP client sessions with pooling and automatic reconnection
pub struct MCPSessionManager {
    connection_params: MCPConnectionParams,
    /// Session pool: maps session keys to client instances
    sessions: Arc<DashMap<String, Arc<MCPClient>>>,
}

impl MCPSessionManager {
    /// Create a new session manager with the given connection parameters
    #[must_use]
    pub fn new(params: MCPConnectionParams) -> Self {
        Self {
            connection_params: params,
            sessions: Arc::new(DashMap::new()),
        }
    }

    /// Generate a session key based on connection parameters and headers
    /// This ensures each unique connection configuration gets its own session
    fn generate_session_key(&self, headers: Option<&HashMap<String, String>>) -> String {
        let mut hasher = DefaultHasher::new();

        match &self.connection_params {
            MCPConnectionParams::Stdio {
                command,
                args,
                env,
                timeout,
            } => {
                // Hash the stdio connection parameters to create unique session keys
                "stdio".hash(&mut hasher);
                command.hash(&mut hasher);
                for arg in args {
                    arg.hash(&mut hasher);
                }
                // Hash environment variables in a deterministic order
                let mut env_sorted: Vec<_> = env.iter().collect();
                env_sorted.sort_by_key(|&(k, _)| k);
                for (k, v) in env_sorted {
                    k.hash(&mut hasher);
                    v.hash(&mut hasher);
                }
                timeout.as_millis().hash(&mut hasher);
                format!("stdio_{:x}", hasher.finish())
            }
            MCPConnectionParams::Http {
                url,
                headers: param_headers,
                timeout,
            } => {
                // Hash the HTTP URL, headers from params, and timeout
                "http".hash(&mut hasher);
                url.hash(&mut hasher);

                // Hash headers from connection params (base headers for the client)
                let mut sorted: Vec<_> = param_headers.iter().collect();
                sorted.sort_by_key(|&(k, _)| k);
                for (k, v) in sorted {
                    k.hash(&mut hasher);
                    v.hash(&mut hasher);
                }

                // Also hash per-session headers if provided
                if let Some(hdrs) = headers {
                    let mut sorted: Vec<_> = hdrs.iter().collect();
                    sorted.sort_by_key(|&(k, _)| k);
                    for (k, v) in sorted {
                        k.hash(&mut hasher);
                        v.hash(&mut hasher);
                    }
                }

                // Hash timeout like we do for stdio
                timeout.as_millis().hash(&mut hasher);

                format!("http_{:x}", hasher.finish())
            }
        }
    }

    /// Check if a client is disconnected by attempting a lightweight operation
    async fn is_disconnected(&self, client: &MCPClient) -> bool {
        // Try to list tools as a lightweight way to check if connection is alive
        // This is an async operation that will fail if the connection is dead
        if let Ok(Ok(_)) = tokio::time::timeout(
            Duration::from_millis(500), // Short timeout for quick check
            client.list_all_tools(),
        )
        .await
        {
            // Connection is alive and tools were listed successfully
            false
        } else {
            // Either an MCP error or timeout indicates disconnection
            debug!("Connection check failed - assuming disconnected");
            true
        }
    }

    /// Create a new MCP client with the configured parameters
    async fn create_client(&self) -> AgentResult<MCPClient> {
        match &self.connection_params {
            MCPConnectionParams::Stdio {
                command, args, env, ..
            } => {
                debug!("Creating stdio MCP client: {} {:?}", command, args);

                let mut cmd = Command::new(command);
                cmd.args(args);
                for (key, value) in env {
                    cmd.env(key, value);
                }

                let transport = TokioChildProcess::new(cmd.configure(|_| {})).map_err(|e| {
                    AgentError::ToolSetupFailed {
                        tool_name: "mcp_stdio".to_string(),
                        reason: format!("Failed to spawn MCP process: {e}"),
                    }
                })?;

                // Create proper client info for MCP handshake
                let client_info = ClientInfo {
                    protocol_version: ProtocolVersion::default(),
                    capabilities: ClientCapabilities::default(),
                    client_info: Implementation {
                        name: "radkit-mcp-client".to_string(),
                        version: env!("CARGO_PKG_VERSION").to_string(),
                        title: None,
                        website_url: None,
                        icons: None,
                    },
                };

                let client = client_info.serve(transport).await.map_err(|e| {
                    AgentError::ToolSetupFailed {
                        tool_name: "mcp_stdio".to_string(),
                        reason: format!("Failed to connect to MCP server: {e:?}"),
                    }
                })?;

                info!("Connected to MCP server via stdio");
                Ok(client)
            }
            MCPConnectionParams::Http {
                url,
                headers,
                timeout,
            } => {
                debug!("Creating HTTP MCP client for: {}", url);

                // Build custom reqwest client with headers and timeout
                let mut header_map = HeaderMap::new();
                for (key, value) in headers {
                    let header_name = HeaderName::from_bytes(key.as_bytes()).map_err(|e| {
                        AgentError::ToolSetupFailed {
                            tool_name: "mcp_http".to_string(),
                            reason: format!("Invalid header name '{key}': {e}"),
                        }
                    })?;
                    let header_value =
                        HeaderValue::from_str(value).map_err(|e| AgentError::ToolSetupFailed {
                            tool_name: "mcp_http".to_string(),
                            reason: format!("Invalid header value for '{key}': {e}"),
                        })?;
                    header_map.insert(header_name, header_value);
                }

                let reqwest_client = reqwest::Client::builder()
                    .default_headers(header_map)
                    .timeout(*timeout)
                    .build()
                    .map_err(|e| AgentError::ToolSetupFailed {
                        tool_name: "mcp_http".to_string(),
                        reason: format!("Failed to build HTTP client: {e}"),
                    })?;

                // Create HTTP transport with custom client and config
                let config = StreamableHttpClientTransportConfig::with_uri(url.clone());
                let transport = StreamableHttpClientTransport::with_client(reqwest_client, config);

                // Create proper client info for MCP handshake
                let client_info = ClientInfo {
                    protocol_version: ProtocolVersion::default(),
                    capabilities: ClientCapabilities::default(),
                    client_info: Implementation {
                        name: "radkit-mcp-client".to_string(),
                        version: env!("CARGO_PKG_VERSION").to_string(),
                        title: None,
                        website_url: None,
                        icons: None,
                    },
                };

                let client = client_info.serve(transport).await.map_err(|e| {
                    AgentError::ToolSetupFailed {
                        tool_name: "mcp_http".to_string(),
                        reason: format!("Failed to connect to MCP HTTP server: {e:?}"),
                    }
                })?;

                info!("Connected to MCP server via HTTP: {}", url);
                Ok(client)
            }
        }
    }

    /// Creates or retrieves a session, handling disconnections automatically
    ///
    /// # Errors
    ///
    /// Returns an error if the MCP server connection cannot be established
    pub async fn create_session(
        &self,
        headers: Option<HashMap<String, String>>,
    ) -> AgentResult<Arc<MCPClient>> {
        let session_key = self.generate_session_key(headers.as_ref());

        // Check if we have an existing session
        if let Some(existing) = self.sessions.get(&session_key) {
            let client = existing.value().clone();
            if !self.is_disconnected(&client).await {
                debug!("Reusing existing MCP session: {}", session_key);
                return Ok(client);
            }
            // Session is disconnected, remove it
            drop(existing); // Release the read lock
            self.sessions.remove(&session_key);
            info!("Removed disconnected MCP session: {}", session_key);
        }

        // Create new session
        info!("Creating new MCP session: {}", session_key);
        let client = self.create_client().await?;
        let client_arc = Arc::new(client);

        // Store in session pool
        self.sessions
            .insert(session_key.clone(), client_arc.clone());

        Ok(client_arc)
    }

    /// Close all sessions and clean up resources
    pub async fn close(&self) {
        info!("Closing all MCP sessions");
        let session_keys: Vec<_> = self
            .sessions
            .iter()
            .map(|entry| entry.key().clone())
            .collect();

        for key in session_keys {
            if let Some((_, client)) = self.sessions.remove(&key) {
                // Try to get ownership of the client for cancellation
                match Arc::try_unwrap(client) {
                    Ok(owned_client) => {
                        if let Err(e) = owned_client.cancel().await {
                            warn!("Error closing MCP session {}: {:?}", key, e);
                        }
                    }
                    Err(shared_client) => {
                        warn!(
                            "Could not close MCP session {}: still has {} references",
                            key,
                            Arc::strong_count(&shared_client)
                        );
                    }
                }
            }
        }

        self.sessions.clear();
    }
}

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

    #[test]
    fn test_http_params_with_headers_and_timeout() {
        // Verify HTTP params can be created with headers and timeout
        let mut headers = HashMap::new();
        headers.insert("Authorization".to_string(), "Bearer token123".to_string());
        headers.insert("X-Custom-Header".to_string(), "custom-value".to_string());

        let params = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: headers.clone(),
            timeout: Duration::from_secs(30),
        };

        // Verify params are properly structured
        if let MCPConnectionParams::Http {
            url,
            headers: h,
            timeout,
        } = params
        {
            assert_eq!(url, "https://example.com/mcp");
            assert_eq!(h.len(), 2);
            assert_eq!(h.get("Authorization"), Some(&"Bearer token123".to_string()));
            assert_eq!(h.get("X-Custom-Header"), Some(&"custom-value".to_string()));
            assert_eq!(timeout, Duration::from_secs(30));
        } else {
            panic!("Expected HTTP params");
        }
    }

    #[test]
    fn test_session_key_generation_includes_headers() {
        // Create manager with HTTP params
        let mut headers1 = HashMap::new();
        headers1.insert("Authorization".to_string(), "Bearer token1".to_string());

        let params1 = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: headers1.clone(),
            timeout: Duration::from_secs(30),
        };

        let manager1 = MCPSessionManager::new(params1);
        let key1 = manager1.generate_session_key(None);

        // Create manager with different headers
        let mut headers2 = HashMap::new();
        headers2.insert("Authorization".to_string(), "Bearer token2".to_string());

        let params2 = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: headers2.clone(),
            timeout: Duration::from_secs(30),
        };

        let manager2 = MCPSessionManager::new(params2);
        let key2 = manager2.generate_session_key(None);

        // Different headers should produce different session keys
        assert_ne!(key1, key2, "Session keys should differ when headers differ");
    }

    #[test]
    fn test_session_key_generation_http_includes_url() {
        // Same headers, different URLs
        let headers = HashMap::new();

        let params1 = MCPConnectionParams::Http {
            url: "https://example.com/mcp1".to_string(),
            headers: headers.clone(),
            timeout: Duration::from_secs(30),
        };

        let manager1 = MCPSessionManager::new(params1);
        let key1 = manager1.generate_session_key(None);

        let params2 = MCPConnectionParams::Http {
            url: "https://example.com/mcp2".to_string(),
            headers,
            timeout: Duration::from_secs(30),
        };

        let manager2 = MCPSessionManager::new(params2);
        let key2 = manager2.generate_session_key(None);

        // Different URLs should produce different session keys
        assert_ne!(key1, key2, "Session keys should differ when URLs differ");

        // Both should start with "http_"
        assert!(key1.starts_with("http_"));
        assert!(key2.starts_with("http_"));
    }

    #[test]
    fn test_session_key_stdio_vs_http() {
        // Create stdio params
        let stdio_params = MCPConnectionParams::Stdio {
            command: "test".to_string(),
            args: vec![],
            env: HashMap::new(),
            timeout: Duration::from_secs(5),
        };

        let stdio_manager = MCPSessionManager::new(stdio_params);
        let stdio_key = stdio_manager.generate_session_key(None);

        // Create HTTP params
        let http_params = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: HashMap::new(),
            timeout: Duration::from_secs(30),
        };

        let http_manager = MCPSessionManager::new(http_params);
        let http_key = http_manager.generate_session_key(None);

        // Different connection types should produce different keys
        assert_ne!(stdio_key, http_key);
        assert!(stdio_key.starts_with("stdio_"));
        assert!(http_key.starts_with("http_"));
    }

    #[test]
    fn test_session_key_deterministic() {
        // Same params should produce same key
        let mut headers = HashMap::new();
        headers.insert("Authorization".to_string(), "Bearer token".to_string());

        let params1 = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: headers.clone(),
            timeout: Duration::from_secs(30),
        };

        let manager1 = MCPSessionManager::new(params1);

        let params2 = MCPConnectionParams::Http {
            url: "https://example.com/mcp".to_string(),
            headers: headers.clone(),
            timeout: Duration::from_secs(30),
        };

        let manager2 = MCPSessionManager::new(params2);

        let key1 = manager1.generate_session_key(None);
        let key2 = manager2.generate_session_key(None);

        // Same params should produce same session key
        assert_eq!(
            key1, key2,
            "Session keys should be deterministic for same params"
        );
    }
}