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
| Name | Required | Description | Default |
|---|---|---|---|
| chainId | Yes | ||
| walletAddress | Yes |
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
- src/tools/FetchBalance.ts:12-26 (handler)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), }; };
- src/tools/FetchBalance.ts:6-9 (schema)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),
- src/index.ts:84-102 (handler)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", }, ], }; }