web3-tools-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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 web3-tools-mcp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,417 @@
1
+ # Viem MCP Server
2
+
3
+ A Model Context Protocol (MCP) server that provides comprehensive blockchain interaction capabilities using the [viem](https://viem.sh/) TypeScript library. This server enables AI assistants to interact with multiple blockchain networks, query contract data, analyze transactions, and work with smart contracts.
4
+
5
+ ## ๐Ÿ“ฆ Installation
6
+
7
+ ### NPM (Recommended)
8
+ ```bash
9
+ npm install -g web3-tools-mcp
10
+ ```
11
+
12
+ ### Usage
13
+ After installation, you can run the server directly:
14
+ ```bash
15
+ web3-tools-mcp
16
+ ```
17
+
18
+ Or with configuration options:
19
+ ```bash
20
+ web3-tools-mcp --etherscan-api-key YOUR_KEY --alchemy-api-key YOUR_KEY
21
+ ```
22
+
23
+ ### Environment Variables
24
+ You can also set configuration via environment variables:
25
+ ```bash
26
+ export ETHERSCAN_API_KEY=your_key_here
27
+ export ALCHEMY_API_KEY=your_key_here
28
+ export INFURA_API_KEY=your_key_here
29
+ export HYPERSYNC_API_KEY=your_key_here
30
+ web3-tools-mcp
31
+ ```
32
+
33
+ ### MCP Client Configuration
34
+ Add to your MCP client configuration (e.g., Claude Desktop):
35
+ ```json
36
+ {
37
+ "mcp": {
38
+ "servers": {
39
+ "web3-tools": {
40
+ "command": "web3-tools-mcp",
41
+ "args": [
42
+ "--etherscan-api-key", "YOUR_ETHERSCAN_KEY",
43
+ "--alchemy-api-key", "YOUR_ALCHEMY_KEY"
44
+ ]
45
+ }
46
+ }
47
+ }
48
+ }
49
+ ```
50
+
51
+ ## ๐ŸŒŸ Key Features
52
+
53
+ - **Multi-chain Support**: Works with Ethereum mainnet, Base, Arbitrum, Polygon, Optimism, Celo, and localhost
54
+ - **Smart Contract Interactions**: Call view/pure functions, get contract ABIs, and analyze contract bytecode
55
+ - **Event Log Analysis**: Query and decode blockchain events with flexible filtering
56
+ - **Token Operations**: Get native and ERC20 token balances for single or multiple addresses
57
+ - **ABI Utilities**: Generate function, event, and error signatures from ABI definitions
58
+ - **Batch Operations**: Execute multiple contract calls or balance queries efficiently
59
+ - **Transaction Analysis**: Trace transactions for detailed execution information
60
+ - **Enhanced RPC Support**: Automatic failover between providers (Alchemy, Infura, public RPCs)
61
+ - **Hypersync Integration**: Fast event log querying with fallback support
62
+
63
+ ## ๐Ÿš€ Supported Blockchain Networks
64
+
65
+ | Network | Chain ID | Default RPC | Hypersync Support |
66
+ |---------|----------|-------------|------------------|
67
+ | Ethereum Mainnet | 1 | โœ… | โœ… |
68
+ | Base | 8453 | โœ… | โœ… |
69
+ | Arbitrum One | 42161 | โœ… | โœ… |
70
+ | Polygon | 137 | โœ… | โœ… |
71
+ | Optimism | 10 | โœ… | โœ… |
72
+ | Celo | 42220 | โœ… | โŒ |
73
+ | Localhost | 31337 | โœ… | โŒ |
74
+
75
+ ## ๐Ÿ›  Available Tools
76
+
77
+ ### 1. ABI Signature Tools
78
+
79
+ #### `get_function_signature`
80
+ Generate 4-byte function selectors from ABI definitions.
81
+
82
+ **Parameters:**
83
+ - `functionAbi` (string): Function ABI (e.g., `"function transfer(address to, uint256 amount)"`)
84
+
85
+ **Example:**
86
+ ```json
87
+ {
88
+ "name": "get_function_signature",
89
+ "arguments": {
90
+ "functionAbi": "function balanceOf(address owner) view returns (uint256)"
91
+ }
92
+ }
93
+ ```
94
+
95
+ #### `get_event_signature`
96
+ Generate 32-byte event signatures (topic0) from ABI definitions.
97
+
98
+ **Parameters:**
99
+ - `eventAbi` (string): Event ABI (e.g., `"event Transfer(address indexed from, address indexed to, uint256 value)"`)
100
+
101
+ #### `get_error_signature`
102
+ Generate 4-byte error selectors from ABI definitions.
103
+
104
+ **Parameters:**
105
+ - `errorAbi` (string): Error ABI (e.g., `"error InsufficientBalance(uint256 available, uint256 required)"`)
106
+
107
+ ### 2. Contract Interaction Tools
108
+
109
+ #### `call_contract_function`
110
+ Call view/pure functions on smart contracts.
111
+
112
+ **Parameters:**
113
+ - `chain` (enum): Blockchain network (`mainnet`, `base`, `arbitrum`, `polygon`, `optimism`, `celo`, `localhost`)
114
+ - `contractAddress` (string): Contract address to call
115
+ - `functionAbi` (string): Function ABI definition
116
+ - `args` (array, optional): Function arguments
117
+ - `blockNumber` (string, optional): Specific block number to query
118
+
119
+ **Example:**
120
+ ```json
121
+ {
122
+ "name": "call_contract_function",
123
+ "arguments": {
124
+ "chain": "mainnet",
125
+ "contractAddress": "0xA0b86a33E6441c1e4e9c08975a0c8246e8dB8C4F",
126
+ "functionAbi": "function balanceOf(address owner) view returns (uint256)",
127
+ "args": ["0x742d35Cc6cF36C3e0C37d3f6D1D5e4f2C8F3E8A9"]
128
+ }
129
+ }
130
+ ```
131
+
132
+ #### `get_contract_abi`
133
+ Get comprehensive contract information including ABI, proxy detection, and verification status from Etherscan.
134
+
135
+ **Parameters:**
136
+ - `chain` (enum): Blockchain network
137
+ - `address` (string): Contract address
138
+
139
+ #### `is_contract`
140
+ Check if an address is a smart contract or EOA (Externally Owned Account).
141
+
142
+ **Parameters:**
143
+ - `chain` (enum): Blockchain network
144
+ - `address` (string): Address to check
145
+
146
+ ### 3. Balance Query Tools
147
+
148
+ #### `get_balance`
149
+ Get native token balance for an address.
150
+
151
+ **Parameters:**
152
+ - `chain` (enum): Blockchain network
153
+ - `address` (string): Address to check
154
+ - `blockNumber` (string, optional): Specific block number
155
+
156
+ #### `get_token_balance`
157
+ Get ERC20/ERC777 token balance for an address.
158
+
159
+ **Parameters:**
160
+ - `chain` (enum): Blockchain network
161
+ - `tokenAddress` (string): Token contract address
162
+ - `holderAddress` (string): Address to check balance for
163
+ - `blockNumber` (string, optional): Specific block number
164
+
165
+ #### `batch_native_balances`
166
+ Get native token balances for multiple addresses in a single call.
167
+
168
+ **Parameters:**
169
+ - `chain` (enum): Blockchain network
170
+ - `addresses` (array): Array of addresses to check
171
+ - `blockNumber` (string, optional): Specific block number
172
+
173
+ #### `batch_token_balances`
174
+ Get multiple token balances for multiple addresses efficiently.
175
+
176
+ **Parameters:**
177
+ - `chain` (enum): Blockchain network
178
+ - `queries` (array): Array of balance queries with `tokenAddress`, `holderAddress`, and optional `label`
179
+ - `blockNumber` (string, optional): Specific block number
180
+
181
+ ### 4. Event Log Tools
182
+
183
+ #### `get_logs`
184
+ Query contract events with decoded output and parameter filtering. Automatically falls back to Hypersync for supported chains when needed.
185
+
186
+ **Parameters:**
187
+ - `chain` (enum): Blockchain network
188
+ - `eventAbi` (string): Event ABI definition for decoding
189
+ - `address` (string, optional): Contract address to filter logs
190
+ - `fromBlock` (string, optional): Start block number
191
+ - `toBlock` (string, optional): End block number
192
+ - `eventArgs` (object, optional): Filter by indexed event parameters
193
+
194
+ **Example:**
195
+ ```json
196
+ {
197
+ "name": "get_logs",
198
+ "arguments": {
199
+ "chain": "mainnet",
200
+ "eventAbi": "event Transfer(address indexed from, address indexed to, uint256 value)",
201
+ "address": "0xA0b86a33E6441c1e4e9c08975a0c8246e8dB8C4F",
202
+ "fromBlock": "18000000",
203
+ "toBlock": "18001000",
204
+ "eventArgs": {
205
+ "from": "0x742d35Cc6cF36C3e0C37d3f6D1D5e4f2C8F3E8A9"
206
+ }
207
+ }
208
+ }
209
+ ```
210
+
211
+ ### 5. Batch Operations
212
+
213
+ #### `batch_contract_calls`
214
+ Execute multiple contract calls in a single batch operation.
215
+
216
+ **Parameters:**
217
+ - `chain` (enum): Blockchain network
218
+ - `calls` (array): Array of contract calls with `contractAddress`, `functionAbi`, `args`, and optional `label`
219
+ - `blockNumber` (string, optional): Specific block number
220
+
221
+ ### 6. Advanced Tools
222
+
223
+ #### `get_storage_at`
224
+ Read raw storage data from a contract with ABI-based decoding.
225
+
226
+ **Parameters:**
227
+ - `chain` (enum): Blockchain network
228
+ - `address` (string): Contract address
229
+ - `slot` (string): Storage slot to read (hex string)
230
+ - `abiType` (string): ABI type for decoding (`uint256`, `address`, `bool`, `bytes32`, etc.)
231
+ - `blockNumber` (string, optional): Specific block number
232
+
233
+ #### `get_block_info`
234
+ Get comprehensive block information including timestamp, hash, and formatted dates.
235
+
236
+ **Parameters:**
237
+ - `chain` (enum): Blockchain network
238
+ - `blockNumber` (string, optional): Block number to query (defaults to latest)
239
+
240
+ #### `trace_transaction`
241
+ Trace a transaction to see detailed execution information including internal calls, state changes, and gas usage.
242
+
243
+ **Parameters:**
244
+ - `chain` (enum): Blockchain network
245
+ - `transactionHash` (string): Transaction hash to trace
246
+ - `traceType` (enum, optional): Type of trace (`trace`, `vmTrace`, `stateDiff`)
247
+
248
+ ## โš™๏ธ Configuration
249
+
250
+ The server supports multiple configuration options through environment variables or command-line arguments:
251
+
252
+ ### API Keys
253
+ - `ETHERSCAN_API_KEY` / `--etherscan-api-key`: For contract ABI retrieval
254
+ - `ALCHEMY_API_KEY` / `--alchemy-api-key`: Enhanced RPC endpoints
255
+ - `INFURA_API_KEY` / `--infura-api-key`: Alternative RPC provider
256
+ - `HYPERSYNC_API_KEY` / `--hypersync-api-key`: Fast event log querying
257
+
258
+ ### Custom RPC URLs
259
+ - `--custom-rpc`: JSON object with custom RPC URLs per chain
260
+
261
+ **Example:**
262
+ ```bash
263
+ bun index.ts --custom-rpc '{"mainnet": "https://my-custom-rpc.com", "base": "https://base-rpc.com"}'
264
+ ```
265
+
266
+ ## ๐Ÿ”ง Technical Features
267
+
268
+ ### Enhanced RPC Configuration
269
+ - **Automatic Provider Selection**: Prioritizes Alchemy โ†’ Infura โ†’ Public RPCs
270
+ - **Custom RPC Support**: Override default providers with custom endpoints
271
+ - **Failover Mechanisms**: Graceful handling of RPC failures
272
+
273
+ ### Hypersync Integration
274
+ - **Fast Event Queries**: Significantly faster than traditional RPC for event logs
275
+ - **Automatic Fallback**: Falls back to regular viem when Hypersync fails
276
+ - **Selective Support**: Available for mainnet, Base, Arbitrum, Polygon, and Optimism
277
+
278
+ ### Data Type Handling
279
+ - **BigInt Serialization**: Automatic conversion of BigInt values to strings for JSON compatibility
280
+ - **Type Conversion**: Smart conversion of arguments based on ABI parameter types
281
+ - **Error Handling**: Comprehensive error messages and graceful failure handling
282
+
283
+ ### Batch Operations
284
+ - **Multicall Support**: Execute multiple contract calls efficiently
285
+ - **Balance Batching**: Query multiple balances in parallel
286
+ - **Result Aggregation**: Organized results with success/failure tracking
287
+
288
+ ## ๐Ÿš€ Usage Examples
289
+
290
+ ### Basic Contract Call
291
+ ```json
292
+ {
293
+ "name": "call_contract_function",
294
+ "arguments": {
295
+ "chain": "mainnet",
296
+ "contractAddress": "0xA0b86a33E6441c1e4e9c08975a0c8246e8dB8C4F",
297
+ "functionAbi": "function name() view returns (string)",
298
+ "args": []
299
+ }
300
+ }
301
+ ```
302
+
303
+ ### Event Log Analysis
304
+ ```json
305
+ {
306
+ "name": "get_logs",
307
+ "arguments": {
308
+ "chain": "base",
309
+ "eventAbi": "event Swap(address indexed sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, address indexed to)",
310
+ "fromBlock": "10000000",
311
+ "toBlock": "latest"
312
+ }
313
+ }
314
+ ```
315
+
316
+ ### Batch Balance Queries
317
+ ```json
318
+ {
319
+ "name": "batch_token_balances",
320
+ "arguments": {
321
+ "chain": "mainnet",
322
+ "queries": [
323
+ {
324
+ "tokenAddress": "0xA0b86a33E6441c1e4e9c08975a0c8246e8dB8C4F",
325
+ "holderAddress": "0x742d35Cc6cF36C3e0C37d3f6D1D5e4f2C8F3E8A9",
326
+ "label": "USDC Balance"
327
+ },
328
+ {
329
+ "tokenAddress": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
330
+ "holderAddress": "0x742d35Cc6cF36C3e0C37d3f6D1D5e4f2C8F3E8A9",
331
+ "label": "WETH Balance"
332
+ }
333
+ ]
334
+ }
335
+ }
336
+ ```
337
+
338
+ ## ๐Ÿ“‹ Requirements
339
+
340
+ - **Runtime**: Bun (recommended) or Node.js
341
+ - **Dependencies**: viem, @envio-dev/hypersync-client, @modelcontextprotocol/sdk
342
+ - **Network Access**: Internet connection for blockchain RPC calls
343
+
344
+ ## ๐Ÿงช Testing
345
+
346
+ The server includes a comprehensive test suite that validates all functionality:
347
+
348
+ ### Running Tests
349
+
350
+ ```bash
351
+ # Run all tests
352
+ bun test
353
+
354
+ # Run tests in watch mode
355
+ bun run test:watch
356
+
357
+ # Run tests with coverage
358
+ bun run test:coverage
359
+
360
+ # Run type checking
361
+ bun run lint
362
+ ```
363
+
364
+ ### Test Coverage
365
+
366
+ The test suite covers:
367
+
368
+ - **ABI Signature Generation**: Function, event, and error signatures
369
+ - **Contract Interactions**: Detecting contracts vs EOAs, calling view functions
370
+ - **Balance Queries**: Native and token balances, batch operations
371
+ - **Block Information**: Latest and historical block data
372
+ - **Multi-chain Support**: Testing across different networks
373
+ - **Error Handling**: Invalid inputs, network failures, malformed requests
374
+ - **Data Type Handling**: BigInt serialization, address normalization
375
+ - **Performance**: Batch vs individual operation timing
376
+
377
+ ### Example Usage
378
+
379
+ ```bash
380
+ # Run example demonstrations
381
+ bun run examples
382
+ ```
383
+
384
+ The examples file demonstrates:
385
+ - Function signature generation
386
+ - Contract vs EOA detection
387
+ - Smart contract function calls
388
+ - Balance queries
389
+ - Block information retrieval
390
+
391
+ ### Test Structure
392
+
393
+ ```
394
+ test.ts # Main test suite
395
+ examples.ts # Usage examples and demonstrations
396
+ ```
397
+
398
+ ## ๐Ÿ”— Related Tools
399
+
400
+ This server is designed to work seamlessly with other blockchain development tools and can be used alongside:
401
+ - Block explorers (Etherscan, Basescan, etc.)
402
+ - DeFi protocols analysis
403
+ - Smart contract development workflows
404
+ - Token analysis and portfolio tracking
405
+ - Event monitoring and alerting systems
406
+
407
+ ## ๐ŸŽฏ Use Cases
408
+
409
+ - **Smart Contract Analysis**: Analyze contract behavior, storage, and interactions
410
+ - **Token Research**: Query token balances, transfers, and metadata
411
+ - **DeFi Protocol Monitoring**: Track liquidity, swaps, and protocol events
412
+ - **Transaction Analysis**: Understand complex transaction flows and internal calls
413
+ - **Multi-chain Portfolio Tracking**: Monitor assets across different networks
414
+ - **Event-driven Analysis**: Build insights from blockchain event data
415
+ - **Contract Verification**: Check contract deployment and proxy patterns
416
+
417
+ This MCP server provides a comprehensive toolkit for blockchain interaction, making it easy for AI assistants to help users analyze, query, and understand blockchain data across multiple networks.