Skip to main content
Glama
zazencodes

Unit Converter MCP

by zazencodes

convert_temperature

Convert temperature values between Celsius, Fahrenheit, and Kelvin units for accurate measurement translation.

Instructions

Convert temperature between units.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
valueYesTemperature value to convert
from_unitYesSource unit
to_unitYesTarget unit

Implementation Reference

  • MCP tool handler registered with @app.tool() that executes the temperature conversion by calling the core tool function and formats the response dictionary.
    @app.tool()
    def convert_temperature(
        value: Annotated[float, Field(description="Temperature value to convert")],
        from_unit: Annotated[TEMPERATURE_UNIT, Field(description="Source unit")],
        to_unit: Annotated[TEMPERATURE_UNIT, Field(description="Target unit")],
    ) -> dict:
        """Convert temperature between units."""
        converted_value = convert_temperature_tool(value, from_unit, to_unit)
        return {
            "original_value": value,
            "original_unit": from_unit,
            "converted_value": converted_value,
            "converted_unit": to_unit,
            "conversion_type": "temperature",
        }
  • Literal type definition for supported temperature units, used in the tool schema for input validation.
    TEMPERATURE_UNIT = Literal["celsius", "fahrenheit", "kelvin"]
  • Core helper function implementing the temperature unit conversion logic via intermediate conversion to Celsius.
    def convert_temperature_tool(
        value: float,
        from_unit: TEMPERATURE_UNIT,
        to_unit: TEMPERATURE_UNIT,
    ) -> float:
        """Convert temperature between units."""
    
        # Dictionary mapping units to their conversion functions to Celsius
        to_celsius: dict[TEMPERATURE_UNIT, Callable[[float], float]] = {
            "fahrenheit": _fahrenheit_to_celsius,
            "kelvin": _kelvin_to_celsius,
            "celsius": lambda x: x,
        }
    
        # Dictionary mapping units to their conversion functions from Celsius
        from_celsius: dict[TEMPERATURE_UNIT, Callable[[float], float]] = {
            "fahrenheit": _celsius_to_fahrenheit,
            "kelvin": _celsius_to_kelvin,
            "celsius": lambda x: x,
        }
    
        # Convert to Celsius first
        celsius = to_celsius[from_unit](value)
    
        # Convert from Celsius to target unit
        return from_celsius[to_unit](celsius)

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