graph-aave-mcp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js ADDED
@@ -0,0 +1,911 @@
1
+ #!/usr/bin/env node
2
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
+ import { z } from "zod";
5
+ import { queryChain } from "./graphClient.js";
6
+ import { CHAINS, CHAIN_NAMES, LENDING_CHAIN_NAMES } from "./subgraphs.js";
7
+ const server = new McpServer({
8
+ name: "graph-aave-mcp",
9
+ version: "1.0.0",
10
+ });
11
+ // ---------------------------------------------------------------------------
12
+ // Helpers
13
+ // ---------------------------------------------------------------------------
14
+ function textResult(data) {
15
+ return {
16
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
17
+ };
18
+ }
19
+ function errorResult(error) {
20
+ const msg = error instanceof Error ? error.message : String(error);
21
+ return {
22
+ content: [{ type: "text", text: `Error: ${msg}` }],
23
+ isError: true,
24
+ };
25
+ }
26
+ // ---------------------------------------------------------------------------
27
+ // Tool 1 — list_aave_chains
28
+ // ---------------------------------------------------------------------------
29
+ server.registerTool("list_aave_chains", {
30
+ description: "Use this when the user asks which AAVE chains are supported, wants to pick a network, " +
31
+ "or needs to discover available AAVE deployments. " +
32
+ "Returns all supported chains with their subgraph IDs, protocol version (V2/V3), " +
33
+ "chain name, 30-day query volume, and key entities. " +
34
+ "Chains: Ethereum, Base, Arbitrum, Polygon, Optimism, Avalanche, Fantom (V3 + V2 legacy), " +
35
+ "plus AAVE Governance V3. Always call this first if chain is ambiguous.",
36
+ }, async () => {
37
+ const list = Object.entries(CHAINS)
38
+ .map(([key, cfg]) => ({
39
+ id: key,
40
+ name: cfg.name,
41
+ chain: cfg.chain,
42
+ version: cfg.version,
43
+ subgraphId: cfg.subgraphId,
44
+ queries30d: cfg.queries30d,
45
+ isGovernance: cfg.isGovernance ?? false,
46
+ description: cfg.description,
47
+ keyEntities: cfg.keyEntities,
48
+ }))
49
+ .sort((a, b) => b.queries30d - a.queries30d);
50
+ return textResult(list);
51
+ });
52
+ // ---------------------------------------------------------------------------
53
+ // Tool 2 — get_aave_reserves
54
+ // ---------------------------------------------------------------------------
55
+ server.registerTool("get_aave_reserves", {
56
+ description: "Use this when the user asks about AAVE lending markets, available assets, " +
57
+ "supply APY, borrow APY, TVL, utilization rate, collateral factors, or " +
58
+ "liquidation thresholds on a specific chain. " +
59
+ "Returns all active reserves sorted by total liquidity (TVL). " +
60
+ "RATE CONVERSION: liquidityRate and variableBorrowRate are in RAY units (1e27). " +
61
+ "Supply APY % = liquidityRate / 1e27 * 100. Borrow APY % = variableBorrowRate / 1e27 * 100. " +
62
+ "Amounts are in native token units — divide by 10^decimals for human-readable. " +
63
+ "Ideal for: 'What assets can I lend on Arbitrum?', 'What is USDC supply rate on Base?', " +
64
+ "'Show me all AAVE V3 markets on Polygon'.",
65
+ inputSchema: {
66
+ chain: z
67
+ .enum(LENDING_CHAIN_NAMES)
68
+ .describe("Chain identifier (e.g. ethereum, base, arbitrum, polygon, optimism, avalanche). " +
69
+ "Use list_aave_chains to see all options."),
70
+ includeInactive: z
71
+ .boolean()
72
+ .default(false)
73
+ .describe("Set true to include frozen, paused, or inactive reserves. Default false (active only)."),
74
+ },
75
+ }, async ({ chain, includeInactive }) => {
76
+ try {
77
+ const cfg = CHAINS[chain];
78
+ const where = includeInactive ? "" : ", where: { isActive: true }";
79
+ const query = `{
80
+ reserves(first: 100, orderBy: totalLiquidity, orderDirection: desc${where}) {
81
+ id
82
+ symbol
83
+ name
84
+ decimals
85
+ underlyingAsset
86
+ isActive
87
+ isFrozen
88
+ isPaused
89
+ borrowingEnabled
90
+ usageAsCollateralEnabled
91
+ availableLiquidity
92
+ totalLiquidity
93
+ totalATokenSupply
94
+ totalCurrentVariableDebt
95
+ totalCurrentStableDebt
96
+ utilizationRate
97
+ liquidityRate
98
+ variableBorrowRate
99
+ stableBorrowRate
100
+ baseLTVasCollateral
101
+ reserveLiquidationThreshold
102
+ reserveLiquidationBonus
103
+ reserveFactor
104
+ price { priceInEth }
105
+ aToken { id }
106
+ vToken { id }
107
+ }
108
+ }`;
109
+ const data = await queryChain(cfg.subgraphId, query);
110
+ return textResult(data);
111
+ }
112
+ catch (error) {
113
+ return errorResult(error);
114
+ }
115
+ });
116
+ // ---------------------------------------------------------------------------
117
+ // Tool 3 — get_aave_reserve
118
+ // ---------------------------------------------------------------------------
119
+ server.registerTool("get_aave_reserve", {
120
+ description: "Use this when the user asks about a specific AAVE asset in detail — " +
121
+ "e.g. 'Tell me everything about USDC on Ethereum AAVE', 'What are the WETH borrow parameters?', " +
122
+ "'What is the liquidation threshold for WBTC collateral?'. " +
123
+ "Returns full reserve config: current rates, TVL, LTV, liquidation parameters, " +
124
+ "lifetime stats (total borrows/repayments/liquidations), and token addresses. " +
125
+ "RATE CONVERSION: divide liquidityRate / variableBorrowRate by 1e27 * 100 for APY %.",
126
+ inputSchema: {
127
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
128
+ symbol: z
129
+ .string()
130
+ .describe("Token symbol — case-insensitive (e.g. USDC, WETH, WBTC, DAI, USDT, LINK, AAVE)"),
131
+ },
132
+ }, async ({ chain, symbol }) => {
133
+ try {
134
+ const cfg = CHAINS[chain];
135
+ const query = `{
136
+ reserves(where: { symbol: "${symbol.toUpperCase()}" }) {
137
+ id
138
+ symbol
139
+ name
140
+ decimals
141
+ underlyingAsset
142
+ isActive
143
+ isFrozen
144
+ isPaused
145
+ borrowingEnabled
146
+ usageAsCollateralEnabled
147
+ availableLiquidity
148
+ totalLiquidity
149
+ totalATokenSupply
150
+ totalCurrentVariableDebt
151
+ totalCurrentStableDebt
152
+ utilizationRate
153
+ liquidityRate
154
+ variableBorrowRate
155
+ stableBorrowRate
156
+ averageStableRate
157
+ baseLTVasCollateral
158
+ reserveLiquidationThreshold
159
+ reserveLiquidationBonus
160
+ reserveFactor
161
+ price { priceInEth }
162
+ aToken { id }
163
+ vToken { id }
164
+ sToken { id }
165
+ lifetimeDepositorsInterestEarned
166
+ lifetimeBorrows
167
+ lifetimeRepayments
168
+ lifetimeWithdrawals
169
+ lifetimeLiquidated
170
+ lifetimeFlashLoans
171
+ lifetimeFlashLoanPremium
172
+ }
173
+ }`;
174
+ const data = await queryChain(cfg.subgraphId, query);
175
+ return textResult(data);
176
+ }
177
+ catch (error) {
178
+ return errorResult(error);
179
+ }
180
+ });
181
+ // ---------------------------------------------------------------------------
182
+ // Tool 4 — get_aave_user_position
183
+ // ---------------------------------------------------------------------------
184
+ server.registerTool("get_aave_user_position", {
185
+ description: "Use this when the user asks about a wallet's AAVE position — " +
186
+ "'What is my health factor?', 'What have I supplied to AAVE?', 'How much have I borrowed?', " +
187
+ "'Am I at risk of liquidation?', 'Show me my collateral and debt on Arbitrum'. " +
188
+ "Returns all supplied assets (with aToken balances), all borrowed assets " +
189
+ "(variable + stable debt), collateral flags, and e-mode category. " +
190
+ "Health Factor ≈ sum(collateral_i * price_i * liqThreshold_i) / sum(debt_i * price_i). " +
191
+ "HF < 1.0 = liquidatable. Amounts in native token units — divide by 10^decimals.",
192
+ inputSchema: {
193
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
194
+ userAddress: z
195
+ .string()
196
+ .describe("Ethereum wallet address of the user (0x..., lowercase). " +
197
+ "Returns empty arrays if address has no AAVE positions."),
198
+ },
199
+ }, async ({ chain, userAddress }) => {
200
+ try {
201
+ const cfg = CHAINS[chain];
202
+ const addr = userAddress.toLowerCase();
203
+ const query = `{
204
+ userReserves(where: { user: "${addr}" }, first: 100) {
205
+ reserve {
206
+ symbol
207
+ name
208
+ decimals
209
+ underlyingAsset
210
+ liquidityRate
211
+ variableBorrowRate
212
+ stableBorrowRate
213
+ baseLTVasCollateral
214
+ reserveLiquidationThreshold
215
+ reserveLiquidationBonus
216
+ price { priceInEth }
217
+ }
218
+ scaledATokenBalance
219
+ currentATokenBalance
220
+ usageAsCollateralEnabledOnUser
221
+ scaledVariableDebt
222
+ currentVariableDebt
223
+ principalStableDebt
224
+ currentStableDebt
225
+ currentTotalDebt
226
+ liquidityRate
227
+ stableBorrowLastUpdateTimestamp
228
+ }
229
+ user(id: "${addr}") {
230
+ id
231
+ borrowedReservesCount
232
+ unclaimedRewards
233
+ eModeCategoryId { id label ltv liquidationThreshold }
234
+ }
235
+ }`;
236
+ const data = await queryChain(cfg.subgraphId, query);
237
+ return textResult(data);
238
+ }
239
+ catch (error) {
240
+ return errorResult(error);
241
+ }
242
+ });
243
+ // ---------------------------------------------------------------------------
244
+ // Tool 5 — simulate_health_factor
245
+ // ---------------------------------------------------------------------------
246
+ server.registerTool("simulate_health_factor", {
247
+ description: "Use this when the user wants to simulate how a price change affects their AAVE health factor — " +
248
+ "'What happens to my health factor if ETH drops 20%?', " +
249
+ "'How much can WBTC fall before I get liquidated?', " +
250
+ "'Simulate a 30% drop in my collateral asset'. " +
251
+ "Fetches the user's full position, computes current health factor, " +
252
+ "then recalculates it after applying the specified price change to the target asset. " +
253
+ "Health Factor < 1.0 means the position is liquidatable.",
254
+ inputSchema: {
255
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
256
+ userAddress: z.string().describe("Wallet address of the user (0x...)"),
257
+ assetSymbol: z
258
+ .string()
259
+ .describe("Symbol of the asset whose price changes (e.g. WETH, WBTC, USDC)"),
260
+ priceChangePct: z
261
+ .number()
262
+ .describe("Price change percentage — negative for drops, positive for gains. " +
263
+ "E.g. -20 means the asset price falls 20%."),
264
+ },
265
+ }, async ({ chain, userAddress, assetSymbol, priceChangePct }) => {
266
+ try {
267
+ const cfg = CHAINS[chain];
268
+ const addr = userAddress.toLowerCase();
269
+ const query = `{
270
+ userReserves(where: { user: "${addr}" }, first: 100) {
271
+ reserve {
272
+ symbol
273
+ decimals
274
+ baseLTVasCollateral
275
+ reserveLiquidationThreshold
276
+ price { priceInEth }
277
+ }
278
+ currentATokenBalance
279
+ usageAsCollateralEnabledOnUser
280
+ currentVariableDebt
281
+ currentStableDebt
282
+ }
283
+ }`;
284
+ const data = (await queryChain(cfg.subgraphId, query));
285
+ if (!data.userReserves || data.userReserves.length === 0) {
286
+ return textResult({ error: "No AAVE positions found for this address on this chain." });
287
+ }
288
+ const target = assetSymbol.toUpperCase();
289
+ const multiplier = 1 + priceChangePct / 100;
290
+ let collateralETH = 0;
291
+ let collateralETHAfter = 0;
292
+ let debtETH = 0;
293
+ let debtETHAfter = 0;
294
+ for (const ur of data.userReserves) {
295
+ const { reserve } = ur;
296
+ const decimals = reserve.decimals;
297
+ const priceEth = Number(reserve.price.priceInEth) / 1e18;
298
+ const liqThreshold = Number(reserve.reserveLiquidationThreshold) / 10000;
299
+ const isTarget = reserve.symbol.toUpperCase() === target;
300
+ // Supplied (collateral)
301
+ const aBalance = Number(ur.currentATokenBalance) / Math.pow(10, decimals);
302
+ if (ur.usageAsCollateralEnabledOnUser && aBalance > 0) {
303
+ const col = aBalance * priceEth * liqThreshold;
304
+ collateralETH += col;
305
+ collateralETHAfter += isTarget ? col * multiplier : col;
306
+ }
307
+ // Borrowed (debt)
308
+ const debt = (Number(ur.currentVariableDebt) + Number(ur.currentStableDebt)) /
309
+ Math.pow(10, decimals);
310
+ if (debt > 0) {
311
+ const d = debt * priceEth;
312
+ debtETH += d;
313
+ debtETHAfter += isTarget ? d * multiplier : d;
314
+ }
315
+ }
316
+ const currentHF = debtETH > 0 ? collateralETH / debtETH : Infinity;
317
+ const simulatedHF = debtETHAfter > 0 ? collateralETHAfter / debtETHAfter : Infinity;
318
+ const liquidationPoint = collateralETH > 0 && debtETH > 0
319
+ ? ((collateralETH / debtETH - 1) * 100).toFixed(2)
320
+ : null;
321
+ return textResult({
322
+ chain,
323
+ userAddress: addr,
324
+ assetSimulated: target,
325
+ priceChangePct,
326
+ currentHealthFactor: currentHF === Infinity ? "∞ (no debt)" : currentHF.toFixed(4),
327
+ simulatedHealthFactor: simulatedHF === Infinity ? "∞ (no debt)" : simulatedHF.toFixed(4),
328
+ liquidationRisk: simulatedHF < 1.0
329
+ ? "LIQUIDATABLE after this price change"
330
+ : simulatedHF < 1.2
331
+ ? "HIGH RISK — close to liquidation threshold"
332
+ : "Safe",
333
+ note: liquidationPoint !== null
334
+ ? `Current collateral buffer: ${liquidationPoint}% above liquidation threshold`
335
+ : null,
336
+ rateConversionNote: "Health Factor < 1.0 means position is liquidatable. " +
337
+ "Computed using on-chain liquidation thresholds from the subgraph.",
338
+ });
339
+ }
340
+ catch (error) {
341
+ return errorResult(error);
342
+ }
343
+ });
344
+ // ---------------------------------------------------------------------------
345
+ // Tool 6 — get_recent_borrows
346
+ // ---------------------------------------------------------------------------
347
+ server.registerTool("get_recent_borrows", {
348
+ description: "Use this when the user asks about recent borrowing activity on AAVE — " +
349
+ "'Who has been borrowing USDC on Ethereum?', 'Show me recent WETH borrows on Arbitrum', " +
350
+ "'What has address 0x... borrowed recently?', 'Show borrow volume by asset'. " +
351
+ "Returns borrow events with: borrower address, asset, raw amount, borrow rate, " +
352
+ "rate mode (variable=2/stable=1), and timestamp. " +
353
+ "Divide amount by 10^decimals for human-readable value.",
354
+ inputSchema: {
355
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
356
+ first: z
357
+ .number()
358
+ .min(1)
359
+ .max(100)
360
+ .default(20)
361
+ .describe("Number of borrow events to return (1–100, default 20)"),
362
+ userAddress: z
363
+ .string()
364
+ .optional()
365
+ .describe("Optional: filter by borrower Ethereum address (0x...)"),
366
+ reserveSymbol: z
367
+ .string()
368
+ .optional()
369
+ .describe("Optional: filter by asset symbol (e.g. USDC, WETH, DAI)"),
370
+ },
371
+ }, async ({ chain, first, userAddress, reserveSymbol }) => {
372
+ try {
373
+ const cfg = CHAINS[chain];
374
+ const filters = [];
375
+ if (userAddress)
376
+ filters.push(`user: "${userAddress.toLowerCase()}"`);
377
+ if (reserveSymbol)
378
+ filters.push(`reserve_: { symbol: "${reserveSymbol.toUpperCase()}" }`);
379
+ const where = filters.length > 0 ? `, where: { ${filters.join(", ")} }` : "";
380
+ const query = `{
381
+ borrows(first: ${first}, orderBy: timestamp, orderDirection: desc${where}) {
382
+ id
383
+ txHash
384
+ timestamp
385
+ user { id }
386
+ reserve { symbol name decimals }
387
+ amount
388
+ borrowRate
389
+ borrowRateMode
390
+ referral
391
+ }
392
+ }`;
393
+ const data = await queryChain(cfg.subgraphId, query);
394
+ return textResult(data);
395
+ }
396
+ catch (error) {
397
+ return errorResult(error);
398
+ }
399
+ });
400
+ // ---------------------------------------------------------------------------
401
+ // Tool 7 — get_recent_supplies
402
+ // ---------------------------------------------------------------------------
403
+ server.registerTool("get_recent_supplies", {
404
+ description: "Use this when the user asks about recent deposit/supply activity on AAVE — " +
405
+ "'Who has been supplying ETH on Base?', 'Show me recent USDC deposits on Polygon', " +
406
+ "'What has address 0x... deposited recently?'. " +
407
+ "V3 chains use the 'supply' entity; V2 chains use 'deposit' — handled automatically. " +
408
+ "Returns: supplier address, asset symbol, raw amount, and timestamp. " +
409
+ "Divide amount by 10^decimals for human-readable value.",
410
+ inputSchema: {
411
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
412
+ first: z
413
+ .number()
414
+ .min(1)
415
+ .max(100)
416
+ .default(20)
417
+ .describe("Number of supply events to return (1–100, default 20)"),
418
+ userAddress: z
419
+ .string()
420
+ .optional()
421
+ .describe("Optional: filter by supplier address (0x...)"),
422
+ reserveSymbol: z
423
+ .string()
424
+ .optional()
425
+ .describe("Optional: filter by asset symbol (e.g. USDC, WETH, WBTC)"),
426
+ },
427
+ }, async ({ chain, first, userAddress, reserveSymbol }) => {
428
+ try {
429
+ const cfg = CHAINS[chain];
430
+ const filters = [];
431
+ if (userAddress)
432
+ filters.push(`user: "${userAddress.toLowerCase()}"`);
433
+ if (reserveSymbol)
434
+ filters.push(`reserve_: { symbol: "${reserveSymbol.toUpperCase()}" }`);
435
+ const where = filters.length > 0 ? `, where: { ${filters.join(", ")} }` : "";
436
+ // V2 uses "deposit" entity, V3 uses "supply"
437
+ const entity = cfg.version === "v2" ? "deposits" : "supplies";
438
+ const query = `{
439
+ ${entity}(first: ${first}, orderBy: timestamp, orderDirection: desc${where}) {
440
+ id
441
+ txHash
442
+ timestamp
443
+ user { id }
444
+ reserve { symbol name decimals }
445
+ amount
446
+ referral
447
+ }
448
+ }`;
449
+ const data = await queryChain(cfg.subgraphId, query);
450
+ return textResult(data);
451
+ }
452
+ catch (error) {
453
+ return errorResult(error);
454
+ }
455
+ });
456
+ // ---------------------------------------------------------------------------
457
+ // Tool 8 — get_aave_liquidations
458
+ // ---------------------------------------------------------------------------
459
+ server.registerTool("get_aave_liquidations", {
460
+ description: "Use this when the user asks about AAVE liquidation events — " +
461
+ "'Show me recent liquidations on Ethereum', 'Has address 0x... been liquidated?', " +
462
+ "'Who are the top liquidators on Arbitrum?', 'What collateral is being seized most?'. " +
463
+ "Returns: liquidator address, liquidated user, collateral asset seized, " +
464
+ "debt asset repaid, amounts, and timestamp. " +
465
+ "Liquidations occur when a user's health factor drops below 1.0.",
466
+ inputSchema: {
467
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
468
+ first: z
469
+ .number()
470
+ .min(1)
471
+ .max(100)
472
+ .default(20)
473
+ .describe("Number of liquidation events to return (1–100, default 20)"),
474
+ userAddress: z
475
+ .string()
476
+ .optional()
477
+ .describe("Optional: filter by the address that was liquidated"),
478
+ liquidator: z
479
+ .string()
480
+ .optional()
481
+ .describe("Optional: filter by liquidator address"),
482
+ },
483
+ }, async ({ chain, first, userAddress, liquidator }) => {
484
+ try {
485
+ const cfg = CHAINS[chain];
486
+ const filters = [];
487
+ if (userAddress)
488
+ filters.push(`user: "${userAddress.toLowerCase()}"`);
489
+ if (liquidator)
490
+ filters.push(`liquidator: "${liquidator.toLowerCase()}"`);
491
+ const where = filters.length > 0 ? `, where: { ${filters.join(", ")} }` : "";
492
+ const query = `{
493
+ liquidationCalls(first: ${first}, orderBy: timestamp, orderDirection: desc${where}) {
494
+ id
495
+ txHash
496
+ timestamp
497
+ user { id }
498
+ liquidator
499
+ collateralReserve { symbol name decimals }
500
+ principalReserve { symbol name decimals }
501
+ collateralAmount
502
+ principalAmount
503
+ liquidatedCollateralAmount
504
+ }
505
+ }`;
506
+ const data = await queryChain(cfg.subgraphId, query);
507
+ return textResult(data);
508
+ }
509
+ catch (error) {
510
+ return errorResult(error);
511
+ }
512
+ });
513
+ // ---------------------------------------------------------------------------
514
+ // Tool 9 — get_aave_flash_loans
515
+ // ---------------------------------------------------------------------------
516
+ server.registerTool("get_aave_flash_loans", {
517
+ description: "Use this when the user asks about AAVE flash loans — " +
518
+ "'Show me recent flash loans on Ethereum', 'What assets are flash-loaned most?', " +
519
+ "'How much in flash loan fees has AAVE earned?'. " +
520
+ "Returns: initiator address, asset borrowed, amount, fee paid (totalFee), and timestamp. " +
521
+ "Flash loans must be borrowed and repaid within a single transaction.",
522
+ inputSchema: {
523
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
524
+ first: z
525
+ .number()
526
+ .min(1)
527
+ .max(100)
528
+ .default(20)
529
+ .describe("Number of flash loan events to return (1–100, default 20)"),
530
+ },
531
+ }, async ({ chain, first }) => {
532
+ try {
533
+ const cfg = CHAINS[chain];
534
+ const query = `{
535
+ flashLoans(first: ${first}, orderBy: timestamp, orderDirection: desc) {
536
+ id
537
+ txHash
538
+ timestamp
539
+ initiator { id }
540
+ reserve { symbol name decimals }
541
+ amount
542
+ totalFee
543
+ lpFee
544
+ protocolFee
545
+ }
546
+ }`;
547
+ const data = await queryChain(cfg.subgraphId, query);
548
+ return textResult(data);
549
+ }
550
+ catch (error) {
551
+ return errorResult(error);
552
+ }
553
+ });
554
+ // ---------------------------------------------------------------------------
555
+ // Tool 10 — get_reserve_rate_history
556
+ // ---------------------------------------------------------------------------
557
+ server.registerTool("get_reserve_rate_history", {
558
+ description: "Use this when the user asks about historical AAVE rates or TVL trends — " +
559
+ "'How has USDC supply rate changed over time?', 'Show me ETH borrow rate history on Polygon', " +
560
+ "'What was the utilization rate last week?'. " +
561
+ "Returns timestamped snapshots of: liquidityRate, variableBorrowRate, stableBorrowRate, " +
562
+ "utilizationRate, availableLiquidity, totalLiquidity, totalCurrentVariableDebt. " +
563
+ "Rates are in RAY units (divide by 1e27 * 100 for APY %). " +
564
+ "Get the reserve ID from get_aave_reserves (the 'id' field = underlyingAsset + poolAddress).",
565
+ inputSchema: {
566
+ chain: z.enum(LENDING_CHAIN_NAMES).describe("Chain identifier"),
567
+ reserveId: z
568
+ .string()
569
+ .describe("Reserve ID — from get_aave_reserves 'id' field " +
570
+ "(concatenation of underlyingAsset address + pool address, lowercase)"),
571
+ first: z
572
+ .number()
573
+ .min(1)
574
+ .max(100)
575
+ .default(30)
576
+ .describe("Number of historical snapshots to return (default 30)"),
577
+ },
578
+ }, async ({ chain, reserveId, first }) => {
579
+ try {
580
+ const cfg = CHAINS[chain];
581
+ const query = `{
582
+ reserveParamsHistoryItems(
583
+ first: ${first},
584
+ orderBy: timestamp,
585
+ orderDirection: desc,
586
+ where: { reserve: "${reserveId.toLowerCase()}" }
587
+ ) {
588
+ timestamp
589
+ liquidityRate
590
+ variableBorrowRate
591
+ stableBorrowRate
592
+ utilizationRate
593
+ availableLiquidity
594
+ totalLiquidity
595
+ totalCurrentVariableDebt
596
+ }
597
+ }`;
598
+ const data = await queryChain(cfg.subgraphId, query);
599
+ return textResult(data);
600
+ }
601
+ catch (error) {
602
+ return errorResult(error);
603
+ }
604
+ });
605
+ // ---------------------------------------------------------------------------
606
+ // Tool 11 — get_governance_proposals
607
+ // ---------------------------------------------------------------------------
608
+ server.registerTool("get_governance_proposals", {
609
+ description: "Use this when the user asks about AAVE governance — " +
610
+ "'Show me recent AAVE governance proposals', 'What proposals are currently active?', " +
611
+ "'What is the status of AAVE proposal #X?', 'Show me governance voting activity'. " +
612
+ "Queries the AAVE Governance V3 subgraph on Ethereum. " +
613
+ "Returns: proposal ID, creator, access level (1=short executor/2=long executor), " +
614
+ "current state, voting duration (seconds), for/against votes, title, and payload info. " +
615
+ "Proposal states: Created=0, Active=1, Queued=2, Executed=3, Failed=4, Cancelled=5, Expired=6.",
616
+ inputSchema: {
617
+ first: z
618
+ .number()
619
+ .min(1)
620
+ .max(50)
621
+ .default(10)
622
+ .describe("Number of proposals to return (1–50, default 10)"),
623
+ state: z
624
+ .number()
625
+ .optional()
626
+ .describe("Optional: filter by state number. " +
627
+ "0=Created, 1=Active, 2=Queued, 3=Executed, 4=Failed, 5=Cancelled, 6=Expired"),
628
+ },
629
+ }, async ({ first, state }) => {
630
+ try {
631
+ const cfg = CHAINS["governance"];
632
+ const where = state !== undefined ? `, where: { state: ${state} }` : "";
633
+ const query = `{
634
+ proposals_collection: proposalMetadata_collection(first: ${first}, orderBy: proposalId, orderDirection: desc) {
635
+ proposalId
636
+ title
637
+ }
638
+ proposals(first: ${first}, orderBy: proposalId, orderDirection: desc${where}) {
639
+ proposalId
640
+ creator
641
+ accessLevel
642
+ ipfsHash
643
+ state
644
+ votingDuration
645
+ snapshotBlockHash
646
+ votes {
647
+ forVotes
648
+ againstVotes
649
+ }
650
+ payloads {
651
+ id
652
+ accessLevel
653
+ state
654
+ }
655
+ }
656
+ }`;
657
+ const data = await queryChain(cfg.subgraphId, query);
658
+ return textResult(data);
659
+ }
660
+ catch (error) {
661
+ return errorResult(error);
662
+ }
663
+ });
664
+ // ---------------------------------------------------------------------------
665
+ // Tool 12 — get_proposal_votes
666
+ // ---------------------------------------------------------------------------
667
+ server.registerTool("get_proposal_votes", {
668
+ description: "Use this when the user asks about votes on a specific AAVE governance proposal — " +
669
+ "'Who voted on proposal #X?', 'Show me the voting breakdown for this proposal', " +
670
+ "'Which addresses voted against?'. " +
671
+ "Returns individual votes with: voter address, voting power, support (true=for/false=against), " +
672
+ "and timestamp.",
673
+ inputSchema: {
674
+ proposalId: z
675
+ .string()
676
+ .describe("AAVE governance proposal ID (numeric string, e.g. '185')"),
677
+ first: z
678
+ .number()
679
+ .min(1)
680
+ .max(100)
681
+ .default(50)
682
+ .describe("Number of votes to return (default 50)"),
683
+ },
684
+ }, async ({ proposalId, first }) => {
685
+ try {
686
+ const cfg = CHAINS["governance"];
687
+ const query = `{
688
+ proposalVotes_collection(
689
+ first: ${first},
690
+ orderBy: votingPower,
691
+ orderDirection: desc,
692
+ where: { proposalId: "${proposalId}" }
693
+ ) {
694
+ voter
695
+ votingPower
696
+ support
697
+ timestamp
698
+ }
699
+ }`;
700
+ const data = await queryChain(cfg.subgraphId, query);
701
+ return textResult(data);
702
+ }
703
+ catch (error) {
704
+ return errorResult(error);
705
+ }
706
+ });
707
+ // ---------------------------------------------------------------------------
708
+ // Tool 13 — query_aave_subgraph
709
+ // ---------------------------------------------------------------------------
710
+ server.registerTool("query_aave_subgraph", {
711
+ description: "Use this when a pre-built tool doesn't cover the user's need — " +
712
+ "execute a raw GraphQL query against any AAVE chain's subgraph. " +
713
+ "Use get_aave_schema first to explore available entities and fields. " +
714
+ "Lending schema entities: reserves, userReserves, borrows, supplies (V3) / deposits (V2), " +
715
+ "repays, liquidationCalls, flashLoans, pool, protocol, reserveParamsHistoryItems. " +
716
+ "Governance schema entities: proposals, proposalVotes_collection, payloads, " +
717
+ "votingPortals, votingConfigs, proposalMetadata_collection.",
718
+ inputSchema: {
719
+ chain: z.enum(CHAIN_NAMES).describe("Chain identifier (includes 'governance')"),
720
+ query: z.string().describe("GraphQL query string"),
721
+ variables: z
722
+ .record(z.unknown())
723
+ .optional()
724
+ .describe("Optional GraphQL variables"),
725
+ },
726
+ }, async ({ chain, query, variables }) => {
727
+ try {
728
+ const cfg = CHAINS[chain];
729
+ const data = await queryChain(cfg.subgraphId, query, variables);
730
+ return textResult(data);
731
+ }
732
+ catch (error) {
733
+ return errorResult(error);
734
+ }
735
+ });
736
+ // ---------------------------------------------------------------------------
737
+ // Tool 14 — get_aave_schema
738
+ // ---------------------------------------------------------------------------
739
+ server.registerTool("get_aave_schema", {
740
+ description: "Use this to introspect the full GraphQL schema for any AAVE chain's subgraph. " +
741
+ "Returns all queryable root fields and their types. " +
742
+ "Useful before writing a custom query_aave_subgraph call, or to understand " +
743
+ "what data is available on a specific chain/version.",
744
+ inputSchema: {
745
+ chain: z.enum(CHAIN_NAMES).describe("Chain identifier (includes 'governance')"),
746
+ },
747
+ }, async ({ chain }) => {
748
+ try {
749
+ const cfg = CHAINS[chain];
750
+ const query = `{
751
+ __schema {
752
+ queryType {
753
+ fields {
754
+ name
755
+ description
756
+ type { name kind ofType { name kind } }
757
+ }
758
+ }
759
+ }
760
+ }`;
761
+ const data = await queryChain(cfg.subgraphId, query);
762
+ return textResult(data);
763
+ }
764
+ catch (error) {
765
+ return errorResult(error);
766
+ }
767
+ });
768
+ // ---------------------------------------------------------------------------
769
+ // Prompts — multi-step guided workflows for any AI agent
770
+ // ---------------------------------------------------------------------------
771
+ server.registerPrompt("analyze_aave_user", {
772
+ description: "Full analysis of a wallet's AAVE position: supplied assets, borrowed assets, " +
773
+ "estimated health factor, liquidation risk, and recent activity",
774
+ argsSchema: {
775
+ address: z.string().describe("Ethereum wallet address (0x...)"),
776
+ chain: z
777
+ .string()
778
+ .default("ethereum")
779
+ .describe("Chain to analyze — use list_aave_chains to see options"),
780
+ },
781
+ }, ({ address, chain }) => ({
782
+ messages: [
783
+ {
784
+ role: "user",
785
+ content: {
786
+ type: "text",
787
+ text: `Analyze the AAVE position of wallet ${address} on ${chain}. Steps:
788
+ 1. Call get_aave_user_position(chain="${chain}", userAddress="${address}") to get all deposits and borrows
789
+ 2. Identify supplied assets: those with currentATokenBalance > 0 (divide by 10^decimals)
790
+ 3. Identify borrowed assets: those with currentTotalDebt > 0 (divide by 10^decimals)
791
+ 4. Convert liquidityRate / variableBorrowRate from RAY (divide by 1e27 * 100) to show APY %
792
+ 5. Estimate health factor: sum(collateral_i * priceEth_i * liqThreshold_i / 10000) / sum(debt_i * priceEth_i)
793
+ (Use priceInEth from each reserve.price — already 18-decimal normalized)
794
+ 6. Call get_recent_borrows(chain="${chain}", userAddress="${address}", first=5) for recent history
795
+ 7. Report: total supplied, total borrowed, health factor, collateral assets, liquidation risk level`,
796
+ },
797
+ },
798
+ ],
799
+ }));
800
+ server.registerPrompt("aave_chain_overview", {
801
+ description: "Comprehensive overview of an AAVE deployment: top markets by TVL, " +
802
+ "current supply/borrow rates, protocol activity, and recent liquidations",
803
+ argsSchema: {
804
+ chain: z
805
+ .string()
806
+ .default("ethereum")
807
+ .describe("Chain to analyze (ethereum, base, arbitrum, polygon, etc.)"),
808
+ },
809
+ }, ({ chain }) => ({
810
+ messages: [
811
+ {
812
+ role: "user",
813
+ content: {
814
+ type: "text",
815
+ text: `Give a comprehensive overview of AAVE on ${chain}. Steps:
816
+ 1. Call list_aave_chains to confirm chain metadata and 30d query volume
817
+ 2. Call get_aave_reserves(chain="${chain}") to get all active markets
818
+ 3. Convert rates: Supply APY = liquidityRate / 1e27 * 100, Borrow APY = variableBorrowRate / 1e27 * 100
819
+ 4. Identify: top 5 reserves by totalLiquidity, highest supply APY assets, highest borrow APY assets
820
+ 5. Call get_recent_borrows(chain="${chain}", first=10) for latest borrowing activity
821
+ 6. Call get_aave_liquidations(chain="${chain}", first=5) to check recent liquidations
822
+ 7. Summarize: protocol TVL, top markets, rate opportunities, and recent activity highlights`,
823
+ },
824
+ },
825
+ ],
826
+ }));
827
+ server.registerPrompt("compare_aave_rates", {
828
+ description: "Compare supply APY and borrow APY for a specific asset across all supported AAVE chains",
829
+ argsSchema: {
830
+ symbol: z
831
+ .string()
832
+ .describe("Token symbol to compare — e.g. USDC, WETH, WBTC, DAI, USDT"),
833
+ },
834
+ }, ({ symbol }) => ({
835
+ messages: [
836
+ {
837
+ role: "user",
838
+ content: {
839
+ type: "text",
840
+ text: `Compare AAVE ${symbol.toUpperCase()} rates across all chains. Steps:
841
+ 1. Call list_aave_chains to see all V3 chains (focus on ethereum, base, arbitrum, polygon, optimism, avalanche first)
842
+ 2. For each chain, call get_aave_reserve(chain=X, symbol="${symbol.toUpperCase()}")
843
+ 3. Convert: Supply APY = liquidityRate / 1e27 * 100, Borrow APY = variableBorrowRate / 1e27 * 100
844
+ 4. Also collect: totalLiquidity, availableLiquidity, utilizationRate for each chain
845
+ 5. Build a comparison table: Chain | Supply APY | Variable Borrow APY | TVL | Utilization %
846
+ 6. Highlight: best chain to supply ${symbol}, cheapest chain to borrow ${symbol}, and why rates differ`,
847
+ },
848
+ },
849
+ ],
850
+ }));
851
+ server.registerPrompt("aave_liquidation_analysis", {
852
+ description: "Monitor and analyze recent liquidations on an AAVE chain: patterns, top liquidators, at-risk assets",
853
+ argsSchema: {
854
+ chain: z
855
+ .string()
856
+ .default("ethereum")
857
+ .describe("Chain to monitor"),
858
+ count: z
859
+ .string()
860
+ .default("20")
861
+ .describe("Number of recent liquidations to analyze"),
862
+ },
863
+ }, ({ chain, count }) => ({
864
+ messages: [
865
+ {
866
+ role: "user",
867
+ content: {
868
+ type: "text",
869
+ text: `Analyze recent liquidations on AAVE ${chain}. Steps:
870
+ 1. Call get_aave_liquidations(chain="${chain}", first=${count}) to get recent liquidation events
871
+ 2. Tally: which collateral assets were most commonly seized, which debt assets were most repaid
872
+ 3. Identify the top liquidators by frequency and total volume
873
+ 4. Call get_aave_reserves(chain="${chain}") to check current LTV and liquidation thresholds for top collateral assets
874
+ 5. Identify: which markets currently have high utilization (>80%) or low health buffers
875
+ 6. Summarize: liquidation frequency, most at-risk asset pairs, top liquidators, and market risk indicators`,
876
+ },
877
+ },
878
+ ],
879
+ }));
880
+ server.registerPrompt("aave_governance_overview", {
881
+ description: "Overview of AAVE governance: recent proposals, voting results, and active/pending decisions",
882
+ argsSchema: {},
883
+ }, () => ({
884
+ messages: [
885
+ {
886
+ role: "user",
887
+ content: {
888
+ type: "text",
889
+ text: `Give me an overview of AAVE governance activity. Steps:
890
+ 1. Call get_governance_proposals(first=10) to get the 10 most recent proposals
891
+ 2. For each proposal, note: proposalId, title, creator, state (0=Created,1=Active,2=Queued,3=Executed,4=Failed), for/against votes
892
+ 3. Identify any currently Active (state=1) or Queued (state=2) proposals
893
+ 4. For the most voted proposal, call get_proposal_votes(proposalId=X, first=10) to show top voters and their voting power
894
+ 5. Summarize: recent governance decisions, current active votes, and overall governance participation`,
895
+ },
896
+ },
897
+ ],
898
+ }));
899
+ // ---------------------------------------------------------------------------
900
+ // Start server
901
+ // ---------------------------------------------------------------------------
902
+ async function main() {
903
+ const transport = new StdioServerTransport();
904
+ await server.connect(transport);
905
+ console.error("Graph AAVE MCP server running on stdio");
906
+ }
907
+ main().catch((error) => {
908
+ console.error("Fatal error:", error);
909
+ process.exit(1);
910
+ });
911
+ //# sourceMappingURL=index.js.map