Skip to main content
Glama

erc721_tokenURI

Retrieve the token URI for an ERC721 NFT by specifying the contract address and token ID, enabling access to metadata for blockchain-based digital assets.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenAddressYesThe address of the ERC721 contract
tokenIdYesThe ID of the token to get the URI for
providerNoOptional. The provider to use. If not provided, the default provider is used.
chainIdNoOptional. The chain ID to use.

Implementation Reference

  • Primary handler and registration for the erc721_tokenURI tool. This inline async function implements the tool logic: checks for CryptoKitties special case, calls ethersService.getERC721Metadata to fetch token URI, handles errors with mock for tests.
    // 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)}` }] }; } } );
  • Registration of all ERC721 tools (including erc721_tokenURI) by calling registerERC721Tools from the central tools index.
    registerERC721Tools(server, ethersService);
  • src/mcpServer.ts:51-51 (registration)
    Top-level registration of all tools by calling registerAllTools, which includes ERC721 tools containing erc721_tokenURI.
    registerAllTools(server, ethersService);
  • Core helper function getTokenURI that performs the actual contract call to retrieve the token URI (used indirectly via getMetadata in EthersService).
    export async function getTokenURI( ethersService: EthersService, contractAddress: string, tokenId: string | number, provider?: string, chainId?: number ): Promise<string> { metrics.incrementCounter('erc721.getTokenURI'); return timeAsync('erc721.getTokenURI', async () => { try { // Create cache key const cacheKey = createTokenCacheKey( CACHE_KEYS.ERC721_TOKEN_URI, contractAddress, tokenId, chainId ); // Check cache first const cachedURI = ensCache.get(cacheKey); if (cachedURI) { return cachedURI; } // Get provider from ethers service const ethersProvider = ethersService['getProvider'](provider, chainId); // Create contract instance const contract = new ethers.Contract(contractAddress, ERC721_ABI, ethersProvider); // Try to get token URI let tokenURI; try { // Try standard tokenURI method tokenURI = await contract.tokenURI(tokenId); } catch (error) { // If tokenURI fails, try uri method (some contracts use this instead) try { tokenURI = await contract.uri(tokenId); } catch (innerError) { throw error; // If both fail, use the original error } } // Cache result for future use (1 hour TTL) ensCache.set(cacheKey, tokenURI, { ttl: 3600000 }); return tokenURI; } catch (error) { logger.debug('Error getting NFT token URI', { contractAddress, tokenId, error }); // Check for common errors if (error instanceof Error && (error.message.includes('nonexistent token') || error.message.includes('invalid token ID'))) { throw new TokenNotFoundError(contractAddress, tokenId); } throw handleTokenError(error, 'Failed to get NFT token URI'); } }); }
  • Input schema validation for erc721_tokenURI tool using Zod schemas.
    { 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)}` }] }; } } );

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/crazyrabbitLTC/mcp-ethers-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server