convert_density
Convert density values between multiple units such as grains per gallon, grams per cubic centimeter, kilograms per cubic meter, and more for accurate measurement adjustments.
Instructions
Convert density between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit | |
| value | Yes | Density value to convert |
Implementation Reference
- src/unit_converter_mcp/server.py:266-280 (handler)MCP tool registration and handler for 'convert_density'. Uses @app.tool() decorator, validates inputs with DENSITY_UNIT schema, calls core logic, and returns formatted response.@app.tool() def convert_density( value: Annotated[float, Field(description="Density value to convert")], from_unit: Annotated[DENSITY_UNIT, Field(description="Source unit")], to_unit: Annotated[DENSITY_UNIT, Field(description="Target unit")], ) -> dict: """Convert density between units.""" converted_value = convert_density_tool(value, from_unit, to_unit) return { "original_value": value, "original_unit": from_unit, "converted_value": converted_value, "converted_unit": to_unit, "conversion_type": "density", }
- Pydantic Literal type defining all supported input/output units for density conversions.DENSITY_UNIT = Literal[ "grains per gallon (UK)", "grains per gallon (US)", "grains per gallon", "grams per cubic centimeter", "grams per liter", "kilograms per liter", "kilograms per cubic meter", "milligrams per liter", "ounces per gallon (UK)", "ounces per gallon (US)", "ounces per gallon", "pounds per cubic foot", "pounds per gallon (UK)", "pounds per gallon (US)", "pounds per gallon", "tonnes per cubic meter", "tons per cubic yard (UK)", "tons per cubic yard (US)", "tons per cubic yard", ]
- Core helper function implementing the density unit conversion logic by normalizing to kilograms per liter base unit.def convert_density_tool( value: float, from_unit: DENSITY_UNIT, to_unit: DENSITY_UNIT, ) -> float: """Convert density between units.""" # Convert to kilograms per liter first to_kilograms_per_liter = { # grain‑based hardness units "grains per gallon (UK)": 1.4253948e-05, "grains per gallon (US)": 1.7118012e-05, "grains per gallon": 1.7118012e-05, # Default to US grains per gallon # metric staples "grams per cubic centimeter": 1.0, "grams per liter": 0.001, "kilograms per liter": 1.0, "kilograms per cubic meter": 0.001, "milligrams per liter": 1e-06, # fluid‑ounce units "ounces per gallon (UK)": 0.006236023, "ounces per gallon (US)": 0.007489152, "ounces per gallon": 0.007489152, # Default to US ounces per gallon # pound‑based units "pounds per cubic foot": 0.016018463, "pounds per gallon (UK)": 0.099776373, "pounds per gallon (US)": 0.119826427, "pounds per gallon": 0.119826427, # Default to US pounds per gallon # tonne/ton bulk‑density units "tonnes per cubic meter": 1.0, "tons per cubic yard (UK)": 1.328939184, "tons per cubic yard (US)": 1.186552843, "tons per cubic yard": 1.186552843, # Default to US tons per cubic yard } kilograms_per_liter = value * to_kilograms_per_liter[from_unit] return kilograms_per_liter / to_kilograms_per_liter[to_unit]
- src/unit_converter_mcp/server.py:8-38 (registration)Imports the density schema (DENSITY_UNIT) and core helper (convert_density_tool) into the server module for use in the MCP tool handler.from .tools import ( ANGLE_UNIT, AREA_UNIT, COMPUTER_DATA_UNIT, DENSITY_UNIT, ENERGY_UNIT, FORCE_UNIT, LENGTH_UNIT, MASS_UNIT, POWER_UNIT, PRESSURE_UNIT, SPEED_UNIT, TEMPERATURE_UNIT, TIME_UNIT, VOLUME_UNIT, convert_angle_tool, convert_area_tool, convert_batch_tool, convert_computer_data_tool, convert_density_tool, convert_energy_tool, convert_force_tool, convert_length_tool, convert_mass_tool, convert_power_tool, convert_pressure_tool, convert_speed_tool, convert_temperature_tool, convert_time_tool, convert_volume_tool, )