convert_length
Convert length measurements between units like meters, feet, miles, and more. Simplify unit conversions for practical use with accurate results.
Instructions
Convert length between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit | |
| value | Yes | Length value to convert |
Implementation Reference
- src/unit_converter_mcp/server.py:96-110 (handler)MCP tool handler 'convert_length' decorated with @app.tool(), which registers the tool and implements the logic by calling convert_length_tool and returning a structured response with original and converted values.@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", }
- LENGTH_UNIT Literal type defining all supported length units, used in the tool schema for input validation with Pydantic Field and Annotated.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 conversion function 'convert_length_tool' that implements the length unit conversion by first converting the input to meters using predefined conversion factors, then converting to the target unit.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]