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)}"
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It explains that the tool returns 'A key for the differentiated expression,' indicating it's a read operation that doesn't modify state, and mentions it uses SymPy's diff function, which implies mathematical computation. However, it lacks details on error handling, performance, or prerequisites like requiring expressions to be introduced first, which the example hints at but doesn't state explicitly.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded: it starts with a clear purpose statement, followed by parameter explanations, an example with multiple use cases, and a returns section. Every sentence adds value, such as the example illustrating first and second derivatives, making it efficient and well-structured without unnecessary details.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a mathematical differentiation tool with no annotations and no output schema, the description is mostly complete. It explains the tool's purpose, parameters, and provides an example with return values. However, it could improve by explicitly stating prerequisites (e.g., expressions must be introduced first) or error conditions, which are hinted at but not fully detailed, leaving minor gaps in context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate. It adds meaning beyond the schema by explaining each parameter: expr_key is 'The key of the expression (previously introduced) to differentiate,' var_name is 'The name of the variable to differentiate with respect to,' and order is 'The order of differentiation (default is 1 for first derivative).' This clarifies the purpose and usage of all three parameters, though it doesn't cover constraints like valid variable names or order ranges.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Differentiates an expression with respect to a variable using SymPy's diff function.' It specifies the verb ('differentiates'), the resource ('an expression'), and the method ('using SymPy's diff function'), which distinguishes it from sibling tools like integrate_expression or simplify_expression that perform other mathematical operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for usage through the example, showing how to use the tool in sequence with intro and introduce_expression. However, it does not explicitly state when to use this tool versus alternatives like calculate_gradient or dsolve_ode, which might be relevant for vector calculus or differential equations. The guidance is implied but not comprehensive.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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