mcp-probe-core 0.3.0

Core MCP (Model Context Protocol) types, traits, and transport implementations
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
//! MCP (Model Context Protocol) message types and JSON-RPC structures.
//!
//! This module provides complete type definitions for all MCP messages according to the
//! MCP specification. All message types are designed to be:
//!
//! - **Spec-compliant**: Exact adherence to MCP JSON-RPC 2.0 protocol
//! - **Type-safe**: Leverage Rust's type system to prevent protocol violations
//! - **Serializable**: Full serde support for JSON serialization/deserialization
//! - **Extensible**: Support for custom extensions and future protocol versions
//! - **Debuggable**: Rich Debug implementations for development and debugging
//!
//! # Message Categories
//!
//! - **Core Messages**: Basic JSON-RPC request/response/notification structures
//! - **Initialization**: Protocol version negotiation and capability discovery
//! - **Tools**: Tool discovery, schema definition, and execution
//! - **Resources**: Resource listing, reading, and subscription
//! - **Prompts**: Prompt templates and completion requests
//! - **Sampling**: LLM completion requests from server to client
//! - **Logging**: Server-to-client logging messages
//!
//! # Examples
//!
//! ```rust
//! use mcp_probe_core::messages::{JsonRpcRequest, InitializeRequest, ProtocolVersion, Implementation};
//! use serde_json::json;
//!
//! // Create an initialization request
//! let init_request = InitializeRequest {
//!     protocol_version: ProtocolVersion::V2024_11_05,
//!     capabilities: Default::default(),
//!     client_info: Implementation {
//!         name: "mcp-probe".to_string(),
//!         version: "0.1.0".to_string(),
//!         metadata: std::collections::HashMap::new(),
//!     },
//! };
//!
//! // Wrap in JSON-RPC request
//! let request = JsonRpcRequest::new(
//!     "1".to_string(),
//!     "initialize".to_string(),
//!     serde_json::to_value(init_request).unwrap(),
//! );
//! ```

pub mod core;
pub mod initialization;
pub mod logging;
pub mod prompts;
pub mod resources;
pub mod sampling;
pub mod tools;

pub use core::*;
pub use initialization::*;
pub use logging::{
    LogLevel, LoggingNotification, ProgressNotification,
    PromptListChangedNotification as LoggingPromptListChangedNotification,
    ResourceListChangedNotification as LoggingResourceListChangedNotification,
    ResourceUpdatedNotification as LoggingResourceUpdatedNotification, SetLevelRequest,
    ToolListChangedNotification as LoggingToolListChangedNotification,
};
pub use prompts::{
    GetPromptRequest, GetPromptResponse, ListPromptsRequest, ListPromptsResponse,
    MessageRole as PromptMessageRole, Prompt, PromptContent, PromptListChangedNotification,
    PromptMessage, ResourceReference as PromptResourceReference,
};
pub use resources::{
    ListResourcesRequest, ListResourcesResponse, ReadResourceRequest, ReadResourceResponse,
    Resource, ResourceContent, ResourceListChangedNotification, ResourceUpdatedNotification,
    SubscribeRequest, UnsubscribeRequest,
};
pub use sampling::{
    CompleteRequest, CompleteResponse, CompletionArgument, CompletionResult, CostPriority,
    IntelligencePriority, MessageRole, ModelPreferences, SamplingContent, SamplingMessage,
    SpeedPriority, StopReason,
};
pub use tools::{
    CallToolRequest, CallToolResponse, ListToolsRequest, ListToolsResponse,
    ResourceReference as ToolResourceReference, Tool, ToolListChangedNotification, ToolResult,
};

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

/// MCP protocol version identifier.
///
/// The MCP protocol uses semantic versioning with date-based versions.
/// This enum provides type-safe handling of supported protocol versions.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProtocolVersion {
    /// MCP Protocol version 2024-11-05 (legacy)
    #[serde(rename = "2024-11-05")]
    V2024_11_05,

    /// MCP Protocol version 2025-03-26 (current stable)
    #[serde(rename = "2025-03-26")]
    V2025_03_26,

    /// Future protocol versions can be added here
    /// Custom version string for forward compatibility
    #[serde(untagged)]
    Custom(String),
}

impl ProtocolVersion {
    /// Get the string representation of the protocol version.
    pub fn as_str(&self) -> &str {
        match self {
            Self::V2024_11_05 => "2024-11-05",
            Self::V2025_03_26 => "2025-03-26",
            Self::Custom(version) => version,
        }
    }

    /// Check if this version is supported by the current implementation.
    pub fn is_supported(&self) -> bool {
        matches!(self, Self::V2024_11_05 | Self::V2025_03_26)
    }

    /// Get all supported protocol versions.
    pub fn supported_versions() -> Vec<Self> {
        vec![Self::V2024_11_05, Self::V2025_03_26]
    }
}

impl Default for ProtocolVersion {
    fn default() -> Self {
        Self::V2025_03_26
    }
}

impl std::fmt::Display for ProtocolVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Generic capability structure for extensible capability negotiation.
///
/// Capabilities are used during initialization to negotiate what features
/// both client and server support. This structure allows for both standard
/// and custom capabilities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Capabilities {
    /// Standard MCP capabilities
    #[serde(flatten)]
    pub standard: StandardCapabilities,

    /// Custom or experimental capabilities
    #[serde(flatten)]
    pub custom: HashMap<String, serde_json::Value>,
}

