erc20_balanceOf
Check ERC20 token balances for any Ethereum address by specifying the token contract and wallet address.
Instructions
Get the ERC20 token balance for a specific address. Alternative naming for compatibility with MCP client tests.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contractAddress | Yes | The address of the ERC20 token contract | |
| tokenAddress | No | DEPRECATED: Use contractAddress instead. The address of the ERC20 token contract | |
| ownerAddress | Yes | The Ethereum address whose balance to check | |
| provider | No | Optional. Either a network name or custom RPC URL. Use getAllNetworks to see available networks and their details, or getNetwork to get info about a specific network. You can use any network name returned by these tools as a provider value. | |
| chainId | No | Optional. The chain ID to use. If provided with a named network and they don't match, the RPC's chain ID will be used. |
Implementation Reference
- src/tools/erc20.ts:223-261 (handler)The handler function executing the erc20_balanceOf tool logic. Maps input parameters, retrieves the ERC20 token balance and symbol using EthersService, formats a user-friendly response.async (params) => { // Map deprecated parameters const mapped = mapParameters(params); try { const contractAddr = mapped.contractAddress || params.tokenAddress; if (!contractAddr) { throw new Error('Either contractAddress or tokenAddress must be provided'); } const balance = await ethersService.getERC20Balance( mapped.ownerAddress, contractAddr, mapped.provider, mapped.chainId ); // Get token info to format the response const tokenInfo = await ethersService.getERC20TokenInfo( contractAddr, mapped.provider, mapped.chainId ); return { content: [{ type: "text", text: `${mapped.ownerAddress} has a balance of ${balance} ${tokenInfo.symbol}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error getting token balance: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/erc20.ts:214-222 (schema)Input schema validation for the erc20_balanceOf tool using Zod, defining parameters like contractAddress, ownerAddress, provider, and chainId.{ contractAddress: contractAddressSchema, tokenAddress: tokenAddressSchema.optional(), // Deprecated ownerAddress: z.string().describe( "The Ethereum address whose balance to check" ), provider: providerSchema, chainId: chainIdSchema },
- src/tools/erc20.ts:212-261 (registration)Direct MCP server.tool registration for the erc20_balanceOf tool, including description, schema, and inline handler."erc20_balanceOf", "Get the ERC20 token balance for a specific address. Alternative naming for compatibility with MCP client tests.", { contractAddress: contractAddressSchema, tokenAddress: tokenAddressSchema.optional(), // Deprecated ownerAddress: z.string().describe( "The Ethereum address whose balance to check" ), provider: providerSchema, chainId: chainIdSchema }, async (params) => { // Map deprecated parameters const mapped = mapParameters(params); try { const contractAddr = mapped.contractAddress || params.tokenAddress; if (!contractAddr) { throw new Error('Either contractAddress or tokenAddress must be provided'); } const balance = await ethersService.getERC20Balance( mapped.ownerAddress, contractAddr, mapped.provider, mapped.chainId ); // Get token info to format the response const tokenInfo = await ethersService.getERC20TokenInfo( contractAddr, mapped.provider, mapped.chainId ); return { content: [{ type: "text", text: `${mapped.ownerAddress} has a balance of ${balance} ${tokenInfo.symbol}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error getting token balance: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/index.ts:24-24 (registration)Top-level registration call to registerERC20Tools, which registers the erc20_balanceOf tool among others.registerERC20Tools(server, ethersService);