Skip to main content
Glama

create_custom_metric

Generate custom metric tensors using specified components and symbols for symbolic algebra tasks. Ideal for advanced mathematical modeling and analysis.

Instructions

Creates a custom metric tensor from provided components and symbols.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
componentsYes
configNoll
symbolsYes

Implementation Reference

  • The main handler function for the 'create_custom_metric' MCP tool. It parses the provided matrix components and symbols, creates a MetricTensor object using einsteinpy.symbolic, stores it globally, and returns a unique key.
    @mcp.tool() def create_custom_metric( components: List[List[str]], symbols: List[str], config: Literal["ll", "uu"] = "ll", ) -> str: """Creates a custom metric tensor from provided components and symbols. Args: components: A matrix of symbolic expressions as strings representing metric components. symbols: A list of symbol names used in the components. config: The tensor configuration - "ll" for covariant (lower indices) or "uu" for contravariant (upper indices). Returns: A key for the stored metric object. """ global expression_counter try: # Parse symbols sympy_symbols = sympy.symbols(", ".join(symbols)) sympy_symbols_dict = {str(sym): sym for sym in sympy_symbols} # Convert components to sympy expressions sympy_components = [] for row in components: sympy_row = [] for expr_str in row: if expr_str == "0": sympy_row.append(0) else: expr = parse_expr(expr_str, local_dict=sympy_symbols_dict) sympy_row.append(expr) sympy_components.append(sympy_row) # Create metric tensor metric_obj = MetricTensor(sympy_components, sympy_symbols, config=config) # Store the metric metric_key = f"metric_custom_{expression_counter}" metrics[metric_key] = metric_obj expressions[metric_key] = metric_obj.tensor() expression_counter += 1 return metric_key except Exception as e: return f"Error creating custom metric: {str(e)}"
  • server.py:816-862 (registration)
    The @mcp.tool() decorator registers this function as an MCP tool named 'create_custom_metric'.
    @mcp.tool() def create_custom_metric( components: List[List[str]], symbols: List[str], config: Literal["ll", "uu"] = "ll", ) -> str: """Creates a custom metric tensor from provided components and symbols. Args: components: A matrix of symbolic expressions as strings representing metric components. symbols: A list of symbol names used in the components. config: The tensor configuration - "ll" for covariant (lower indices) or "uu" for contravariant (upper indices). Returns: A key for the stored metric object. """ global expression_counter try: # Parse symbols sympy_symbols = sympy.symbols(", ".join(symbols)) sympy_symbols_dict = {str(sym): sym for sym in sympy_symbols} # Convert components to sympy expressions sympy_components = [] for row in components: sympy_row = [] for expr_str in row: if expr_str == "0": sympy_row.append(0) else: expr = parse_expr(expr_str, local_dict=sympy_symbols_dict) sympy_row.append(expr) sympy_components.append(sympy_row) # Create metric tensor metric_obj = MetricTensor(sympy_components, sympy_symbols, config=config) # Store the metric metric_key = f"metric_custom_{expression_counter}" metrics[metric_key] = metric_obj expressions[metric_key] = metric_obj.tensor() expression_counter += 1 return metric_key except Exception as e: return f"Error creating custom metric: {str(e)}"
  • Fallback handler for 'create_custom_metric' when EinsteinPy is not available, returning an error message.
    def create_custom_metric( components: List[List[str]], symbols: List[str], config: Literal["ll", "uu"] = "ll", ) -> str: """Creates a custom metric tensor from provided components and symbols.""" return "Error: EinsteinPy library is not available. Please install it with 'pip install einsteinpy'."

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