mcpx 0.1.5

A Rust SDK for the Model Context Protocol (MCP)
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Resource types for the MCP protocol

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::{Annotations, Request, Notification};
use super::messages::MessageResult;

/// A known resource that the server is capable of reading.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Resource {
    /// The URI of this resource.
    pub uri: String,

    /// A human-readable name for this resource.
    pub name: String,

    /// A description of what this resource represents.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The MIME type of this resource, if known.
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,

    /// Optional annotations for the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Annotations>,

    /// The size of the raw resource content in bytes, if known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<i64>,
}

impl Resource {
    /// Create a new resource
    pub fn new(uri: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            name: name.into(),
            description: None,
            mime_type: None,
            annotations: None,
            size: None,
        }
    }

    /// Create a new resource with description
    pub fn with_description(uri: impl Into<String>, name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            name: name.into(),
            description: Some(description.into()),
            mime_type: None,
            annotations: None,
            size: None,
        }
    }

    /// Set the MIME type for the resource
    pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
        self.mime_type = Some(mime_type.into());
        self
    }

    /// Set the annotations for the resource
    pub fn with_annotations(mut self, annotations: Annotations) -> Self {
        self.annotations = Some(annotations);
        self
    }

    /// Set the size for the resource
    pub fn with_size(mut self, size: i64) -> Self {
        self.size = Some(size);
        self
    }
}

impl Default for Resource {
    fn default() -> Self {
        Self {
            uri: String::new(),
            name: String::new(),
            description: None,
            mime_type: None,
            annotations: None,
            size: None,
        }
    }
}

/// A template description for resources available on the server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceTemplate {
    /// A URI template that can be used to construct resource URIs.
    #[serde(rename = "uriTemplate")]
    pub uri_template: String,

    /// A human-readable name for the type of resource this template refers to.
    pub name: String,

    /// A description of what this template is for.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The MIME type for all resources that match this template.
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,

    /// Optional annotations for the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Annotations>,
}

impl ResourceTemplate {
    /// Create a new resource template
    pub fn new(uri_template: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            uri_template: uri_template.into(),
            name: name.into(),
            description: None,
            mime_type: None,
            annotations: None,
        }
    }

    /// Create a new resource template with description
    pub fn with_description(
        uri_template: impl Into<String>,
        name: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            uri_template: uri_template.into(),
            name: name.into(),
            description: Some(description.into()),
            mime_type: None,
            annotations: None,
        }
    }

    /// Set the MIME type for the resource template
    pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
        self.mime_type = Some(mime_type.into());
        self
    }

    /// Set the annotations for the resource template
    pub fn with_annotations(mut self, annotations: Annotations) -> Self {
        self.annotations = Some(annotations);
        self
    }
}

/// The contents of a specific resource or sub-resource.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceContents {
    /// The URI of this resource.
    pub uri: String,

    /// The MIME type of this resource, if known.
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
}

impl ResourceContents {
    /// Create new resource contents
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: None,
        }
    }

    /// Create new resource contents with MIME type
    pub fn with_mime_type(uri: impl Into<String>, mime_type: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: Some(mime_type.into()),
        }
    }
}

/// Text contents of a resource
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TextResourceContents {
    /// The URI of this resource.
    pub uri: String,

    /// The MIME type of this resource, if known.
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,

    /// The text of the item.
    pub text: String,
}

impl TextResourceContents {
    /// Create new text resource contents
    pub fn new(uri: impl Into<String>, text: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: None,
            text: text.into(),
        }
    }

    /// Create new text resource contents with MIME type
    pub fn with_mime_type(uri: impl Into<String>, text: impl Into<String>, mime_type: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: Some(mime_type.into()),
            text: text.into(),
        }
    }
}

/// Binary contents of a resource
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BlobResourceContents {
    /// The URI of this resource.
    pub uri: String,

    /// The MIME type of this resource, if known.
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,

    /// The base64-encoded binary data of the item.
    pub blob: String,
}

impl BlobResourceContents {
    /// Create new blob resource contents
    pub fn new(uri: impl Into<String>, blob: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: None,
            blob: blob.into(),
        }
    }

    /// Create new blob resource contents with MIME type
    pub fn with_mime_type(uri: impl Into<String>, blob: impl Into<String>, mime_type: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            mime_type: Some(mime_type.into()),
            blob: blob.into(),
        }
    }
}

/// Request to list resources
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesRequest {
    /// Method is always "resources/list"
    pub method: String,

    /// Optional request parameters
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<ListResourcesParams>,
}

impl Request for ListResourcesRequest {
    const METHOD: &'static str = "resources/list";

    fn method(&self) -> &str {
        &self.method
    }

    fn params(&self) -> Option<&serde_json::Value> {
        None
    }
}

impl ListResourcesRequest {
    /// Create a new list resources request
    pub fn new() -> Self {
        Self {
            method: Self::METHOD.to_string(),
            params: None,
        }
    }

    /// Create a new list resources request with a cursor
    pub fn with_cursor(cursor: impl Into<String>) -> Self {
        Self {
            method: Self::METHOD.to_string(),
            params: Some(ListResourcesParams {
                cursor: Some(cursor.into()),
            }),
        }
    }
}

