read_contract
Retrieve specific data from smart contracts on EVM chains by providing contract address, ABI, function name, and required arguments.
Instructions
Read a value from a contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | ||
| args | Yes | ||
| chainId | Yes | ||
| contractAddress | Yes | ||
| functionName | Yes |
Implementation Reference
- src/tools/ReadContract.ts:14-28 (handler)The main handler function that implements the read_contract tool by calling readContract on a viem client.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:41-44 (registration)Registration of the read_contract tool in the list tools handler, specifying name, description, and schema.name: "read_contract", description: "Read a value from a contract", inputSchema: z.toJSONSchema(ReadContractSchema), },
- src/index.ts:104-120 (registration)Execution handler in the call tool switch statement that invokes the readContract handler for read_contract tool.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", }, ], }; }