derivative
Compute symbolic and numerical derivatives for mathematical expressions, including higher-order and partial derivatives, with optional point evaluation.
Instructions
Compute symbolic and numerical derivatives with support for higher orders and partial derivatives.
Examples:
FIRST DERIVATIVE: expression="x^3 + 2x^2", variable="x", order=1 Result: derivative="3x^2 + 4*x"
SECOND DERIVATIVE (acceleration/concavity): expression="x^3", variable="x", order=2 Result: derivative="6*x"
EVALUATE AT POINT: expression="sin(x)", variable="x", order=1, point=0 Result: derivative="cos(x)", value_at_point=1.0
PRODUCT RULE: expression="sin(x)*cos(x)", variable="x", order=1 Result: derivative="cos(x)^2 - sin(x)^2"
PARTIAL DERIVATIVE: expression="x^2*y", variable="y", order=1 Result: derivative="x^2" (treating x as constant)
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 differentiate (e.g., 'x^3 + 2*x^2', 'sin(x)') | |
| variable | Yes | Variable to differentiate with respect to (e.g., 'x', 't') | |
| order | No | Derivative order (1=first derivative, 2=second, etc.) | |
| point | No | Optional point for numerical evaluation of the derivative |
Input Schema (JSON Schema)
Implementation Reference
- The complete 'derivative' tool implementation: @mcp.tool decorator registers the tool with schema and description; async handler uses SymPy (sympify, Symbol, diff, N, subs) to compute symbolic derivative of specified order w.r.t. variable, optionally evaluates numerically at point, returns formatted string with metadata.@mcp.tool( name="derivative", description="""Compute symbolic and numerical derivatives with support for higher orders and partial derivatives. Examples: FIRST DERIVATIVE: expression="x^3 + 2*x^2", variable="x", order=1 Result: derivative="3*x^2 + 4*x" SECOND DERIVATIVE (acceleration/concavity): expression="x^3", variable="x", order=2 Result: derivative="6*x" EVALUATE AT POINT: expression="sin(x)", variable="x", order=1, point=0 Result: derivative="cos(x)", value_at_point=1.0 PRODUCT RULE: expression="sin(x)*cos(x)", variable="x", order=1 Result: derivative="cos(x)^2 - sin(x)^2" PARTIAL DERIVATIVE: expression="x^2*y", variable="y", order=1 Result: derivative="x^2" (treating x as constant)""", annotations=ToolAnnotations( title="Derivative Calculator", readOnlyHint=True, idempotentHint=True, ), ) async def derivative( expression: Annotated[ str, Field( description="Mathematical expression to differentiate (e.g., 'x^3 + 2*x^2', 'sin(x)')", min_length=1, ), ], variable: Annotated[ str, Field( description="Variable to differentiate with respect to (e.g., 'x', 't')", min_length=1 ), ], order: Annotated[ int, Field(description="Derivative order (1=first derivative, 2=second, etc.)", ge=1) ] = 1, point: Annotated[ float | None, Field(description="Optional point for numerical evaluation of the derivative") ] = None, ) -> str: """Compute symbolic derivatives using SymPy. Supports higher orders and partial derivatives. Optional numerical evaluation at a point.""" try: expr = sympify(expression) var = Symbol(variable) # Compute derivative derivative_expr = diff(expr, var, order) derivative_str = str(derivative_expr) # Build metadata metadata = { "expression": expression, "variable": variable, "order": order, } # Evaluate at point if provided if point is not None: value = float(N(derivative_expr.subs(var, point))) metadata["value_at_point"] = value metadata["point"] = point return format_result(derivative_str, metadata) except Exception as e: raise ValueError( f"Derivative calculation failed: {str(e)}. Example: expression='x^2', variable='x'" )
- src/vibe_math_mcp/tools/__init__.py:10-10 (registration)Imports the calculus.py module, triggering registration of the 'derivative' tool via its @mcp.tool decorator.from . import calculus
- src/vibe_math_mcp/server.py:693-693 (registration)Imports tools.calculus in server.py, ensuring the 'derivative' tool is registered on the MCP server instance.from .tools import array, basic, batch, calculus, financial, linalg, statistics # noqa: E402