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
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | The ABI (Application Binary Interface) of the smart contract function, as a JSON array | |
| args | No | The arguments to pass to the function, as an array (e.g., ['0x1234...']) | |
| contractAddress | Yes | The address of the smart contract to interact with | |
| functionName | Yes | The name of the function to call on the contract (e.g., 'balanceOf') | |
| network | No | Network name (e.g., 'ethereum', 'optimism', 'arbitrum', 'base', 'polygon') or chain ID. Defaults to Ethereum mainnet. |
Implementation Reference
- src/core/tools.ts:1104-1143 (handler)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 }; } }
- src/core/tools.ts:1077-1103 (schema)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." ) },
- src/core/tools.ts:1075-1144 (registration)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); }
- src/server/server.ts:19-19 (registration)Calls registerEVMTools which registers the 'read_contract' tool (among others) with the MCP server instance.registerEVMPrompts(server);