Skip to main content
Glama
sdiehl
by sdiehl

differentiate_expression

Compute derivatives of mathematical expressions with respect to a specified variable using symbolic differentiation. Supports higher-order derivatives for advanced calculus and algebraic tasks.

Instructions

Differentiates an expression with respect to a variable using SymPy's diff function.

Args:
    expr_key: The key of the expression (previously introduced) to differentiate.
    var_name: The name of the variable to differentiate with respect to.
    order: The order of differentiation (default is 1 for first derivative).

Example:
    # Introduce a variable
    intro("x", [Assumption.REAL], [])

    # Create an expression to differentiate: x^3
    expr_key = introduce_expression("x**3")

    # First derivative
    first_deriv = differentiate_expression(expr_key, "x")
    # Returns 3x²

    # Second derivative
    second_deriv = differentiate_expression(expr_key, "x", 2)
    # Returns 6x

Returns:
    A key for the differentiated expression.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expr_keyYes
orderNo
var_nameYes

Implementation Reference

  • The core handler function for the 'differentiate_expression' MCP tool. It retrieves a stored SymPy expression by key, differentiates it with respect to a specified variable and order using sympy.diff, stores the result in the global expressions dictionary with a new key, and returns that key. The @mcp.tool() decorator registers this function as an MCP tool.
    @mcp.tool()
    def differentiate_expression(expr_key: str, var_name: str, order: int = 1) -> str:
        """Differentiates an expression with respect to a variable using SymPy's diff function.
    
        Args:
            expr_key: The key of the expression (previously introduced) to differentiate.
            var_name: The name of the variable to differentiate with respect to.
            order: The order of differentiation (default is 1 for first derivative).
    
        Example:
            # Introduce a variable
            intro("x", [Assumption.REAL], [])
    
            # Create an expression to differentiate: x^3
            expr_key = introduce_expression("x**3")
    
            # First derivative
            first_deriv = differentiate_expression(expr_key, "x")
            # Returns 3x²
    
            # Second derivative
            second_deriv = differentiate_expression(expr_key, "x", 2)
            # Returns 6x
    
        Returns:
            A key for the differentiated expression.
        """
        global expression_counter
    
        if expr_key not in expressions:
            return f"Error: Expression with key '{expr_key}' not found."
    
        if var_name not in local_vars:
            return f"Error: Variable '{var_name}' not found. Please introduce it first."
    
        if order < 1:
            return "Error: Order of differentiation must be at least 1."
    
        try:
            expr = expressions[expr_key]
            var = local_vars[var_name]
    
            result = diff(expr, var, order)
    
            result_key = f"expr_{expression_counter}"
            expressions[result_key] = result
            expression_counter += 1
    
            return result_key
        except Exception as e:
            return f"Error during differentiation: {str(e)}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sdiehl/sympy-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server