/// Standard MCP capabilities as defined in the specification.
///
/// These capabilities control what features are available during the MCP session.
/// Both client and server declare their capabilities during initialization.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct StandardCapabilities {
    /// Server capability: Can provide tools for execution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<ToolCapabilities>,

    /// Server capability: Can provide resources for reading
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resources: Option<ResourceCapabilities>,

    /// Server capability: Can provide prompt templates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompts: Option<PromptCapabilities>,

    /// Client capability: Can handle sampling requests from server
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sampling: Option<SamplingCapabilities>,

    /// Server capability: Can send log messages to client
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logging: Option<LoggingCapabilities>,

    /// Client capability: Can provide root directories for server operations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub roots: Option<RootsCapabilities>,
}

/// Tool-related capabilities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ToolCapabilities {
    /// Whether the server supports listing changed tools
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_changed: Option<bool>,
}

/// Resource-related capabilities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ResourceCapabilities {
    /// Whether the server supports subscribing to resource changes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscribe: Option<bool>,

    /// Whether the server supports listing changed resources
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_changed: Option<bool>,
}

/// Prompt-related capabilities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct PromptCapabilities {
    /// Whether the server supports listing changed prompts
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_changed: Option<bool>,
}

/// Sampling-related capabilities (client-side).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct SamplingCapabilities {
    /// Whether the client supports receiving sampling requests
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
}

/// Logging-related capabilities.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct LoggingCapabilities {
    /// Whether the server supports different log levels
    #[serde(skip_serializing_if = "Option::is_none")]
    pub level: Option<bool>,
}

/// Roots-related capabilities (client-side).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct RootsCapabilities {
    /// Whether the client supports providing root directories
    #[serde(skip_serializing_if = "Option::is_none")]
    pub list_changed: Option<bool>,
}

/// Implementation information for client or server.
///
/// This provides metadata about the MCP implementation, useful for
/// debugging, telemetry, and compatibility checking.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Implementation {
    /// Name of the implementation (e.g., "mcp-probe")
    pub name: String,

    /// Version of the implementation (e.g., "0.1.0")
    pub version: String,

    /// Additional implementation metadata
    #[serde(flatten)]
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Implementation {
    /// Create a new implementation info structure.
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            metadata: HashMap::new(),
        }
    }

    /// Add custom metadata to the implementation info.
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
}

/// Progress token for long-running operations.
///
/// Operations that may take significant time can include progress tokens
/// to allow clients to track progress and provide user feedback.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ProgressToken {
    /// String-based progress token
    String(String),
    /// Numeric progress token
    Number(i64),
}

impl From<String> for ProgressToken {
    fn from(s: String) -> Self {
        Self::String(s)
    }
}

impl From<&str> for ProgressToken {
    fn from(s: &str) -> Self {
        Self::String(s.to_string())
    }
}

impl From<i64> for ProgressToken {
    fn from(n: i64) -> Self {
        Self::Number(n)
    }
}

impl std::fmt::Display for ProgressToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::String(s) => write!(f, "{}", s),
            Self::Number(n) => write!(f, "{}", n),
        }
    }
}

/// Pagination cursor for list operations.
///
/// Used to support efficient pagination of large result sets in list operations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PaginationCursor {
    /// Opaque cursor value for pagination
    pub cursor: String,
}

impl PaginationCursor {
    /// Create a new pagination cursor.
    pub fn new(cursor: impl Into<String>) -> Self {
        Self {
            cursor: cursor.into(),
        }
    }
}

impl From<String> for PaginationCursor {
    fn from(cursor: String) -> Self {
        Self::new(cursor)
    }
}

impl From<&str> for PaginationCursor {
    fn from(cursor: &str) -> Self {
        Self::new(cursor)
    }
}

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

    #[test]
    fn test_protocol_version_serialization() {
        let version = ProtocolVersion::V2024_11_05;
        let json = serde_json::to_string(&version).unwrap();
        assert_eq!(json, "\"2024-11-05\"");

        let deserialized: ProtocolVersion = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, version);
    }

    #[test]
    fn test_protocol_version_custom() {
        let custom = ProtocolVersion::Custom("2025-01-01".to_string());
        assert_eq!(custom.as_str(), "2025-01-01");
        assert!(!custom.is_supported());
    }

    #[test]
    fn test_capabilities_serialization() {
        let capabilities = Capabilities {
            standard: StandardCapabilities {
                tools: Some(ToolCapabilities {
                    list_changed: Some(true),
                }),
                resources: Some(ResourceCapabilities {
                    subscribe: Some(true),
                    list_changed: Some(false),
                }),
                ..Default::default()
            },
            custom: {
                let mut custom = HashMap::new();
                custom.insert("experimental".to_string(), serde_json::json!(true));
                custom
            },
        };

        let json = serde_json::to_value(&capabilities).unwrap();
        let deserialized: Capabilities = serde_json::from_value(json).unwrap();
        assert_eq!(deserialized, capabilities);
    }

    #[test]
    fn test_implementation_creation() {
        let impl_info = Implementation::new("mcp-probe", "0.1.0")
            .with_metadata("platform", serde_json::json!("rust"));

        assert_eq!(impl_info.name, "mcp-probe");
        assert_eq!(impl_info.version, "0.1.0");
        assert_eq!(
            impl_info.metadata.get("platform").unwrap(),
            &serde_json::json!("rust")
        );
    }

    #[test]
    fn test_progress_token_variants() {
        let string_token = ProgressToken::from("progress-1");
        let number_token = ProgressToken::from(42i64);

        assert_eq!(string_token.to_string(), "progress-1");
        assert_eq!(number_token.to_string(), "42");

        // Test serialization
        let json_string = serde_json::to_string(&string_token).unwrap();
        let json_number = serde_json::to_string(&number_token).unwrap();

        assert_eq!(json_string, "\"progress-1\"");
        assert_eq!(json_number, "42");
    }
}