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
/*#![allow(dead_code)]
use std::collections::HashMap;
use rmcp::{handler::server::{
wrapper::Parameters,
}, schemars, tool, tool_router, ErrorData};
use rmcp::model::{CallToolResult};
use rmcp::schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::controller::StoreType;
use crate::model::pnr::{PnrRecord, PnrRecordType, PnrZone};
use crate::tool::McpTool;
#[derive(Debug, Deserialize, JsonSchema)]
struct PnrRequest {
#[schemars(description = "Name of the PNR zone")]
name: String,
#[schemars(description = "Target address of the default PNR record")]
address: String,
#[schemars(description = "Time To Live (TTL) for the default PNR record (default: 60)")]
ttl: u64,
#[schemars(description = "Store PNR zone on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct UpdatePnrRequest {
#[schemars(description = "Existing name of the PNR zone to update")]
name: String,
#[schemars(description = "Target address of the default PNR record")]
address: String,
#[schemars(description = "Time To Live (TTL) for the default PNR record (default: 60)")]
ttl: u64,
#[schemars(description = "Store PNR zone on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct UpdatePnrRecordRequest {
#[schemars(description = "Existing name of the PNR zone to update")]
name: String,
#[schemars(description = "Name of the PNR record to add/update")]
record: String,
#[schemars(description = "Target address of the PNR record")]
address: String,
#[schemars(description = "Time To Live (TTL) for the PNR record (default: 60)")]
ttl: u64,
#[schemars(description = "Type of the PNR record (A or X) (default: X)")]
record_type: Option<String>,
#[schemars(description = "Store PNR zone on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct GetPnrRequest {
#[schemars(description = "Name of the PNR zone")]
name: String,
}
#[derive(Debug, Serialize, JsonSchema)]
struct PnrResponse {
#[schemars(description = "Name of the PNR zone")]
name: String,
#[schemars(description = "Target address of the default PNR record")]
address: String,
#[schemars(description = "Time To Live (TTL) for the default PNR record (default: 60)")]
ttl: u64,
#[schemars(description = "Resolver pointer address")]
resolver_address: String,
#[schemars(description = "Personal pointer address")]
personal_address: String,
}
impl From<PnrZone> for CallToolResult {
fn from(pnr_zone: PnrZone) -> CallToolResult {
CallToolResult::structured(json!(pnr_zone))
}
}
#[tool_router(router = pnr_tool_router, vis = "pub")]
impl McpTool {
#[tool(description = "Create PNR zone with default PNR record")]
async fn create_pnr_zone(
&self,
Parameters(PnrRequest { name, address, ttl, store_type }): Parameters<PnrRequest>,
) -> Result<CallToolResult, ErrorData> {
let ttl_or_default = if ttl == 0 { 60 } else { ttl };
let pnr_zone = PnrZone::new(
name,
HashMap::from([("".to_string(), PnrRecord::new(address.clone(), PnrRecordType::X, ttl_or_default))]),
None,
None
);
Ok(self.pnr_service.create_mutable_pnr(
pnr_zone, self.evm_wallet.get_ref().clone(), StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Update PNR zone with default PNR record")]
async fn update_pnr_zone(
&self,
Parameters(UpdatePnrRequest { name, address, ttl, store_type }): Parameters<UpdatePnrRequest>,
) -> Result<CallToolResult, ErrorData> {
let ttl_or_default = if ttl == 0 { 60 } else { ttl };
let pnr_zone = PnrZone::new(
name.clone(),
HashMap::from([("".to_string(), PnrRecord::new(address.clone(), PnrRecordType::X, ttl_or_default))]),
None,
None
);
Ok(self.pnr_service.update_pnr(
name, pnr_zone, self.evm_wallet.get_ref().clone(), StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Update or add PNR record to an existing PNR zone")]
async fn update_pnr_record(
&self,
Parameters(UpdatePnrRecordRequest { name, record, address, ttl, record_type, store_type }): Parameters<UpdatePnrRecordRequest>,
) -> Result<CallToolResult, ErrorData> {
let ttl_or_default = if ttl == 0 { 60 } else { ttl };
let record_type = match record_type.as_deref() {
Some("A") => PnrRecordType::A,
_ => PnrRecordType::X,
};
let pnr_record = PnrRecord::new(address, record_type, ttl_or_default);
Ok(self.pnr_service.update_pnr_record(
name, record, pnr_record, self.evm_wallet.get_ref().clone(), StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Get PNR zone by name")]
async fn get_pnr_zone(
&self,
Parameters(GetPnrRequest { name }): Parameters<GetPnrRequest>,
) -> Result<CallToolResult, ErrorData> {
Ok(self.pnr_service.get_pnr(name).await?.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_get_pnr_request_serialization() {
let json = r#"{
"name": "test_pnr"
}"#;
let request: GetPnrRequest = serde_json::from_str(json).unwrap();
assert_eq!(request.name, "test_pnr");
}
#[tokio::test]
async fn test_pnr_request_serialization() {
let json = r#"{
"name": "test_pnr",
"address": "0x123",
"ttl": 60,
"store_type": "memory"
}"#;
let request: PnrRequest = serde_json::from_str(json).unwrap();
assert_eq!(request.name, "test_pnr");
assert_eq!(request.address, "0x123");
assert_eq!(request.ttl, 60);
assert_eq!(request.store_type, "memory");
}
#[tokio::test]
async fn test_update_pnr_record_request_serialization() {
let json = r#"{
"name": "test_pnr",
"record": "www",
"address": "0x123",
"ttl": 60,
"record_type": "A",
"store_type": "memory"
}"#;
let request: UpdatePnrRecordRequest = serde_json::from_str(json).unwrap();
assert_eq!(request.name, "test_pnr");
assert_eq!(request.record, "www");
assert_eq!(request.address, "0x123");
assert_eq!(request.ttl, 60);
assert_eq!(request.record_type, Some("A".to_string()));
assert_eq!(request.store_type, "memory");
}
#[tokio::test]
async fn test_update_pnr_request_serialization() {
let json = r#"{
"name": "test_pnr",
"address": "0x123",
"ttl": 60,
"store_type": "memory"
}"#;
let request: UpdatePnrRequest = serde_json::from_str(json).unwrap();
assert_eq!(request.name, "test_pnr");
assert_eq!(request.address, "0x123");
assert_eq!(request.ttl, 60);
assert_eq!(request.store_type, "memory");
}
}
*/