integral
Compute symbolic and numerical integrals to find antiderivatives or calculate areas under curves. Supports definite and indefinite integration with exact or approximate methods.
Instructions
Compute symbolic and numerical integrals (definite and indefinite).
Examples:
INDEFINITE INTEGRAL (antiderivative): expression="x^2", variable="x" Result: "x^3/3"
DEFINITE INTEGRAL (area): expression="x^2", variable="x", lower_bound=0, upper_bound=1 Result: 0.333
TRIGONOMETRIC: expression="sin(x)", variable="x", lower_bound=0, upper_bound=3.14159 Result: 2.0 (area under one period)
NUMERICAL METHOD (non-elementary): expression="exp(-x^2)", variable="x", lower_bound=0, upper_bound=1, method="numerical" Result: 0.746824 (Gaussian integral approximation)
SYMBOLIC ANTIDERIVATIVE: expression="1/x", variable="x" Result: "log(x)"
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification. | |
| output_mode | No | Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details. | full |
| expression | Yes | Mathematical expression to integrate (e.g., 'x^2', 'sin(x)') | |
| variable | Yes | Integration variable (e.g., 'x', 't') | |
| lower_bound | No | Lower bound for definite integral (omit for indefinite) | |
| upper_bound | No | Upper bound for definite integral (omit for indefinite) | |
| method | No | Integration method: symbolic=exact/analytical, numerical=approximate (requires bounds) | symbolic |
Input Schema (JSON Schema)
Implementation Reference
- The main handler function implementing symbolic (SymPy integrate) and numerical (SciPy quad) integration for definite and indefinite integrals. Handles metadata and formatting.async def integral( expression: Annotated[ str, Field( description="Mathematical expression to integrate (e.g., 'x^2', 'sin(x)')", min_length=1 ), ], variable: Annotated[ str, Field(description="Integration variable (e.g., 'x', 't')", min_length=1) ], lower_bound: Annotated[ float | None, Field(description="Lower bound for definite integral (omit for indefinite)") ] = None, upper_bound: Annotated[ float | None, Field(description="Upper bound for definite integral (omit for indefinite)") ] = None, method: Annotated[ Literal["symbolic", "numerical"], Field( description="Integration method: symbolic=exact/analytical, numerical=approximate (requires bounds)" ), ] = "symbolic", ) -> str: """Compute integrals using SymPy (symbolic/exact) or SciPy (numerical/approximate). Supports indefinite (antiderivatives) and definite (area) integrals.""" try: expr = sympify(expression) var = Symbol(variable) is_definite = lower_bound is not None and upper_bound is not None if method == "symbolic": if is_definite: # Definite symbolic integral result = integrate(expr, (var, lower_bound, upper_bound)) numeric_value = float(N(result)) metadata = { "expression": expression, "variable": variable, "lower_bound": lower_bound, "upper_bound": upper_bound, "symbolic_result": str(result), "type": "definite", } return format_result(numeric_value, metadata) else: # Indefinite symbolic integral result = integrate(expr, var) antiderivative_str = str(result) metadata = { "expression": expression, "variable": variable, "type": "indefinite", } return format_result(antiderivative_str, metadata) elif method == "numerical": if not is_definite: raise ValueError("Numerical integration requires lower_bound and upper_bound") # Convert SymPy expression to numeric function func = lambdify(var, expr, "numpy") # Use SciPy's quad for numerical integration result, error = integrate_numeric.quad(func, lower_bound, upper_bound) metadata = { "expression": expression, "variable": variable, "lower_bound": lower_bound, "upper_bound": upper_bound, "error_estimate": float(error), "method": "numerical", "type": "definite", } return format_result(float(result), metadata) else: raise ValueError(f"Unknown method: {method}") except Exception as e: raise ValueError( f"Integration failed: {str(e)}. " f"Example: expression='x^2', variable='x', lower_bound=0, upper_bound=1" )
- Pydantic-based schema definition via @mcp.tool decorator, including input parameters (expression, variable, bounds, method), detailed description with examples, and tool annotations.@mcp.tool( name="integral", description="""Compute symbolic and numerical integrals (definite and indefinite). Examples: INDEFINITE INTEGRAL (antiderivative): expression="x^2", variable="x" Result: "x^3/3" DEFINITE INTEGRAL (area): expression="x^2", variable="x", lower_bound=0, upper_bound=1 Result: 0.333 TRIGONOMETRIC: expression="sin(x)", variable="x", lower_bound=0, upper_bound=3.14159 Result: 2.0 (area under one period) NUMERICAL METHOD (non-elementary): expression="exp(-x^2)", variable="x", lower_bound=0, upper_bound=1, method="numerical" Result: 0.746824 (Gaussian integral approximation) SYMBOLIC ANTIDERIVATIVE: expression="1/x", variable="x" Result: "log(x)" """, annotations=ToolAnnotations( title="Integral Calculator", readOnlyHint=True, idempotentHint=True, ), )
- src/vibe_math_mcp/tools/calculus.py:95-125 (registration)Tool registration via the @mcp.tool decorator on the handler function.@mcp.tool( name="integral", description="""Compute symbolic and numerical integrals (definite and indefinite). Examples: INDEFINITE INTEGRAL (antiderivative): expression="x^2", variable="x" Result: "x^3/3" DEFINITE INTEGRAL (area): expression="x^2", variable="x", lower_bound=0, upper_bound=1 Result: 0.333 TRIGONOMETRIC: expression="sin(x)", variable="x", lower_bound=0, upper_bound=3.14159 Result: 2.0 (area under one period) NUMERICAL METHOD (non-elementary): expression="exp(-x^2)", variable="x", lower_bound=0, upper_bound=1, method="numerical" Result: 0.746824 (Gaussian integral approximation) SYMBOLIC ANTIDERIVATIVE: expression="1/x", variable="x" Result: "log(x)" """, annotations=ToolAnnotations( title="Integral Calculator", readOnlyHint=True, idempotentHint=True, ), )
- src/vibe_math_mcp/server.py:693-693 (registration)Import of the 'calculus' module in server.py, which triggers automatic registration of all decorated tools including 'integral' via MCP decorators.from .tools import array, basic, batch, calculus, financial, linalg, statistics # noqa: E402