Skip to main content
Glama

read_contract

Retrieve data from smart contracts by calling view/pure functions using the contract address, ABI, and function name. Supports multiple networks including BSC, Ethereum, and Base.

Instructions

Read data from a smart contract by calling a view/pure function

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
abiYesThe ABI of the smart contract function, as a JSON array
argsNoThe arguments to pass to the function
contractAddressYesThe address of the smart contract to interact with
functionNameYesThe name of the function to call on the contract
networkNoNetwork name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet.bsc

Implementation Reference

  • The MCP tool handler function for 'read_contract'. It validates inputs implicitly via schema, parses ABI if string, prepares viem readContract parameters, calls the services.readContract helper, and returns success or error response.
    async ({ contractAddress, abi, functionName, args = [], network }) => { try { // Parse ABI if it's a string const parsedAbi = typeof abi === "string" ? JSON.parse(abi) : abi const params = { address: contractAddress as Address, abi: parsedAbi, functionName, args } const result = await services.readContract(params, network) return mcpToolRes.success(result) } catch (error) { return mcpToolRes.error(error, "reading contract") } }
  • Zod input schema definition for the 'read_contract' tool parameters.
    { contractAddress: z .string() .describe("The address of the smart contract to interact with"), abi: z .array(z.any()) .describe("The ABI of the smart contract function, as a JSON array"), functionName: z .string() .describe("The name of the function to call on the contract"), args: z .array(z.any()) .optional() .describe("The arguments to pass to the function"), network: defaultNetworkParam },
  • The registration of the 'read_contract' tool on the MCP server within registerContractTools function, including name, description, schema, and handler.
    server.tool( "read_contract", "Read data from a smart contract by calling a view/pure function", { contractAddress: z .string() .describe("The address of the smart contract to interact with"), abi: z .array(z.any()) .describe("The ABI of the smart contract function, as a JSON array"), functionName: z .string() .describe("The name of the function to call on the contract"), args: z .array(z.any()) .optional() .describe("The arguments to pass to the function"), network: defaultNetworkParam }, async ({ contractAddress, abi, functionName, args = [], network }) => { try { // Parse ABI if it's a string const parsedAbi = typeof abi === "string" ? JSON.parse(abi) : abi const params = { address: contractAddress as Address, abi: parsedAbi, functionName, args } const result = await services.readContract(params, network) return mcpToolRes.success(result) } catch (error) { return mcpToolRes.error(error, "reading contract") } } )
  • Helper function readContract that wraps viem's publicClient.readContract for the specified network.
    export async function readContract( params: ReadContractParameters, network = "ethereum" ) { const client = getPublicClient(network) return await client.readContract(params) }
  • Type definition for ReadContractParameters imported from viem (line shows import).
    type ReadContractParameters

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/bnb-chain/bnbchain-mcp'

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