convert_temperature
Convert temperature values between Celsius, Fahrenheit, and Kelvin units for accurate measurement translation.
Instructions
Convert temperature between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | Temperature value to convert | |
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit |
Implementation Reference
- src/unit_converter_mcp/server.py:62-76 (handler)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)