Skip to main content
Glama

read-contract

Call read-only functions on smart contracts to retrieve blockchain data without executing transactions, using ABI, contract address, and function parameters.

Instructions

Call a read-only function on a contract, and returning the response

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
abiYes
addressYes
functionNameYes
argsYes

Implementation Reference

  • Implements the core logic of the 'read-contract' tool by calling readContract from @wagmi/core, parsing ABI, and formatting the result or error as text content.
    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()
    }),
  • Registers the 'read-contract' tool on the FastMCP server, including name, description, schema, and handler.
    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,
                }
              ]
            }
          }
        },
      });
    };
  • Invokes the registration of the 'read-contract' tool during server initialization.
    registerReadContractTools(server);

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/mcpilot'

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