erc721_balanceOf
Check the balance of ERC721 tokens for a specific owner address within an Ethereum-based contract using the MCP Ethers Wallet.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chainId | No | Optional. The chain ID to use. | |
| ownerAddress | Yes | The address to check balance for | |
| provider | No | Optional. The provider to use. If not provided, the default provider is used. | |
| tokenAddress | Yes | The address of the ERC721 contract |
Implementation Reference
- src/tools/erc721.ts:189-218 (handler)The main execution handler for the erc721_balanceOf tool. It calls ethersService.getERC721TokensOfOwner to fetch owned tokens and returns the count as the balance in a formatted text response.async (params) => { try { // Get the tokens owned by this address const tokens = await ethersService.getERC721TokensOfOwner( params.tokenAddress, params.ownerAddress, false, params.provider, params.chainId ); // The balance is the number of tokens const balance = tokens.length.toString(); return { content: [{ type: "text", text: `${params.ownerAddress} has ${balance} NFTs from contract ${params.tokenAddress}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error getting NFT balance: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/erc721.ts:183-187 (schema)Zod input schema defining parameters for erc721_balanceOf: tokenAddress (contract), ownerAddress, optional provider and chainId.{ tokenAddress: contractAddressSchema.describe("The address of the ERC721 contract"), ownerAddress: addressSchema.describe("The address to check balance for"), provider: providerSchema.describe("Optional. The provider to use. If not provided, the default provider is used."), chainId: chainIdSchema.describe("Optional. The chain ID to use.")
- src/tools/erc721.ts:179-219 (registration)Direct registration of the erc721_balanceOf tool within registerERC721Tools function using server.tool, including description, schema, and inline handler.// Client test compatible version - erc721_balanceOf server.tool( "erc721_balanceOf", "Get the number of ERC721 NFTs owned by a specific address. Alternative naming for compatibility with MCP client tests.", { tokenAddress: contractAddressSchema.describe("The address of the ERC721 contract"), ownerAddress: addressSchema.describe("The address to check balance for"), provider: providerSchema.describe("Optional. The provider to use. If not provided, the default provider is used."), chainId: chainIdSchema.describe("Optional. The chain ID to use.") }, async (params) => { try { // Get the tokens owned by this address const tokens = await ethersService.getERC721TokensOfOwner( params.tokenAddress, params.ownerAddress, false, params.provider, params.chainId ); // The balance is the number of tokens const balance = tokens.length.toString(); return { content: [{ type: "text", text: `${params.ownerAddress} has ${balance} NFTs from contract ${params.tokenAddress}` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error getting NFT balance: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/index.ts:25-25 (registration)Top-level call to registerERC721Tools in the central tools index, which includes the erc721_balanceOf tool registration.registerERC721Tools(server, ethersService);