Skip to main content
Glama

getContractABI

Retrieve the Application Binary Interface (ABI) for verified smart contracts on EVM chains like Ethereum, Polygon, Arbitrum, Base, and Optimism. This tool fetches ABIs from Etherscan with Sourcify fallback, enabling interaction with contract functions and data.

Instructions

컨트랙트 ABI를 조회합니다 (Etherscan → Sourcify 폴백, verified contract 필요)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYes컨트랙트 주소 (0x...)
chainNo체인 (ethereum, polygon, arbitrum, base, optimism)ethereum

Implementation Reference

  • The handler function performs the logic for fetching the contract ABI, including validation, cache checking, RPC code verification, and fetching from Etherscan/Sourcify.
    async function handler(args: z.infer<typeof inputSchema>): Promise<ToolResult<ContractABIData>> {
      const { address, chain } = args;
    
      if (!isSupportedChain(chain)) {
        return makeError(`Unsupported chain: ${chain}`, "CHAIN_NOT_SUPPORTED");
      }
    
      if (!isAddress(address)) {
        return makeError(`Invalid address: ${address}`, "INVALID_INPUT");
      }
    
      const cacheKey = `contractabi:${chain}:${address.toLowerCase()}`;
      const cached = cache.get<ContractABIData>(cacheKey);
      if (cached.hit) return makeSuccess(chain as SupportedChain, cached.data, true);
    
      const client = getClient(chain as SupportedChain);
    
      // 컨트랙트 여부 확인
      let isContract = false;
      try {
        const code = await client.getCode({ address: address as `0x${string}` });
        isContract = !!code && code !== "0x";
      } catch {
        // RPC 실패 시 무시, ABI 조회 시도는 계속
      }
    
      if (!isContract) {
        return makeError(`Address is not a contract: ${address}`, "ABI_NOT_FOUND");
      }
    
      // ABI 조회 (Etherscan → Sourcify)
      const abiResult = await getABI(address, chain as SupportedChain);
    
      if (!abiResult) {
        return makeError(`ABI not found for ${address} on ${chain}. Contract may not be verified.`, "ABI_NOT_FOUND");
      }
    
      const abi = abiResult.abi as Array<{ type?: string }>;
      const functionCount = abi.filter((item) => item.type === "function").length;
      const eventCount = abi.filter((item) => item.type === "event").length;
    
      const data: ContractABIData = {
        address,
        chain,
        abi: abiResult.abi,
        source: abiResult.source,
        contractName: abiResult.contractName,
        isContract: true,
        functionCount,
        eventCount,
      };
    
      cache.set(cacheKey, data, ABI_CACHE_TTL);
      return makeSuccess(chain as SupportedChain, data, false);
    }
  • The Zod schema defines the input parameters for the getContractABI tool, ensuring the address and chain are properly formatted.
    const inputSchema = z.object({
      address: z.string().describe("컨트랙트 주소 (0x...)"),
      chain: z.string().default("ethereum").describe("체인 (ethereum, polygon, arbitrum, base, optimism)"),
    });
  • The register function defines the tool with the MCP server using the handler and input schema.
    export function register(server: McpServer) {
      server.tool(
        "getContractABI",
        "컨트랙트 ABI를 조회합니다 (Etherscan → Sourcify 폴백, verified contract 필요)",
        inputSchema.shape,
        async (args) => {
          const result = await handler(args as z.infer<typeof inputSchema>);
          return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
        },
      );
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, description carries full burden. Valuably discloses data source hierarchy (Etherscan primary, Sourcify fallback) and contract verification requirement. Missing error behavior (what happens if unverified) and return format details.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single sentence front-loaded with action, parenthetical efficiently packing source chain and requirements. No redundancy; every clause adds value regarding implementation or prerequisites.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Acceptable for 2-parameter tool with complete schema coverage. Covers data sources and verification constraint, but lacks output description (critical given no output schema) and failure modes (unverified/partial verification handling).

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, establishing baseline 3. Description adds no explicit parameter guidance, but parenthetical context ('0x...' implied by address context) is implicitly clear. No additional semantic value needed given complete schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clear verb (조회/retrieve) and resource (contract ABI) with specific implementation details (Etherscan → Sourcify fallback). Inherently distinguishes from siblings like getContractEvents or getTokenInfo by focusing on ABI retrieval, though it could explicitly contrast with identifyAddress or decodeTx.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

States prerequisite 'verified contract needed' implying when not to use (unverified contracts), but lacks explicit guidance on alternatives (e.g., 'use decodeTx if unverified') or when this is preferred over reading from local cache.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/calintzy/evmscope'

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