convert_length
Convert length measurements between units like meters, feet, miles, and nanometers. Enter a value with source and target units for accurate conversion results.
Instructions
Convert length between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | Length value to convert | |
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit |
Implementation Reference
- src/unit_converter_mcp/server.py:96-110 (handler)The @app.tool()-decorated function that implements the 'convert_length' MCP tool, handling input parameters, calling the conversion helper, and returning the formatted result.@app.tool() def convert_length( value: Annotated[float, Field(description="Length value to convert")], from_unit: Annotated[LENGTH_UNIT, Field(description="Source unit")], to_unit: Annotated[LENGTH_UNIT, Field(description="Target unit")], ) -> dict: """Convert length between units.""" converted_value = convert_length_tool(value, from_unit, to_unit) return { "original_value": value, "original_unit": from_unit, "converted_value": converted_value, "converted_unit": to_unit, "conversion_type": "length", }
- Literal type definition for supported LENGTH_UNIT units used in the tool's input schema.LENGTH_UNIT = Literal[ "angstrom", "astronomical unit", "cable", "centimeter", "chain (surveyors)", "decimeter", "em (pica)", "fathom", "foot", "foot (US survey)", "furlong", "hand", "hectometer", "inch", "kilometer", "light year", "meter", "micrometer", "mil", "mile", "nautical mile", "nautical mile (UK)", "millimeter", "nanometer", "parsec", "picometer", "Scandinavian mile", "thou", "yard", ]
- Core helper function that performs the actual length unit conversion by converting both units to meters as intermediate.def convert_length_tool( value: float, from_unit: LENGTH_UNIT, to_unit: LENGTH_UNIT, ) -> float: """Convert length between units.""" # Convert to meters first to_meters = { "angstrom": 1e-10, "astronomical unit": 149_598_550_000.0, "cable": 182.88, "centimeter": 0.01, "chain (surveyors)": 20.11684023368, "decimeter": 0.1, "em (pica)": 0.0042333, "fathom": 1.8288, "foot": 0.3048, "foot (US survey)": 0.304800609601, "furlong": 201.168, "hand": 0.1016, "hectometer": 100.0, "inch": 0.0254, "kilometer": 1000.0, "light year": 9_460_528_405_000_000.0, "meter": 1.0, "micrometer": 1e-06, "mil": 2.54e-05, "mile": 1609.344, "nautical mile": 1852.0, "nautical mile (UK)": 1853.184, "millimeter": 0.001, "nanometer": 1e-09, "parsec": 30_856_776_000_000_000.0, "picometer": 1e-12, "Scandinavian mile": 10_000.0, "thou": 2.54e-05, "yard": 0.9144, } meters = value * to_meters[from_unit] return meters / to_meters[to_unit]