-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtools.ts
More file actions
1366 lines (1290 loc) · 36.2 KB
/
Copy pathtools.ts
File metadata and controls
1366 lines (1290 loc) · 36.2 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import type { Address, Hash, Hex } from 'viem';
import { z } from 'zod';
import { DEFAULT_NETWORK, getRpcUrl, getSupportedNetworks } from './chains.js';
import { isWalletEnabled, getWalletMode } from './config.js';
import { getWalletProvider } from './wallet';
import * as services from './services/index.js';
/**
* Register all EVM-related tools with the MCP server
*
* @param server The MCP server instance
*/
export function registerEVMTools(server: McpServer) {
// Register read-only tools (always available)
registerReadOnlyTools(server);
// Register wallet-dependent tools (only if wallet is enabled)
if (isWalletEnabled()) {
registerWalletTools(server);
} else {
console.error('Wallet functionality is disabled. Wallet-dependent tools will not be available.');
}
}
/**
* Register read-only tools that don't require wallet functionality
*/
function registerReadOnlyTools(server: McpServer) {
// NETWORK INFORMATION TOOLS
// Get chain information
server.tool(
'get_chain_info',
'Get information about Sei network',
{
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet', etc.) or chain ID. Supports all Sei networks. Defaults to Sei mainnet.")
},
async ({ network = DEFAULT_NETWORK }) => {
try {
const chainId = await services.getChainId(network);
const blockNumber = await services.getBlockNumber(network);
const rpcUrl = getRpcUrl(network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
network,
chainId,
blockNumber: blockNumber.toString(),
rpcUrl
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching chain info: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get supported networks
server.tool('get_supported_networks', 'Get a list of supported EVM networks', {}, async () => {
try {
const networks = getSupportedNetworks();
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
supportedNetworks: networks
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching supported networks: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
});
// BLOCK TOOLS
// Get block by number
server.tool(
'get_block_by_number',
'Get a block by its block number',
{
blockNumber: z.number().describe('The block number to fetch'),
network: z.string().optional().describe('Network name or chain ID. Defaults to Sei mainnet.')
},
async ({ blockNumber, network = DEFAULT_NETWORK }) => {
try {
const block = await services.getBlockByNumber(blockNumber, network);
return {
content: [
{
type: 'text',
text: services.helpers.formatJson(block)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching block ${blockNumber}: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get latest block
server.tool(
'get_latest_block',
'Get the latest block from the EVM',
{
network: z.string().optional().describe('Network name or chain ID. Defaults to Sei mainnet.')
},
async ({ network = DEFAULT_NETWORK }) => {
try {
const block = await services.getLatestBlock(network);
return {
content: [
{
type: 'text',
text: services.helpers.formatJson(block)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching latest block: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// BALANCE TOOLS
// Get Sei balance
server.tool(
'get_balance',
'Get the native token balance (Sei) for an address',
{
address: z.string().describe("The wallet address name (e.g., '0x1234...') to check the balance for"),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet', etc.) or chain ID. Supports all Sei networks. Defaults to Sei mainnet.")
},
async ({ address, network = DEFAULT_NETWORK }) => {
try {
const balance = await services.getBalance(address, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
address,
network,
wei: balance.wei.toString(),
ether: balance.sei
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching balance: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get ERC20 balance
server.tool(
'get_erc20_balance',
'Get the ERC20 token balance of an EVM address',
{
address: z.string().describe('The EVM address to check'),
tokenAddress: z.string().describe('The ERC20 token contract address'),
network: z.string().optional().describe('Network name or chain ID. Defaults to Sei mainnet.')
},
async ({ address, tokenAddress, network = DEFAULT_NETWORK }) => {
try {
const balance = await services.getERC20Balance(tokenAddress as Address, address as Address, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
address,
tokenAddress,
network,
balance: {
raw: balance.raw.toString(),
formatted: balance.formatted,
decimals: balance.token.decimals
}
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching ERC20 balance for ${address}: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get ERC20 token balance
server.tool(
'get_token_balance',
'Get the balance of an ERC20 token for an address',
{
tokenAddress: z.string().describe("The contract address name of the ERC20 token (e.g., '0x3894085Ef7Ff0f0aeDf52E2A2704928d1Ec074F1')"),
ownerAddress: z.string().describe("The wallet address name to check the balance for (e.g., '0x1234...')"),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet', etc.) or chain ID. Supports all Sei networks. Defaults to Sei mainnet.")
},
async ({ tokenAddress, ownerAddress, network = DEFAULT_NETWORK }) => {
try {
const balance = await services.getERC20Balance(tokenAddress, ownerAddress, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
tokenAddress,
owner: ownerAddress,
network,
raw: balance.raw.toString(),
formatted: balance.formatted,
symbol: balance.token.symbol,
decimals: balance.token.decimals
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching token balance: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// TRANSACTION TOOLS
// Get transaction by hash
server.tool(
'get_transaction',
'Get detailed information about a specific transaction by its hash. Includes sender, recipient, value, data, and more.',
{
txHash: z.string().describe("The transaction hash to look up (e.g., '0x1234...')"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet', etc.) or chain ID. Defaults to Sei mainnet.")
},
async ({ txHash, network = DEFAULT_NETWORK }) => {
try {
const tx = await services.getTransaction(txHash as Hash, network);
return {
content: [
{
type: 'text',
text: services.helpers.formatJson(tx)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching transaction ${txHash}: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get transaction receipt
server.tool(
'get_transaction_receipt',
'Get a transaction receipt by its hash',
{
txHash: z.string().describe('The transaction hash to look up'),
network: z.string().optional().describe('Network name or chain ID. Defaults to Sei mainnet.')
},
async ({ txHash, network = DEFAULT_NETWORK }) => {
try {
const receipt = await services.getTransactionReceipt(txHash as Hash, network);
return {
content: [
{
type: 'text',
text: services.helpers.formatJson(receipt)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching transaction receipt ${txHash}: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Estimate gas
server.tool(
'estimate_gas',
'Estimate the gas cost for a transaction',
{
to: z.string().describe('The recipient address'),
value: z.string().optional().describe("The amount of Sei to send (e.g., '0.1')"),
data: z.string().optional().describe('The transaction data as a hex string'),
network: z.string().optional().describe('Network name or chain ID. Defaults to Sei mainnet.')
},
async ({ to, value, data, network = DEFAULT_NETWORK }) => {
try {
const params: { to: Address; value?: bigint; data?: `0x${string}` } = { to: to as Address };
if (value) {
params.value = services.helpers.parseEther(value);
}
if (data) {
params.data = data as `0x${string}`;
}
const gas = await services.estimateGas(params, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
network,
estimatedGas: gas.toString()
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error estimating gas: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
}
/**
* Register wallet-dependent tools that require wallet functionality
*/
function registerWalletTools(server: McpServer) {
// TRANSFER TOOLS
// Transfer Sei
server.tool(
'transfer_sei',
'Transfer native tokens (Sei) to an address',
{
to: z.string().describe("The recipient address (e.g., '0x1234...'"),
amount: z.string().describe("Amount to send in SEI (or the native token of the network), as a string (e.g., '0.1')"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ to, amount, network = DEFAULT_NETWORK }) => {
try {
const txHash = await services.transferSei(to, amount, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash,
to,
amount,
network
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring Sei: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Transfer ERC20
server.tool(
'transfer_erc20',
'Transfer ERC20 tokens to another address',
{
tokenAddress: z.string().describe('The address of the ERC20 token contract'),
toAddress: z.string().describe('The recipient address'),
amount: z.string().describe("The amount of tokens to send (in token units, e.g., '10' for 10 tokens)"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ tokenAddress, toAddress, amount, network = DEFAULT_NETWORK }) => {
try {
const result = await services.transferERC20(tokenAddress, toAddress, amount, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash: result.txHash,
network,
tokenAddress,
recipient: toAddress,
amount: result.amount.formatted,
symbol: result.token.symbol
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring ERC20 tokens: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Approve ERC20 token spending
server.tool(
'approve_token_spending',
'Approve another address (like a DeFi protocol or exchange) to spend your ERC20 tokens. This is often required before interacting with DeFi protocols.',
{
tokenAddress: z.string().describe("The contract address of the ERC20 token to approve for spending (e.g., '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')"),
spenderAddress: z.string().describe('The contract address being approved to spend your tokens (e.g., a DEX or lending protocol)'),
amount: z
.string()
.describe(
"The amount of tokens to approve in token units, not wei (e.g., '1000' to approve spending 1000 tokens). Use a very large number for unlimited approval."
),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ tokenAddress, spenderAddress, amount, network = DEFAULT_NETWORK }) => {
try {
const result = await services.approveERC20(tokenAddress, spenderAddress, amount, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash: result.txHash,
network,
tokenAddress,
spender: spenderAddress,
amount: result.amount.formatted,
symbol: result.token.symbol
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error approving token spending: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Transfer NFT (ERC721)
server.tool(
'transfer_nft',
'Transfer an NFT (ERC721 token) from one address to another. Requires the private key of the current owner for signing the transaction.',
{
tokenAddress: z.string().describe("The contract address of the NFT collection (e.g., '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D')"),
tokenId: z.string().describe("The ID of the specific NFT to transfer (e.g., '1234')"),
toAddress: z.string().describe('The recipient wallet address that will receive the NFT'),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Most NFTs are on Sei mainnet, which is the default.")
},
async ({ tokenAddress, tokenId, toAddress, network = DEFAULT_NETWORK }) => {
try {
const result = await services.transferERC721(tokenAddress, toAddress, BigInt(tokenId), network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash: result.txHash,
network,
collection: tokenAddress,
tokenId: result.tokenId,
recipient: toAddress,
name: result.token.name,
symbol: result.token.symbol
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring NFT: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Transfer ERC1155 token
server.tool(
'transfer_erc1155',
'Transfer ERC1155 tokens to another address. ERC1155 is a multi-token standard that can represent both fungible and non-fungible tokens in a single contract.',
{
tokenAddress: z.string().describe("The contract address of the ERC1155 token collection (e.g., '0x76BE3b62873462d2142405439777e971754E8E77')"),
tokenId: z.string().describe("The ID of the specific token to transfer (e.g., '1234')"),
amount: z.string().describe("The quantity of tokens to send (e.g., '1' for a single NFT or '10' for 10 fungible tokens)"),
toAddress: z.string().describe('The recipient wallet address that will receive the tokens'),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. ERC1155 tokens exist across many networks. Defaults to Sei mainnet.")
},
async ({ tokenAddress, tokenId, amount, toAddress, network = DEFAULT_NETWORK }) => {
try {
const result = await services.transferERC1155(tokenAddress, toAddress, BigInt(tokenId), amount, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash: result.txHash,
network,
contract: tokenAddress,
tokenId: result.tokenId,
amount: result.amount,
recipient: toAddress
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring ERC1155 tokens: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Transfer ERC20 tokens
server.tool(
'transfer_token',
'Transfer ERC20 tokens to an address',
{
tokenAddress: z.string().describe("The contract address of the ERC20 token to transfer (e.g., '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')"),
toAddress: z.string().describe("The recipient address that will receive the tokens (e.g., '0x1234...')"),
amount: z.string().describe("Amount of tokens to send as a string (e.g., '100' for 100 tokens). This will be adjusted for the token's decimals."),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Supports all Sei networks. Defaults to Sei mainnet.")
},
async ({ tokenAddress, toAddress, amount, network = DEFAULT_NETWORK }) => {
try {
const result = await services.transferERC20(tokenAddress, toAddress, amount, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
txHash: result.txHash,
tokenAddress,
toAddress,
amount: result.amount.formatted,
symbol: result.token.symbol,
network
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error transferring tokens: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// CONTRACT TOOLS
// Read contract
server.tool(
'read_contract',
"Read data from a smart contract by calling a view/pure function. This doesn't modify blockchain state and doesn't require gas or signing.",
{
contractAddress: z.string().describe('The address of the smart contract to interact with'),
abi: z.array(z.any()).describe('The ABI (Application Binary Interface) of the smart contract function, as a JSON array'),
functionName: z.string().describe("The name of the function to call on the contract (e.g., 'balanceOf')"),
args: z.array(z.any()).optional().describe("The arguments to pass to the function, as an array (e.g., ['0x1234...'])"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ contractAddress, abi, functionName, args = [], network = DEFAULT_NETWORK }) => {
try {
// Parse ABI if it's a string
const parsedAbi = typeof abi === 'string' ? JSON.parse(abi) : abi;
const params = {
address: contractAddress as Address,
abi: parsedAbi,
functionName,
args
};
const result = await services.readContract(params, network);
return {
content: [
{
type: 'text',
text: services.helpers.formatJson(result)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error reading contract: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Write to contract
server.tool(
'write_contract',
'Write data to a smart contract by calling a state-changing function. This modifies blockchain state and requires gas payment and transaction signing.',
{
contractAddress: z.string().describe('The address of the smart contract to interact with'),
abi: z.array(z.any()).describe('The ABI (Application Binary Interface) of the smart contract function, as a JSON array'),
functionName: z.string().describe("The name of the function to call on the contract (e.g., 'transfer')"),
args: z.array(z.any()).describe("The arguments to pass to the function, as an array (e.g., ['0x1234...', '1000000000000000000'])"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ contractAddress, abi, functionName, args, network = DEFAULT_NETWORK }) => {
try {
// Parse ABI if it's a string
const parsedAbi = typeof abi === 'string' ? JSON.parse(abi) : abi;
const contractParams: Record<string, unknown> = {
address: contractAddress as Address,
abi: parsedAbi,
functionName,
args
};
const txHash = await services.writeContract(contractParams, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
network,
transactionHash: txHash,
message: 'Contract write transaction sent successfully'
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error writing to contract: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Deploy contract
server.tool(
'deploy_contract',
'Deploy a new smart contract to the blockchain. This creates a new contract instance and returns both the deployment transaction hash and the deployed contract address.',
{
bytecode: z.string().describe("The compiled contract bytecode as a hex string (e.g., '0x608060405234801561001057600080fd5b50...')"),
abi: z.array(z.any()).describe('The contract ABI (Application Binary Interface) as a JSON array, needed for constructor function'),
args: z
.array(z.any())
.optional()
.describe(
"The constructor arguments to pass during deployment, as an array (e.g., ['param1', 'param2']). Leave empty if constructor has no parameters."
),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ bytecode, abi, args = [], network = DEFAULT_NETWORK }) => {
try {
// Parse ABI if it's a string
const parsedAbi = typeof abi === 'string' ? JSON.parse(abi) : abi;
// Ensure bytecode is a proper hex string
const formattedBytecode = bytecode.startsWith('0x') ? (bytecode as Hex) : (`0x${bytecode}` as Hex);
const result = await services.deployContract(formattedBytecode, parsedAbi, args, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: true,
network,
contractAddress: result.address,
transactionHash: result.transactionHash,
message: 'Contract deployed successfully'
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error deploying contract: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Check if address is a contract
server.tool(
'is_contract',
'Check if an address is a smart contract or an externally owned account (EOA)',
{
address: z.string().describe("The wallet or contract address to check (e.g., '0x1234...')"),
network: z
.string()
.optional()
.describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet', etc.) or chain ID. Supports all Sei networks. Defaults to Sei mainnet.")
},
async ({ address, network = DEFAULT_NETWORK }) => {
try {
const isContract = await services.isContract(address, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
address,
network,
isContract,
type: isContract ? 'Contract' : 'Externally Owned Account (EOA)'
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error checking if address is a contract: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};
}
}
);
// Get ERC20 token information
server.tool(
'get_token_info',
'Get comprehensive information about an ERC20 token including name, symbol, decimals, total supply, and other metadata. Use this to analyze any token on EVM chains.',
{
tokenAddress: z.string().describe("The contract address of the ERC20 token (e.g., '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')"),
network: z.string().optional().describe("Network name (e.g., 'sei', 'sei-testnet', 'sei-devnet') or chain ID. Defaults to Sei mainnet.")
},
async ({ tokenAddress, network = DEFAULT_NETWORK }) => {
try {
const tokenInfo = await services.getERC20TokenInfo(tokenAddress as Address, network);
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
address: tokenAddress,
network,
...tokenInfo
},
null,
2
)
}
]
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error fetching token info: ${error instanceof Error ? error.message : String(error)}`
}
],
isError: true
};