-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·341 lines (301 loc) · 9.28 KB
/
Copy pathserver.js
File metadata and controls
executable file
·341 lines (301 loc) · 9.28 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
#!/usr/bin/env node
// Load .env file if it exists
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const { spawn } = require('child_process');
const { randomUUID } = require('crypto');
const app = express();
const PORT = process.env.PORT || 8080;
console.log('🚀 Starting SEI Blockchain MCP Server...');
console.log(`📍 Port: ${PORT}`);
console.log(`🔑 WALLET_PRIVATE_KEY configured: ${process.env.WALLET_PRIVATE_KEY ? 'Yes' : 'No'}`);
console.log(`🌐 SEI_RPC_URL: ${process.env.SEI_RPC_URL || 'https://evm-rpc.sei-apis.com'}`);
app.use(cors());
app.use(express.json({ limit: '10mb' }));
let mcpProcess = null;
let mcpInitialized = false;
const pendingRequests = new Map();
async function initializeMCP() {
return new Promise((resolve, reject) => {
console.log('Initializing MCP...');
console.log('Environment for MCP:', {
WALLET_PRIVATE_KEY: process.env.WALLET_PRIVATE_KEY ? 'Set' : 'Not set',
SEI_RPC_URL: process.env.SEI_RPC_URL ? 'Set' : 'Using default'
});
mcpProcess = spawn('node', ['./mcp/index.js'], {
env: {
...process.env,
WALLET_PRIVATE_KEY: process.env.WALLET_PRIVATE_KEY || '',
SEI_RPC_URL: process.env.SEI_RPC_URL || 'https://evm-rpc.sei-apis.com'
},
stdio: ['pipe', 'pipe', 'pipe']
});
let buffer = '';
let initTimeout = setTimeout(() => {
reject(new Error('MCP initialization timeout'));
}, 30000);
mcpProcess.stdout.on('data', (data) => {
buffer += data.toString();
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.trim()) {
try {
const response = JSON.parse(line);
if (response.id === 'init' && response.result) {
clearTimeout(initTimeout);
console.log('✅ MCP initialized');
// Send initialized notification
mcpProcess.stdin.write(JSON.stringify({
jsonrpc: '2.0',
method: 'notifications/initialized',
params: {}
}) + '\n');
// Mark as initialized after notification
setTimeout(() => {
mcpInitialized = true;
console.log('✅ MCP ready for requests');
}, 500);
resolve(response.result);
}
if (response.id && pendingRequests.has(response.id)) {
const { resolve } = pendingRequests.get(response.id);
pendingRequests.delete(response.id);
resolve(response);
}
} catch (e) {
// Ignore parse errors for non-JSON lines
}
}
}
});
mcpProcess.stderr.on('data', (data) => {
console.error('MCP stderr:', data.toString());
});
mcpProcess.on('error', (err) => {
clearTimeout(initTimeout);
console.error('MCP error:', err);
reject(err);
});
mcpProcess.on('exit', (code) => {
console.log(`MCP exited: ${code}`);
mcpInitialized = false;
});
// Send initialize request
setTimeout(() => {
mcpProcess.stdin.write(JSON.stringify({
jsonrpc: '2.0',
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: {
name: 'sei-mcp-http-server',
version: '1.0.0'
}
},
id: 'init'
}) + '\n');
}, 1000);
});
}
async function sendMCPRequest(method, params = {}) {
if (!mcpInitialized) {
throw new Error('MCP not initialized');
}
return new Promise((resolve, reject) => {
const id = randomUUID();
const timeout = setTimeout(() => {
pendingRequests.delete(id);
reject(new Error('Request timeout'));
}, 30000);
pendingRequests.set(id, {
resolve: (response) => {
clearTimeout(timeout);
resolve(response);
},
reject
});
mcpProcess.stdin.write(JSON.stringify({
jsonrpc: '2.0',
method,
params,
id
}) + '\n');
});
}
// Root endpoint
app.get('/', (req, res) => {
res.json({
name: 'SEI Blockchain MCP Server',
version: '1.0.0',
status: mcpInitialized ? 'operational' : 'offline',
chain: 'SEI',
chainId: 1329,
protocol: 'MCP',
endpoints: ['/health', '/info', '/mcp']
});
});
// Health check endpoint
app.get('/health', (req, res) => {
const healthy = mcpInitialized || mcpProcess !== null;
res.status(healthy ? 200 : 503).json({
status: healthy ? 'healthy' : 'unhealthy',
service: 'SEI Blockchain MCP',
version: '1.0.0',
chain: 'SEI',
chainId: 1329,
timestamp: new Date().toISOString()
});
});
// Info endpoint
app.get('/info', (req, res) => {
res.json({
name: 'SEI Blockchain MCP',
description: 'MCP server for interacting with SEI blockchain and DragonSwap V2',
version: '1.0.0',
chain: 'SEI',
chainId: 1329,
rpcUrl: process.env.SEI_RPC_URL || 'https://evm-rpc.sei-apis.com',
walletConfigured: !!process.env.WALLET_PRIVATE_KEY,
dex: 'DragonSwap V2',
capabilities: [
'Wallet management with private key',
'Token operations (balances, transfers, approvals)',
'DragonSwap V2 swaps and liquidity',
'Transaction preparation and execution',
'Gas estimation and management'
]
});
});
// MCP Protocol endpoint (JSON-RPC)
app.post('/mcp', async (req, res) => {
try {
const { jsonrpc, method, params, id, tool } = req.body;
// Handle legacy format (tool + params)
if (tool && !method) {
if (!mcpInitialized) {
return res.status(503).json({
error: 'MCP not ready',
message: 'Please wait for initialization'
});
}
try {
const response = await sendMCPRequest('tools/call', {
name: tool,
arguments: params || {}
});
return res.json({
result: response.result,
error: response.error || null
});
} catch (error) {
return res.status(500).json({
error: error.message
});
}
}
// Handle standard JSON-RPC format
if (jsonrpc !== '2.0') {
return res.status(400).json({
jsonrpc: '2.0',
error: { code: -32600, message: 'Invalid Request' },
id: id || null
});
}
switch (method) {
case 'initialize':
res.json({
jsonrpc: '2.0',
result: {
protocolVersion: '2024-11-05',
serverInfo: {
name: 'SEI Blockchain MCP',
version: '1.0.0'
},
capabilities: { tools: {} }
},
id
});
break;
case 'tools/list':
if (!mcpInitialized) throw new Error('MCP not ready');
const listResponse = await sendMCPRequest('tools/list');
res.json({
jsonrpc: '2.0',
result: listResponse.result,
id
});
break;
case 'tools/call':
if (!mcpInitialized) throw new Error('MCP not ready');
const callResponse = await sendMCPRequest('tools/call', params);
res.json({
jsonrpc: '2.0',
result: callResponse.result,
id
});
break;
default:
res.status(404).json({
jsonrpc: '2.0',
error: { code: -32601, message: `Method not found: ${method}` },
id
});
}
} catch (error) {
console.error('MCP error:', error);
res.status(500).json({
jsonrpc: '2.0',
error: { code: -32603, message: error.message },
id: req.body.id || null
});
}
});
// MCP Discovery endpoint (GET)
app.get('/mcp', (req, res) => {
res.json({
name: 'SEI Blockchain MCP',
version: '1.0.0',
protocol_version: '2024-11-05',
endpoint: '/mcp',
status: mcpInitialized ? 'ready' : 'offline',
description: 'Full-featured MCP server for SEI blockchain with wallet support',
features: [
'Private key wallet management',
'Native SEI transfers',
'ERC20 token operations',
'DragonSwap V2 integration',
'Transaction execution'
]
});
});
// Initialize MCP
initializeMCP()
.then(() => console.log('✅ MCP ready'))
.catch((error) => {
console.error('⚠️ MCP init failed:', error.message);
console.log('Server will run with limited functionality');
});
// Start server
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`\n🚀 SEI Blockchain MCP Server running on port ${PORT}`);
console.log('📍 Health check: http://localhost:' + PORT + '/health');
console.log('📍 Info: http://localhost:' + PORT + '/info');
console.log('📍 MCP endpoint: http://localhost:' + PORT + '/mcp');
console.log('\n✨ Ready for MCP connections!\n');
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down...');
server.close(() => console.log('HTTP server closed'));
if (mcpProcess) mcpProcess.kill('SIGTERM');
setTimeout(() => process.exit(0), 5000);
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down...');
server.close(() => console.log('HTTP server closed'));
if (mcpProcess) mcpProcess.kill('SIGTERM');
setTimeout(() => process.exit(0), 5000);
});