modular-agent-core 0.25.0

Modular Agent Core
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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Model Context Protocol (MCP) integration for external tool servers.
//!
//! This module provides integration with MCP-compliant tool servers, allowing
//! external tools to be registered and called through the standard tool registry.
//!
//! MCP is a protocol for connecting LLM applications with external tool providers.
//! This module supports loading MCP server configurations from JSON files
//! (compatible with Claude Desktop format) and manages connection pooling
//! for efficient server communication.
//!
//! # Features
//!
//! - Load MCP server configurations from JSON files
//! - Automatic connection pooling for MCP servers
//! - Register MCP tools with the global tool registry
//! - Graceful shutdown of all MCP connections
//!
//! # Example
//!
//! ```no_run
//! use modular_agent_core::mcp::{register_tools_from_mcp_json, shutdown_all_mcp_connections};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Load and register tools from MCP configuration
//!     let tools = register_tools_from_mcp_json("mcp.json").await?;
//!     println!("Registered {} MCP tools", tools.len());
//!
//!     // ... use tools ...
//!
//!     // Clean up connections on shutdown
//!     shutdown_all_mcp_connections().await?;
//!     Ok(())
//! }
//! ```

#![cfg(feature = "mcp")]

use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};

use modular_agent_core::{AgentContext, AgentError, AgentValue, async_trait};
use rmcp::{
    model::{CallToolRequestParam, CallToolResult},
    service::ServiceExt,
    transport::{ConfigureCommandExt, TokioChildProcess},
};
use serde::Deserialize;
use tokio::process::Command;
use tokio::sync::Mutex as AsyncMutex;

use crate::tool::{Tool, ToolInfo, register_tool};

/// Tool implementation that delegates to an MCP server.
///
/// Uses connection pooling to efficiently reuse connections to MCP servers.
struct MCPTool {
    /// Name of the MCP server this tool belongs to.
    server_name: String,
    /// Configuration for connecting to the MCP server.
    server_config: MCPServerConfig,
    /// The underlying MCP tool definition.
    tool: rmcp::model::Tool,
    /// Tool metadata for registration.
    info: ToolInfo,
}

impl MCPTool {
    /// Creates a new MCPTool from server configuration and tool definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The fully qualified tool name (typically "server::tool")
    /// * `server_name` - Name of the MCP server
    /// * `server_config` - Configuration for the MCP server
    /// * `tool` - The MCP tool definition
    fn new(
        name: String,
        server_name: String,
        server_config: MCPServerConfig,
        tool: rmcp::model::Tool,
    ) -> Self {
        let info = ToolInfo::new(
            name,
            tool.description.clone().unwrap_or_default().into_owned(),
            serde_json::to_value(&tool.input_schema).ok(),
        );
        Self {
            server_name,
            server_config,
            tool,
            info,
        }
    }

    /// Invokes the tool on the MCP server.
    ///
    /// Gets or creates a connection from the pool and calls the tool.
    /// A transport-level failure invalidates the pooled connection,
    /// reconnects, and retries the call exactly once.
    async fn tool_call(
        &self,
        _ctx: AgentContext,
        value: AgentValue,
    ) -> Result<AgentValue, AgentError> {
        let arguments = value.as_object().map(|obj| {
            obj.iter()
                .map(|(k, v)| {
                    (
                        k.clone(),
                        serde_json::to_value(v).unwrap_or(serde_json::Value::Null),
                    )
                })
                .collect::<serde_json::Map<String, serde_json::Value>>()
        });

        let entry = {
            let mut pool = connection_pool().lock().await;
            pool.get_or_create(&self.server_name, &self.server_config)
                .await?
        };

        let tool_result = match self.call_once(&entry, arguments.clone()).await {
            Ok(result) => result,
            Err(e) => {
                log::warn!(
                    "MCP tool call '{}' failed ({}); reconnecting to server '{}' and retrying",
                    self.tool.name,
                    e,
                    self.server_name
                );
                entry.dead.store(true, Ordering::Release);
                let entry = {
                    let mut pool = connection_pool().lock().await;
                    pool.invalidate(&self.server_name);
                    pool.get_or_create(&self.server_name, &self.server_config)
                        .await?
                };
                // Mark the replacement dead too so the next caller reconnects
                // immediately instead of paying a guaranteed-failed call first.
                self.call_once(&entry, arguments)
                    .await
                    .inspect_err(|_| entry.dead.store(true, Ordering::Release))?
            }
        };

        call_tool_result_to_agent_value(tool_result)
    }

