mcp_daemon 0.2.1

Diverged Implementation of Model Context Protocol (MCP) with Extended Functionality
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use url::Url;

/// The latest supported version of the MCP protocol
pub const LATEST_PROTOCOL_VERSION: &str = "2024-11-05";

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Information about an MCP implementation (client or server)
pub struct Implementation {
    /// Name of the implementation
    pub name: String,
    /// Version of the implementation
    pub version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Request to initialize an MCP connection
pub struct InitializeRequest {
    /// Version of the MCP protocol to use
    pub protocol_version: String,
    /// Capabilities supported by the client
    pub capabilities: ClientCapabilities,
    /// Information about the client implementation
    pub client_info: Implementation,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Response to an initialization request
pub struct InitializeResponse {
    /// Version of the MCP protocol being used
    pub protocol_version: String,
    /// Capabilities supported by the server
    pub capabilities: ServerCapabilities,
    /// Information about the server implementation
    pub server_info: Implementation,
    /// Optional instructions for the client
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities supported by the server
pub struct ServerCapabilities {
    /// Tool-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<ToolCapabilities>,
    /// Experimental features
    #[serde(skip_serializing_if = "Option::is_none")]
    pub experimental: Option<serde_json::Value>,
    /// Logging-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logging: Option<LoggingCapabilities>,
    /// Prompt-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompts: Option<PromptCapabilities>,
    /// Resource-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resources: Option<ResourceCapabilities>,
    /// Progress tracking capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub progress: Option<ProgressCapabilities>,
    /// Completion-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completion: Option<CompletionCapabilities>,
    /// Sampling-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sampling: Option<SamplingCapabilities>,
    /// Root-related capabilities
    #[serde(skip_serializing_if = "Option::is_none")]
    pub roots: Option<RootCapabilities>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to tool functionality
pub struct ToolCapabilities {
    /// Whether the tool list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to logging functionality
pub struct LoggingCapabilities {
    /// Whether window-based logging is supported
    pub window: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to sampling functionality
pub struct SamplingCapabilities {
    /// Whether the sampling list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to completion functionality
pub struct CompletionCapabilities {
    /// Whether the completion list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to progress tracking
pub struct ProgressCapabilities {
    /// Whether window-based progress tracking is supported
    pub window: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Progress information for a long-running operation
pub struct Progress {
    /// Unique token identifying this progress item
    pub token: String,
    /// Current progress value
    pub value: ProgressValue,
    /// Optional metadata associated with the progress
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Value representing the state of a progress operation
pub struct ProgressValue {
    /// Type of progress (e.g., 'percentage', 'count')
    pub kind: String,
    /// Optional title describing the operation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional message about the current state
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// Progress as a percentage
    #[serde(skip_serializing_if = "Option::is_none")]
    pub percentage: Option<f64>,
    /// Whether the operation can be cancelled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cancellable: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Request to cancel a progress operation
pub struct ProgressCancelRequest {
    /// Token of the progress operation to cancel
    pub token: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to prompt functionality
pub struct PromptCapabilities {
    /// Whether the prompt list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to resource functionality
pub struct ResourceCapabilities {
    /// Whether resource subscription is supported
    pub subscribe: Option<bool>,
    /// Whether the resource list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities supported by the client
pub struct ClientCapabilities {
    /// Experimental features
    pub experimental: Option<serde_json::Value>,
    /// Sampling-related capabilities
    pub sampling: Option<SamplingCapabilities>,
    /// Root-related capabilities
    pub roots: Option<RootCapabilities>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
/// Capabilities related to root functionality
pub struct RootCapabilities {
    /// Whether the root list has changed
    pub list_changed: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Description of a tool that can be called by the client
pub struct Tool {
    /// Name of the tool
    pub name: String,
    /// Optional description of what the tool does
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// JSON schema describing the tool's input parameters
    pub input_schema: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Request to call a tool
pub struct CallToolRequest {
    /// Name of the tool to call
    pub name: String,
    /// Optional arguments to pass to the tool
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<HashMap<String, serde_json::Value>>,
    /// Optional metadata for the request
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Response from a tool call
pub struct CallToolResponse {
    /// Content returned by the tool
    pub content: Vec<Content>,
    /// Whether the response represents an error
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
    /// Optional metadata for the response
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
/// Content that can be returned by a tool
pub enum Content {
    /// Text content
    #[serde(rename = "text")]
    Text {
        /// The text content
        text: String
    },
    /// Image content with data and MIME type
    #[serde(rename = "image")]
    Image {
        /// The image data (e.g., base64 encoded)
        data: String,
        /// The MIME type of the image
        mime_type: String
    },
    /// Resource content
    #[serde(rename = "resource")]
    Resource {
        /// The resource contents
        resource: ResourceContents
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Contents of a resource
pub struct ResourceContents {
    /// URI where the resource can be accessed
    pub uri: Url,
    /// Optional MIME type of the resource content
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    /// Optional text content
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Optional binary content as base64
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blob: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Request to read a resource
pub struct ReadResourceRequest {
    /// URI of the resource to read
    pub uri: Url,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Request to list items with pagination
pub struct ListRequest {
    /// Optional cursor for pagination
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    /// Optional metadata for the request
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Response containing a list of tools
pub struct ToolsListResponse {
    /// List of available tools
    pub tools: Vec<Tool>,
    /// Optional cursor for the next page
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
    /// Optional metadata for the response
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// Response containing a list of prompts
pub struct PromptsListResponse {
    /// List of available prompts
    pub prompts: Vec<Prompt>,
    /// Optional cursor for the next page
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
    /// Optional metadata for the response
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// Description of a prompt that can be used by the client
pub struct Prompt {
    /// Name of the prompt
    pub name: String,
    /// Optional description of what the prompt does
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional list of arguments required by the prompt
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Vec<PromptArgument>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// Description of an argument required by a prompt
pub struct PromptArgument {
    /// Name of the argument
    pub name: String,
    /// Optional description of what the argument is for
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Whether the argument is required
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// Response containing a list of resources
pub struct ResourcesListResponse {
    /// List of available resources
    pub resources: Vec<Resource>,
    /// Optional cursor for the next page
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
    /// Optional metadata for the response
    #[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
    pub meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Description of a resource that can be accessed by the client
pub struct Resource {
    /// URI where the resource can be accessed
    pub uri: Url,
    /// Name of the resource
    pub name: String,
    /// Optional description of what the resource contains
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional MIME type of the resource content
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
}

/// Error codes for various error conditions in the MCP protocol
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCode {
    // SDK error codes
    /// Indicates that the connection to the server was closed unexpectedly
    ConnectionClosed = -32000,
    /// Indicates that a request to the server timed out
    RequestTimeout = -32001,

    // Standard JSON-RPC error codes
    /// Indicates an error while parsing the JSON-RPC request
    ParseError = -32700,
    /// Indicates that the JSON-RPC request was invalid
    InvalidRequest = -32600,
    /// Indicates that the method specified in the JSON-RPC request was not found
    MethodNotFound = -32601,
    /// Indicates that the parameters provided in the JSON-RPC request were invalid
    InvalidParams = -32602,
    /// Indicates an internal error occurred while processing the JSON-RPC request
    InternalError = -32603,
}

/// Message content types shared between prompts and tool responses
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum MessageContent {
    /// Represents text content
    #[serde(rename = "text")]
    Text {
        /// The text content
        text: String
    },
    /// Represents image content
    #[serde(rename = "image")]
    Image {
        /// The image data (e.g., base64 encoded)
        data: String,
        /// The MIME type of the image
        mime_type: String
    },
}

/// Represents a message in a prompt
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PromptMessage {
    /// The role of the message sender (e.g., "user", "assistant")
    pub role: String,
    /// The content of the message
    pub content: MessageContent,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_capabilities() {
        let capabilities = ServerCapabilities::default();
        let json = serde_json::to_string(&capabilities).unwrap();
        assert_eq!(json, "{}");
    }
}