convert_batch
Process multiple unit conversions in one batch request. Submit values, source and target units, and conversion types (temperature, length, mass, etc.) for structured results, including converted values or error messages.
Instructions
Perform multiple unit conversions in a single batch request.
Each request in the batch should contain:
value: The numeric value to convert
from_unit: Source unit for conversion
to_unit: Target unit for conversion
conversion_type: Type of conversion (temperature, length, mass, etc.)
request_id: Optional identifier for tracking individual requests
Returns a structured response with individual results for each conversion, including success/failure status and either converted values or error messages.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| requests | Yes | List of conversion requests. Each request should contain: value (float), from_unit (str), to_unit (str), conversion_type (str), and optionally request_id (str) |
Implementation Reference
- src/unit_converter_mcp/server.py:300-335 (handler)Registered MCP tool handler for 'convert_batch'. Processes batch requests by delegating to convert_batch_tool, adds summary statistics, and formats the response.@app.tool() def convert_batch( requests: Annotated[ list[dict], Field( description="List of conversion requests. Each request should contain: value (float), from_unit (str), to_unit (str), conversion_type (str), and optionally request_id (str)" ), ], ) -> dict: """Perform multiple unit conversions in a single batch request. Each request in the batch should contain: - value: The numeric value to convert - from_unit: Source unit for conversion - to_unit: Target unit for conversion - conversion_type: Type of conversion (temperature, length, mass, etc.) - request_id: Optional identifier for tracking individual requests Returns a structured response with individual results for each conversion, including success/failure status and either converted values or error messages. """ results = convert_batch_tool(requests) # Count successful and failed conversions successful = sum(1 for result in results if result["success"]) failed = len(results) - successful return { "batch_results": results, "summary": { "total_requests": len(requests), "successful_conversions": successful, "failed_conversions": failed, }, }
- Core batch conversion handler: validates each request, dispatches to specific unit converters via CONVERSION_FUNCTIONS mapping, handles errors, and produces result dictionaries.def convert_batch_tool(requests: list[dict[str, Any]]) -> list[dict[str, Any]]: """Perform batch unit conversions. Args: requests: List of conversion request dictionaries, each containing: - value: float - The numeric value to convert - from_unit: str - Source unit - to_unit: str - Target unit - conversion_type: str - Type of conversion - request_id: str (optional) - Identifier for tracking Returns: List of conversion result dictionaries with success/failure status and either converted values or error messages. """ results: list[dict[str, Any]] = [] for request_data in requests: try: # Validate required fields first value = request_data.get("value") from_unit = request_data.get("from_unit") to_unit = request_data.get("to_unit") conversion_type = request_data.get("conversion_type") request_id = request_data.get("request_id") if value is None: raise ValueError("Missing required field: value") if not from_unit: raise ValueError("Missing required field: from_unit") if not to_unit: raise ValueError("Missing required field: to_unit") if not conversion_type: raise ValueError("Missing required field: conversion_type") # Create request object with validated data request = BatchConversionRequest( value=float(value), from_unit=str(from_unit), to_unit=str(to_unit), conversion_type=conversion_type, request_id=request_id, ) # Validate conversion type if request.conversion_type not in CONVERSION_FUNCTIONS: raise ValueError( f"Unsupported conversion type: {request.conversion_type}" ) # Perform the conversion conversion_func = CONVERSION_FUNCTIONS[request.conversion_type] converted_value = conversion_func( request.value, request.from_unit, request.to_unit ) # Create successful result result = BatchConversionResult( request_id=request.request_id, success=True, original_value=request.value, original_unit=request.from_unit, converted_value=converted_value, converted_unit=request.to_unit, conversion_type=request.conversion_type, ) except Exception as e: # Create error result request_id = request_data.get("request_id", f"error_{len(results)}") result = BatchConversionResult( request_id=request_id, success=False, error_message=str(e), ) results.append(result.to_dict()) return results
- Literal type defining valid conversion_type values for batch requests.CONVERSION_TYPE = Literal[ "angle", "area", "computer_data", "density", "energy", "force", "length", "mass", "power", "pressure", "speed", "temperature", "time", "volume", ]
- Dictionary mapping conversion types to their respective single-conversion handler functions, used for dynamic dispatch in batch processing.CONVERSION_FUNCTIONS: dict[str, Callable[..., float]] = { "angle": convert_angle_tool, "area": convert_area_tool, "computer_data": convert_computer_data_tool, "density": convert_density_tool, "energy": convert_energy_tool, "force": convert_force_tool, "length": convert_length_tool, "mass": convert_mass_tool, "power": convert_power_tool, "pressure": convert_pressure_tool, "speed": convert_speed_tool, "temperature": convert_temperature_tool, "time": convert_time_tool, "volume": convert_volume_tool, }
- Helper class for structuring individual batch conversion results, with to_dict method for serialization.class BatchConversionResult: """Represents the result of a single conversion within a batch.""" def __init__( self, request_id: str, success: bool, original_value: float | None = None, original_unit: str | None = None, converted_value: float | None = None, converted_unit: str | None = None, conversion_type: str | None = None, error_message: str | None = None, ): """Initialize a batch conversion result. Args: request_id: Identifier matching the original request success: Whether the conversion succeeded original_value: Original value that was converted original_unit: Original unit converted_value: Result of conversion (if successful) converted_unit: Target unit (if successful) conversion_type: Type of conversion performed error_message: Error details (if failed) """ self.request_id = request_id self.success = success self.original_value = original_value self.original_unit = original_unit self.converted_value = converted_value self.converted_unit = converted_unit self.conversion_type = conversion_type self.error_message = error_message def to_dict(self) -> dict[str, Any]: """Convert result to dictionary format matching individual tool responses.""" result = { "request_id": self.request_id, "success": self.success, } if self.success: result.update( { "original_value": self.original_value, "original_unit": self.original_unit, "converted_value": self.converted_value, "converted_unit": self.converted_unit, "conversion_type": self.conversion_type, } ) else: result["error"] = self.error_message return result