    /// Performs a single tool call on the given pooled connection.
    ///
    /// Returns `Err` only for transport/protocol-level failures (missing
    /// service or an `Err` from the rmcp call); tool-level failures arrive
    /// as `Ok` with `is_error` set and must not trigger a reconnect.
    async fn call_once(
        &self,
        entry: &PoolEntry,
        arguments: Option<serde_json::Map<String, serde_json::Value>>,
    ) -> Result<CallToolResult, AgentError> {
        let connection = entry.conn.lock().await;
        let service = connection.service.as_ref().ok_or_else(|| {
            AgentError::Other(format!(
                "MCP service for '{}' is not available (tool '{}')",
                self.server_name, self.info.name
            ))
        })?;
        service
            .call_tool(CallToolRequestParam {
                name: self.tool.name.clone(),
                arguments,
                task: None,
            })
            .await
            .map_err(|e| {
                AgentError::Other(format!("Failed to call MCP tool '{}': {e}", self.info.name))
            })
    }
}

#[async_trait]
impl Tool for MCPTool {
    fn info(&self) -> &ToolInfo {
        &self.info
    }

    async fn call(&self, ctx: AgentContext, args: AgentValue) -> Result<AgentValue, AgentError> {
        self.tool_call(ctx, args).await
    }
}

/// Root configuration structure for MCP servers.
///
/// Compatible with the Claude Desktop MCP configuration format (`mcp.json`).
///
/// # Example JSON
///
/// ```json
/// {
///   "mcpServers": {
///     "filesystem": {
///       "command": "npx",
///       "args": ["-y", "@anthropic/mcp-server-filesystem", "/path/to/dir"]
///     }
///   }
/// }
/// ```
#[derive(Debug, Deserialize)]
pub struct MCPConfig {
    /// Map of server names to their configurations.
    #[serde(rename = "mcpServers")]
    pub mcp_servers: HashMap<String, MCPServerConfig>,
}

/// Configuration for a single MCP server.
///
/// Specifies how to start the MCP server process.
#[derive(Debug, Clone, Deserialize)]
pub struct MCPServerConfig {
    /// The command to execute (e.g., "npx", "node", "python").
    pub command: String,

    /// Arguments to pass to the command.
    pub args: Vec<String>,

    /// Optional environment variables for the process.
    #[serde(default)]
    pub env: Option<HashMap<String, String>>,
}

/// Type alias for a running MCP service connection.
type MCPService = rmcp::service::RunningService<rmcp::service::RoleClient, ()>;

/// A single connection to an MCP server.
struct MCPConnection {
    /// The running service, or None if not connected.
    service: Option<MCPService>,
}

/// A pooled connection together with its liveness flag.
///
/// The `dead` flag lives outside the connection mutex so that pool
/// operations can check and set liveness without waiting behind an
/// in-flight tool call holding the connection lock.
#[derive(Clone)]
struct PoolEntry {
    conn: Arc<AsyncMutex<MCPConnection>>,
    dead: Arc<AtomicBool>,
}

/// Connection pool for managing MCP server connections.
///
/// Maintains persistent connections to MCP servers and reuses them
/// across multiple tool calls for efficiency.
struct MCPConnectionPool {
    /// Map of server names to their connections.
    connections: HashMap<String, PoolEntry>,
}

impl MCPConnectionPool {
    /// Creates a new empty connection pool.
    fn new() -> Self {
        Self {
            connections: HashMap::new(),
        }
    }

