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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Core JSON-RPC 2.0 message structures for MCP communication.
//!
//! This module provides the fundamental JSON-RPC message types that form the
//! foundation of all MCP communication. These types strictly follow the
//! JSON-RPC 2.0 specification with MCP-specific extensions.
//!
//! # Message Types
//!
//! - **Request**: Client-to-server messages expecting a response
//! - **Response**: Server-to-client messages in reply to requests
//! - **Notification**: One-way messages that don't expect responses
//! - **Error**: Error responses for failed requests
//!
//! # Examples
//!
//! ```rust
//! use mcp_probe_core::messages::core::{JsonRpcRequest, JsonRpcResponse, JsonRpcError};
//! use serde_json::json;
//!
//! // Create a request
//! let request = JsonRpcRequest::new(
//!     "1".to_string(),
//!     "tools/list".to_string(),
//!     json!({}),
//! );
//!
//! // Create a success response
//! let response = JsonRpcResponse::success("1".to_string(), json!({"tools": []}));
//!
//! // Create an error response
//! let error_response = JsonRpcResponse::error(
//!     "1".to_string(),
//!     JsonRpcError::method_not_found("unknown_method"),
//! );
//! ```

use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;

/// JSON-RPC 2.0 request message.
///
/// Represents a request from client to server that expects a response.
/// All MCP operations (except notifications) use this message type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    /// JSON-RPC version (always "2.0")
    pub jsonrpc: String,

    /// Unique identifier for request/response correlation
    pub id: RequestId,

    /// Method name being invoked
    pub method: String,

    /// Parameters for the method (can be object or array)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcRequest {
    /// Create a new JSON-RPC request with the given ID, method, and parameters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcRequest;
    /// use serde_json::json;
    ///
    /// let request = JsonRpcRequest::new(
    ///     "1".to_string(),
    ///     "initialize".to_string(),
    ///     json!({"protocolVersion": "2024-11-05"}),
    /// );
    /// ```
    pub fn new(id: impl Into<RequestId>, method: impl Into<String>, params: Value) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id: id.into(),
            method: method.into(),
            params: Some(params),
        }
    }

    /// Create a new JSON-RPC request without parameters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcRequest;
    ///
    /// let request = JsonRpcRequest::without_params("1", "tools/list");
    /// ```
    pub fn without_params(id: impl Into<RequestId>, method: impl Into<String>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id: id.into(),
            method: method.into(),
            params: None,
        }
    }

    /// Generate a new request with a random UUID as the ID.
    ///
    /// This is useful when you don't need to track specific request IDs.
    pub fn with_random_id(method: impl Into<String>, params: Value) -> Self {
        Self::new(Uuid::new_v4().to_string(), method, params)
    }

    /// Check if this request has parameters.
    pub fn has_params(&self) -> bool {
        self.params.is_some()
    }

    /// Get the parameters as a specific type.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcRequest;
    /// use serde_json::json;
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Deserialize, Serialize)]
    /// struct InitParams {
    ///     #[serde(rename = "protocolVersion")]
    ///     protocol_version: String,
    /// }
    ///
    /// let request = JsonRpcRequest::new(
    ///     "1",
    ///     "initialize",
    ///     json!({"protocolVersion": "2024-11-05"})
    /// );
    ///
    /// let params: InitParams = request.params_as().unwrap();
    /// assert_eq!(params.protocol_version, "2024-11-05");
    /// ```
    pub fn params_as<T>(&self) -> Result<T, serde_json::Error>
    where
        T: for<'de> Deserialize<'de>,
    {
        match &self.params {
            Some(params) => serde_json::from_value(params.clone()),
            None => serde_json::from_value(Value::Null),
        }
    }
}

/// JSON-RPC 2.0 response message.
///
/// Represents a response from server to client for a previous request.
/// Can contain either a successful result or an error.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonRpcResponse {
    /// JSON-RPC version (always "2.0")
    pub jsonrpc: String,

    /// ID from the corresponding request
    pub id: RequestId,

    /// Success result (mutually exclusive with error)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,

    /// Error result (mutually exclusive with result)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

impl JsonRpcResponse {
    /// Create a successful response with the given result.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcResponse;
    /// use serde_json::json;
    ///
    /// let response = JsonRpcResponse::success("1", json!({"status": "ok"}));
    /// ```
    pub fn success(id: impl Into<RequestId>, result: Value) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id: id.into(),
            result: Some(result),
            error: None,
        }
    }

    /// Create an error response with the given error.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::{JsonRpcResponse, JsonRpcError};
    ///
    /// let response = JsonRpcResponse::error(
    ///     "1",
    ///     JsonRpcError::method_not_found("unknown_method"),
    /// );
    /// ```
    pub fn error(id: impl Into<RequestId>, error: JsonRpcError) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id: id.into(),
            result: None,
            error: Some(error),
        }
    }

    /// Check if this response represents a success.
    pub fn is_success(&self) -> bool {
        self.result.is_some() && self.error.is_none()
    }

    /// Check if this response represents an error.
    pub fn is_error(&self) -> bool {
        self.error.is_some()
    }

    /// Get the result as a specific type.
    ///
    /// Returns an error if the response is an error response or if
    /// deserialization fails.
    pub fn result_as<T>(&self) -> Result<T, Box<dyn std::error::Error + Send + Sync>>
    where
        T: for<'de> Deserialize<'de>,
    {
        match (&self.result, &self.error) {
            (Some(result), None) => Ok(serde_json::from_value(result.clone())?),
            (None, Some(error)) => Err(format!("JSON-RPC error: {error}").into()),
            _ => Err("Invalid response: both result and error are present or missing".into()),
        }
    }
}

