-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathmcp.rs
More file actions
83 lines (77 loc) · 2.54 KB
/
mcp.rs
File metadata and controls
83 lines (77 loc) · 2.54 KB
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
use std::{collections::HashMap, process::Stdio};
use rmcp::{RoleClient, ServiceExt, service::RunningService, transport::ConfigureCommandExt};
use serde::{Deserialize, Serialize};
use crate::mcp_adaptor::McpManager;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct McpServerConfig {
name: String,
#[serde(flatten)]
transport: McpServerTransportConfig,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "protocol", rename_all = "lowercase")]
pub enum McpServerTransportConfig {
Streamable {
url: String,
},
Stdio {
command: String,
#[serde(default)]
args: Vec<String>,
#[serde(default)]
envs: HashMap<String, String>,
},
}
#[derive(Debug, Serialize, Deserialize)]
pub struct McpConfig {
server: Vec<McpServerConfig>,
}
impl McpConfig {
pub async fn create_manager(&self) -> anyhow::Result<McpManager> {
let mut clients = HashMap::new();
let mut task_set = tokio::task::JoinSet::<anyhow::Result<_>>::new();
for server in &self.server {
let server = server.clone();
task_set.spawn(async move {
let client = server.transport.start().await?;
anyhow::Result::Ok((server.name.clone(), client))
});
}
let start_up_result = task_set.join_all().await;
for result in start_up_result {
match result {
Ok((name, client)) => {
clients.insert(name, client);
}
Err(e) => {
eprintln!("Failed to start server: {:?}", e);
}
}
}
Ok(McpManager { clients })
}
}
impl McpServerTransportConfig {
pub async fn start(&self) -> anyhow::Result<RunningService<RoleClient, ()>> {
let client = match self {
McpServerTransportConfig::Streamable { url } => {
let transport =
rmcp::transport::StreamableHttpClientTransport::from_uri(url.to_string());
().serve(transport).await?
}
McpServerTransportConfig::Stdio {
command,
args,
envs,
} => {
let transport = rmcp::transport::TokioChildProcess::new(
tokio::process::Command::new(command).configure(|cmd| {
cmd.args(args).envs(envs).stderr(Stdio::null());
}),
)?;
().serve(transport).await?
}
};
Ok(client)
}
}