alou 0.1.0

High-performance Rust implementation of Alou AI agent with MCP tool integration and DeepSeek API support
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
use crate::types::{McpServerConfig, McpConfig};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Result, Context};

/// 原始MCP服务器配置结构(从mcp.json文件读取)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawMcpServerConfig {
    pub command: Option<String>,
    pub args: Option<Vec<String>>,
    pub env: Option<HashMap<String, String>>,
    pub cwd: Option<String>,
    pub url: Option<String>,
    pub http_url: Option<String>,
    pub headers: Option<HashMap<String, String>>,
    pub tcp: Option<String>,
    pub timeout: Option<u64>,
    pub trust: Option<bool>,
    pub description: Option<String>,
    pub include_tools: Option<Vec<String>>,
    pub exclude_tools: Option<Vec<String>>,
    pub extension_name: Option<String>,
    pub oauth: Option<serde_json::Value>,
    pub auth_provider_type: Option<String>,
}

/// MCP配置加载器
pub struct McpConfigLoader;

impl McpConfigLoader {
    /// 从mcp.json文件加载MCP服务器配置
    /// 
    /// # Arguments
    /// * `project_root` - 项目根目录
    /// 
    /// # Returns
    /// MCP服务器配置的映射,如果文件不存在则返回None
    pub fn load_mcp_config() -> Result<Option<McpServerConfig>> {
        // 使用当前工作目录作为项目根目录
        let project_root = std::env::current_dir()
            .context("Failed to get current directory")?
            .to_string_lossy()
            .to_string();
        
        match Self::load_mcp_config_with_root(&project_root)? {
            Some(servers) => {
                // 返回第一个服务器配置,或者创建一个默认配置
                if let Some((_, config)) = servers.into_iter().next() {
                    Ok(Some(config))
                } else {
                    Ok(None)
                }
            }
            None => Ok(None),
        }
    }
    
    pub fn load_mcp_config_with_root(project_root: &str) -> Result<Option<HashMap<String, McpServerConfig>>> {
        let mcp_config_path = Path::new(project_root).join("mcp.json");
        
        // 检查mcp.json文件是否存在
        if !mcp_config_path.exists() {
            return Ok(None);
        }
        
        // 读取并解析mcp.json文件
        let mcp_config_content = fs::read_to_string(&mcp_config_path)
            .with_context(|| format!("无法读取MCP配置文件: {}", mcp_config_path.display()))?;
        
        let mcp_config: serde_json::Value = serde_json::from_str(&mcp_config_content)
            .with_context(|| "无法解析MCP配置文件JSON")?;
        
        // 转换原始配置对象为McpServerConfig实例
        let mut mcp_servers = HashMap::new();
        
        if let Some(servers) = mcp_config.get("mcpServers") {
            if let Some(servers_obj) = servers.as_object() {
                for (server_name, server_config) in servers_obj {
                    let raw_config: RawMcpServerConfig = serde_json::from_value(server_config.clone())
                        .with_context(|| format!("无法解析服务器配置: {}", server_name))?;
                    
                    let config = Self::convert_raw_config(raw_config)?;
                    mcp_servers.insert(server_name.clone(), config);
                }
            }
        }
        
        Ok(Some(mcp_servers))
    }

    /// 将原始配置转换为McpServerConfig
    fn convert_raw_config(raw: RawMcpServerConfig) -> Result<McpServerConfig> {
        let oauth = if let Some(oauth_value) = raw.oauth {
            Some(serde_json::from_value(oauth_value)?)
        } else {
            None
        };

        Ok(McpServerConfig {
            command: raw.command,
            args: raw.args,
            env: raw.env,
            cwd: raw.cwd,
            url: raw.url,
            http_url: raw.http_url,
            headers: raw.headers,
            timeout: raw.timeout,
            trust: raw.trust,
            include_tools: raw.include_tools,
            exclude_tools: raw.exclude_tools,
            oauth,
        })
    }

    /// 从多个可能的路径加载MCP配置
    /// 
    /// # Arguments
    /// * `possible_paths` - 可能的配置文件路径列表
    /// 
    /// # Returns
    /// 找到的第一个有效配置
    pub fn load_from_possible_paths(possible_paths: Vec<PathBuf>) -> Result<Option<HashMap<String, McpServerConfig>>> {
        for path in possible_paths {
            if path.exists() {
                if let Some(parent) = path.parent() {
                    return Self::load_mcp_config_with_root(parent.to_str().unwrap_or("."));
                }
            }
        }
        Ok(None)
    }

    /// 保存MCP配置到文件
    /// 
    /// # Arguments
    /// * `config` - MCP配置
    /// * `file_path` - 保存路径
    pub fn save_mcp_config(config: &McpConfig, file_path: &Path) -> Result<()> {
        let json_content = serde_json::to_string_pretty(config)
            .with_context(|| "无法序列化MCP配置")?;
        
        fs::write(file_path, json_content)
            .with_context(|| format!("无法写入MCP配置文件: {}", file_path.display()))?;
        
        Ok(())
    }