/// JSON-RPC 2.0 notification message.
///
/// Represents a one-way message that doesn't expect a response.
/// Used for events, logging, and other fire-and-forget communications.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonRpcNotification {
    /// JSON-RPC version (always "2.0")
    pub jsonrpc: String,

    /// Method name being invoked
    pub method: String,

    /// Parameters for the method (can be object or array)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcNotification {
    /// Create a new JSON-RPC notification with the given method and parameters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcNotification;
    /// use serde_json::json;
    ///
    /// let notification = JsonRpcNotification::new(
    ///     "notifications/cancelled",
    ///     json!({"requestId": "1"}),
    /// );
    /// ```
    pub fn new(method: impl Into<String>, params: Value) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            method: method.into(),
            params: Some(params),
        }
    }

    /// Create a new JSON-RPC notification without parameters.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcNotification;
    ///
    /// let notification = JsonRpcNotification::without_params("ping");
    /// ```
    pub fn without_params(method: impl Into<String>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            method: method.into(),
            params: None,
        }
    }

    /// Check if this notification has parameters.
    pub fn has_params(&self) -> bool {
        self.params.is_some()
    }

    /// Get the parameters as a specific type.
    pub fn params_as<T>(&self) -> Result<T, serde_json::Error>
    where
        T: for<'de> Deserialize<'de>,
    {
        match &self.params {
            Some(params) => serde_json::from_value(params.clone()),
            None => serde_json::from_value(Value::Null),
        }
    }
}

/// JSON-RPC 2.0 error object.
///
/// Represents an error that occurred during request processing.
/// Includes standard error codes and optional additional data.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JsonRpcError {
    /// Numeric error code
    pub code: i32,

    /// Human-readable error message
    pub message: String,

    /// Additional error data (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

impl JsonRpcError {
    /// Create a new JSON-RPC error.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcError;
    /// use serde_json::json;
    ///
    /// let error = JsonRpcError::new(-32000, "Custom error", Some(json!({"details": "More info"})));
    /// ```
    pub fn new(code: i32, message: impl Into<String>, data: Option<Value>) -> Self {
        Self {
            code,
            message: message.into(),
            data,
        }
    }

    /// Create a "Parse error" (-32700).
    ///
    /// Used when the JSON cannot be parsed.
    pub fn parse_error() -> Self {
        Self::new(-32700, "Parse error", None)
    }

    /// Create an "Invalid Request" error (-32600).
    ///
    /// Used when the request is not a valid JSON-RPC request.
    pub fn invalid_request(details: impl Into<String>) -> Self {
        Self::new(
            -32600,
            "Invalid Request",
            Some(Value::String(details.into())),
        )
    }

    /// Create a "Method not found" error (-32601).
    ///
    /// Used when the requested method doesn't exist.
    pub fn method_not_found(method: impl Into<String>) -> Self {
        Self::new(
            -32601,
            "Method not found",
            Some(Value::String(format!(
                "Method '{}' not found",
                method.into()
            ))),
        )
    }

    /// Create an "Invalid params" error (-32602).
    ///
    /// Used when method parameters are invalid.
    pub fn invalid_params(details: impl Into<String>) -> Self {
        Self::new(
            -32602,
            "Invalid params",
            Some(Value::String(details.into())),
        )
    }

    /// Create an "Internal error" (-32603).
    ///
    /// Used for server-side internal errors.
    pub fn internal_error(details: impl Into<String>) -> Self {
        Self::new(
            -32603,
            "Internal error",
            Some(Value::String(details.into())),
        )
    }

    /// Create a custom application error.
    ///
    /// Custom error codes should be in the range -32000 to -32099.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mcp_probe_core::messages::core::JsonRpcError;
    ///
    /// let error = JsonRpcError::application_error(
    ///     -32000,
    ///     "Tool execution failed",
    ///     "Tool 'calculator' returned non-zero exit code",
    /// );
    /// ```
    pub fn application_error(
        code: i32,
        message: impl Into<String>,
        details: impl Into<String>,
    ) -> Self {
        Self::new(code, message, Some(Value::String(details.into())))
    }

    /// Check if this is a standard JSON-RPC error (vs application-specific).
    pub fn is_standard_error(&self) -> bool {
        matches!(self.code, -32700..=-32600)
    }

    /// Check if this is an application-specific error.
    pub fn is_application_error(&self) -> bool {
        matches!(self.code, -32099..=-32000)
    }
}

