Skip to main content
Glama

fetch_balance

Retrieve wallet balances across EVM-compatible chains by specifying the wallet address and chain ID using this utility.

Instructions

Get the balance of a wallet

Input Schema

NameRequiredDescriptionDefault
chainIdYes
walletAddressYes

Input Schema (JSON Schema)

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "additionalProperties": false, "properties": { "chainId": { "type": "number" }, "walletAddress": { "pattern": "^0x.*", "type": "string" } }, "required": [ "walletAddress", "chainId" ], "type": "object" }

Implementation Reference

  • Core handler function that fetches the native balance of a wallet on a specified chain using viem client and returns raw and formatted values.
    export const fetchBalance = async (options: FetchBalanceOptions) => { const { walletAddress, chainId } = options; const chain = getChainById(chainId); const client = createClient(chain); const balance = await client.getBalance({ address: walletAddress as `0x${string}`, }); return { balance: balance.toString(), formattedBalance: formatUnits(balance, chain.nativeCurrency.decimals), }; };
  • Zod input schema for the fetch_balance tool, validating walletAddress as a 0x-prefixed string and chainId as a number.
    export const FetchBalanceSchema = z.object({ walletAddress: z.string().startsWith("0x"), chainId: z.number(), });
  • src/index.ts:36-38 (registration)
    Registers the fetch_balance tool in the MCP server's listTools response with name, description, and schema.
    name: "fetch_balance", description: "Get the balance of a wallet", inputSchema: z.toJSONSchema(FetchBalanceSchema),
  • Server-side handler in CallToolRequestSchema that parses arguments, calls fetchBalance, and formats the MCP response.
    case "fetch_balance": { const args = FetchBalanceSchema.parse(request.params.arguments); const result = await fetchBalance(args); return { content: [ { type: "text", text: result.balance, description: "The balance of the wallet", }, { type: "text", text: result.formattedBalance, description: "The formatted balance of the wallet", }, ], }; }

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