    /// Gets an existing connection or creates a new one for the server.
    ///
    /// A live existing connection is reused. A connection marked dead or
    /// whose service is gone is discarded and replaced with a fresh one.
    async fn get_or_create(
        &mut self,
        server_name: &str,
        config: &MCPServerConfig,
    ) -> Result<PoolEntry, AgentError> {
        if let Some(entry) = self.connections.get(server_name) {
            // try_lock only: a busy connection has an in-flight call, which
            // implies its service is still present, so treat it as live
            // rather than blocking the whole pool behind that call.
            let service_gone = entry
                .conn
                .try_lock()
                .map(|c| c.service.is_none())
                .unwrap_or(false);
            if !entry.dead.load(Ordering::Acquire) && !service_gone {
                log::debug!("Reusing existing MCP connection for '{}'", server_name);
                return Ok(entry.clone());
            }
            log::info!(
                "Discarding dead MCP connection for '{}', creating a new one",
                server_name
            );
            if let Some(stale) = self.connections.remove(server_name) {
                cancel_in_background(stale, server_name.to_string());
            }
        }

        log::info!(
            "Starting MCP server '{}' (command: {})",
            server_name,
            config.command
        );

        // Start new MCP service
        let service = ()
            .serve(
                TokioChildProcess::new(Command::new(&config.command).configure(|cmd| {
                    for arg in &config.args {
                        cmd.arg(arg);
                    }
                    if let Some(env) = &config.env {
                        for (key, value) in env {
                            cmd.env(key, value);
                        }
                    }
                }))
                .map_err(|e| {
                    log::error!("Failed to start MCP process for '{}': {}", server_name, e);
                    AgentError::Other(format!(
                        "Failed to start MCP process for '{}': {e}",
                        server_name
                    ))
                })?,
            )
            .await
            .map_err(|e| {
                log::error!("Failed to start MCP service for '{}': {}", server_name, e);
                AgentError::Other(format!(
                    "Failed to start MCP service for '{}': {e}",
                    server_name
                ))
            })?;

        log::info!("Successfully started MCP server '{}'", server_name);

        let entry = PoolEntry {
            conn: Arc::new(AsyncMutex::new(MCPConnection {
                service: Some(service),
            })),
            dead: Arc::new(AtomicBool::new(false)),
        };
        self.connections
            .insert(server_name.to_string(), entry.clone());
        Ok(entry)
    }

    /// Removes the server's pooled connection if it has been marked dead.
    ///
    /// The dead-flag guard makes late invalidations from concurrently failed
    /// calls no-ops once a healthy replacement connection is in the pool.
    fn invalidate(&mut self, server_name: &str) {
        let is_dead = self
            .connections
            .get(server_name)
            .is_some_and(|e| e.dead.load(Ordering::Acquire));
        if is_dead && let Some(entry) = self.connections.remove(server_name) {
            log::info!("Invalidating MCP connection for '{}'", server_name);
            cancel_in_background(entry, server_name.to_string());
        }
    }

    /// Removes and returns all pooled connections.
    ///
    /// Synchronous on purpose: callers hold the pool lock, and the pool lock
    /// must never be held across an await on a connection lock (an in-flight
    /// rmcp call has no default timeout, so that wait could be unbounded).
    fn take_all(&mut self) -> Vec<(String, PoolEntry)> {
        self.connections.drain().collect()
    }
}

/// Cancels the entry's service in a background task.
///
/// Cancellation needs the connection lock, which may be held by an in-flight
/// call for its full duration; a spawned task keeps pool operations (which
/// may hold the pool lock) from ever waiting on a connection lock.
fn cancel_in_background(entry: PoolEntry, server_name: String) {
    entry.dead.store(true, Ordering::Release);
    tokio::spawn(async move {
        let mut connection = entry.conn.lock().await;
        if let Some(service) = connection.service.take()
            && let Err(e) = service.cancel().await
        {
            log::warn!("Failed to cancel MCP service '{}': {}", server_name, e);
        }
    });
}

/// Global connection pool instance.
static CONNECTION_POOL: OnceLock<AsyncMutex<MCPConnectionPool>> = OnceLock::new();

/// Returns the global connection pool, initializing it if necessary.
fn connection_pool() -> &'static AsyncMutex<MCPConnectionPool> {
    CONNECTION_POOL.get_or_init(|| AsyncMutex::new(MCPConnectionPool::new()))
}

