Skip to main content
Glama

calculate_gradient

Compute the gradient of a scalar field to obtain its vector field representation, enabling advanced symbolic algebra operations with precise mathematical results.

Instructions

Calculates the gradient of a scalar field using SymPy's gradient function.

Args: scalar_field_key: The key of the scalar field expression. Example: # First create a coordinate system create_coordinate_system("R") # Create a scalar field f = x^2 + y^2 + z^2 scalar_field = introduce_expression("R_x**2 + R_y**2 + R_z**2") # Calculate gradient grad_result = calculate_gradient(scalar_field) # Returns (2x, 2y, 2z) Returns: A key for the gradient vector field expression.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scalar_field_keyYes

Implementation Reference

  • The core handler implementation for the 'calculate_gradient' MCP tool. It retrieves a scalar field expression by key, computes its gradient using SymPy's vector.gradient function (which requires a coordinate system context from prior tool calls), stores the resulting vector field with a generated key, and returns that key. The @mcp.tool() decorator registers it as an MCP tool.
    @mcp.tool() def calculate_gradient(scalar_field_key: str) -> str: """Calculates the gradient of a scalar field using SymPy's gradient function. Args: scalar_field_key: The key of the scalar field expression. Example: # First create a coordinate system create_coordinate_system("R") # Create a scalar field f = x^2 + y^2 + z^2 scalar_field = introduce_expression("R_x**2 + R_y**2 + R_z**2") # Calculate gradient grad_result = calculate_gradient(scalar_field) # Returns (2x, 2y, 2z) Returns: A key for the gradient vector field expression. """ global expression_counter if scalar_field_key not in expressions: return f"Error: Scalar field with key '{scalar_field_key}' not found." try: scalar_field = expressions[scalar_field_key] # Calculate gradient grad_result = gradient(scalar_field) # Store the result result_key = f"vector_{expression_counter}" expressions[result_key] = grad_result expression_counter += 1 return result_key except Exception as e: return f"Error calculating gradient: {str(e)}"
  • server.py:1270-1270 (registration)
    The @mcp.tool() decorator registers the calculate_gradient function as an MCP tool.
    @mcp.tool()
  • Unit test demonstrating usage of calculate_gradient tool.
    def test_calculate_gradient(self): # Create coordinate system create_coordinate_system("R") # Introduce variables intro("x", [Assumption.REAL], []) intro("y", [Assumption.REAL], []) intro("z", [Assumption.REAL], []) # Create a simple scalar field scalar_field_key = introduce_expression("x**2 + y**2 + z**2") # Calculate gradient grad_key = calculate_gradient(scalar_field_key) # Check if gradient calculation was successful if "error" not in grad_key.lower(): assert grad_key.startswith("vector_") else: assert False, f"Failed to calculate gradient: {grad_key}"

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