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
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Request for mcp operation.
pub struct McpRequest {
pub jsonrpc: String,
pub id: Value,
pub method: String,
pub params: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Response from mcp operation.
pub struct McpResponse {
pub jsonrpc: String,
pub id: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<McpError>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
/// Error type for mcp operations.
pub struct McpError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl McpResponse {
/// Creates a successful MCP response
///
/// # Examples
///
/// ```rust
/// use pmat::models::mcp::McpResponse;
/// use serde_json::json;
///
/// let response = McpResponse::success(
/// json!(1),
/// json!({"status": "ok"})
/// );
///
/// assert_eq!(response.jsonrpc, "2.0");
/// assert!(response.result.is_some());
/// assert!(response.error.is_none());
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn success(id: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: Some(result),
error: None,
}
}
/// Creates an error MCP response
///
/// # Examples
///
/// ```rust
/// use pmat::models::mcp::McpResponse;
/// use serde_json::json;
///
/// let response = McpResponse::error(
/// json!(1),
/// -32601,
/// "Method not found".to_string()
/// );
///
/// assert_eq!(response.jsonrpc, "2.0");
/// assert!(response.error.is_some());
/// assert_eq!(response.error.unwrap().code, -32601);
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn error(id: Value, code: i32, message: String) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: None,
error: Some(McpError {
code,
message,
data: None,
}),
}
}
}