/// Parameters for list resources request
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesParams {
    /// An opaque token representing the current pagination position.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

/// Result of listing resources
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ListResourcesResult {
    /// The list of resources
    pub resources: Vec<Resource>,

    /// An opaque token representing the pagination position after the last returned result.
    #[serde(rename = "nextCursor", skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,

    /// Metadata for the result
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<super::messages::ResultMeta>,
}

impl MessageResult for ListResourcesResult {}

impl ListResourcesResult {
    /// Create a new list resources result
    pub fn new(resources: Vec<Resource>) -> Self {
        Self {
            resources,
            next_cursor: None,
            meta: None,
        }
    }

    /// Create a new list resources result with a next cursor
    pub fn with_next_cursor(resources: Vec<Resource>, next_cursor: impl Into<String>) -> Self {
        Self {
            resources,
            next_cursor: Some(next_cursor.into()),
            meta: None,
        }
    }
}

/// Request to read a resource
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceRequest {
    /// Method is always "resources/read"
    pub method: String,

    /// Request parameters
    pub params: ReadResourceParams,
}

impl Request for ReadResourceRequest {
    const METHOD: &'static str = "resources/read";

    fn method(&self) -> &str {
        &self.method
    }

    fn params(&self) -> Option<&serde_json::Value> {
        None
    }
}

impl ReadResourceRequest {
    /// Create a new read resource request
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            method: Self::METHOD.to_string(),
            params: ReadResourceParams {
                uri: uri.into(),
            },
        }
    }
}

/// Parameters for read resource request
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceParams {
    /// The URI of the resource to read.
    pub uri: String,
}

/// Result of reading a resource
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReadResourceResult {
    /// The contents of the resource
    pub contents: Vec<ResourceContent>,

    /// Metadata for the result
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<super::messages::ResultMeta>,
}

impl ReadResourceResult {
    /// Create a new read resource result
    pub fn new(contents: Vec<ResourceContent>) -> Self {
        Self {
            contents,
            meta: None,
        }
    }
}

/// Content of a resource, either text or binary
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ResourceContent {
    /// Text content
    Text(TextResourceContents),
    /// Binary content
    Blob(BlobResourceContents),
}

/// Request to subscribe to resource updates
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SubscribeRequest {
    /// Method is always "resources/subscribe"
    pub method: String,

    /// Request parameters
    pub params: SubscribeParams,
}

impl Request for SubscribeRequest {
    const METHOD: &'static str = "resources/subscribe";

    fn method(&self) -> &str {
        &self.method
    }

    fn params(&self) -> Option<&serde_json::Value> {
        None
    }
}

impl SubscribeRequest {
    /// Create a new subscribe request
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            method: Self::METHOD.to_string(),
            params: SubscribeParams {
                uri: uri.into(),
            },
        }
    }
}

/// Parameters for subscribe request
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SubscribeParams {
    /// The URI of the resource to subscribe to.
    pub uri: String,
}

/// Request to unsubscribe from resource updates
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UnsubscribeRequest {
    /// Method is always "resources/unsubscribe"
    pub method: String,

    /// Request parameters
    pub params: UnsubscribeParams,
}

impl Request for UnsubscribeRequest {
    const METHOD: &'static str = "resources/unsubscribe";

    fn method(&self) -> &str {
        &self.method
    }

    fn params(&self) -> Option<&serde_json::Value> {
        None
    }
}

impl UnsubscribeRequest {
    /// Create a new unsubscribe request
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            method: Self::METHOD.to_string(),
            params: UnsubscribeParams {
                uri: uri.into(),
            },
        }
    }
}

/// Parameters for unsubscribe request
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UnsubscribeParams {
    /// The URI of the resource to unsubscribe from.
    pub uri: String,
}

/// Notification that a resource has been updated
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceUpdatedNotification {
    /// Method is always "notifications/resources/updated"
    pub method: String,

    /// Notification parameters
    pub params: ResourceUpdatedParams,
}

impl Notification for ResourceUpdatedNotification {
    const METHOD: &'static str = "notifications/resources/updated";

    fn method(&self) -> &str {
        &self.method
    }

    fn params(&self) -> Option<&serde_json::Value> {
        None
    }
}

impl ResourceUpdatedNotification {
    /// Create a new resource updated notification
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            method: "notifications/resources/updated".to_string(),
            params: ResourceUpdatedParams {
                uri: uri.into(),
            },
        }
    }
}

/// Parameters for resource updated notification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceUpdatedParams {
    /// The URI of the resource that has been updated.
    pub uri: String,
}

/// Notification that the list of resources has changed
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceListChangedNotification {
    /// Method is always "notifications/resources/list_changed"
    pub method: String,

    /// Optional notification parameters
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<super::messages::NotificationParams>,
}

impl ResourceListChangedNotification {
    /// Create a new resource list changed notification
    pub fn new() -> Self {
        Self {
            method: "notifications/resources/list_changed".to_string(),
            params: None,
        }
    }
}

/// The contents of a resource, embedded into a prompt or tool call result.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EmbeddedResource {
    /// Type is always "resource"
    pub r#type: String,

    /// The resource content
    pub resource: ResourceContent,

    /// Optional annotations for the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Annotations>,
}

impl EmbeddedResource {
    /// Create a new embedded resource with text content
    pub fn text(uri: impl Into<String>, text: impl Into<String>) -> Self {
        Self {
            r#type: "resource".to_string(),
            resource: ResourceContent::Text(TextResourceContents::new(uri, text)),
            annotations: None,
        }
    }

    /// Create a new embedded resource with binary content
    pub fn blob(uri: impl Into<String>, blob: impl Into<String>) -> Self {
        Self {
            r#type: "resource".to_string(),
            resource: ResourceContent::Blob(BlobResourceContents::new(uri, blob)),
            annotations: None,
        }
    }

    /// Set the annotations for the embedded resource
    pub fn with_annotations(mut self, annotations: Annotations) -> Self {
        self.annotations = Some(annotations);
        self
    }
}

/// A reference to a resource or resource template definition.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResourceReference {
    /// Type is always "ref/resource"
    pub r#type: String,

    /// The URI or URI template of the resource.
    pub uri: String,
}

impl ResourceReference {
    /// Create a new resource reference
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            r#type: "ref/resource".to_string(),
            uri: uri.into(),
        }
    }
}