Skip to main content
Glama
zazencodes

Unit Converter MCP

by zazencodes

convert_mass

Convert weight measurements between units like grams, kilograms, ounces, pounds, and more. Input a value, select source and target units, and get precise results instantly.

Instructions

Convert weight between units.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
from_unitYesSource unit
to_unitYesTarget unit
valueYesWeight value to convert

Implementation Reference

  • MCP handler for the 'convert_mass' tool. Registers the tool via @app.tool() and implements the logic by calling the core convert_mass_tool, formatting input/output with Pydantic annotations using MASS_UNIT, and returning a structured response.
    @app.tool() def convert_mass( value: Annotated[float, Field(description="Weight value to convert")], from_unit: Annotated[MASS_UNIT, Field(description="Source unit")], to_unit: Annotated[MASS_UNIT, Field(description="Target unit")], ) -> dict: """Convert weight between units.""" converted_value = convert_mass_tool(value, from_unit, to_unit) return { "original_value": value, "original_unit": from_unit, "converted_value": converted_value, "converted_unit": to_unit, "conversion_type": "mass", }
  • Core helper function implementing the mass unit conversion logic by converting to kilograms as base unit using a dictionary of conversion factors.
    def convert_mass_tool( value: float, from_unit: MASS_UNIT, to_unit: MASS_UNIT, ) -> float: """Convert mass between units.""" # Convert to kilograms first to_kilograms = { "carat": 0.0002, "decagram": 0.01, "hectogram": 0.1, "gram": 0.001, "milligram": 1e-6, "microgram": 1e-9, "nanogram": 1e-12, "picogram": 1e-15, "femtogram": 1e-18, "grain": 6.479891e-05, "ounce": 0.028349523125, "troy ounce": 0.0311034768, "pound": 0.45359237, "stone": 6.35029318, "short ton (US)": 907.18474, "long ton (UK)": 1_016.0469088, "tonne": 1_000.0, "kilotonne": 1_000_000.0, "megatonne": 1_000_000_000.0, "kilogram": 1.0, } kg = value * to_kilograms[from_unit] return kg / to_kilograms[to_unit]
  • Type definition (Literal) for MASS_UNIT used in tool signatures for input validation of from_unit and to_unit parameters.
    MASS_UNIT = Literal[ "carat", "decagram", "hectogram", "gram", "milligram", "microgram", "nanogram", "picogram", "femtogram", "grain", "ounce", "troy ounce", "pound", "stone", "short ton (US)", "long ton (UK)", "tonne", "kilotonne", "megatonne", "kilogram", ]
  • Imports MASS_UNIT and convert_mass_tool from tools package, necessary for defining and using the convert_mass handler.
    from .tools import ( ANGLE_UNIT, AREA_UNIT, COMPUTER_DATA_UNIT, DENSITY_UNIT, ENERGY_UNIT, FORCE_UNIT, LENGTH_UNIT, MASS_UNIT, POWER_UNIT, PRESSURE_UNIT, SPEED_UNIT, TEMPERATURE_UNIT, TIME_UNIT, VOLUME_UNIT, convert_angle_tool, convert_area_tool, convert_batch_tool, convert_computer_data_tool, convert_density_tool, convert_energy_tool, convert_force_tool, convert_length_tool, convert_mass_tool, convert_power_tool, convert_pressure_tool, convert_speed_tool, convert_temperature_tool, convert_time_tool, convert_volume_tool, )

Other Tools

Related Tools

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/zazencodes/unit-converter-mcp'

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