get-contract-abi
Retrieve the Application Binary Interface (ABI) for any Ethereum smart contract using its address to interact with or analyze contract functions.
Instructions
Get the ABI for a smart contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Contract address (0x format) |
Implementation Reference
- src/server.ts:266-283 (handler)Executes the get-contract-abi tool: validates input address using ContractSchema, fetches ABI via etherscanService, and returns it as text content.if (name === "get-contract-abi") { try { const { address } = ContractSchema.parse(args); const abi = await etherscanService.getContractABI(address); return { content: [ { type: "text", text: `Contract ABI for ${address}:\n\n${abi}` }, ], }; } catch (error) { if (error instanceof z.ZodError) { throw new Error( `Invalid input: ${error.errors.map((e) => e.message).join(", ")}` ); } throw error; } }
- src/server.ts:56-60 (schema)Zod schema for validating the input address parameter required by get-contract-abi tool.const ContractSchema = z.object({ address: z .string() .regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum address format"), });
- src/server.ts:123-137 (registration)Registers the get-contract-abi tool in the list of available tools, including its description and input schema.{ name: "get-contract-abi", description: "Get the ABI for a smart contract", inputSchema: { type: "object", properties: { address: { type: "string", description: "Contract address (0x format)", pattern: "^0x[a-fA-F0-9]{40}$", }, }, required: ["address"], }, },
- Core implementation in EtherscanService: validates address, fetches ABI from Etherscan API using 'getabi' action, returns the ABI JSON string.async getContractABI(address: string): Promise<string> { try { const validAddress = ethers.getAddress(address); // Get contract ABI const result = await fetch( `https://api.etherscan.io/api?module=contract&action=getabi&address=${validAddress}&apikey=${this.provider.apiKey}` ); const data = await result.json(); if (data.status !== "1" || !data.result) { throw new Error(data.message || "Failed to fetch contract ABI"); } return data.result; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to get contract ABI: ${error.message}`); } throw error; } }