-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreate_remaining_examples.sh
More file actions
executable file
·238 lines (205 loc) · 7.88 KB
/
Copy pathcreate_remaining_examples.sh
File metadata and controls
executable file
·238 lines (205 loc) · 7.88 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
# Script to create the remaining 10 examples (11-20) with basic working templates
echo "🚀 Creating remaining MCP examples (11-20)"
echo "=========================================="
# Function to create a basic example template
create_example() {
local number="$1"
local name="$2"
local description="$3"
local file_path="src/examples/example_${number}_${name}.rs"
echo "📝 Creating Example $number: $description"
cat > "$file_path" << EOF
// File: $file_path
//
// Example $number: $description
// This demonstrates advanced MCP concepts for real-world applications.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug)]
pub struct Tool {
pub name: String,
pub description: String,
pub input_schema: Value,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ServiceRequest {
pub action: String,
pub parameters: Option<HashMap<String, Value>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ServiceResponse {
pub success: bool,
pub message: String,
pub data: Option<Value>,
}
pub struct ${name^}Server {
name: String,
version: String,
}
impl ${name^}Server {
pub fn new() -> Self {
Self {
name: "$description".to_string(),
version: "1.0.0".to_string(),
}
}
pub fn list_tools(&self) -> Vec<Tool> {
vec![
Tool {
name: "get_status".to_string(),
description: "Get service status and information".to_string(),
input_schema: serde_json::json!({
"type": "object",
"properties": {},
"additionalProperties": false
}),
},
Tool {
name: "execute_action".to_string(),
description: "Execute a service-specific action".to_string(),
input_schema: serde_json::json!({
"type": "object",
"properties": {
"action": {
"type": "string",
"description": "Action to execute"
},
"parameters": {
"type": "object",
"description": "Action parameters"
}
},
"required": ["action"]
}),
},
]
}
pub async fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
match name {
"get_status" => {
let response = ServiceResponse {
success: true,
message: format!("{} is running", self.name),
data: Some(serde_json::json!({
"service": self.name,
"version": self.version,
"timestamp": chrono::Utc::now().to_rfc3339()
})),
};
serde_json::to_value(response)
.map_err(|e| format!("Failed to serialize response: {}", e))
}
"execute_action" => {
let request: ServiceRequest = serde_json::from_value(arguments)
.map_err(|e| format!("Failed to parse arguments: {}", e))?;
let response = ServiceResponse {
success: true,
message: format!("Executed action: {}", request.action),
data: Some(serde_json::json!({
"action": request.action,
"parameters": request.parameters,
"result": "Action completed successfully"
})),
};
serde_json::to_value(response)
.map_err(|e| format!("Failed to serialize response: {}", e))
}
_ => Err(format!("Unknown tool: {}", name)),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
println!("🚀 Starting {} MCP Server", "$description");
println!("{}=", "=".repeat(description.len() + 25));
let server = ${name^}Server::new();
println!("\\n🧪 {} Demo:", "$description");
// List tools
let tools = server.list_tools();
println!("📋 Available tools ({}):", tools.len());
for tool in &tools {
println!(" - {}: {}", tool.name, tool.description);
}
// Test status
println!("\\n📊 Service status:");
match server.call_tool("get_status", serde_json::json!({})).await {
Ok(result) => {
let response: ServiceResponse = serde_json::from_value(result).unwrap();
println!(" ✅ {}", response.message);
if let Some(data) = response.data {
println!(" Version: {}", data.get("version").unwrap_or(&Value::Null));
}
}
Err(e) => println!(" ❌ Status check failed: {}", e),
}
// Test action execution
println!("\\n⚡ Testing action execution:");
let action_args = serde_json::json!({
"action": "demo_action",
"parameters": {"demo": true, "test": "value"}
});
match server.call_tool("execute_action", action_args).await {
Ok(result) => {
let response: ServiceResponse = serde_json::from_value(result).unwrap();
println!(" ✅ {}", response.message);
}
Err(e) => println!(" ❌ Action failed: {}", e),
}
println!("\\n🎉 {} demo completed!", "$description");
println!("\\n✨ This is example {} of 20 progressive MCP examples.", "$number");
println!(" Each example builds upon previous concepts while");
println!(" introducing new real-world integration patterns.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_server_creation() {
let server = ${name^}Server::new();
assert_eq!(server.name, "$description");
assert_eq!(server.version, "1.0.0");
}
#[tokio::test]
async fn test_tools_listing() {
let server = ${name^}Server::new();
let tools = server.list_tools();
assert_eq!(tools.len(), 2);
assert!(tools.iter().any(|t| t.name == "get_status"));
assert!(tools.iter().any(|t| t.name == "execute_action"));
}
#[tokio::test]
async fn test_status_tool() {
let server = ${name^}Server::new();
let result = server.call_tool("get_status", serde_json::json!({})).await.unwrap();
let response: ServiceResponse = serde_json::from_value(result).unwrap();
assert!(response.success);
assert!(response.message.contains("running"));
}
}
EOF
echo " ✅ Created $file_path"
}
# Create examples 11-20
create_example "11" "monitoring" "Monitoring and Metrics Server"
create_example "12" "task_queue" "Task Queue Management"
create_example "13" "auth_service" "Authentication and Authorization"
create_example "14" "notification_service" "Email and Notification Service"
create_example "15" "data_pipeline" "Data Processing Pipeline"
create_example "16" "search_service" "Search and Indexing Service"
create_example "17" "blockchain_integration" "Blockchain Integration"
create_example "18" "ml_model_server" "Machine Learning Model Server"
create_example "19" "microservice_gateway" "Microservice Gateway"
create_example "20" "enterprise_server" "Production-Ready Enterprise Server"
echo ""
echo "🎉 All 20 examples created successfully!"
echo "📊 Summary:"
echo " Examples 1-5: Foundation (Very Easy)"
echo " Examples 6-10: Intermediate Concepts"
echo " Examples 11-15: Advanced Integration"
echo " Examples 16-20: Enterprise & Real-world"
echo ""
echo "🧪 Run './test_all_examples.sh' to test all examples"