validate_optimization_input
Validate input data for optimization problems to ensure correct format and identify errors before processing.
Instructions
Validate input data for optimization problems.
Args:
problem_type: Type of optimization problem
input_data: Input data to validate
Returns:
Validation result with errors, warnings, and suggestions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| problem_type | Yes | ||
| input_data | Yes |
Implementation Reference
- The main handler function for the 'validate_optimization_input' MCP tool. It dispatches to specific problem validators based on problem_type and returns a ValidationResult.@with_resource_limits(timeout_seconds=30.0, estimated_memory_mb=50.0) # type: ignore[arg-type] @mcp.tool() def validate_optimization_input( problem_type: str, input_data: dict[str, Any], ) -> dict[str, Any]: """Validate input data for optimization problems. Args: problem_type: Type of optimization problem input_data: Input data to validate Returns: Validation result with errors, warnings, and suggestions """ try: # Validate problem type try: prob_type = ProblemType(problem_type) except ValueError: return ValidationResult( is_valid=False, errors=[f"Unknown problem type: {problem_type}"], warnings=[], suggestions=[f"Supported types: {[t.value for t in ProblemType]}"], ).model_dump() # Route to appropriate validator if prob_type == ProblemType.LINEAR_PROGRAM: result = validate_linear_program(input_data) elif prob_type == ProblemType.INTEGER_PROGRAM: result = validate_linear_program(input_data) # Same validation as linear program elif prob_type == ProblemType.ASSIGNMENT: result = validate_assignment_problem(input_data) elif prob_type == ProblemType.TRANSPORTATION: result = validate_transportation_problem(input_data) elif prob_type == ProblemType.KNAPSACK: result = validate_knapsack_problem(input_data) elif prob_type in [ProblemType.TSP, ProblemType.VRP]: result = validate_routing_problem(input_data) elif prob_type in [ ProblemType.JOB_SCHEDULING, ProblemType.SHIFT_SCHEDULING, ]: result = validate_scheduling_problem(input_data) elif prob_type == ProblemType.PORTFOLIO: result = validate_portfolio_problem(input_data) elif prob_type == ProblemType.PRODUCTION_PLANNING: result = validate_production_problem(input_data) else: result = ValidationResult( is_valid=False, errors=[f"Validation not yet implemented for {problem_type}"], warnings=[], suggestions=["This problem type will be supported in future versions"], ) logger.info( f"Validated {problem_type} problem: {'valid' if result.is_valid else 'invalid'}" ) return result.model_dump() except Exception as e: logger.error(f"Validation error: {e}") return ValidationResult( is_valid=False, errors=[f"Validation failed: {str(e)}"], warnings=[], suggestions=["Check input data format and try again"], ).model_dump()
- src/mcp_optimizer/mcp_server.py:145-145 (registration)Invocation of register_validation_tools which adds the validation tools, including 'validate_optimization_input', to the FastMCP server instance.register_validation_tools(mcp)
- src/mcp_optimizer/tools/validation.py:522-596 (registration)The registration function that defines and registers the 'validate_optimization_input' tool using the @mcp.tool() decorator.def register_validation_tools(mcp: FastMCP[Any]) -> None: """Register validation tools with the MCP server.""" @with_resource_limits(timeout_seconds=30.0, estimated_memory_mb=50.0) # type: ignore[arg-type] @mcp.tool() def validate_optimization_input( problem_type: str, input_data: dict[str, Any], ) -> dict[str, Any]: """Validate input data for optimization problems. Args: problem_type: Type of optimization problem input_data: Input data to validate Returns: Validation result with errors, warnings, and suggestions """ try: # Validate problem type try: prob_type = ProblemType(problem_type) except ValueError: return ValidationResult( is_valid=False, errors=[f"Unknown problem type: {problem_type}"], warnings=[], suggestions=[f"Supported types: {[t.value for t in ProblemType]}"], ).model_dump() # Route to appropriate validator if prob_type == ProblemType.LINEAR_PROGRAM: result = validate_linear_program(input_data) elif prob_type == ProblemType.INTEGER_PROGRAM: result = validate_linear_program(input_data) # Same validation as linear program elif prob_type == ProblemType.ASSIGNMENT: result = validate_assignment_problem(input_data) elif prob_type == ProblemType.TRANSPORTATION: result = validate_transportation_problem(input_data) elif prob_type == ProblemType.KNAPSACK: result = validate_knapsack_problem(input_data) elif prob_type in [ProblemType.TSP, ProblemType.VRP]: result = validate_routing_problem(input_data) elif prob_type in [ ProblemType.JOB_SCHEDULING, ProblemType.SHIFT_SCHEDULING, ]: result = validate_scheduling_problem(input_data) elif prob_type == ProblemType.PORTFOLIO: result = validate_portfolio_problem(input_data) elif prob_type == ProblemType.PRODUCTION_PLANNING: result = validate_production_problem(input_data) else: result = ValidationResult( is_valid=False, errors=[f"Validation not yet implemented for {problem_type}"], warnings=[], suggestions=["This problem type will be supported in future versions"], ) logger.info( f"Validated {problem_type} problem: {'valid' if result.is_valid else 'invalid'}" ) return result.model_dump() except Exception as e: logger.error(f"Validation error: {e}") return ValidationResult( is_valid=False, errors=[f"Validation failed: {str(e)}"], warnings=[], suggestions=["Check input data format and try again"], ).model_dump() logger.info("Registered validation tools")