eth_getCode
Retrieve smart contract bytecode from any EVM-compatible blockchain address to analyze deployed contract logic and verify code execution capabilities.
Instructions
Returns code at a given address
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Contract address | |
| blockNumber | No | Block number or 'latest', 'earliest', 'pending' | latest |
Implementation Reference
- src/index.ts:637-667 (handler)The handler function for the 'eth_getCode' tool. It makes an RPC call to retrieve the contract code at the specified address and block, computes the code length, formats the response using formatResponse, and handles errors by returning an error message.async ({ address, blockNumber }) => { try { const result = await makeRPCCall("eth_getCode", [address, blockNumber]); return { content: [ { type: "text", text: formatResponse( { address, code: result, code_length: result.length, block: blockNumber, }, "Contract Code", ), }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error: ${error.message}`, }, ], }; } },
- src/index.ts:629-636 (schema)Zod input schema for the 'eth_getCode' tool, validating 'address' as a required string and 'blockNumber' as an optional string defaulting to 'latest'.{ address: z.string().describe("Contract address"), blockNumber: z .string() .optional() .default("latest") .describe("Block number or 'latest', 'earliest', 'pending'"), },
- src/index.ts:626-628 (registration)Registration of the 'eth_getCode' tool with the MCP server, specifying the tool name and description.server.tool( "eth_getCode", "Returns code at a given address",
- src/index.ts:90-96 (helper)Generic helper function used by the 'eth_getCode' handler (and others) to perform RPC calls to the Ethereum provider via ethers.js JsonRpcProvider.try { const result = await provider.send(method, params); return result; } catch (error: any) { throw new Error(`RPC call failed: ${error.message}`); } }