client_manager.rs 19.9 KB
Newer Older
1
2
use std::{borrow::Cow, collections::HashMap, time::Duration};

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
use backoff::ExponentialBackoffBuilder;
use dashmap::DashMap;
use rmcp::{
    model::{
        CallToolRequestParam, GetPromptRequestParam, GetPromptResult, Prompt,
        ReadResourceRequestParam, ReadResourceResult, Resource, Tool as McpTool,
    },
    service::RunningService,
    transport::{
        sse_client::SseClientConfig, streamable_http_client::StreamableHttpClientTransportConfig,
        ConfigureCommandExt, SseClientTransport, StreamableHttpClientTransport, TokioChildProcess,
    },
    RoleClient, ServiceExt,
};
use serde::{Deserialize, Serialize};

use crate::mcp::{
    config::{McpConfig, McpServerConfig, McpTransport},
    error::{McpError, McpResult},
};

/// Information about an available tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolInfo {
    pub name: String,
    pub description: String,
    pub server: String,
    pub parameters: Option<serde_json::Value>,
}

/// Information about an available prompt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptInfo {
    pub name: String,
    pub description: Option<String>,
    pub server: String,
    pub arguments: Option<Vec<serde_json::Value>>,
}

/// Information about an available resource
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceInfo {
    pub uri: String,
    pub name: String,
    pub description: Option<String>,
    pub mime_type: Option<String>,
    pub server: String,
}

/// Manages MCP client connections and tool execution
pub struct McpClientManager {
    /// Map of server_name -> MCP client
    clients: HashMap<String, RunningService<RoleClient, ()>>,
    /// Map of tool_name -> (server_name, tool_definition)
    tools: DashMap<String, (String, McpTool)>,
    /// Map of prompt_name -> (server_name, prompt_definition)
    prompts: DashMap<String, (String, Prompt)>,
    /// Map of resource_uri -> (server_name, resource_definition)
    resources: DashMap<String, (String, Resource)>,
}

impl McpClientManager {
    /// Create a new manager and connect to all configured servers
    pub async fn new(config: McpConfig) -> McpResult<Self> {
        let mut mgr = Self {
            clients: HashMap::new(),
            tools: DashMap::new(),
            prompts: DashMap::new(),
            resources: DashMap::new(),
        };

        for server_config in config.servers {
            match Self::connect_server(&server_config).await {
                Ok(client) => {
                    mgr.load_server_inventory(&server_config.name, &client)
                        .await;
                    mgr.clients.insert(server_config.name.clone(), client);
                }
                Err(e) => {
                    tracing::error!(
                        "Failed to connect to server '{}': {}",
                        server_config.name,
                        e
                    );
                }
            }
        }

        if mgr.clients.is_empty() {
            return Err(McpError::ConnectionFailed(
                "Failed to connect to any MCP servers".to_string(),
            ));
        }
        Ok(mgr)
    }

    /// Discover and cache tools/prompts/resources for a connected server
    async fn load_server_inventory(
        &self,
        server_name: &str,
        client: &RunningService<RoleClient, ()>,
    ) {
        // Tools
        match client.peer().list_all_tools().await {
            Ok(ts) => {
                tracing::info!("Discovered {} tools from '{}'", ts.len(), server_name);
                for t in ts {
                    if self.tools.contains_key(t.name.as_ref()) {
                        tracing::warn!(
                            "Tool '{}' from server '{}' is overwriting an existing tool.",
                            &t.name,
                            server_name
                        );
                    }
                    self.tools
                        .insert(t.name.to_string(), (server_name.to_string(), t));
                }
            }
            Err(e) => tracing::warn!("Failed to list tools from '{}': {}", server_name, e),
        }

        // Prompts
        match client.peer().list_all_prompts().await {
            Ok(ps) => {
                tracing::info!("Discovered {} prompts from '{}'", ps.len(), server_name);
                for p in ps {
                    if self.prompts.contains_key(&p.name) {
                        tracing::warn!(
                            "Prompt '{}' from server '{}' is overwriting an existing prompt.",
                            &p.name,
                            server_name
                        );
                    }
                    self.prompts
                        .insert(p.name.clone(), (server_name.to_string(), p));
                }
            }
            Err(e) => tracing::debug!("No prompts or failed to list on '{}': {}", server_name, e),
        }

        // Resources
        match client.peer().list_all_resources().await {
            Ok(rs) => {
                tracing::info!("Discovered {} resources from '{}'", rs.len(), server_name);
                for r in rs {
                    if self.resources.contains_key(&r.uri) {
                        tracing::warn!(
                            "Resource '{}' from server '{}' is overwriting an existing resource.",
                            &r.uri,
                            server_name
                        );
                    }
                    self.resources
                        .insert(r.uri.clone(), (server_name.to_string(), r));
                }
            }
            Err(e) => tracing::debug!("No resources or failed to list on '{}': {}", server_name, e),
        }
    }

