Skip to main content
Glama

numpy_mcp_matlib_operation

Perform matrix operations like creating random matrices, zeros, ones, identity matrices, reshaping, concatenating, transposing, and stacking arrays for mathematical computations.

Instructions

Do matrix operations: rand-mat, zeros, ones, eye, identity, arange, linspace, reshape, flatten, concatenate, transpose, stack

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationYes
dataNo
shapeNo
mNo
nNo
kNo
startNo
stopNo
stepNo
numNo
axisNo

Implementation Reference

  • The handler function implementing the 'numpy_mcp_matlib_operation' tool. It provides a unified interface for various NumPy matrix and array operations such as creating zero/ones matrices, identity/eye matrices, random matrices, arange/linspace, reshape, flatten, concatenate, transpose, and stack.
    def matlib_operation(
        operation: str,
        data: Optional[Union[list, int, float]] = None,
        shape: Optional[Union[list, int]] = None,
        m: Optional[int] = None,
        n: Optional[int] = None,
        k: int = 0,
        start: Optional[float] = None,
        stop: Optional[float] = None,
        step: Optional[float] = None,
        num: Optional[int] = None,
        axis: int = 0,
    ) -> list:
        """
        Unified interface for numerical matrix operations using numpy.
    
        Args:
            operation: The matrix operation to perform. One of:
                - 'rand-mat': Matrix of random values (0-1) (m: int, n: int) -> 2D array
                - 'zeros': Matrix of zeros (shape: list) -> array
                - 'ones': Matrix of ones (shape: list) -> array
                - 'eye': 2D array with ones on diagonal (m: int, n: int, k: int) -> 2D array
                - 'identity': Identity matrix (n: int) -> 2D array
                - 'arange': Evenly spaced values (start: float, stop: float, step: float) -> 1D array
                - 'linspace': Evenly spaced numbers (start: float, stop: float, num: int) -> 1D array
                - 'reshape': Reshape array (data: list, shape: list) -> array
                - 'flatten': Flatten array (data: list) -> 1D array
                - 'concatenate': Join arrays (data: list[list], axis: int) -> array
                - 'transpose': Transpose array (data: list) -> array
                - 'stack': Stack arrays (data: list[list], axis: int) -> array
    
            data: Input data for matrix operations
            shape: Shape of the output matrix
            m: First dimension
            n: Second dimension
            k: Diagonal offset for eye operation
            start: Start value for arange/linspace
            stop: Stop value for arange/linspace
            step: Step size for arange
            num: Number of samples for linspace
            axis: Axis along which to perform operations
    
        Returns:
            list: The resulting matrix/array as a nested list
        """
    
        if operation == "zeros":
            return np.zeros(shape).tolist()
    
        elif operation == "ones":
            return np.ones(shape).tolist()
    
        elif operation == "eye":
            return np.eye(m or n, n, k).tolist()
    
        elif operation == "identity":
            return np.eye(n).tolist()
    
        elif operation == "rand-mat":
            return np.random.rand(m or 1, n or (m or 1)).tolist()
    
        elif operation == "arange":
            if start is None or stop is None:
                raise ValueError("start and stop are required for arange operation")
            if step is None:
                return np.arange(start, stop).tolist()
            return np.arange(start, stop, step).tolist()
    
        elif operation == "linspace":
            if start is None or stop is None or num is None:
                raise ValueError("start, stop, and num are required for linspace operation")
            return np.linspace(start, stop, num).tolist()
    
        elif operation == "reshape":
            if data is None or shape is None:
                raise ValueError("data and shape are required for reshape operation")
            return np.array(data).reshape(shape).tolist()
    
        elif operation == "flatten":
            if data is None:
                raise ValueError("data is required for flatten operation")
            return np.array(data).flatten().tolist()
    
        elif operation == "concatenate":
            if data is None:
                raise ValueError("data is required for concatenate operation")
            return np.concatenate([np.array(x) for x in data], axis=axis).tolist()
    
        elif operation == "transpose":
            if data is None:
                raise ValueError("data is required for transpose operation")
            return np.array(data).T.tolist()
    
        elif operation == "stack":
            if data is None:
                raise ValueError("data is required for stack operation")
            return np.stack([np.array(x) for x in data], axis=axis).tolist()
    
        else:
            raise ValueError(f"Unknown operation: {operation}")
  • Registers the matlib_operation handler as a tool in the 'numpy_mcp' FastMCP server instance, likely exposing it as 'numpy_mcp_matlib_operation'.
    numpy_mcp.tool(
        matlib_operation,
        description="Do matrix operations: rand-mat, zeros, ones, eye, identity, arange, linspace, reshape, flatten, concatenate, transpose, stack",
    )
  • Imports the matlib_operation function for use in tool registration.
    from fmcp.numpy_mcp.core.matlib import matlib_operation

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/abhiphile/fermat-mcp'

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