/// Shuts down all MCP server connections.
///
/// Call this during application shutdown to cleanly terminate all
/// MCP server processes.
///
/// The pool remains usable after this call: a tool call running concurrently
/// with the drain may re-create a connection, which is only cleaned up by a
/// subsequent call to this function.
///
/// Connections still busy past the shutdown timeout are cancelled best-effort
/// in background tasks, which may not complete if the tokio runtime is torn
/// down immediately afterwards.
///
/// # Example
///
/// ```no_run
/// use modular_agent_core::mcp::shutdown_all_mcp_connections;
///
/// #[tokio::main]
/// async fn main() {
///     // ... use MCP tools ...
///
///     // Clean shutdown
///     shutdown_all_mcp_connections().await.expect("Failed to shutdown MCP");
/// }
/// ```
pub async fn shutdown_all_mcp_connections() -> Result<(), AgentError> {
    log::info!("Shutting down all MCP server connections");
    let entries = { connection_pool().lock().await.take_all() };
    for (name, entry) in entries {
        entry.dead.store(true, Ordering::Release);
        // Bound the wait: an in-flight rmcp call holds the connection lock
        // with no default request timeout, so a wedged server could otherwise
        // block shutdown forever. Lock via a clone so `entry` stays movable
        // into the busy-connection fallback below.
        let conn = entry.conn.clone();
        match tokio::time::timeout(std::time::Duration::from_secs(5), conn.lock()).await {
            Ok(mut connection) => {
                if let Some(service) = connection.service.take() {
                    if let Err(e) = service.cancel().await {
                        log::error!("Failed to cancel MCP service '{}': {}", name, e);
                    } else {
                        log::debug!("Successfully shut down MCP server '{}'", name);
                    }
                }
            }
            Err(_) => {
                log::warn!(
                    "MCP connection '{}' busy during shutdown; cancelling in background",
                    name
                );
                cancel_in_background(entry, name);
            }
        }
    }
    log::info!("All MCP server connections shut down");
    Ok(())
}

/// Registers all tools from a single MCP server.
///
/// Connects to the MCP server, lists its available tools, and registers
/// each one with the global tool registry.
///
/// # Arguments
///
/// * `server_name` - Name of the MCP server
/// * `server_config` - Configuration for the MCP server
///
/// # Returns
///
/// A vector of registered tool names in the format "server_name::tool_name".
async fn register_tools_from_server(
    server_name: String,
    server_config: MCPServerConfig,
) -> Result<Vec<String>, AgentError> {
    log::debug!("Registering tools from MCP server '{}'", server_name);

    // Get or create connection from pool
    let entry = {
        let mut pool = connection_pool().lock().await;
        pool.get_or_create(&server_name, &server_config).await?
    };

    // List all available tools from this server
    log::debug!("Listing tools from MCP server '{}'", server_name);
    let tools_list = {
        let connection = entry.conn.lock().await;
        let service = connection.service.as_ref().ok_or_else(|| {
            log::error!("MCP service for '{}' is not available", server_name);
            AgentError::Other(format!(
                "MCP service for '{}' is not available",
                server_name
            ))
        })?;
        service.list_tools(Default::default()).await.map_err(|e| {
            log::error!("Failed to list MCP tools for '{}': {}", server_name, e);
            AgentError::Other(format!(
                "Failed to list MCP tools for '{}': {e}",
                server_name
            ))
        })?
    };

    let mut registered_tool_names = Vec::new();

    // Register all tools from this server using connection pool
    for tool_info in tools_list.tools {
        let mcp_tool_name = format!("{}::{}", server_name, tool_info.name);
        registered_tool_names.push(mcp_tool_name.clone());

        register_tool(MCPTool::new(
            mcp_tool_name.clone(),
            server_name.clone(),
            server_config.clone(),
            tool_info,
        ));
        log::debug!("Registered MCP tool '{}'", mcp_tool_name);
    }

    log::info!(
        "Registered {} tools from MCP server '{}'",
        registered_tool_names.len(),
        server_name
    );

    Ok(registered_tool_names)
}