impl std::fmt::Display for JsonRpcError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "JSON-RPC Error {}: {}", self.code, self.message)?;
        if let Some(data) = &self.data {
            write!(f, " ({data})")?;
        }
        Ok(())
    }
}

impl std::error::Error for JsonRpcError {}

/// Request ID for JSON-RPC messages.
///
/// Can be a string, number, or null according to JSON-RPC 2.0 specification.
/// MCP typically uses string IDs for better traceability.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RequestId {
    /// String identifier
    String(String),
    /// Numeric identifier
    Number(i64),
    /// Null identifier (discouraged in MCP)
    Null,
}

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

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

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

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

impl std::fmt::Display for RequestId {
    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}"),
            Self::Null => write!(f, "null"),
        }
    }
}

/// Enum for any JSON-RPC message type.
///
/// This is useful for generic message handling where you need to
/// differentiate between requests, responses, and notifications.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonRpcMessage {
    /// Request message
    Request(JsonRpcRequest),
    /// Response message
    Response(JsonRpcResponse),
    /// Notification message
    Notification(JsonRpcNotification),
}

impl JsonRpcMessage {
    /// Get the method name if this is a request or notification.
    pub fn method(&self) -> Option<&str> {
        match self {
            Self::Request(req) => Some(&req.method),
            Self::Notification(notif) => Some(&notif.method),
            Self::Response(_) => None,
        }
    }

    /// Get the request ID if this is a request or response.
    pub fn id(&self) -> Option<&RequestId> {
        match self {
            Self::Request(req) => Some(&req.id),
            Self::Response(resp) => Some(&resp.id),
            Self::Notification(_) => None,
        }
    }

    /// Check if this message expects a response.
    pub fn expects_response(&self) -> bool {
        matches!(self, Self::Request(_))
    }
}

impl From<JsonRpcRequest> for JsonRpcMessage {
    fn from(req: JsonRpcRequest) -> Self {
        Self::Request(req)
    }
}

impl From<JsonRpcResponse> for JsonRpcMessage {
    fn from(resp: JsonRpcResponse) -> Self {
        Self::Response(resp)
    }
}

impl From<JsonRpcNotification> for JsonRpcMessage {
    fn from(notif: JsonRpcNotification) -> Self {
        Self::Notification(notif)
    }
}

/// Type alias for request IDs to match client code expectations.
pub type JsonRpcId = RequestId;

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

    #[test]
    fn test_request_creation() {
        let request = JsonRpcRequest::new("1", "test_method", json!({"param": "value"}));

        assert_eq!(request.jsonrpc, "2.0");
        assert_eq!(request.id, RequestId::String("1".to_string()));
        assert_eq!(request.method, "test_method");
        assert!(request.has_params());
    }

    #[test]
    fn test_request_without_params() {
        let request = JsonRpcRequest::without_params("1", "test_method");

        assert!(!request.has_params());
        assert_eq!(request.params, None);
    }

    #[test]
    fn test_success_response() {
        let response = JsonRpcResponse::success("1", json!({"result": "ok"}));

        assert!(response.is_success());
        assert!(!response.is_error());
        assert_eq!(response.id, RequestId::String("1".to_string()));
    }

    #[test]
    fn test_error_response() {
        let error = JsonRpcError::method_not_found("unknown");
        let response = JsonRpcResponse::error("1", error);

        assert!(!response.is_success());
        assert!(response.is_error());
        assert_eq!(response.error.as_ref().unwrap().code, -32601);
    }

    #[test]
    fn test_notification_creation() {
        let notification = JsonRpcNotification::new("event", json!({"data": "value"}));

        assert_eq!(notification.method, "event");
        assert!(notification.has_params());
    }

    #[test]
    fn test_json_rpc_error_types() {
        let parse_error = JsonRpcError::parse_error();
        assert_eq!(parse_error.code, -32700);
        assert!(parse_error.is_standard_error());

        let app_error = JsonRpcError::application_error(-32000, "App error", "Details");
        assert_eq!(app_error.code, -32000);
        assert!(app_error.is_application_error());
    }

    #[test]
    fn test_request_id_variants() {
        let string_id = RequestId::from("test");
        let number_id = RequestId::from(42i64);
        let null_id = RequestId::Null;

        assert_eq!(string_id.to_string(), "test");
        assert_eq!(number_id.to_string(), "42");
        assert_eq!(null_id.to_string(), "null");
    }

    #[test]
    fn test_message_serialization() {
        let request = JsonRpcRequest::new("1", "test", json!({}));
        let json = serde_json::to_string(&request).unwrap();
        let deserialized: JsonRpcRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(request, deserialized);
    }

    #[test]
    fn test_generic_message_handling() {
        let request = JsonRpcMessage::Request(JsonRpcRequest::new("1", "test", json!({})));
        let notification =
            JsonRpcMessage::Notification(JsonRpcNotification::new("event", json!({})));

        assert_eq!(request.method(), Some("test"));
        assert_eq!(notification.method(), Some("event"));

        assert!(request.expects_response());
        assert!(!notification.expects_response());
    }
}