    /// Connect to a single MCP server with retry logic for remote transports
    async fn connect_server(config: &McpServerConfig) -> McpResult<RunningService<RoleClient, ()>> {
        let needs_retry = matches!(
            &config.transport,
            McpTransport::Sse { .. } | McpTransport::Streamable { .. }
        );
        if needs_retry {
            Self::connect_server_with_retry(config).await
        } else {
            Self::connect_server_impl(config).await
        }
    }

    /// Connect with exponential backoff retry for remote servers
    async fn connect_server_with_retry(
        config: &McpServerConfig,
    ) -> McpResult<RunningService<RoleClient, ()>> {
        let backoff = ExponentialBackoffBuilder::new()
            .with_initial_interval(Duration::from_secs(1))
            .with_max_interval(Duration::from_secs(30))
183
            .with_max_elapsed_time(Some(Duration::from_secs(30)))
184
185
186
187
188
189
            .build();

        backoff::future::retry(backoff, || async {
            match Self::connect_server_impl(config).await {
                Ok(client) => Ok(client),
                Err(e) => {
190
191
192
193
194
195
196
197
198
199
200
                    if Self::is_permanent_error(&e) {
                        tracing::error!(
                            "Permanent error connecting to '{}': {} - not retrying",
                            config.name,
                            e
                        );
                        Err(backoff::Error::permanent(e))
                    } else {
                        tracing::warn!("Failed to connect to '{}', retrying: {}", config.name, e);
                        Err(backoff::Error::transient(e))
                    }
201
202
203
204
205
206
                }
            }
        })
        .await
    }

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    /// Determine if an error is permanent (should not retry) or transient (should retry)
    fn is_permanent_error(error: &McpError) -> bool {
        match error {
            McpError::Config(_) => true,
            McpError::Auth(_) => true,
            McpError::ServerNotFound(_) => true,
            McpError::Transport(_) => true,
            McpError::ConnectionFailed(msg) => {
                msg.contains("initialize")
                    || msg.contains("connection closed")
                    || msg.contains("connection refused")
                    || msg.contains("invalid URL")
                    || msg.contains("not found")
            }
            // Tool-related errors shouldn't occur during connection
            _ => false,
        }
    }

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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    /// Internal implementation of server connection
    async fn connect_server_impl(
        config: &McpServerConfig,
    ) -> McpResult<RunningService<RoleClient, ()>> {
        tracing::info!(
            "Connecting to MCP server '{}' via {:?}",
            config.name,
            config.transport
        );

        match &config.transport {
            McpTransport::Stdio {
                command,
                args,
                envs,
            } => {
                let transport = TokioChildProcess::new(
                    tokio::process::Command::new(command).configure(|cmd| {
                        cmd.args(args)
                            .envs(envs.iter())
                            .stderr(std::process::Stdio::inherit());
                    }),
                )
                .map_err(|e| McpError::Transport(format!("create stdio transport: {}", e)))?;

                let client = ().serve(transport).await.map_err(|e| {
                    McpError::ConnectionFailed(format!("initialize stdio client: {}", e))
                })?;

                tracing::info!("Connected to stdio server '{}'", config.name);
                Ok(client)
            }

            McpTransport::Sse { url, token } => {
                let transport = if let Some(tok) = token {
                    let client = reqwest::Client::builder()
                        .default_headers({
                            let mut headers = reqwest::header::HeaderMap::new();
                            headers.insert(
                                reqwest::header::AUTHORIZATION,
                                format!("Bearer {}", tok).parse().map_err(|e| {
                                    McpError::Transport(format!("auth token: {}", e))
                                })?,
                            );
                            headers
                        })
                        .build()
                        .map_err(|e| McpError::Transport(format!("build HTTP client: {}", e)))?;

                    let cfg = SseClientConfig {
                        sse_endpoint: url.clone().into(),
                        ..Default::default()
                    };

                    SseClientTransport::start_with_client(client, cfg)
                        .await
                        .map_err(|e| McpError::Transport(format!("create SSE transport: {}", e)))?
                } else {
                    SseClientTransport::start(url.as_str())
                        .await
                        .map_err(|e| McpError::Transport(format!("create SSE transport: {}", e)))?
                };

                let client = ().serve(transport).await.map_err(|e| {
                    McpError::ConnectionFailed(format!("initialize SSE client: {}", e))
                })?;

                tracing::info!("Connected to SSE server '{}' at {}", config.name, url);
                Ok(client)
            }

            McpTransport::Streamable { url, token } => {
                let transport = if let Some(tok) = token {
                    let mut cfg = StreamableHttpClientTransportConfig::with_uri(url.as_str());
                    cfg.auth_header = Some(format!("Bearer {}", tok));
                    StreamableHttpClientTransport::from_config(cfg)
                } else {
                    StreamableHttpClientTransport::from_uri(url.as_str())
                };

                let client = ().serve(transport).await.map_err(|e| {
                    McpError::ConnectionFailed(format!("initialize streamable client: {}", e))
                })?;

                tracing::info!(
                    "Connected to streamable HTTP server '{}' at {}",
                    config.name,
                    url
                );
                Ok(client)
            }
        }
    }

