Skip to main content
Glama
zazencodes

Unit Converter MCP

by zazencodes

convert_mass

Convert weight values between units like grams, pounds, ounces, and tonnes. Enter a value, source unit, and target unit to get the converted result.

Instructions

Convert weight between units.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
valueYesWeight value to convert
from_unitYesSource unit
to_unitYesTarget unit

Implementation Reference

  • MCP tool handler for 'convert_mass'. Registers the tool via @app.tool() decorator and handles the execution by calling the underlying convert_mass_tool, formatting input/output as a dict.
    @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 intermediate unit.
    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 for supported mass units, used in tool schema validation.
    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",
    ]

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