Apply recent changes
This commit is contained in:
87
crates/owlen-core/src/mcp/factory.rs
Normal file
87
crates/owlen-core/src/mcp/factory.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
/// MCP Client Factory
|
||||
///
|
||||
/// Provides a unified interface for creating MCP clients based on configuration.
|
||||
/// Supports switching between local (in-process) and remote (STDIO) execution modes.
|
||||
use super::client::McpClient;
|
||||
use super::{remote_client::RemoteMcpClient, LocalMcpClient};
|
||||
use crate::config::{Config, McpMode};
|
||||
use crate::tools::registry::ToolRegistry;
|
||||
use crate::validation::SchemaValidator;
|
||||
use crate::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Factory for creating MCP clients based on configuration
|
||||
pub struct McpClientFactory {
|
||||
config: Arc<Config>,
|
||||
registry: Arc<ToolRegistry>,
|
||||
validator: Arc<SchemaValidator>,
|
||||
}
|
||||
|
||||
impl McpClientFactory {
|
||||
pub fn new(
|
||||
config: Arc<Config>,
|
||||
registry: Arc<ToolRegistry>,
|
||||
validator: Arc<SchemaValidator>,
|
||||
) -> Self {
|
||||
Self {
|
||||
config,
|
||||
registry,
|
||||
validator,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an MCP client based on the current configuration
|
||||
pub fn create(&self) -> Result<Box<dyn McpClient>> {
|
||||
match self.config.mcp.mode {
|
||||
McpMode::Legacy => {
|
||||
// Use local in-process client
|
||||
Ok(Box::new(LocalMcpClient::new(
|
||||
self.registry.clone(),
|
||||
self.validator.clone(),
|
||||
)))
|
||||
}
|
||||
McpMode::Enabled => {
|
||||
// Attempt to use remote client, fall back to local if unavailable
|
||||
match RemoteMcpClient::new() {
|
||||
Ok(client) => Ok(Box::new(client)),
|
||||
Err(e) => {
|
||||
eprintln!("Warning: Failed to start remote MCP client: {}. Falling back to local mode.", e);
|
||||
Ok(Box::new(LocalMcpClient::new(
|
||||
self.registry.clone(),
|
||||
self.validator.clone(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if remote MCP mode is available
|
||||
pub fn is_remote_available() -> bool {
|
||||
RemoteMcpClient::new().is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_factory_creates_local_client_in_legacy_mode() {
|
||||
let mut config = Config::default();
|
||||
config.mcp.mode = McpMode::Legacy;
|
||||
|
||||
let ui = Arc::new(crate::ui::NoOpUiController);
|
||||
let registry = Arc::new(ToolRegistry::new(
|
||||
Arc::new(tokio::sync::Mutex::new(config.clone())),
|
||||
ui,
|
||||
));
|
||||
let validator = Arc::new(SchemaValidator::new());
|
||||
|
||||
let factory = McpClientFactory::new(Arc::new(config), registry, validator);
|
||||
|
||||
// Should create without error
|
||||
let result = factory.create();
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user