    /// 验证MCP配置
    /// 
    /// # Arguments
    /// * `config` - 要验证的配置
    /// 
    /// # Returns
    /// 验证结果
    pub fn validate_config(config: &McpConfig) -> Result<()> {
        for (server_name, server_config) in &config.mcp_servers {
            Self::validate_server_config(server_name, server_config)?;
        }
        Ok(())
    }

    /// 验证单个服务器配置
    fn validate_server_config(server_name: &str, config: &McpServerConfig) -> Result<()> {
        // 检查是否有有效的连接方式
        let has_command = config.command.is_some();
        let has_url = config.url.is_some();
        let has_http_url = config.http_url.is_some();

        if !has_command && !has_url && !has_http_url {
            return Err(anyhow::anyhow!(
                "服务器 '{}' 缺少连接配置:需要 command、url 或 http_url 中的至少一个",
                server_name
            ));
        }

        // 验证URL格式
        if let Some(url) = &config.url {
            url::Url::parse(url)
                .with_context(|| format!("服务器 '{}' 的URL格式无效: {}", server_name, url))?;
        }

        if let Some(http_url) = &config.http_url {
            url::Url::parse(http_url)
                .with_context(|| format!("服务器 '{}' 的HTTP URL格式无效: {}", server_name, http_url))?;
        }

        // 验证超时设置
        if let Some(timeout) = config.timeout {
            if timeout == 0 {
                return Err(anyhow::anyhow!(
                    "服务器 '{}' 的超时时间不能为0",
                    server_name
                ));
            }
        }

        Ok(())
    }
}

/// MCP配置构建器
pub struct McpConfigBuilder {
    servers: HashMap<String, McpServerConfig>,
}

impl McpConfigBuilder {
    /// 创建新的配置构建器
    pub fn new() -> Self {
        Self {
            servers: HashMap::new(),
        }
    }

    /// 添加服务器配置
    /// 
    /// # Arguments
    /// * `name` - 服务器名称
    /// * `config` - 服务器配置
    pub fn add_server(mut self, name: String, config: McpServerConfig) -> Self {
        self.servers.insert(name, config);
        self
    }

    /// 添加stdio服务器
    /// 
    /// # Arguments
    /// * `name` - 服务器名称
    /// * `command` - 命令
    /// * `args` - 参数
    pub fn add_stdio_server(mut self, name: String, command: String, args: Vec<String>) -> Self {
        let config = McpServerConfig {
            command: Some(command),
            args: Some(args),
            env: None,
            cwd: None,
            url: None,
            http_url: None,
            headers: None,
            timeout: None,
            trust: None,
            include_tools: None,
            exclude_tools: None,
            oauth: None,
        };
        self.servers.insert(name, config);
        self
    }

    /// 添加HTTP服务器
    /// 
    /// # Arguments
    /// * `name` - 服务器名称
    /// * `url` - 服务器URL
    /// * `headers` - 可选的请求头
    pub fn add_http_server(mut self, name: String, url: String, headers: Option<HashMap<String, String>>) -> Self {
        let config = McpServerConfig {
            command: None,
            args: None,
            env: None,
            cwd: None,
            url: Some(url),
            http_url: None,
            headers,
            timeout: None,
            trust: None,
            include_tools: None,
            exclude_tools: None,
            oauth: None,
        };
        self.servers.insert(name, config);
        self
    }

    /// 添加Streamable HTTP服务器
    /// 
    /// # Arguments
    /// * `name` - 服务器名称
    /// * `http_url` - HTTP URL
    /// * `headers` - 可选的请求头
    pub fn add_streamable_http_server(mut self, name: String, http_url: String, headers: Option<HashMap<String, String>>) -> Self {
        let config = McpServerConfig {
            command: None,
            args: None,
            env: None,
            cwd: None,
            url: None,
            http_url: Some(http_url),
            headers,
            timeout: None,
            trust: None,
            include_tools: None,
            exclude_tools: None,
            oauth: None,
        };
        self.servers.insert(name, config);
        self
    }

    /// 构建MCP配置
    pub fn build(self) -> McpConfig {
        McpConfig {
            mcp_servers: self.servers,
        }
    }
}

impl Default for McpConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// MCP配置工厂
pub struct McpConfigFactory;

impl McpConfigFactory {
    /// 创建默认配置
    pub fn create_default() -> McpConfig {
        McpConfigBuilder::new()
            .add_stdio_server(
                "filesystem".to_string(),
                "npx".to_string(),
                vec!["@modelcontextprotocol/server-filesystem".to_string(), "/Users".to_string()],
            )
            .build()
    }

    /// 创建开发环境配置
    pub fn create_development() -> McpConfig {
        McpConfigBuilder::new()
            .add_stdio_server(
                "filesystem".to_string(),
                "npx".to_string(),
                vec!["@modelcontextprotocol/server-filesystem".to_string(), "/Users".to_string()],
            )
            .add_stdio_server(
                "memory".to_string(),
                "npx".to_string(),
                vec!["@modelcontextprotocol/server-memory".to_string()],
            )
            .build()
    }

