-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-tools-mcp.js
More file actions
285 lines (255 loc) · 7.81 KB
/
base-tools-mcp.js
File metadata and controls
285 lines (255 loc) · 7.81 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
// Import the required libraries
import { ethers } from 'ethers';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
// Create a new MCP server
const server = new McpServer({
name: 'Base Tools MCP',
version: '1.0.0',
description: 'A Model Context Protocol server for Base Network interactions'
});
// Environment variables
const seedPhrase = process.env.SEED_PHRASE || 'increase stove still book elevator place knife intact degree globe notable feature';
const coinbaseApiKeyName = process.env.COINBASE_API_KEY_NAME || 'builder';
const coinbaseApiPrivateKey = process.env.COINBASE_API_PRIVATE_KEY;
const coinbaseKeyId = process.env.COINBASE_KEY_ID || 'e51be061-9079-4aae-a45c-0e1171b1b9e8';
const coinbaseProjectId = process.env.COINBASE_PROJECT_ID || '3563a2d7-bde9-46d7-bf48-c28eaf7e1772';
// Initialize wallet from seed phrase
const initWallet = () => {
try {
const provider = new ethers.JsonRpcProvider('https://mainnet.base.org');
const wallet = ethers.Wallet.fromPhrase(seedPhrase).connect(provider);
return { provider, wallet };
} catch (error) {
console.error('Failed to initialize wallet:', error);
throw error;
}
};
// Tool: Get wallet address
server.tool(
'get-address',
{},
async () => {
try {
const { wallet } = initWallet();
return {
content: [
{
type: 'text',
text: `Your wallet address is: ${wallet.address}`
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error retrieving wallet address: ${error.message}`
}
]
};
}
}
);
// Tool: List wallet balances
server.tool(
'list-balances',
{},
async () => {
try {
const { provider, wallet } = initWallet();
const balance = await provider.getBalance(wallet.address);
const etherBalance = ethers.formatEther(balance);
return {
content: [
{
type: 'text',
text: `Wallet Address: ${wallet.address}\nETH Balance: ${etherBalance} ETH`
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error retrieving wallet balance: ${error.message}`
}
]
};
}
}
);
// Tool: Transfer funds
server.tool(
'transfer-funds',
{
destination: z.string().describe('The destination address'),
amount: z.string().describe('The amount of ETH to transfer'),
},
async ({ destination, amount }) => {
try {
const { provider, wallet } = initWallet();
// Validate destination address
if (!ethers.isAddress(destination)) {
return {
content: [
{
type: 'text',
text: 'Invalid destination address'
}
]
};
}
// Convert amount to proper format
const value = ethers.parseEther(amount);
// Check if we have enough balance
const balance = await provider.getBalance(wallet.address);
if (balance < value) {
return {
content: [
{
type: 'text',
text: `Insufficient balance. You have ${ethers.formatEther(balance)} ETH but attempted to send ${amount} ETH.`
}
]
};
}
// Create and send transaction
const tx = await wallet.sendTransaction({
to: destination,
value
});
return {
content: [
{
type: 'text',
text: `Transaction sent! Transaction hash: ${tx.hash}\nView on Etherscan: https://basescan.org/tx/${tx.hash}`
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring funds: ${error.message}`
}
]
};
}
}
);
// Tool: Get ERC20 token balance
server.tool(
'erc20-balance',
{
contractAddress: z.string().describe('The ERC20 token contract address'),
},
async ({ contractAddress }) => {
try {
const { provider, wallet } = initWallet();
// ERC20 token ABI (minimal for balanceOf)
const abi = [
'function balanceOf(address owner) view returns (uint256)',
'function decimals() view returns (uint8)',
'function symbol() view returns (string)'
];
// Create contract instance
const tokenContract = new ethers.Contract(contractAddress, abi, provider);
// Get token details
const symbol = await tokenContract.symbol();
const decimals = await tokenContract.decimals();
const balance = await tokenContract.balanceOf(wallet.address);
// Format the balance based on decimals
const formattedBalance = ethers.formatUnits(balance, decimals);
return {
content: [
{
type: 'text',
text: `${symbol} Balance: ${formattedBalance} ${symbol}\nContract Address: ${contractAddress}`
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error retrieving token balance: ${error.message}`
}
]
};
}
}
);
// Tool: Transfer ERC20 token
server.tool(
'erc20-transfer',
{
contractAddress: z.string().describe('The ERC20 token contract address'),
toAddress: z.string().describe('The recipient address'),
amount: z.string().describe('The amount of tokens to transfer'),
},
async ({ contractAddress, toAddress, amount }) => {
try {
const { wallet } = initWallet();
// ERC20 token ABI (minimal for transfer)
const abi = [
'function transfer(address to, uint amount) returns (bool)',
'function decimals() view returns (uint8)',
'function symbol() view returns (string)',
'function balanceOf(address owner) view returns (uint256)'
];
// Create contract instance with signer
const tokenContract = new ethers.Contract(contractAddress, abi, wallet);
// Get token details
const symbol = await tokenContract.symbol();
const decimals = await tokenContract.decimals();
const balance = await tokenContract.balanceOf(wallet.address);
// Format the balance based on decimals
const formattedBalance = ethers.formatUnits(balance, decimals);
// Parse amount with correct decimals
const tokenAmount = ethers.parseUnits(amount, decimals);
// Check if we have enough balance
if (balance < tokenAmount) {
return {
content: [
{
type: 'text',
text: `Insufficient balance. You have ${formattedBalance} ${symbol} but attempted to send ${amount} ${symbol}.`
}
]
};
}
// Send the transfer transaction
const tx = await tokenContract.transfer(toAddress, tokenAmount);
return {
content: [
{
type: 'text',
text: `Transaction sent! Transaction hash: ${tx.hash}\nView on BaseScan: https://basescan.org/tx/${tx.hash}`
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring tokens: ${error.message}`
}
]
};
}
}
);
// Register the bridge tool with the server
import bridgeTool from './src/lib/mcp/bridgeTool.js';
server.tool(bridgeTool.info.name, bridgeTool.tool, bridgeTool.info);
// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);
console.log('Base Tools MCP Server started successfully');