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
use std::collections::HashMap;
/// MCP 服务端完整配置
#[derive(Debug, Clone)]
pub struct McpServerConfig {
/// 服务端标识名称
pub name: String,
/// 传输层配置
pub transport: TransportConfig,
}
/// 传输层配置
#[derive(Debug, Clone)]
pub enum TransportConfig {
/// stdio 传输:框架启动子进程,通过 stdin/stdout 通信
/// 适用场景:本地工具(filesystem、git、sqlite 等)
Stdio {
command: String,
args: Vec<String>,
/// 额外注入的环境变量
env: Vec<(String, String)>,
},
/// HTTP 传输:MCP Streamable HTTP(推荐)
///
/// 符合 MCP 官方规范:
/// - 直接 POST 到端点 URL
/// - 自动携带 MCP-Protocol-Version 请求头
/// - 支持 MCP-Session-Id 会话管理
/// - 支持 GET SSE 通知流(服务端可选)
///
/// 适用场景:远程 MCP 服务
Http {
/// MCP 服务端端点 URL
base_url: String,
/// 自定义请求头(如 Authorization)
headers: HashMap<String, String>,
},
/// SSE (Server-Sent Events) 传输:旧版 HTTP+SSE
///
/// 适用于旧版 MCP SDK(2024-11-05 协议):
/// - 在 `{base_url}/sse` 建立 SSE 连接
/// - 从 endpoint 事件动态获取 POST URI
Sse {
base_url: String,
/// 自定义请求头(如 Authorization)
headers: HashMap<String, String>,
},
}
impl McpServerConfig {
/// 创建 stdio 配置(最常用)
///
/// # 示例
/// ```
/// use echo_integration::mcp::server_config::McpServerConfig;
/// McpServerConfig::stdio("filesystem", "npx", vec![
/// "-y", "@modelcontextprotocol/server-filesystem", "/tmp"
/// ]);
/// ```
pub fn stdio(
name: impl Into<String>,
command: impl Into<String>,
args: Vec<impl Into<String>>,
) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Stdio {
command: command.into(),
args: args.into_iter().map(Into::into).collect(),
env: vec![],
},
}
}
/// 创建 stdio 配置(带额外环境变量)
pub fn stdio_with_env(
name: impl Into<String>,
command: impl Into<String>,
args: Vec<impl Into<String>>,
env: Vec<(impl Into<String>, impl Into<String>)>,
) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Stdio {
command: command.into(),
args: args.into_iter().map(Into::into).collect(),
env: env.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
},
}
}
/// 创建 HTTP 配置(MCP Streamable HTTP,推荐用于远程服务)
///
/// # 示例
/// ```
/// use echo_integration::mcp::server_config::McpServerConfig;
/// McpServerConfig::http("my-api", "http://localhost:3000/mcp");
/// ```
pub fn http(name: impl Into<String>, base_url: impl Into<String>) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Http {
base_url: base_url.into(),
headers: HashMap::new(),
},
}
}
/// 创建 HTTP 配置(带自定义请求头)
///
/// # 示例
/// ```
/// use std::collections::HashMap;
/// use echo_integration::mcp::server_config::McpServerConfig;
/// let mut headers = HashMap::new();
/// headers.insert("Authorization".to_string(), "Bearer token".to_string());
/// McpServerConfig::http_with_headers("secure-api", "https://api.example.com/mcp", headers);
/// ```
pub fn http_with_headers(
name: impl Into<String>,
base_url: impl Into<String>,
headers: HashMap<String, String>,
) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Http {
base_url: base_url.into(),
headers,
},
}
}
/// 创建 SSE 配置(旧版 HTTP+SSE,用于旧版 MCP SDK)
///
/// # 示例
/// ```
/// use echo_integration::mcp::server_config::McpServerConfig;
/// McpServerConfig::sse("legacy-api", "http://localhost:8080");
/// ```
pub fn sse(name: impl Into<String>, base_url: impl Into<String>) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Sse {
base_url: base_url.into(),
headers: HashMap::new(),
},
}
}
/// 创建 SSE 配置(带自定义请求头)
pub fn sse_with_headers(
name: impl Into<String>,
base_url: impl Into<String>,
headers: HashMap<String, String>,
) -> Self {
Self {
name: name.into(),
transport: TransportConfig::Sse {
base_url: base_url.into(),
headers,
},
}
}
}