Skip to main content
Glama

read_contract

Retrieve specific data from smart contracts on EVM chains by providing contract address, ABI, function name, and required arguments.

Instructions

Read a value from a contract

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
abiYes
argsYes
chainIdYes
contractAddressYes
functionNameYes

Implementation Reference

  • The main handler function that implements the read_contract tool by calling readContract on a viem client.
    export const readContract = async (options: ReadContractOptions) => { const { contractAddress, chainId, abi, functionName, args } = options; const chain = getChainById(chainId); const client = createClient(chain); const result = await client.readContract({ address: contractAddress as `0x${string}`, abi: JSON.parse(abi), functionName, args, }); return result; };
  • Zod schema defining the input parameters for the read_contract tool.
    export const ReadContractSchema = z.object({ contractAddress: z.string().startsWith("0x"), chainId: z.number(), abi: z.string(), functionName: z.string(), args: z.array(z.string()), });
  • src/index.ts:41-44 (registration)
    Registration of the read_contract tool in the list tools handler, specifying name, description, and schema.
    name: "read_contract", description: "Read a value from a contract", inputSchema: z.toJSONSchema(ReadContractSchema), },
  • src/index.ts:104-120 (registration)
    Execution handler in the call tool switch statement that invokes the readContract handler for read_contract tool.
    case "read_contract": { const args = ReadContractSchema.parse(request.params.arguments); const result = await readContract(args); return { content: [ { type: "text", text: JSON.stringify(result, (_, value) => typeof value === "bigint" ? value.toString() : value ), description: "The result JSON formatted of the contract function call", }, ], }; }

Other Tools

Related Tools

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/EmanuelJr/web3-mcp-server'

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