read-contract
Call read-only functions on smart contracts to retrieve blockchain data without executing transactions, using MetaMask for secure access.
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. | |
| functionName | Yes | Function to call on the contract. | |
| args | No | Arguments to pass when calling the contract. | |
| chainId | No | ID of chain to use when fetching data. |
Implementation Reference
- src/tools/read-contract.ts:19-51 (handler)The handler function that executes the read-contract tool logic: calls readContract from wagmi with provided args, returns the 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 schema defining the input parameters for the read-contract tool: abi, address, functionName, args, chainId.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)Registration of the read-contract tool on the FastMCP server, including 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)Invocation of the read-contract tool registration function as part of the overall tools registration.registerReadContractTools(server, wagmiConfig);