read-contract
Execute read-only functions on smart contracts to retrieve data using ABI, contract address, function name, and arguments, enabling secure blockchain interactions without exposing private keys.
Instructions
Call a read-only function on a contract, and returning the response
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | ||
| address | Yes | ||
| args | Yes | ||
| functionName | Yes |
Implementation Reference
- The main handler function that executes the 'read-contract' tool by parsing ABI, calling wagmi's readContract, and handling errors.execute: async (_args) => { try { const abi = JSON.parse(_args.abi) as Abi const address = _args.abi as Address const functionName = _args.functionName const args = _args.args const result = await readContract(wagmiConfig, { abi, address, functionName, 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, } ] } } },
- Zod schema defining the input parameters for the 'read-contract' tool: abi (string), address (string), functionName (string), args (array of strings).parameters: z.object({ abi: z.string(), address: z.string(), functionName: z.string(), args: z.string().array() }),
- packages/metamask-mcp/src/tools/read-contract.ts:7-59 (registration)The registration function that defines the tool metadata, schema, and handler, and adds it to the FastMCP server.export function registerReadContractTools(server: FastMCP): void { server.addTool({ name: "read-contract", description: "Call a read-only function on a contract, and returning the response", parameters: z.object({ abi: z.string(), address: z.string(), functionName: z.string(), args: z.string().array() }), execute: async (_args) => { try { const abi = JSON.parse(_args.abi) as Abi const address = _args.abi as Address const functionName = _args.functionName const args = _args.args const result = await readContract(wagmiConfig, { abi, address, functionName, 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, } ] } } }, }); };
- packages/metamask-mcp/src/index.ts:54-54 (registration)Invocation of the register function in the main server setup to register the 'read-contract' tool.registerReadContractTools(server);