eth_getCode
Retrieve smart contract bytecode from any Ethereum address to verify functionality or analyze deployed code at a specific block.
Instructions
Retrieves the code at a given Ethereum address
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | The Ethereum address to get code from | |
| blockParameter | No | Block parameter (default: "latest") | latest |
Implementation Reference
- index.js:112-132 (handler)The handler function for the 'eth_getCode' tool. It makes an RPC call using makeRpcCall to retrieve the bytecode at the specified address and block parameter, then formats the response text accordingly, indicating if it's a contract or not.async (args) => { try { console.error(`Getting code for address: ${args.address} at block: ${args.blockParameter}`); const code = await makeRpcCall('eth_getCode', [args.address, args.blockParameter]); return { content: [{ type: "text", text: code === '0x' ? `No code found at address ${args.address} (this may be a regular wallet address, not a contract)` : `Contract code at ${args.address}:\n${code}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: Failed to get code. ${error.message}` }], isError: true }; } }
- index.js:109-111 (schema)Zod schema defining the input parameters for the 'eth_getCode' tool: 'address' (required, valid Ethereum address) and 'blockParameter' (optional, defaults to 'latest').address: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe('The Ethereum address to get code from'), blockParameter: z.string().default('latest').describe('Block parameter (default: "latest")') },
- index.js:105-133 (registration)Registration of the 'eth_getCode' tool on the MCP server using server.tool(), specifying name, description, input schema, and handler function.server.tool( 'eth_getCode', 'Retrieves the code at a given Ethereum address', { address: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe('The Ethereum address to get code from'), blockParameter: z.string().default('latest').describe('Block parameter (default: "latest")') }, async (args) => { try { console.error(`Getting code for address: ${args.address} at block: ${args.blockParameter}`); const code = await makeRpcCall('eth_getCode', [args.address, args.blockParameter]); return { content: [{ type: "text", text: code === '0x' ? `No code found at address ${args.address} (this may be a regular wallet address, not a contract)` : `Contract code at ${args.address}:\n${code}` }] }; } catch (error) { return { content: [{ type: "text", text: `Error: Failed to get code. ${error.message}` }], isError: true }; } } );
- index.js:84-102 (helper)Helper function 'makeRpcCall' used by the eth_getCode handler to perform the actual Ethereum JSON-RPC call to eth_getCode.async function makeRpcCall(method, params = []) { try { const response = await axios.post(ETH_RPC_URL, { jsonrpc: '2.0', id: 1, method, params }); if (response.data.error) { throw new Error(`RPC Error: ${response.data.error.message}`); } return response.data.result; } catch (error) { console.error(`Error making RPC call to ${method}:`, error.message); throw error; } }