mcp_plugin_api/
resource.rs1use serde_json::{json, Value};
8
9#[derive(Debug, Clone)]
15pub struct ResourceContent {
16 pub uri: String,
18 pub mime_type: Option<String>,
20 pub text: Option<String>,
22 pub blob: Option<String>,
24}
25
26impl ResourceContent {
27 pub fn text(uri: impl Into<String>, content: impl Into<String>, mime_type: Option<String>) -> Self {
29 Self {
30 uri: uri.into(),
31 mime_type,
32 text: Some(content.into()),
33 blob: None,
34 }
35 }
36
37 pub fn blob(uri: impl Into<String>, base64_data: impl Into<String>, mime_type: Option<String>) -> Self {
39 Self {
40 uri: uri.into(),
41 mime_type,
42 text: None,
43 blob: Some(base64_data.into()),
44 }
45 }
46
47 pub fn to_json(&self) -> Value {
49 let mut obj = serde_json::Map::new();
50 obj.insert("uri".to_string(), json!(self.uri));
51 if let Some(ref mt) = self.mime_type {
52 obj.insert("mimeType".to_string(), json!(mt));
53 }
54 if let Some(ref t) = self.text {
55 obj.insert("text".to_string(), json!(t));
56 }
57 if let Some(ref b) = self.blob {
58 obj.insert("blob".to_string(), json!(b));
59 }
60 Value::Object(obj)
61 }
62}
63
64pub type ResourceContents = Vec<ResourceContent>;
66
67pub type ResourceHandler = fn(&str) -> Result<ResourceContents, String>;
71
72#[derive(Debug, Clone)]
77pub struct Resource {
78 pub uri: String,
80 pub name: Option<String>,
82 pub title: Option<String>,
84 pub description: Option<String>,
86 pub mime_type: Option<String>,
88 pub handler: ResourceHandler,
90}
91
92impl Resource {
93 pub fn builder(uri: impl Into<String>, handler: ResourceHandler) -> ResourceBuilder {
104 ResourceBuilder {
105 uri: uri.into(),
106 name: None,
107 title: None,
108 description: None,
109 mime_type: None,
110 handler,
111 }
112 }
113
114 pub fn to_list_item(&self) -> Value {
127 let mut obj = serde_json::Map::new();
128 obj.insert("uri".to_string(), json!(self.uri));
129 if let Some(ref n) = self.name {
130 obj.insert("name".to_string(), json!(n));
131 }
132 if let Some(ref t) = self.title {
133 obj.insert("title".to_string(), json!(t));
134 }
135 if let Some(ref d) = self.description {
136 obj.insert("description".to_string(), json!(d));
137 }
138 if let Some(ref mt) = self.mime_type {
139 obj.insert("mimeType".to_string(), json!(mt));
140 }
141 Value::Object(obj)
142 }
143}
144
145pub struct ResourceBuilder {
147 uri: String,
148 name: Option<String>,
149 title: Option<String>,
150 description: Option<String>,
151 mime_type: Option<String>,
152 handler: ResourceHandler,
153}
154
155impl ResourceBuilder {
156 pub fn name(mut self, name: impl Into<String>) -> Self {
158 self.name = Some(name.into());
159 self
160 }
161
162 pub fn title(mut self, title: impl Into<String>) -> Self {
164 self.title = Some(title.into());
165 self
166 }
167
168 pub fn description(mut self, description: impl Into<String>) -> Self {
170 self.description = Some(description.into());
171 self
172 }
173
174 pub fn mime_type(mut self, mime_type: impl Into<String>) -> Self {
176 self.mime_type = Some(mime_type.into());
177 self
178 }
179
180 pub fn build(self) -> Resource {
182 Resource {
183 uri: self.uri,
184 name: self.name,
185 title: self.title,
186 description: self.description,
187 mime_type: self.mime_type,
188 handler: self.handler,
189 }
190 }
191}