convert_angle
Convert angle measurements between degrees, radians, arcmin, arcsec, turns, and gons for mathematical and engineering calculations.
Instructions
Convert angle between units.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | Yes | Angle value to convert | |
| from_unit | Yes | Source unit | |
| to_unit | Yes | Target unit |
Implementation Reference
- Core handler function that executes the angle unit conversion logic using intermediate degrees conversion.def convert_angle_tool( value: float, from_unit: ANGLE_UNIT, to_unit: ANGLE_UNIT, ) -> float: """Convert angle between units.""" # Dictionary mapping units to their conversion functions to degrees to_degrees: dict[ANGLE_UNIT, Callable[[float], float]] = { "radians": _radians_to_degrees, "arcmin": _arcmin_to_degrees, "arcsec": _arcsec_to_degrees, "turns": _turns_to_degrees, "gons": _gons_to_degrees, "degrees": lambda x: x, } # Dictionary mapping units to their conversion functions from degrees from_degrees: dict[ANGLE_UNIT, Callable[[float], float]] = { "radians": _degrees_to_radians, "arcmin": _degrees_to_arcmin, "arcsec": _degrees_to_arcsec, "turns": _degrees_to_turns, "gons": _degrees_to_gons, "degrees": lambda x: x, } # Convert to degrees first degrees = to_degrees[from_unit](value) # Convert from degrees to target unit return from_degrees[to_unit](degrees)
- src/unit_converter_mcp/server.py:79-93 (handler)MCP tool handler for 'convert_angle', defines parameters with descriptions (schema), calls core logic, and returns formatted response.@app.tool() def convert_angle( value: Annotated[float, Field(description="Angle value to convert")], from_unit: Annotated[ANGLE_UNIT, Field(description="Source unit")], to_unit: Annotated[ANGLE_UNIT, Field(description="Target unit")], ) -> dict: """Convert angle between units.""" converted_value = convert_angle_tool(value, from_unit, to_unit) return { "original_value": value, "original_unit": from_unit, "converted_value": converted_value, "converted_unit": to_unit, "conversion_type": "angle", }
- Type schema definition for valid angle units used in tool parameters.ANGLE_UNIT = Literal["degrees", "radians", "arcmin", "arcsec", "turns", "gons"]
- src/unit_converter_mcp/server.py:8-38 (registration)Imports the convert_angle_tool and ANGLE_UNIT for use in tool registration.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, )
- Supporting helper functions for individual unit conversions to/from degrees.def _radians_to_degrees(value: float) -> float: """Convert radians to degrees.""" return value * 180.0 / math.pi def _degrees_to_radians(value: float) -> float: """Convert degrees to radians.""" return value * math.pi / 180.0 def _arcmin_to_degrees(value: float) -> float: """Convert arc-minutes to degrees.""" return value / 60.0 def _degrees_to_arcmin(value: float) -> float: """Convert degrees to arc-minutes.""" return value * 60.0 def _arcsec_to_degrees(value: float) -> float: """Convert arc-seconds to degrees.""" return value / 3_600.0 def _degrees_to_arcsec(value: float) -> float: """Convert degrees to arc-seconds.""" return value * 3_600.0 def _turns_to_degrees(value: float) -> float: """Convert turns to degrees.""" return value * 360.0 def _degrees_to_turns(value: float) -> float: """Convert degrees to turns.""" return value / 360.0 def _gons_to_degrees(value: float) -> float: """Convert gons to degrees.""" return value * (9.0 / 10.0) def _degrees_to_gons(value: float) -> float: """Convert degrees to gons.""" return value * (10.0 / 9.0)