    /// 创建生产环境配置
    pub fn create_production() -> McpConfig {
        McpConfigBuilder::new()
            .add_stdio_server(
                "filesystem".to_string(),
                "npx".to_string(),
                vec!["@modelcontextprotocol/server-filesystem".to_string(), "/Users".to_string()],
            )
            .build()
    }

    /// 从环境变量创建配置
    pub fn from_env() -> Result<McpConfig> {
        let mut builder = McpConfigBuilder::new();

        // 从环境变量读取服务器配置
        if let Ok(servers_json) = std::env::var("ALOU_MCP_SERVERS") {
            let servers: HashMap<String, RawMcpServerConfig> = serde_json::from_str(&servers_json)?;
            for (name, raw_config) in servers {
                let config = McpConfigLoader::convert_raw_config(raw_config)?;
                builder = builder.add_server(name, config);
            }
        }

        Ok(builder.build())
    }
}

/// 配置工具函数
pub struct ConfigUtils;

impl ConfigUtils {
    /// 合并两个配置
    /// 
    /// # Arguments
    /// * `base` - 基础配置
    /// * `override_config` - 覆盖配置
    /// 
    /// # Returns
    /// 合并后的配置
    pub fn merge_configs(base: McpConfig, override_config: McpConfig) -> McpConfig {
        let mut merged_servers = base.mcp_servers;
        
        for (name, config) in override_config.mcp_servers {
            merged_servers.insert(name, config);
        }
        
        McpConfig {
            mcp_servers: merged_servers,
        }
    }

    /// 过滤配置中的服务器
    /// 
    /// # Arguments
    /// * `config` - 原始配置
    /// * `server_names` - 要保留的服务器名称列表
    /// 
    /// # Returns
    /// 过滤后的配置
    pub fn filter_servers(config: McpConfig, server_names: Vec<String>) -> McpConfig {
        let filtered_servers: HashMap<String, McpServerConfig> = config
            .mcp_servers
            .into_iter()
            .filter(|(name, _)| server_names.contains(name))
            .collect();
        
        McpConfig {
            mcp_servers: filtered_servers,
        }
    }

    /// 获取配置摘要
    /// 
    /// # Arguments
    /// * `config` - MCP配置
    /// 
    /// # Returns
    /// 配置摘要字符串
    pub fn get_config_summary(config: &McpConfig) -> String {
        let mut summary = format!("MCP配置摘要 ({} 个服务器):\n", config.mcp_servers.len());
        
        for (name, server_config) in &config.mcp_servers {
            let connection_type = if server_config.command.is_some() {
                "stdio"
            } else if server_config.http_url.is_some() {
                "streamable_http"
            } else if server_config.url.is_some() {
                "http"
            } else {
                "unknown"
            };
            
            summary.push_str(&format!("  - {}: {}\n", name, connection_type));
        }
        
        summary
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use std::fs;

    #[test]
    fn test_load_mcp_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("mcp.json");
        
        let config_content = r#"{
            "mcpServers": {
                "test_server": {
                    "command": "npx",
                    "args": ["@modelcontextprotocol/server-filesystem", "/Users"],
                    "timeout": 30000
                }
            }
        }"#;
        
        fs::write(&config_path, config_content).unwrap();
        
        let result = McpConfigLoader::load_mcp_config();
        assert!(result.is_ok());
        
        let config = result.unwrap();
        assert!(config.is_some());
        
        let _servers = config.unwrap();
        // TODO: 修复测试,因为现在返回的是单个配置而不是HashMap
    }

    #[test]
    fn test_config_builder() {
        let config = McpConfigBuilder::new()
            .add_stdio_server(
                "test".to_string(),
                "npx".to_string(),
                vec!["test-server".to_string()],
            )
            .build();
        
        assert_eq!(config.mcp_servers.len(), 1);
        assert!(config.mcp_servers.contains_key("test"));
    }

    #[test]
    fn test_config_validation() {
        let valid_config = McpConfigBuilder::new()
            .add_stdio_server(
                "test".to_string(),
                "npx".to_string(),
                vec!["test-server".to_string()],
            )
            .build();
        
        assert!(McpConfigLoader::validate_config(&valid_config).is_ok());
    }

    #[test]
    fn test_config_utils() {
        let config1 = McpConfigBuilder::new()
            .add_stdio_server("server1".to_string(), "cmd1".to_string(), vec![])
            .build();
        
        let config2 = McpConfigBuilder::new()
            .add_stdio_server("server2".to_string(), "cmd2".to_string(), vec![])
            .build();
        
        let merged = ConfigUtils::merge_configs(config1, config2);
        assert_eq!(merged.mcp_servers.len(), 2);
        
        let filtered = ConfigUtils::filter_servers(merged, vec!["server1".to_string()]);
        assert_eq!(filtered.mcp_servers.len(), 1);
    }
}