read-contract
Use this tool to call read-only functions on a smart contract using its ABI, address, and function name. Retrieve contract data securely without executing transactions, ensuring private keys remain safe.
Instructions
Call a read-only function on a contract, and returning the response.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | The contract's ABI. | |
| address | Yes | The contract's address. | |
| args | No | Arguments to pass when calling the contract. | |
| chainId | No | ID of chain to use when fetching data. | |
| functionName | Yes | Function to call on the contract. |
Implementation Reference
- src/tools/read-contract.ts:19-51 (handler)Executes the read-contract tool: calls readContract from @wagmi/core, returns result as text content or error message.execute: async (args) => { try { const result = await readContract(wagmiConfig, args); return { content: [ { type: "text", text: `${result}`, }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } },
- src/tools/read-contract.ts:12-18 (schema)Zod input schema defining parameters for the read-contract tool: abi, address, functionName, args (optional), chainId (optional).parameters: z.object({ abi: Abi.describe("The contract's ABI."), address: Address.describe("The contract's address."), functionName: z.string().describe("Function to call on the contract."), args: z.unknown().array().optional().describe("Arguments to pass when calling the contract."), chainId: z.coerce.number().optional().describe("ID of chain to use when fetching data."), }),
- src/tools/read-contract.ts:9-52 (registration)Registers the read-contract tool on the FastMCP server inside registerReadContractTools function, defining name, description, schema, and handler.server.addTool({ name: "read-contract", description: "Call a read-only function on a contract, and returning the response.", parameters: z.object({ abi: Abi.describe("The contract's ABI."), address: Address.describe("The contract's address."), functionName: z.string().describe("Function to call on the contract."), args: z.unknown().array().optional().describe("Arguments to pass when calling the contract."), chainId: z.coerce.number().optional().describe("ID of chain to use when fetching data."), }), execute: async (args) => { try { const result = await readContract(wagmiConfig, args); return { content: [ { type: "text", text: `${result}`, }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } }, });
- src/tools/register-tools.ts:51-51 (registration)Calls registerReadContractTools to register the read-contract tool as part of overall tools registration.registerReadContractTools(server, wagmiConfig);