validate_formula
Check mathematical formula syntax for errors and receive guidance on corrections to ensure accurate plotting and computation.
Instructions
Validates the syntax of a mathematical formula, offering intelligent help on failure.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| formula | Yes |
Implementation Reference
- src/main.py:81-101 (handler)The main handler function for the 'validate_formula' tool. It is registered via the @mcp.tool() decorator. Validates mathematical formulas using sympy, provides success message or user-friendly error explanation using LLM sampling on failure.@mcp.tool() async def validate_formula(ctx: MCPContext, formula: str) -> str: """Validates the syntax of a mathematical formula, offering intelligent help on failure.""" try: sympy.sympify(formula) return f"Formula '{formula}' is syntactically valid." except (sympy.SympifyError, SyntaxError) as e: technical_error = f"Details: {e}" ctx.info(f"Validation failed with technical error: {technical_error}") # Use LLM Sampling to get a user-friendly explanation try: sample_result = await ctx.sample(f"Please explain this Python sympy error in simple terms for a non-programmer: {technical_error}") user_friendly_error = sample_result.text except Exception as sample_err: ctx.error(f"LLM Sampling failed: {sample_err}") user_friendly_error = technical_error # Fallback to technical error # Elicit user confirmation with the friendly error return f"The formula '{formula}' is invalid. Here is an explanation: {user_friendly_error}"