read_contract
Retrieve data from smart contracts on EVM chains by specifying contract address, chain ID, ABI, function name, and arguments.
Instructions
Read a value from a contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contractAddress | Yes | ||
| chainId | Yes | ||
| abi | Yes | ||
| functionName | Yes | ||
| args | Yes |
Implementation Reference
- src/tools/ReadContract.ts:14-28 (handler)The main handler function `readContract` that executes the tool logic using viem client to read from the contract.export const readContract = async (options: ReadContractOptions) => { const { contractAddress, chainId, abi, functionName, args } = options; const chain = getChainById(chainId); const client = createClient(chain); const result = await client.readContract({ address: contractAddress as `0x${string}`, abi: JSON.parse(abi), functionName, args, }); return result; };
- src/tools/ReadContract.ts:5-11 (schema)Zod schema defining the input parameters for the read_contract tool.export const ReadContractSchema = z.object({ contractAddress: z.string().startsWith("0x"), chainId: z.number(), abi: z.string(), functionName: z.string(), args: z.array(z.string()), });
- src/index.ts:40-44 (registration)Registration of the 'read_contract' tool in the list of tools returned by ListToolsRequest.{ name: "read_contract", description: "Read a value from a contract", inputSchema: z.toJSONSchema(ReadContractSchema), },
- src/index.ts:104-120 (registration)Dispatcher case in CallToolRequest handler that parses args, calls readContract, and formats the response.case "read_contract": { const args = ReadContractSchema.parse(request.params.arguments); const result = await readContract(args); return { content: [ { type: "text", text: JSON.stringify(result, (_, value) => typeof value === "bigint" ? value.toString() : value ), description: "The result JSON formatted of the contract function call", }, ], }; }