Skip to main content
Glama
sdiehl
by sdiehl

calculate_curl

Compute the curl of a vector field in a specified coordinate system using SymPy. Input a vector field key to generate a curl expression for analyzing rotational behavior in vector calculus.

Instructions

Calculates the curl of a vector field using SymPy's curl function.

Args:
    vector_field_key: The key of the vector field expression.

Example:
    # First create a coordinate system
    create_coordinate_system("R")

    # Create a vector field F = (y, -x, 0)
    vector_field = create_vector_field("R", "R_y", "-R_x", "0")

    # Calculate curl
    curl_result = calculate_curl(vector_field)
    # Returns (0, 0, -2)

Returns:
    A key for the curl expression.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
vector_field_keyYes

Implementation Reference

  • The core handler function for the 'calculate_curl' MCP tool. It retrieves a stored vector field by key, computes its curl using SymPy's vector.curl function, stores the resulting vector field expression with a new auto-generated key in the global 'expressions' dictionary, and returns that key. The @mcp.tool() decorator registers this function as an MCP tool.
    @mcp.tool()
    def calculate_curl(vector_field_key: str) -> str:
        """Calculates the curl of a vector field using SymPy's curl function.
    
        Args:
            vector_field_key: The key of the vector field expression.
    
        Example:
            # First create a coordinate system
            create_coordinate_system("R")
    
            # Create a vector field F = (y, -x, 0)
            vector_field = create_vector_field("R", "R_y", "-R_x", "0")
    
            # Calculate curl
            curl_result = calculate_curl(vector_field)
            # Returns (0, 0, -2)
    
        Returns:
            A key for the curl expression.
        """
        global expression_counter
    
        if vector_field_key not in expressions:
            return f"Error: Vector field with key '{vector_field_key}' not found."
    
        try:
            vector_field = expressions[vector_field_key]
    
            # Calculate curl
            curl_result = curl(vector_field)
    
            # Store the result
            result_key = f"vector_{expression_counter}"
            expressions[result_key] = curl_result
            expression_counter += 1
    
            return result_key
        except Exception as e:
            return f"Error calculating curl: {str(e)}"
  • Helper tool to create a SymPy 3D coordinate system (CoordSys3D), required before creating vector fields for curl computation.
    def create_coordinate_system(name: str, coord_names: Optional[List[str]] = None) -> str:
        """Creates a 3D coordinate system for vector calculus operations.
    
        Args:
            name: The name for the coordinate system.
            coord_names: Optional list of coordinate names (3 names for x, y, z).
                        If not provided, defaults to [name+'_x', name+'_y', name+'_z'].
    
        Example:
            # Create a coordinate system
            coord_sys = create_coordinate_system("R")
            # Creates a coordinate system R with coordinates R_x, R_y, R_z
    
            # Create a coordinate system with custom coordinate names
            coord_sys = create_coordinate_system("C", ["rho", "phi", "z"])
    
        Returns:
            The name of the created coordinate system.
        """
        if name in coordinate_systems:
            return f"Warning: Overwriting existing coordinate system '{name}'."
    
        try:
            if coord_names and len(coord_names) != 3:
                return "Error: coord_names must contain exactly 3 names for x, y, z coordinates."
    
            if coord_names:
                # Create a CoordSys3D with custom coordinate names
                cs = CoordSys3D(name, variable_names=coord_names)
            else:
                # Create a CoordSys3D with default coordinate naming
                cs = CoordSys3D(name)
    
            coordinate_systems[name] = cs
    
            # Add the coordinate system to the expressions dict to make it accessible
            # in expressions through parsing
            expressions[name] = cs
    
            # Add the coordinate variables to local_vars for easier access
            for i, base_vector in enumerate(cs.base_vectors()):
                vector_name = (
                    f"{name}_{['x', 'y', 'z'][i]}"
                    if not coord_names
                    else f"{name}_{coord_names[i]}"
                )
                local_vars[vector_name] = base_vector
    
            return name
        except Exception as e:
            return f"Error creating coordinate system: {str(e)}"
  • Helper tool to create a vector field expression in a coordinate system by parsing string components and combining with base vectors, prerequisite for calculate_curl.
    @mcp.tool()
    def create_vector_field(
        coord_sys_name: str, component_x: str, component_y: str, component_z: str
    ) -> str:
        """Creates a vector field in the specified coordinate system.
    
        Args:
            coord_sys_name: The name of the coordinate system to use.
            component_x: String expression for the x-component of the vector field.
            component_y: String expression for the y-component of the vector field.
            component_z: String expression for the z-component of the vector field.
    
        Example:
            # First create a coordinate system
            create_coordinate_system("R")
    
            # Create a vector field F = (y, -x, z)
            vector_field = create_vector_field("R", "R_y", "-R_x", "R_z")
    
        Returns:
            A key for the vector field expression.
        """
        global expression_counter
    
        if coord_sys_name not in coordinate_systems:
            return f"Error: Coordinate system '{coord_sys_name}' not found. Create it first using create_coordinate_system."
    
        try:
            cs = coordinate_systems[coord_sys_name]
    
            # Parse the component expressions
            parse_dict = {**local_vars, **functions, coord_sys_name: cs}
            x_comp = parse_expr(component_x, local_dict=parse_dict)
            y_comp = parse_expr(component_y, local_dict=parse_dict)
            z_comp = parse_expr(component_z, local_dict=parse_dict)
    
            # Create the vector field
            vector_field = (
                x_comp * cs.base_vectors()[0]
                + y_comp * cs.base_vectors()[1]
                + z_comp * cs.base_vectors()[2]
            )
    
            # Store the vector field
            result_key = f"vector_{expression_counter}"
            expressions[result_key] = vector_field
            expression_counter += 1
    
            return result_key
        except Exception as e:
            return f"Error creating vector field: {str(e)}"
  • server.py:1186-1226 (registration)
    The @mcp.tool() decorator on calculate_curl registers it in the FastMCP server instance 'mcp'.
    @mcp.tool()
    def calculate_curl(vector_field_key: str) -> str:
        """Calculates the curl of a vector field using SymPy's curl function.
    
        Args:
            vector_field_key: The key of the vector field expression.
    
        Example:
            # First create a coordinate system
            create_coordinate_system("R")
    
            # Create a vector field F = (y, -x, 0)
            vector_field = create_vector_field("R", "R_y", "-R_x", "0")
    
            # Calculate curl
            curl_result = calculate_curl(vector_field)
            # Returns (0, 0, -2)
    
        Returns:
            A key for the curl expression.
        """
        global expression_counter
    
        if vector_field_key not in expressions:
            return f"Error: Vector field with key '{vector_field_key}' not found."
    
        try:
            vector_field = expressions[vector_field_key]
    
            # Calculate curl
            curl_result = curl(vector_field)
    
            # Store the result
            result_key = f"vector_{expression_counter}"
            expressions[result_key] = curl_result
            expression_counter += 1
    
            return result_key
        except Exception as e:
            return f"Error calculating curl: {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