-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-tools.js
More file actions
479 lines (439 loc) · 13.8 KB
/
mcp-tools.js
File metadata and controls
479 lines (439 loc) · 13.8 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
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
import {
getCachedData,
searchChains,
getChainById,
getAllChains,
getAllRelations,
getRelationsById,
getEndpointsById,
getAllEndpoints,
getAllKeywords,
validateChainData,
traverseRelations,
countChainsByTag,
getRpcMonitoringResults,
getRpcMonitoringStatus,
} from './dataService.js';
/**
* Get the list of MCP tool definitions (schemas)
* @returns {Array} Array of tool definition objects
*/
export function getToolDefinitions() {
return [
{
name: 'get_chains',
description: 'Get all blockchain chains, optionally filtered by tag (Testnet, L2, or Beacon)',
inputSchema: {
type: 'object',
properties: {
tag: {
type: 'string',
description: 'Optional tag to filter chains (e.g., "Testnet", "L2", "Beacon")',
enum: ['Testnet', 'L2', 'Beacon'],
},
},
},
},
{
name: 'get_chain_by_id',
description: 'Get detailed information about a specific blockchain chain by its chain ID',
inputSchema: {
type: 'object',
properties: {
chainId: {
type: 'number',
description: 'The chain ID to query (e.g., 1 for Ethereum mainnet, 137 for Polygon)',
},
},
required: ['chainId'],
},
},
{
name: 'search_chains',
description: 'Search for blockchain chains by name or other attributes',
inputSchema: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query string (e.g., "ethereum", "polygon")',
},
},
required: ['query'],
},
},
{
name: 'get_endpoints',
description: 'Get RPC, firehose, and substreams endpoints for a specific chain or all chains',
inputSchema: {
type: 'object',
properties: {
chainId: {
type: 'number',
description: 'Optional chain ID. If provided, returns endpoints for that chain only. If omitted, returns all endpoints.',
},
},
},
},
{
name: 'get_relations',
description: 'Get chain relationships (testnet/mainnet, L2/L1, etc.) for a specific chain or all chains',
inputSchema: {
type: 'object',
properties: {
chainId: {
type: 'number',
description: 'Optional chain ID. If provided, returns relations for that chain only. If omitted, returns all relations.',
},
},
},
},
{
name: 'get_slip44',
description: 'Get SLIP-0044 coin type information by coin type ID or all coin types',
inputSchema: {
type: 'object',
properties: {
coinType: {
type: 'number',
description: 'Optional coin type ID (e.g., 0 for Bitcoin, 60 for Ethereum). If omitted, returns all coin types.',
},
},
},
},
{
name: 'get_sources',
description: 'Get the status of all data sources (theGraph, chainlist, chains, slip44)',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'get_keywords',
description: 'Get extracted keywords such as blockchain names, network names, software client names, tags, and relation kinds',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'validate_chains',
description: 'Validate chain data for potential quality issues across 6 validation rules (relation conflicts, slip44 mismatches, name/testnet mismatches, sepolia/hoodie issues, status conflicts, goerli deprecation)',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'get_rpc_monitor',
description: 'Get RPC monitor status and summary endpoint health counts across all chains (without per-chain endpoint listing)',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'get_stats',
description: 'Get aggregate statistics: total chains, mainnets, testnets, L2s, beacons, and RPC health percentage',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'traverse_relations',
description: 'BFS graph traversal of chain relations from a starting chain. Returns all reachable chains (nodes) and their relationship edges up to a given depth.',
inputSchema: {
type: 'object',
properties: {
chainId: {
type: 'number',
description: 'The chain ID to start traversal from (e.g., 1 for Ethereum)',
},
depth: {
type: 'number',
description: 'Maximum traversal depth (1-5, default: 2)',
},
},
required: ['chainId'],
},
},
{
name: 'get_rpc_monitor_by_id',
description: 'Get RPC endpoint monitoring results for a specific chain by its chain ID',
inputSchema: {
type: 'object',
properties: {
chainId: {
type: 'number',
description: 'The chain ID to get RPC monitoring results for (e.g., 1 for Ethereum mainnet)',
},
},
required: ['chainId'],
},
},
];
}
// --- Response helpers ---
function textResponse(data) {
return {
content: [{ type: 'text', text: JSON.stringify(data, null, 2) }],
};
}
function errorResponse(error, message) {
const payload = message ? { error, message } : { error };
return {
content: [{ type: 'text', text: JSON.stringify(payload) }],
isError: true,
};
}
function isValidChainId(chainId) {
return typeof chainId === 'number' && !Number.isNaN(chainId);
}
// --- Individual tool handlers ---
function handleGetChains(args) {
let chains = getAllChains();
if (args.tag) {
chains = chains.filter((chain) => chain.tags?.includes(args.tag));
}
return textResponse({ count: chains.length, chains });
}
function handleGetChainById(args) {
const { chainId } = args;
if (!isValidChainId(chainId)) {
return errorResponse('Invalid chain ID');
}
const chain = getChainById(chainId);
if (!chain) {
return errorResponse('Chain not found');
}
return textResponse(chain);
}
function handleSearchChains(args) {
const { query } = args;
if (!query) {
return errorResponse('Query is required');
}
const results = searchChains(query);
return textResponse({ query, count: results.length, results });
}
function handleGetEndpoints(args) {
if (args.chainId === undefined) {
const endpoints = getAllEndpoints();
return textResponse({ count: endpoints.length, endpoints });
}
const { chainId } = args;
if (!isValidChainId(chainId)) {
return errorResponse('Invalid chain ID');
}
const result = getEndpointsById(chainId);
if (!result) {
return errorResponse('Chain not found');
}
return textResponse(result);
}
function handleGetRelations(args) {
if (args.chainId === undefined) {
return textResponse(getAllRelations());
}
const { chainId } = args;
if (!isValidChainId(chainId)) {
return errorResponse('Invalid chain ID');
}
const result = getRelationsById(chainId);
if (!result) {
return errorResponse('Chain not found');
}
return textResponse(result);
}
function handleGetSlip44(args) {
const cachedData = getCachedData();
if (!cachedData.slip44) {
return errorResponse('SLIP-0044 data not loaded');
}
if (args.coinType === undefined) {
return textResponse({
count: Object.keys(cachedData.slip44).length,
coinTypes: cachedData.slip44,
});
}
const { coinType } = args;
if (typeof coinType !== 'number' || Number.isNaN(coinType)) {
return errorResponse('Invalid coin type');
}
const coinTypeData = cachedData.slip44[coinType];
if (!coinTypeData) {
return errorResponse('Coin type not found');
}
return textResponse(coinTypeData);
}
function handleGetSources() {
const cachedData = getCachedData();
return textResponse({
lastUpdated: cachedData.lastUpdated,
sources: {
theGraph: cachedData.theGraph ? 'loaded' : 'not loaded',
chainlist: cachedData.chainlist ? 'loaded' : 'not loaded',
chains: cachedData.chains ? 'loaded' : 'not loaded',
slip44: cachedData.slip44 ? 'loaded' : 'not loaded',
},
});
}
function handleGetKeywords() {
const cachedData = getCachedData();
const keywordResults = getAllKeywords();
return textResponse({
lastUpdated: cachedData.lastUpdated,
...keywordResults,
});
}
function handleValidateChains() {
const validationResults = validateChainData();
if (validationResults.error) {
return errorResponse(validationResults.error);
}
return textResponse(validationResults);
}
function handleGetStats() {
const chains = getAllChains();
const monitorResults = getRpcMonitoringResults();
const { totalChains, totalMainnets, totalTestnets, totalL2s, totalBeacons } = countChainsByTag(chains);
const rpcTested = monitorResults.testedEndpoints;
const rpcWorking = monitorResults.workingEndpoints;
const rpcFailed = monitorResults.failedEndpoints || 0;
const rpcHealthPercent = rpcTested > 0 ? Math.round((rpcWorking / rpcTested) * 10000) / 100 : null;
return textResponse({
totalChains,
totalMainnets,
totalTestnets,
totalL2s,
totalBeacons,
rpc: {
totalEndpoints: monitorResults.totalEndpoints,
tested: rpcTested,
working: rpcWorking,
failed: rpcFailed,
healthPercent: rpcHealthPercent,
},
lastUpdated: monitorResults.lastUpdated,
});
}
function handleTraverseRelations(args) {
const { chainId, depth } = args;
if (!isValidChainId(chainId)) {
return errorResponse('Invalid chain ID');
}
const maxDepth = depth ?? 2;
if (typeof maxDepth !== 'number' || maxDepth < 1 || maxDepth > 5) {
return errorResponse('Invalid depth. Must be between 1 and 5');
}
const result = traverseRelations(chainId, maxDepth);
if (!result) {
return errorResponse('Chain not found');
}
return textResponse(result);
}
function getStatusLabel(status, results) {
if (status.isMonitoring) return 'Running';
if (results.testedEndpoints > 0) return 'Completed';
return 'Starting up...';
}
function formatRpcMonitorStatus(status, results) {
const lines = [
'## RPC Monitoring Status',
'',
`**Status:** ${getStatusLabel(status, results)}`,
`**Last Updated:** ${results.lastUpdated ?? 'N/A'}`,
'',
'### Summary',
`- Total endpoints discovered: ${results.totalEndpoints}`,
`- Endpoints tested: ${results.testedEndpoints}`,
`- Working endpoints: ${results.workingEndpoints}`,
`- Failed endpoints: ${results.failedEndpoints ?? 0}`,
'- Use `get_rpc_monitor_by_id` for per-chain endpoint details.',
];
if (!status.isMonitoring && results.testedEndpoints === 0) {
lines.push(
'',
'> Monitoring has been started but has not completed a run yet. Check back shortly.',
'> Use `get_rpc_monitor_by_id` with a chain ID once data is available.'
);
} else {
lines.push('', '> Per-chain endpoint lists are available via `get_rpc_monitor_by_id`.');
}
return lines.join('\n');
}
function handleGetRpcMonitor() {
const results = getRpcMonitoringResults();
const status = getRpcMonitoringStatus();
return { content: [{ type: 'text', text: formatRpcMonitorStatus(status, results) }] };
}
function handleGetRpcMonitorById(args) {
const { chainId } = args;
if (!isValidChainId(chainId)) {
return errorResponse('Invalid chain ID');
}
const results = getRpcMonitoringResults();
const status = getRpcMonitoringStatus();
const chainResults = results.results.filter((r) => r.chainId === chainId);
if (chainResults.length === 0) {
const notRunYet = results.testedEndpoints === 0 && !status.isMonitoring;
const message = notRunYet
? `No monitoring data available yet for chain ${chainId}. Monitoring has not completed a full run.`
: `No working RPC endpoints found for chain ${chainId}.`;
return { content: [{ type: 'text', text: message }] };
}
const workingCount = chainResults.filter((r) => r.status === 'working').length;
const lines = [
`## RPC Monitor — ${chainResults[0].chainName} (chain ${chainId})`,
'',
`**Last Updated:** ${results.lastUpdated ?? 'Never'}`,
`**Working endpoints:** ${workingCount} / ${chainResults.length}`,
'',
'### Endpoints',
];
for (const ep of chainResults) {
const block = ep.blockNumber == null ? '' : ` — block #${ep.blockNumber}`;
const latency = ep.latencyMs == null ? '' : ` [${ep.latencyMs}ms]`;
const client = ep.clientVersion && ep.clientVersion !== 'unavailable' ? ` (${ep.clientVersion})` : '';
lines.push(
`- **${ep.status}** ${ep.url}${block}${latency}${client}`,
...(ep.error ? [` - Error: ${ep.error}`] : [])
);
}
return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// --- Dispatch map ---
const toolHandlers = {
get_chains: handleGetChains,
get_chain_by_id: handleGetChainById,
search_chains: handleSearchChains,
get_endpoints: handleGetEndpoints,
get_relations: handleGetRelations,
get_slip44: handleGetSlip44,
get_sources: handleGetSources,
get_keywords: handleGetKeywords,
validate_chains: handleValidateChains,
get_stats: handleGetStats,
traverse_relations: handleTraverseRelations,
get_rpc_monitor: handleGetRpcMonitor,
get_rpc_monitor_by_id: handleGetRpcMonitorById,
};
/**
* Handle an MCP tool call by name and arguments
* @param {string} name - Tool name
* @param {Object} args - Tool arguments
* @returns {Promise<Object>} MCP response with content array
*/
export async function handleToolCall(name, args) {
try {
const handler = toolHandlers[name];
if (!handler) {
return errorResponse(`Unknown tool: ${name}`);
}
return handler(args);
} catch (error) {
return errorResponse('Internal error', error.message);
}
}