    fn client_for(&self, server_name: &str) -> McpResult<&RunningService<RoleClient, ()>> {
        self.clients
            .get(server_name)
            .ok_or_else(|| McpError::ServerNotFound(server_name.to_string()))
    }

    fn tool_entry(&self, name: &str) -> McpResult<(String, McpTool)> {
        self.tools
            .get(name)
            .map(|e| e.value().clone())
            .ok_or_else(|| McpError::ToolNotFound(name.to_string()))
    }

    fn prompt_entry(&self, name: &str) -> McpResult<(String, Prompt)> {
        self.prompts
            .get(name)
            .map(|e| e.value().clone())
            .ok_or_else(|| McpError::PromptNotFound(name.to_string()))
    }

    fn resource_entry(&self, uri: &str) -> McpResult<(String, Resource)> {
        self.resources
            .get(uri)
            .map(|e| e.value().clone())
            .ok_or_else(|| McpError::ResourceNotFound(uri.to_string()))
    }

    /// Call a tool by name
    pub async fn call_tool(
        &self,
        tool_name: &str,
        arguments: Option<serde_json::Map<String, serde_json::Value>>,
    ) -> McpResult<rmcp::model::CallToolResult> {
        let (server_name, _tool) = self.tool_entry(tool_name)?;
        let client = self.client_for(&server_name)?;

        tracing::debug!("Calling tool '{}' on '{}'", tool_name, server_name);

        client
            .peer()
            .call_tool(CallToolRequestParam {
                name: Cow::Owned(tool_name.to_string()),
                arguments,
            })
            .await
            .map_err(|e| McpError::ToolExecution(format!("Tool call failed: {}", e)))
    }

    /// Get all available tools
    pub fn list_tools(&self) -> Vec<ToolInfo> {
        self.tools
            .iter()
            .map(|entry| {
                let tool_name = entry.key().clone();
                let (server_name, tool) = entry.value();
                ToolInfo {
                    name: tool_name,
                    description: tool.description.as_deref().unwrap_or_default().to_string(),
                    server: server_name.clone(),
                    parameters: Some(serde_json::Value::Object((*tool.input_schema).clone())),
                }
            })
            .collect()
    }

    /// Get a specific tool by name
    pub fn get_tool(&self, name: &str) -> Option<ToolInfo> {
        self.tools.get(name).map(|entry| {
            let (server_name, tool) = entry.value();
            ToolInfo {
                name: name.to_string(),
                description: tool.description.as_deref().unwrap_or_default().to_string(),
                server: server_name.clone(),
                parameters: Some(serde_json::Value::Object((*tool.input_schema).clone())),
            }
        })
    }

    /// Check if a tool exists
    pub fn has_tool(&self, name: &str) -> bool {
        self.tools.contains_key(name)
    }

    /// Get list of connected servers
    pub fn list_servers(&self) -> Vec<String> {
        self.clients.keys().cloned().collect()
    }

