-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmod.rs
More file actions
312 lines (299 loc) · 10 KB
/
Copy pathmod.rs
File metadata and controls
312 lines (299 loc) · 10 KB
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
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.
pub mod handlers;
pub mod schema;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use ldk_server_client::client::LdkServerClient;
use serde_json::Value;
use crate::mcp::{ToolCallResult, ToolDefinition};
use crate::protocol::McpError;
type ToolHandler = for<'a> fn(
&'a LdkServerClient,
Value,
)
-> Pin<Box<dyn Future<Output = Result<Value, McpError>> + Send + 'a>>;
pub struct ToolRegistry {
definitions: Vec<ToolDefinition>,
handlers: HashMap<&'static str, ToolHandler>,
}
struct ToolSpec {
name: &'static str,
description: &'static str,
input_schema: fn() -> Value,
handler: ToolHandler,
}
fn tool_spec(
name: &'static str, description: &'static str, input_schema: fn() -> Value,
handler: ToolHandler,
) -> ToolSpec {
ToolSpec { name, description, input_schema, handler }
}
impl ToolRegistry {
pub fn list_tools(&self) -> &[ToolDefinition] {
&self.definitions
}
pub async fn call_tool(
&self, client: &LdkServerClient, name: &str, args: Value,
) -> ToolCallResult {
let Some(handler) = self.handlers.get(name) else {
return ToolCallResult::error(format!("Unknown tool: {name}"));
};
match handler(client, args).await {
Ok(value) => {
let text = serde_json::to_string(&value)
.unwrap_or_else(|e| format!("Failed to serialize response: {e}"));
ToolCallResult::success(text)
},
Err(e) => ToolCallResult::error(format!("{}: {}", e.category(), e.message)),
}
}
}
pub fn build_tool_registry() -> ToolRegistry {
let tools = vec![
tool_spec(
"get_node_info",
"Retrieve node info including node_id, sync status, and best block",
schema::get_node_info_schema,
|client, args| Box::pin(handlers::handle_get_node_info(client, args)),
),
tool_spec(
"get_balances",
"Retrieve an overview of all known balances (on-chain and Lightning)",
schema::get_balances_schema,
|client, args| Box::pin(handlers::handle_get_balances(client, args)),
),
tool_spec(
"onchain_receive",
"Generate a new on-chain Bitcoin funding address",
schema::onchain_receive_schema,
|client, args| Box::pin(handlers::handle_onchain_receive(client, args)),
),
tool_spec(
"onchain_send",
"Send an on-chain Bitcoin payment to an address",
schema::onchain_send_schema,
|client, args| Box::pin(handlers::handle_onchain_send(client, args)),
),
tool_spec(
"bolt11_receive",
"Create a BOLT11 Lightning invoice to receive a payment",
schema::bolt11_receive_schema,
|client, args| Box::pin(handlers::handle_bolt11_receive(client, args)),
),
tool_spec(
"bolt11_receive_for_hash",
"Create a BOLT11 Lightning invoice for a specific payment hash",
schema::bolt11_receive_for_hash_schema,
|client, args| Box::pin(handlers::handle_bolt11_receive_for_hash(client, args)),
),
tool_spec(
"bolt11_claim_for_hash",
"Manually claim a BOLT11 payment for a specific payment hash",
schema::bolt11_claim_for_hash_schema,
|client, args| Box::pin(handlers::handle_bolt11_claim_for_hash(client, args)),
),
tool_spec(
"bolt11_fail_for_hash",
"Manually fail a BOLT11 payment for a specific payment hash",
schema::bolt11_fail_for_hash_schema,
|client, args| Box::pin(handlers::handle_bolt11_fail_for_hash(client, args)),
),
tool_spec(
"bolt11_receive_via_jit_channel",
"Create a BOLT11 Lightning invoice to receive via an LSPS2 JIT channel",
schema::bolt11_receive_via_jit_channel_schema,
|client, args| Box::pin(handlers::handle_bolt11_receive_via_jit_channel(client, args)),
),
tool_spec(
"bolt11_receive_variable_amount_via_jit_channel",
"Create a variable-amount BOLT11 Lightning invoice to receive via an LSPS2 JIT channel",
schema::bolt11_receive_variable_amount_via_jit_channel_schema,
|client, args| {
Box::pin(handlers::handle_bolt11_receive_variable_amount_via_jit_channel(
client, args,
))
},
),
tool_spec(
"bolt11_send",
"Pay a BOLT11 Lightning invoice",
schema::bolt11_send_schema,
|client, args| Box::pin(handlers::handle_bolt11_send(client, args)),
),
tool_spec(
"bolt12_receive",
"Create a BOLT12 offer for receiving Lightning payments",
schema::bolt12_receive_schema,
|client, args| Box::pin(handlers::handle_bolt12_receive(client, args)),
),
tool_spec(
"bolt12_send",
"Pay a BOLT12 Lightning offer",
schema::bolt12_send_schema,
|client, args| Box::pin(handlers::handle_bolt12_send(client, args)),
),
tool_spec(
"spontaneous_send",
"Send a spontaneous (keysend) payment to a Lightning node",
schema::spontaneous_send_schema,
|client, args| Box::pin(handlers::handle_spontaneous_send(client, args)),
),
tool_spec(
"unified_send",
"Send a payment given a BIP 21 URI or BIP 353 Human-Readable Name",
schema::unified_send_schema,
|client, args| Box::pin(handlers::handle_unified_send(client, args)),
),
tool_spec(
"open_channel",
"Open a new Lightning channel with a remote node",
schema::open_channel_schema,
|client, args| Box::pin(handlers::handle_open_channel(client, args)),
),
tool_spec(
"splice_in",
"Increase a channel's balance by splicing in on-chain funds",
schema::splice_in_schema,
|client, args| Box::pin(handlers::handle_splice_in(client, args)),
),
tool_spec(
"splice_out",
"Decrease a channel's balance by splicing out to on-chain",
schema::splice_out_schema,
|client, args| Box::pin(handlers::handle_splice_out(client, args)),
),
tool_spec(
"close_channel",
"Cooperatively close a Lightning channel",
schema::close_channel_schema,
|client, args| Box::pin(handlers::handle_close_channel(client, args)),
),
tool_spec(
"force_close_channel",
"Force close a Lightning channel unilaterally",
schema::force_close_channel_schema,
|client, args| Box::pin(handlers::handle_force_close_channel(client, args)),
),
tool_spec(
"list_channels",
"List all known Lightning channels",
schema::list_channels_schema,
|client, args| Box::pin(handlers::handle_list_channels(client, args)),
),
tool_spec(
"update_channel_config",
"Update forwarding fees and CLTV delta for a channel",
schema::update_channel_config_schema,
|client, args| Box::pin(handlers::handle_update_channel_config(client, args)),
),
tool_spec(
"list_payments",
"List all payments (supports pagination via page_token)",
schema::list_payments_schema,
|client, args| Box::pin(handlers::handle_list_payments(client, args)),
),
tool_spec(
"get_payment_details",
"Get details of a specific payment by its ID",
schema::get_payment_details_schema,
|client, args| Box::pin(handlers::handle_get_payment_details(client, args)),
),
tool_spec(
"list_forwarded_payments",
"List all forwarded payments (supports pagination via page_token)",
schema::list_forwarded_payments_schema,
|client, args| Box::pin(handlers::handle_list_forwarded_payments(client, args)),
),
tool_spec(
"connect_peer",
"Connect to a Lightning peer without opening a channel",
schema::connect_peer_schema,
|client, args| Box::pin(handlers::handle_connect_peer(client, args)),
),
tool_spec(
"disconnect_peer",
"Disconnect from a Lightning peer",
schema::disconnect_peer_schema,
|client, args| Box::pin(handlers::handle_disconnect_peer(client, args)),
),
tool_spec(
"list_peers",
"List all known Lightning peers",
schema::list_peers_schema,
|client, args| Box::pin(handlers::handle_list_peers(client, args)),
),
tool_spec(
"decode_invoice",
"Decode a BOLT11 or BOLT12 invoice and return its parsed fields",
schema::decode_invoice_schema,
|client, args| Box::pin(handlers::handle_decode_invoice(client, args)),
),
tool_spec(
"decode_offer",
"Decode a BOLT12 offer and return its parsed fields",
schema::decode_offer_schema,
|client, args| Box::pin(handlers::handle_decode_offer(client, args)),
),
tool_spec(
"sign_message",
"Sign a message with the node's secret key",
schema::sign_message_schema,
|client, args| Box::pin(handlers::handle_sign_message(client, args)),
),
tool_spec(
"verify_signature",
"Verify a signature against a message and public key",
schema::verify_signature_schema,
|client, args| Box::pin(handlers::handle_verify_signature(client, args)),
),
tool_spec(
"export_pathfinding_scores",
"Export the pathfinding scores used by the Lightning router",
schema::export_pathfinding_scores_schema,
|client, args| Box::pin(handlers::handle_export_pathfinding_scores(client, args)),
),
tool_spec(
"graph_list_channels",
"List all known short channel IDs in the network graph",
schema::graph_list_channels_schema,
|client, args| Box::pin(handlers::handle_graph_list_channels(client, args)),
),
tool_spec(
"graph_get_channel",
"Get channel information from the network graph by short channel ID",
schema::graph_get_channel_schema,
|client, args| Box::pin(handlers::handle_graph_get_channel(client, args)),
),
tool_spec(
"graph_list_nodes",
"List all known node IDs in the network graph",
schema::graph_list_nodes_schema,
|client, args| Box::pin(handlers::handle_graph_list_nodes(client, args)),
),
tool_spec(
"graph_get_node",
"Get node information from the network graph by node ID",
schema::graph_get_node_schema,
|client, args| Box::pin(handlers::handle_graph_get_node(client, args)),
),
];
let mut definitions = Vec::with_capacity(tools.len());
let mut handlers = HashMap::with_capacity(tools.len());
for spec in tools {
definitions.push(ToolDefinition {
name: spec.name.to_string(),
description: spec.description.to_string(),
input_schema: (spec.input_schema)(),
});
handlers.insert(spec.name, spec.handler);
}
ToolRegistry { definitions, handlers }
}