Skip to main content
Glama

formatUnits

Convert Ethereum token amounts from wei to human-readable units like ether or gwei. Specify the value and decimal places or unit name for accurate formatting.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
valueYesThe value to format
unitYesThe number of decimals or unit name (e.g., 'gwei', 18)

Implementation Reference

  • The handler function that executes the formatUnits tool logic: takes a value (in wei or similar) and unit (decimals or name like 'gwei'), uses ethers.formatUnits to convert to human-readable format, and returns it as text content.
    async ({ value, unit }) => { try { const formattedValue = ethers.formatUnits(value, unit); return { content: [{ type: "text", text: formattedValue }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error formatting units: ${error instanceof Error ? error.message : String(error)}` }] }; } }
  • Zod input schema defining parameters: value (string) and unit (string or number).
    { value: z.string().describe( "The value to format" ), unit: z.union([z.string(), z.number()]).describe( "The number of decimals or unit name (e.g., 'gwei', 18)" ) },
  • The server.tool registration call that defines and registers the formatUnits tool with its schema and handler within the registerCoreTools function.
    server.tool( "formatUnits", { value: z.string().describe( "The value to format" ), unit: z.union([z.string(), z.number()]).describe( "The number of decimals or unit name (e.g., 'gwei', 18)" ) }, async ({ value, unit }) => { try { const formattedValue = ethers.formatUnits(value, unit); return { content: [{ type: "text", text: formattedValue }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error formatting units: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
  • Call to registerCoreTools in the central registerAllTools function, which includes the formatUnits tool among core tools.
    registerCoreTools(server, ethersService);

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