Skip to main content
Glama
EmanuelJr
by EmanuelJr

read_contract

Retrieve data from smart contracts on EVM chains by specifying contract address, chain ID, ABI, function name, and arguments.

Instructions

Read a value from a contract

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contractAddressYes
chainIdYes
abiYes
functionNameYes
argsYes

Implementation Reference

  • The main handler function `readContract` that executes the tool logic using viem client to read from the contract.
    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:40-44 (registration)
    Registration of the 'read_contract' tool in the list of tools returned by ListToolsRequest.
    {
      name: "read_contract",
      description: "Read a value from a contract",
      inputSchema: z.toJSONSchema(ReadContractSchema),
    },
  • src/index.ts:104-120 (registration)
    Dispatcher case in CallToolRequest handler that parses args, calls readContract, and formats the response.
    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",
          },
        ],
      };
    }

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