Skip to main content
Glama

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

Implementation Reference

  • 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, }, ], }; } },
  • 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."), }),
  • 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, }, ], }; } }, });
  • Invocation of the read-contract tool registration function as part of the 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