    /// Get a prompt by name with arguments
    pub async fn get_prompt(
        &self,
        prompt_name: &str,
        arguments: Option<serde_json::Map<String, serde_json::Value>>,
    ) -> McpResult<GetPromptResult> {
        let (server_name, _prompt) = self.prompt_entry(prompt_name)?;
        let client = self.client_for(&server_name)?;

        tracing::debug!("Getting prompt '{}' from '{}'", prompt_name, server_name);

        client
            .peer()
            .get_prompt(GetPromptRequestParam {
                name: prompt_name.to_string(),
                arguments,
            })
            .await
            .map_err(|e| McpError::ToolExecution(format!("Failed to get prompt: {}", e)))
    }

    /// List all available prompts
    pub fn list_prompts(&self) -> Vec<PromptInfo> {
        self.prompts
            .iter()
            .map(|entry| {
                let name = entry.key().clone();
                let (server_name, prompt) = entry.value();
                PromptInfo {
                    name,
                    description: prompt.description.clone(),
                    server: server_name.clone(),
                    arguments: prompt
                        .arguments
                        .clone()
                        .map(|args| args.into_iter().map(|arg| serde_json::json!(arg)).collect()),
                }
            })
            .collect()
    }

    /// Get a specific prompt info by name
    pub fn get_prompt_info(&self, name: &str) -> Option<PromptInfo> {
        self.prompts.get(name).map(|entry| {
            let (server_name, prompt) = entry.value();
            PromptInfo {
                name: name.to_string(),
                description: prompt.description.clone(),
                server: server_name.clone(),
                arguments: prompt
                    .arguments
                    .clone()
                    .map(|args| args.into_iter().map(|arg| serde_json::json!(arg)).collect()),
            }
        })
    }

    /// Read a resource by URI
    pub async fn read_resource(&self, uri: &str) -> McpResult<ReadResourceResult> {
        let (server_name, _resource) = self.resource_entry(uri)?;
        let client = self.client_for(&server_name)?;

        tracing::debug!("Reading resource '{}' from '{}'", uri, server_name);

        client
            .peer()
            .read_resource(ReadResourceRequestParam {
                uri: uri.to_string(),
            })
            .await
            .map_err(|e| McpError::ToolExecution(format!("Failed to read resource: {}", e)))
    }

    /// List all available resources
    pub fn list_resources(&self) -> Vec<ResourceInfo> {
        self.resources
            .iter()
            .map(|entry| {
                let uri = entry.key().clone();
                let (server_name, resource) = entry.value();
                ResourceInfo {
                    uri,
                    name: resource.name.clone(),
                    description: resource.description.clone(),
                    mime_type: resource.mime_type.clone(),
                    server: server_name.clone(),
                }
            })
            .collect()
    }

    /// Get a specific resource info by URI
    pub fn get_resource_info(&self, uri: &str) -> Option<ResourceInfo> {
        self.resources.get(uri).map(|entry| {
            let (server_name, resource) = entry.value();
            ResourceInfo {
                uri: uri.to_string(),
                name: resource.name.clone(),
                description: resource.description.clone(),
                mime_type: resource.mime_type.clone(),
                server: server_name.clone(),
            }
        })
    }

    /// Subscribe to resource changes
    pub async fn subscribe_resource(&self, uri: &str) -> McpResult<()> {
        let (server_name, _resource) = self.resource_entry(uri)?;
        let client = self.client_for(&server_name)?;

        tracing::debug!("Subscribing to '{}' on '{}'", uri, server_name);

        client
            .peer()
            .subscribe(rmcp::model::SubscribeRequestParam {
                uri: uri.to_string(),
            })
            .await
            .map_err(|e| McpError::ToolExecution(format!("Failed to subscribe: {}", e)))
    }

    /// Unsubscribe from resource changes
    pub async fn unsubscribe_resource(&self, uri: &str) -> McpResult<()> {
        let (server_name, _resource) = self.resource_entry(uri)?;
        let client = self.client_for(&server_name)?;

        tracing::debug!("Unsubscribing from '{}' on '{}'", uri, server_name);

        client
            .peer()
            .unsubscribe(rmcp::model::UnsubscribeRequestParam {
                uri: uri.to_string(),
            })
            .await
            .map_err(|e| McpError::ToolExecution(format!("Failed to unsubscribe: {}", e)))
    }

    /// Disconnect from all servers (for cleanup)
    pub async fn shutdown(&mut self) {
        for (name, client) in self.clients.drain() {
            if let Err(e) = client.cancel().await {
                tracing::warn!("Error disconnecting from '{}': {}", name, e);
            }
        }
        self.tools.clear();
        self.prompts.clear();
        self.resources.clear();
    }
}