Skip to main content
Glama

read_contract

Call view/pure functions on smart contracts without modifying blockchain state or requiring gas fees. Input contract address, ABI, function name, and arguments to retrieve data.

Instructions

Read data from a smart contract by calling a view/pure function. This doesn't modify blockchain state and doesn't require gas or signing.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
abiYesThe ABI (Application Binary Interface) of the smart contract function, as a JSON array
argsNoThe arguments to pass to the function, as an array (e.g., ['0x1234...'])
contractAddressYesThe address of the smart contract to interact with
functionNameYesThe name of the function to call on the contract (e.g., 'balanceOf')
networkNoNetwork name (e.g., 'ethereum', 'optimism', 'arbitrum', 'base', 'polygon') or chain ID. Defaults to Ethereum mainnet.

Implementation Reference

  • The handler function for the 'read_contract' MCP tool. It parses the ABI if necessary, constructs the readContract parameters, calls the services.readContract helper, and returns the formatted result or error response.
    async ({ contractAddress, abi, functionName, args = [], network = 'ethereum' }) => { 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 { content: [ { type: 'text', text: services.helpers.formatJson(result) } ] }; } catch (error) { return { content: [ { type: 'text', text: `Error reading contract: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
  • Zod input schema defining parameters for the 'read_contract' tool: contractAddress, abi, functionName, args, and network.
    { contractAddress: z .string() .describe('The address of the smart contract to interact with'), abi: z .array(z.any()) .describe( 'The ABI (Application Binary Interface) of the smart contract function, as a JSON array' ), functionName: z .string() .describe( "The name of the function to call on the contract (e.g., 'balanceOf')" ), args: z .array(z.any()) .optional() .describe( "The arguments to pass to the function, as an array (e.g., ['0x1234...'])" ), network: z .string() .optional() .describe( "Network name (e.g., 'ethereum', 'optimism', 'arbitrum', 'base', 'polygon') or chain ID. Defaults to Ethereum mainnet." ) },
  • Registration of the 'read_contract' tool with the MCP server using server.tool(), including description, input schema, and handler function.
    'read_contract', "Read data from a smart contract by calling a view/pure function. This doesn't modify blockchain state and doesn't require gas or signing.", { contractAddress: z .string() .describe('The address of the smart contract to interact with'), abi: z .array(z.any()) .describe( 'The ABI (Application Binary Interface) of the smart contract function, as a JSON array' ), functionName: z .string() .describe( "The name of the function to call on the contract (e.g., 'balanceOf')" ), args: z .array(z.any()) .optional() .describe( "The arguments to pass to the function, as an array (e.g., ['0x1234...'])" ), network: z .string() .optional() .describe( "Network name (e.g., 'ethereum', 'optimism', 'arbitrum', 'base', 'polygon') or chain ID. Defaults to Ethereum mainnet." ) }, async ({ contractAddress, abi, functionName, args = [], network = 'ethereum' }) => { 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 { content: [ { type: 'text', text: services.helpers.formatJson(result) } ] }; } catch (error) { return { content: [ { type: 'text', text: `Error reading contract: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );
  • Helper function readContract that wraps viem's readContract method, getting the public client for the network and executing the contract read.
    export async function readContract( params: ReadContractParameters, network = 'ethereum' ) { const client = getPublicClient(network); return await client.readContract(params); }
  • Calls registerEVMTools which registers the 'read_contract' tool (among others) with the MCP server instance.
    registerEVMPrompts(server);

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/chulanpro5/evm-mcp-server'

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