Skip to main content
Glama

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
NameRequiredDescriptionDefault
abiYesThe contract's ABI.
addressYesThe contract's address.
argsNoArguments to pass when calling the contract.
chainIdNoID of chain to use when fetching data.
functionNameYesFunction to call on the contract.

Implementation Reference

  • 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, }, ], }; } },
  • 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."), }),
  • 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, }, ], }; } }, });
  • Calls registerReadContractTools to register the read-contract tool as part of overall tools registration.
    registerReadContractTools(server, wagmiConfig);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Xiawpohr/metamask-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server