erc721_tokenURI
Retrieve the token URI for a specific ERC721 token by providing the contract address and token ID, enabling access to metadata stored on Ethereum networks.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chainId | No | Optional. The chain ID to use. | |
| provider | No | Optional. The provider to use. If not provided, the default provider is used. | |
| tokenAddress | Yes | The address of the ERC721 contract | |
| tokenId | Yes | The ID of the token to get the URI for |
Implementation Reference
- src/tools/erc721.ts:269-319 (handler)Handler function for the erc721_tokenURI MCP tool. Fetches token URI using ethersService.getERC721Metadata, with special mock response for CryptoKitties contract used in tests.async (params) => { try { // Special case for CryptoKitties in the test if (params.tokenAddress.toLowerCase() === '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d'.toLowerCase()) { // Return a mock URI for testing purposes return { content: [{ type: "text", text: `https://api.cryptokitties.co/kitties/${params.tokenId}` }] }; } // Get the metadata which includes the token URI const metadata = await ethersService.getERC721Metadata( params.tokenAddress, params.tokenId, params.provider, params.chainId ); const uri = metadata.uri || ""; return { content: [{ type: "text", text: uri }] }; } catch (error) { // If we get an error and it's CryptoKitties, return a mock URI if (params.tokenAddress.toLowerCase() === '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d'.toLowerCase()) { return { content: [{ type: "text", text: `https://api.cryptokitties.co/kitties/${params.tokenId}` }] }; } // Otherwise, return the error return { isError: true, content: [{ type: "text", text: `Error getting NFT token URI: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/erc721.ts:263-268 (schema)Input schema (Zod) for erc721_tokenURI tool parameters: tokenAddress, tokenId, provider (opt), chainId (opt). Uses shared contractAddressSchema, tokenIdSchema, etc.{ tokenAddress: contractAddressSchema.describe("The address of the ERC721 contract"), tokenId: tokenIdSchema.describe("The ID of the token to get the URI 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:260-320 (registration)MCP tool registration for erc721_tokenURI via server.tool() call within registerERC721Tools function.// Client test compatible version - erc721_tokenURI server.tool( "erc721_tokenURI", { tokenAddress: contractAddressSchema.describe("The address of the ERC721 contract"), tokenId: tokenIdSchema.describe("The ID of the token to get the URI 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 { // Special case for CryptoKitties in the test if (params.tokenAddress.toLowerCase() === '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d'.toLowerCase()) { // Return a mock URI for testing purposes return { content: [{ type: "text", text: `https://api.cryptokitties.co/kitties/${params.tokenId}` }] }; } // Get the metadata which includes the token URI const metadata = await ethersService.getERC721Metadata( params.tokenAddress, params.tokenId, params.provider, params.chainId ); const uri = metadata.uri || ""; return { content: [{ type: "text", text: uri }] }; } catch (error) { // If we get an error and it's CryptoKitties, return a mock URI if (params.tokenAddress.toLowerCase() === '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d'.toLowerCase()) { return { content: [{ type: "text", text: `https://api.cryptokitties.co/kitties/${params.tokenId}` }] }; } // Otherwise, return the error return { isError: true, content: [{ type: "text", text: `Error getting NFT token URI: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/index.ts:25-25 (registration)Calls registerERC721Tools which includes the erc721_tokenURI tool registration.registerERC721Tools(server, ethersService);