/// Loads MCP configuration from a JSON file and registers all tools
///
/// # Arguments
/// * `json_path` - Path to the mcp.json file
///
/// # Returns
/// A vector of registered tool names in the format "server_name::tool_name"
///
/// # Example
/// ```no_run
/// use modular_agent_core::mcp::register_tools_from_mcp_json;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let tool_names = register_tools_from_mcp_json("mcp.json").await?;
///     println!("Registered {} tools", tool_names.len());
///     Ok(())
/// }
/// ```
pub async fn register_tools_from_mcp_json<P: AsRef<Path>>(
    json_path: P,
) -> Result<Vec<String>, AgentError> {
    let path = json_path.as_ref();
    log::info!("Loading MCP configuration from: {}", path.display());

    // Read the JSON file
    let json_content = std::fs::read_to_string(path).map_err(|e| {
        log::error!("Failed to read MCP config file '{}': {}", path.display(), e);
        AgentError::Other(format!("Failed to read MCP config file: {e}"))
    })?;

    // Parse the JSON
    let config: MCPConfig = serde_json::from_str(&json_content).map_err(|e| {
        log::error!("Failed to parse MCP config JSON: {}", e);
        AgentError::Other(format!("Failed to parse MCP config JSON: {e}"))
    })?;

    log::info!("Found {} MCP servers in config", config.mcp_servers.len());

    let mut registered_tool_names = Vec::new();

    // Iterate through each MCP server
    for (server_name, server_config) in config.mcp_servers {
        let tools = register_tools_from_server(server_name, server_config).await?;
        registered_tool_names.extend(tools);
    }

    log::info!(
        "Successfully registered {} MCP tools total",
        registered_tool_names.len()
    );

    Ok(registered_tool_names)
}

/// Converts an MCP tool call result to an AgentValue.
///
/// Extracts text content from the result and returns it as an array.
/// If the result indicates an error, returns an AgentError instead.
fn call_tool_result_to_agent_value(result: CallToolResult) -> Result<AgentValue, AgentError> {
    let mut contents = Vec::new();
    for c in result.content.iter() {
        match &c.raw {
            rmcp::model::RawContent::Text(text) => {
                contents.push(AgentValue::string(text.text.clone()));
            }
            _ => {
                // Handle other content types as needed
            }
        }
    }
    let data = AgentValue::array(contents.into());
    if result.is_error == Some(true) {
        return Err(AgentError::Other(
            serde_json::to_string(&data).map_err(|e| AgentError::InvalidValue(e.to_string()))?,
        ));
    }
    Ok(data)
}

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

    fn test_entry(dead: bool) -> PoolEntry {
        PoolEntry {
            conn: Arc::new(AsyncMutex::new(MCPConnection { service: None })),
            dead: Arc::new(AtomicBool::new(dead)),
        }
    }

    fn bogus_config() -> MCPServerConfig {
        MCPServerConfig {
            command: "modular-agent-test-nonexistent-command".to_string(),
            args: Vec::new(),
            env: None,
        }
    }

    #[tokio::test]
    async fn invalidate_removes_dead_entry() {
        let mut pool = MCPConnectionPool::new();
        pool.connections.insert("s".to_string(), test_entry(true));
        pool.invalidate("s");
        assert!(pool.connections.is_empty());
    }

    #[tokio::test]
    async fn invalidate_keeps_entry_not_marked_dead() {
        // Guards the race where a late invalidation from a concurrently failed
        // call must not destroy a healthy replacement connection.
        let mut pool = MCPConnectionPool::new();
        pool.connections.insert("s".to_string(), test_entry(false));
        pool.invalidate("s");
        assert!(pool.connections.contains_key("s"));
    }

    #[tokio::test]
    async fn get_or_create_reuses_busy_connection() {
        let mut pool = MCPConnectionPool::new();
        let entry = test_entry(false);
        pool.connections.insert("s".to_string(), entry.clone());
        // Hold the connection lock to simulate an in-flight call: the pool
        // must treat a busy connection as live and return it without
        // awaiting the lock (a bogus config would make any spawn fail).
        let _guard = entry.conn.try_lock().unwrap();
        let got = pool.get_or_create("s", &bogus_config()).await.unwrap();
        assert!(Arc::ptr_eq(&got.conn, &entry.conn));
    }

    #[tokio::test]
    async fn get_or_create_discards_dead_entry() {
        let mut pool = MCPConnectionPool::new();
        pool.connections.insert("s".to_string(), test_entry(true));
        // The Err from the bogus command proves a fresh spawn was attempted
        // instead of reusing the dead entry.
        assert!(pool.get_or_create("s", &bogus_config()).await.is_err());
        assert!(pool.connections.is_empty());
    }

    #[tokio::test]
    async fn get_or_create_discards_entry_with_missing_service() {
        let mut pool = MCPConnectionPool::new();
        pool.connections.insert("s".to_string(), test_entry(false));
        assert!(pool.get_or_create("s", &bogus_config()).await.is_err());
        assert!(pool.connections.is_empty());
    }
}