-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path06-mcp-server.rs
More file actions
73 lines (66 loc) · 2.16 KB
/
06-mcp-server.rs
File metadata and controls
73 lines (66 loc) · 2.16 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
// Example 6: MCP Server Integration
//
// Connects to the filesystem MCP server via stdio transport.
// Requires: npm install -g @modelcontextprotocol/server-filesystem
//
// Run: cargo run --example 06-mcp-server
use open_agent_sdk::{Agent, AgentOptions, SDKMessage};
use open_agent_sdk::types::{self, McpServerConfig};
use std::collections::HashMap;
#[tokio::main]
async fn main() {
println!("--- Example 6: MCP Server ---");
let mut mcp_servers = HashMap::new();
mcp_servers.insert(
"filesystem".to_string(),
McpServerConfig::Stdio {
command: "npx".to_string(),
args: vec![
"-y".to_string(),
"@modelcontextprotocol/server-filesystem".to_string(),
"/tmp".to_string(),
],
env: HashMap::new(),
},
);
let agent = Agent::new(AgentOptions {
mcp_servers,
max_turns: Some(10),
..Default::default()
})
.await;
match agent {
Ok(mut agent) => {
let (mut rx, handle) = agent
.query("List files in /tmp using the filesystem MCP tools. Be brief.")
.await;
while let Some(event) = rx.recv().await {
match event {
SDKMessage::Assistant { message, .. } => {
let text = types::extract_text(&message);
if !text.is_empty() {
print!("{}", text);
}
}
SDKMessage::Result { .. } => {
println!("\n--- Done ---");
}
SDKMessage::Error { message } => {
eprintln!("Error: {}", message);
}
_ => {}
}
}
handle.await.unwrap();
agent.close().await;
}
Err(e) => {
eprintln!(
"Failed to create agent: {}\n\
Make sure the MCP server is installed:\n\
npm install -g @modelcontextprotocol/server-filesystem",
e
);
}
}
}