Skip to main content
Glama

get-contract-abi

Retrieve the Application Binary Interface (ABI) for a smart contract by providing its address in 0x format. This enables interaction with contracts on the specified blockchain.

Instructions

Get the ABI for a smart contract

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesContract address (0x format)

Implementation Reference

  • Handler logic for the 'get-contract-abi' tool: parses input using ContractSchema, calls etherscanService.getContractABI, and formats the response.
    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 parameters (address) of the 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:112-126 (registration)
    Tool registration in the ListTools response, including name, 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 of fetching the contract ABI from Etherscan API using the getabi endpoint.
    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/otc-ai/mcp-otc'

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