Skip to main content
Glama
5ajaki

MCP Etherscan Server

by 5ajaki

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
NameRequiredDescriptionDefault
addressYesContract address (0x format)

Implementation Reference

  • 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;
      }
    }
  • 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;
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/5ajaki/mcp-etherscan-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server