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
//! MCP (Model Context Protocol) Integration
//!
//! 提供完整的 MCP 协议支持,允许连接任意 MCP 服务器并将其工具映射为内置工具。
//!
//! # 架构概览
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ MatrixCode Agent │
//! │ ┌─────────────────────────────────────────────────────┐ │
//! │ │ Tool System │ │
//! │ │ ┌─────────┐ ┌─────────┐ ┌───────────────────────┐ │ │
//! │ │ │ Built-in│ │ CodeGraph│ │ MCP Tool Wrapper │ │ │
//! │ │ │ Tools │ │ Tools │ │ (Transparent Proxy) │ │ │
//! │ │ └─────────┘ └─────────┘ └───────────┬───────────┘ │ │
//! │ └─────────────────────────────────────│───────────────┘ │
//! └────────────────────────────────────────│───────────────────┘
//! │
//! ┌─────────────────────────────────────────│───────────────────┐
//! │ MCP Module │ │
//! │ ┌──────────────┐ ┌──────────────┐ ┌───┴───────┐ │
//! │ │ Config │ │ Transport │ │ McpClient │ │
//! │ │ (config.rs) │ │ (transport.rs)│ │ (client.rs)│ │
//! │ └──────────────┘ └──────────────┘ └─────┬─────┘ │
//! └───────────────────────────────────────────│─────────────────┘
//! │ MCP Protocol
//! ┌───────────────────────────────────────────│─────────────────┐
//! │ External MCP Servers │ │
//! │ ┌─────────────┐ ┌─────────────┐ ┌───────┴───┐ │
//! │ │ Playwright │ │ Filesystem │ │ Other │ │
//! │ │ MCP Server │ │ MCP Server │ │ MCP │ │
//! │ └─────────────┘ └─────────────┘ └───────────┘ │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # 快速开始
//!
//! ## 1. 配置 MCP 服务器
//!
//! 创建 `mcp.toml` 文件:
//!
//! ```toml
//! [servers.playwright]
//! command = "npx"
//! args = ["-y", "@playwright/mcp@latest"]
//!
//! [servers.filesystem]
//! command = "npx"
//! args = ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/files"]
//! ```
//!
//! ## 2. 连接并使用工具
//!
//! ```ignore
//! use matrixcode_core::mcp::{McpConfig, McpToolManager};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // 加载配置
//! let config = McpConfig::from_file("mcp.toml")?;
//!
//! // 创建工具管理器
//! let manager = McpToolManager::new();
//!
//! // 连接所有服务器
//! for (key, server_config) in config.enabled_servers() {
//! let transport = server_config.to_transport_config()?;
//! let tools = manager.connect_server(&server_config.get_name(&key), transport).await?;
//! println!("Connected: {} tools", tools.len());
//! }
//!
//! // 获取所有工具
//! let tools = manager.get_tools().await;
//!
//! // 使用工具...
//!
//! // 关闭
//! manager.shutdown().await;
//! Ok(())
//! }
//! ```
//!
//! # 支持的传输方式
//!
//! - **Stdio**: 通过子进程 stdin/stdout 通信(推荐)
//! - **SSE**: 通过 HTTP Server-Sent Events 通信(远程服务器)
//!
//! # 内置 MCP 配置
//!
//! 模块提供常用 MCP 服务器的预设配置:
//!
//! - `playwright_config()`: Playwright 浏览器自动化
//! - `default_mcp_config()`: 常用配置组合
// Re-export main types
pub use *;
pub use ;
pub use ;
pub use ;
pub use ;
// ============================================================================
// Convenience Functions
// ============================================================================
/// 连接 Playwright MCP 服务器
///
/// # Example
///
/// ```ignore
/// use matrixcode_core::mcp::connect_playwright;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let tools = connect_playwright().await?;
/// println!("Playwright tools: {:?}", tools.iter().map(|t| t.definition().name).collect::<Vec<_>>());
/// Ok(())
/// }
/// ```
pub async
/// 连接配置文件中的所有 MCP 服务器
///
/// 从当前目录或用户主目录查找 `mcp.toml` / `mcp.json` 配置文件,
/// 并连接所有启用的 MCP 服务器。
///
/// # Example
///
/// ```ignore
/// use matrixcode_core::mcp::connect_all_from_config;
/// use std::path::Path;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let tools = connect_all_from_config(Path::new(".")).await?;
/// println!("Total tools: {}", tools.len());
/// Ok(())
/// }
/// ```
pub async