-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
267 lines (238 loc) · 7.94 KB
/
Copy pathindex.js
File metadata and controls
267 lines (238 loc) · 7.94 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
require('dotenv').config();
const { McpServer } = require('@modelcontextprotocol/sdk/server/mcp.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
const { z } = require('zod');
const { ethers } = require('ethers');
// Load feeds.json
const feedsData = require('./feeds.json');
// Full Chainlink AggregatorV3Interface ABI
const priceFeedAbi = [
'function decimals() view returns (uint8)',
'function description() view returns (string)',
'function version() view returns (uint256)',
'function getRoundData(uint80 _roundId) view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)',
'function latestRoundData() view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)'
];
// Create MCP server
const server = new McpServer({
name: 'Chainlink Feeds',
version: '1.0.0',
capabilities: {
tools: {}
}
});
// Tool schema for latest price
const latestPriceSchema = z.object({
pair: z.string().describe('The price feed pair, e.g., FIL/ETH or FDUSD/USD'),
chain: z.string().refine((val) => feedsData[val.toLowerCase()], {
message: 'Unsupported chain'
}).describe('The blockchain network, e.g., ethereum or base')
});
// Tool: Get latest price
server.tool(
'getLatestPrice',
'Fetches the latest price for a given pair on a specified chain',
latestPriceSchema,
async ({ pair, chain }) => {
try {
// Validate inputs
const chainKey = chain.toLowerCase();
latestPriceSchema.parse({ pair, chain });
// Find feed by pair
const feed = feedsData[chainKey].feeds.find((f) => f.name.toLowerCase() === pair.toLowerCase());
if (!feed) {
throw new Error(`Pair ${pair} not found on chain ${chain}`);
}
// Initialize provider and contract
const provider = new ethers.JsonRpcProvider(`${feedsData[chainKey].baseUrl}/${process.env.INFURA_API_KEY}`);
const priceFeedContract = new ethers.Contract(feed.proxyAddress, priceFeedAbi, provider);
// Fetch decimals and latest round data
const [decimals, roundData] = await Promise.all([
priceFeedContract.decimals(),
priceFeedContract.latestRoundData()
]);
const price = ethers.formatUnits(roundData.answer, decimals);
const timestamp = Number(roundData.updatedAt) * 1000;
return {
content: [{
type: 'text',
text: JSON.stringify({
chain,
pair,
price: Number(price),
decimals: Number(decimals),
roundId: roundData.roundId.toString(),
timestamp: new Date(timestamp).toISOString(),
proxyAddress: feed.proxyAddress,
feedCategory: feed.feedCategory
}, null, 2)
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error.message}`
}],
isError: true
};
}
}
);
// Tool schema for querying price by round ID
const queryPriceSchema = z.object({
roundId: z.string().regex(/^\d+$/, 'Round ID must be a number').describe('The round ID for the price data'),
pair: z.string().describe('The price feed pair, e.g., FIL/ETH or FDUSD/USD'),
chain: z.string().refine((val) => feedsData[val.toLowerCase()], {
message: 'Unsupported chain'
}).describe('The blockchain network, e.g., ethereum or base')
});
// Tool: Query price by round ID (placeholder)
server.tool(
'queryPriceByRound',
'Queries the price for a given pair and round ID on a specified chain (placeholder due to historical data limitations)',
queryPriceSchema,
async ({ roundId, pair, chain }) => {
try {
// Validate inputs
const chainKey = chain.toLowerCase();
queryPriceSchema.parse({ roundId, pair, chain });
// Find feed by pair
const feed = feedsData[chainKey].feeds.find((f) => f.name.toLowerCase() === pair.toLowerCase());
if (!feed) {
throw new Error(`Pair ${pair} not found on chain ${chain}`);
}
// Initialize provider and contract
const provider = new ethers.JsonRpcProvider(`${feedsData[chainKey].baseUrl}/${process.env.INFURA_API_KEY}`);
const priceFeedContract = new ethers.Contract(feed.proxyAddress, priceFeedAbi, provider);
// Note: getRoundData may not be supported for historical rounds
const decimals = await priceFeedContract.decimals();
const roundData = await priceFeedContract.latestRoundData(); // Placeholder
const price = ethers.formatUnits(roundData.answer, decimals);
const timestamp = Number(roundData.updatedAt) * 1000;
return {
content: [{
type: 'text',
text: JSON.stringify({
chain,
pair,
price: Number(price),
decimals: Number(decimals),
roundId,
timestamp: new Date(timestamp).toISOString(),
proxyAddress: feed.proxyAddress,
feedCategory: feed.feedCategory
}, null, 2)
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error.message}`
}],
isError: true
};
}
}
);
// Tool schema for listing supported chains
const listSupportedChainsSchema = z.object({}).describe('No parameters required');
// Tool: List all supported chains
server.tool(
'listSupportedChains',
'Returns a comma-separated list of all supported blockchain networks',
listSupportedChainsSchema,
async () => {
try {
// Get all chain names as comma-separated string
const chains = Object.keys(feedsData).join(',');
return {
content: [{
type: 'text',
text: chains
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error.message}`
}],
isError: true
};
}
}
);
// Tool schema for listing feeds by chain
const listSupportedFeedsByChainSchema = z.object({
chain: z.string().refine((val) => feedsData[val.toLowerCase()], {
message: 'Unsupported chain'
}).describe('The blockchain network, e.g., ethereum or base')
});
// Tool: List feeds for a specific chain
server.tool(
'listSupportedFeedsByChain',
'Returns a comma-separated list of price feed names for a specified blockchain network',
listSupportedFeedsByChainSchema,
async ({ chain }) => {
try {
// Validate inputs
const chainKey = chain.toLowerCase();
listSupportedFeedsByChainSchema.parse({ chain });
// Get feed names as comma-separated string
const feedNames = feedsData[chainKey].feeds.map((feed) => feed.name).join(',');
return {
content: [{
type: 'text',
text: feedNames
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error.message}`
}],
isError: true
};
}
}
);
// Tool schema for listing all supported feeds
const listSupportedFeedsSchema = z.object({}).describe('No parameters required');
// Tool: List all supported chains and feeds
server.tool(
'listSupportedFeeds',
'Returns a Markdown list of all supported chains and their price feed names',
listSupportedFeedsSchema,
async () => {
try {
// Prepare Markdown list
const markdownList = Object.keys(feedsData).map((chain) => {
const feedNames = feedsData[chain].feeds.map((feed) => feed.name).join(',');
return `- ${chain}: ${feedNames}`;
}).join('\n');
return {
content: [{
type: 'text',
text: markdownList
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: `Error: ${error.message}`
}],
isError: true
};
}
}
);
// Start server with Stdio transport
async function startServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
startServer().catch(console.error);