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
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | The ABI of the smart contract function, as a JSON array | |
| args | No | The arguments to pass to the function | |
| contractAddress | Yes | The address of the smart contract to interact with | |
| functionName | Yes | The name of the function to call on the contract | |
| network | No | Network name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet. | bsc |
Implementation Reference
- src/evm/modules/contracts/tools.ts:57-75 (handler)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 },
- src/evm/modules/contracts/tools.ts:38-76 (registration)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") } } )
- src/evm/services/contracts.ts:15-21 (helper)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) }
- src/evm/services/contracts.ts:6-6 (schema)Type definition for ReadContractParameters imported from viem (line shows import).type ReadContractParameters