forked from paiml/rust-mcp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_server_resources.rs
More file actions
234 lines (209 loc) · 7.01 KB
/
04_server_resources.rs
File metadata and controls
234 lines (209 loc) · 7.01 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
//! Example: Server with resource support
//!
//! This example demonstrates:
//! - Creating a server that provides resources
//! - Implementing resource handlers
//! - Resource listing and reading
//! - URI template support
use async_trait::async_trait;
use pmcp::{
types::{
capabilities::ServerCapabilities, Content, ListResourcesResult, ReadResourceResult,
ResourceInfo,
},
ResourceHandler, Server,
};
use std::collections::HashMap;
// Mock file system resource handler
struct FileSystemResources {
files: HashMap<String, String>,
}
impl FileSystemResources {
fn new() -> Self {
let mut files = HashMap::new();
// Simulate some files
files.insert(
"file://config/app.json".to_string(),
r#"{
"name": "Example App",
"version": "1.0.0",
"features": {
"auth": true,
"logging": true
}
}"#
.to_string(),
);
files.insert(
"file://data/users.csv".to_string(),
"id,name,email\n1,Alice,alice@example.com\n2,Bob,bob@example.com".to_string(),
);
files.insert(
"file://logs/app.log".to_string(),
"[2025-01-15 10:00:00] INFO: Application started\n[2025-01-15 10:00:05] DEBUG: Connected to database\n".to_string(),
);
Self { files }
}
}
#[async_trait]
impl ResourceHandler for FileSystemResources {
async fn read(
&self,
uri: &str,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ReadResourceResult> {
match self.files.get(uri) {
Some(content) => Ok(ReadResourceResult::new(vec![Content::text(content)])),
None => Err(pmcp::Error::protocol(
pmcp::ErrorCode::METHOD_NOT_FOUND,
format!("Resource not found: {}", uri),
)),
}
}
async fn list(
&self,
_cursor: Option<String>,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ListResourcesResult> {
let resources: Vec<ResourceInfo> = self
.files
.keys()
.map(|uri| {
ResourceInfo::new(uri, uri.rsplit('/').next().unwrap_or(""))
.with_description(format!("Mock file at {}", uri))
.with_mime_type(guess_mime_type(uri))
})
.collect();
Ok(ListResourcesResult::new(resources))
}
}
fn guess_mime_type(uri: &str) -> String {
if uri.ends_with(".json") {
"application/json".to_string()
} else if uri.ends_with(".csv") {
"text/csv".to_string()
} else if uri.ends_with(".log") {
"text/plain".to_string()
} else {
"application/octet-stream".to_string()
}
}
// Template-based resource handler
struct TemplateResources;
#[async_trait]
impl ResourceHandler for TemplateResources {
async fn read(
&self,
uri: &str,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ReadResourceResult> {
// Example: Handle parameterized URIs like "template://greeting/{name}"
if uri.starts_with("template://greeting/") {
let name = uri.strip_prefix("template://greeting/").unwrap_or("World");
Ok(ReadResourceResult::new(vec![Content::Text {
text: format!("Hello, {}! Welcome to MCP resources.", name),
}]))
} else if uri.starts_with("template://time/") {
let timezone = uri.strip_prefix("template://time/").unwrap_or("UTC");
Ok(ReadResourceResult::new(vec![Content::text(format!(
"Current time in {}: {}",
timezone,
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
))]))
} else {
Err(pmcp::Error::protocol(
pmcp::ErrorCode::METHOD_NOT_FOUND,
format!("Resource not found: {}", uri),
))
}
}
async fn list(
&self,
_cursor: Option<String>,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ListResourcesResult> {
Ok(ListResourcesResult::new(vec![
ResourceInfo::new("template://greeting/{name}", "Greeting Template")
.with_description("Personalized greeting message")
.with_mime_type("text/plain"),
ResourceInfo::new("template://time/{timezone}", "Time Template")
.with_description("Current time in specified timezone")
.with_mime_type("text/plain"),
]))
}
}
// Combine multiple resource handlers
struct CombinedResources {
filesystem: FileSystemResources,
templates: TemplateResources,
}
#[async_trait]
impl ResourceHandler for CombinedResources {
async fn read(
&self,
uri: &str,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ReadResourceResult> {
if uri.starts_with("file://") {
self.filesystem.read(uri, _extra).await
} else if uri.starts_with("template://") {
self.templates.read(uri, _extra).await
} else {
Err(pmcp::Error::protocol(
pmcp::ErrorCode::METHOD_NOT_FOUND,
format!("Resource not found: {}", uri),
))
}
}
async fn list(
&self,
cursor: Option<String>,
_extra: pmcp::RequestHandlerExtra,
) -> pmcp::Result<ListResourcesResult> {
// Simple pagination: use cursor to determine which handler to list from
match cursor.as_deref() {
None | Some("") => {
// List filesystem resources first
let mut result = self.filesystem.list(None, _extra).await?;
result.next_cursor = Some("templates".to_string());
Ok(result)
},
Some("templates") => {
// Then list template resources
self.templates.list(None, _extra).await
},
_ => Ok(ListResourcesResult::new(vec![])),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt()
.with_env_filter("pmcp=info")
.init();
println!("=== MCP Server Resources Example ===");
println!("Starting server with file and template resources...\n");
// Build server with resource support
let server = Server::builder()
.name("resource-server")
.version("1.0.0")
.capabilities(ServerCapabilities::resources_only())
.resources(CombinedResources {
filesystem: FileSystemResources::new(),
templates: TemplateResources,
})
.build()?;
println!("Server ready! Available resources:");
println!("\n📁 File Resources:");
println!(" - file://config/app.json");
println!(" - file://data/users.csv");
println!(" - file://logs/app.log");
println!("\n🔗 Template Resources:");
println!(" - template://greeting/{{name}}");
println!(" - template://time/{{timezone}}");
println!("\nListening on stdio...");
// Run server
server.run_stdio().await?;
Ok(())
}