convert_volume
Convert volume measurements between units like liters, gallons, cubic meters, and fluid ounces. Input a value with source and target units for accurate conversion results.
Instructions
Convert volume between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | Volume value to convert | |
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit |
Implementation Reference
- src/unit_converter_mcp/server.py:232-246 (handler)MCP handler for the 'convert_volume' tool. Validates input using VOLUME_UNIT schema, calls convert_volume_tool for conversion, and returns formatted result.@app.tool() def convert_volume( value: Annotated[float, Field(description="Volume value to convert")], from_unit: Annotated[VOLUME_UNIT, Field(description="Source unit")], to_unit: Annotated[VOLUME_UNIT, Field(description="Target unit")], ) -> dict: """Convert volume between units.""" converted_value = convert_volume_tool(value, from_unit, to_unit) return { "original_value": value, "original_unit": from_unit, "converted_value": converted_value, "converted_unit": to_unit, "conversion_type": "volume", }
- Pydantic Literal type defining all supported volume units for input validation in the convert_volume tool.VOLUME_UNIT = Literal[ "acre foot", "barrel (oil)", "bushel (UK)", "bushel (US)", "bushel", "centiliter", "cubic centimeter", "cubic decimeter", "cubic foot", "cubic inch", "cubic kilometer", "cubic meter", "cubic mile", "cubic millimeter", "cubic yard", "cup", "deciliter", "fluid ounce (imperial)", "fluid ounce (US)", "fluid ounce", "gallon (imperial)", "gallon (US)", "gallon", "kiloliter", "liter", "milliliter", "microliter", "nanoliter", "picoliter", "pint (imperial)", "pint (US)", "pint", "quart (imperial)", "quart (US)", "quart", "tablespoon", "teaspoon", ]
- Core helper function implementing volume unit conversion by normalizing all units to liters using predefined conversion factors.def convert_volume_tool( value: float, from_unit: VOLUME_UNIT, to_unit: VOLUME_UNIT, ) -> float: """Convert volume between units.""" # Convert to liters first to_liters = { "acre foot": 1233481.83754752, "barrel (oil)": 158.987294928, "bushel (UK)": 36.36872, "bushel (US)": 35.23907016688, "bushel": 35.23907016688, # Default to US bushel "centiliter": 0.01, "cubic centimeter": 0.001, "cubic decimeter": 1.0, "cubic foot": 28.316846592, "cubic inch": 0.016387064, "cubic kilometer": 1_000_000_000_000.0, "cubic meter": 1000.0, "cubic mile": 4_168_181_825_000.0, "cubic millimeter": 1e-06, "cubic yard": 764.554857984, "cup": 0.2365882365, "deciliter": 0.1, "fluid ounce (imperial)": 0.0284130625, "fluid ounce (US)": 0.029573529562, "fluid ounce": 0.029573529562, # Default to US fluid ounce "gallon (imperial)": 4.54609, "gallon (US)": 3.785411784, "gallon": 3.785411784, # Default to US gallon "kiloliter": 1000.0, "liter": 1.0, "milliliter": 0.001, "microliter": 1e-06, "nanoliter": 1e-09, "picoliter": 1e-12, "pint (imperial)": 0.56826125, "pint (US)": 0.473176473, "pint": 0.473176473, # Default to US pint "quart (imperial)": 1.1365225, "quart (US)": 0.946352946, "quart": 0.946352946, # Default to US quart "tablespoon": 0.014786764781, "teaspoon": 0.004928921594, } liters = value * to_liters[from_unit] return liters / to_liters[to_unit]