convert_volume
Convert volume measurements between units such as liters, gallons, cubic meters, and more. Input a value, source unit, and target unit for accurate conversions.
Instructions
Convert volume between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit | |
| value | Yes | Volume value to convert |
Implementation Reference
- src/unit_converter_mcp/server.py:232-246 (handler)MCP tool handler for 'convert_volume'. Decorated with @app.tool(), performs the tool execution by calling the core convert_volume_tool and formatting the response.@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", }
- Schema definition for volume units as a Literal type, used for input validation in the tool parameters.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 implementation of the volume conversion logic, normalizing units to liters and performing the conversion.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]
- src/unit_converter_mcp/server.py:37-37 (registration)Import of the convert_volume_tool used by the handler.convert_volume_tool,