forked from paiml/rust-mcp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_client_tools.rs
More file actions
131 lines (112 loc) · 3.56 KB
/
03_client_tools.rs
File metadata and controls
131 lines (112 loc) · 3.56 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
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
//! Example: Client tool discovery and invocation
//!
//! This example demonstrates:
//! - Listing available tools from a server
//! - Calling tools with arguments
//! - Handling tool responses
//! - Error handling for tool calls
use pmcp::{Client, ClientCapabilities, StdioTransport};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt()
.with_env_filter("pmcp=info")
.init();
println!("=== MCP Client Tools Example ===\n");
// Create and initialize client
let transport = StdioTransport::new();
let mut client = Client::new(transport);
// Use minimal capabilities - client doesn't need to advertise any special features
// to call tools on a server
let capabilities = ClientCapabilities::minimal();
println!("Connecting to server...");
let _server_info = client.initialize(capabilities).await?;
println!("✅ Connected!\n");
// List available tools
println!("📋 Listing available tools:");
let tools_result = client.list_tools(None).await?;
for tool in &tools_result.tools {
println!("\n🔧 Tool: {}", tool.name);
if let Some(desc) = &tool.description {
println!(" Description: {}", desc);
}
// Print input schema if available
if !tool.input_schema.is_null() {
println!(
" Input schema: {}",
serde_json::to_string_pretty(&tool.input_schema)?
);
}
}
// Example: Call a calculator tool
println!("\n\n📐 Calling calculator tool:");
let calc_args = json!({
"operation": "multiply",
"a": 42,
"b": std::f64::consts::PI
});
println!(
" Arguments: {}",
serde_json::to_string_pretty(&calc_args)?
);
match client.call_tool("calculator".to_string(), calc_args).await {
Ok(result) => {
println!(
" ✅ Result: {}",
serde_json::to_string_pretty(&result.content)?
);
},
Err(e) => {
println!(" ❌ Error: {}", e);
},
}
// Example: Call a string manipulation tool
println!("\n\n📝 Calling string manipulation tool:");
let string_args = json!({
"text": "Hello, MCP!",
"operation": "reverse"
});
println!(
" Arguments: {}",
serde_json::to_string_pretty(&string_args)?
);
match client
.call_tool("string_manipulator".to_string(), string_args)
.await
{
Ok(result) => {
println!(
" ✅ Result: {}",
serde_json::to_string_pretty(&result.content)?
);
},
Err(e) => {
println!(" ❌ Error: {}", e);
},
}
// Example: Handle tool errors
println!("\n\n⚠️ Testing error handling:");
let bad_args = json!({
"operation": "divide",
"a": 10,
"b": 0 // Division by zero
});
println!(" Arguments: {}", serde_json::to_string_pretty(&bad_args)?);
match client.call_tool("calculator".to_string(), bad_args).await {
Ok(result) => {
println!(
" Result: {}",
serde_json::to_string_pretty(&result.content)?
);
},
Err(e) => {
println!(" ✅ Error caught: {}", e);
// Check error type
if let Some(code) = e.error_code() {
println!(" Error code: {:?}", code);
}
},
}
Ok(())
}