Skip to main content
Glama
avanishd-3

Math MCP Server

by avanishd-3

matrix_multiplication

Multiply two matrices with 64-bit floating point precision. Provide matrices as nested lists where each sub-list represents a row. Ensure compatibility: columns in first matrix equal rows in second.

Instructions

Multiplies two matrices with 64 bit floating point precision and returns the result as a matrix. You need to provide them in the format of nested lists. For example, [[1, 2], [3, 4]] would represent a 2x2 matrix. Each sub list represents a row in the matrix, and each element in the sub list represents a column. The matrices must be compatible for multiplication, meaning the number of columns in the first matrix must equal the number of rows in the second matrix. You can also use fractions for individual elements if you want to, like [1/2, 1/3, 1/4].

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
matrix_1Yes
matrix_2Yes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The matrix_multiplication tool handler: decorated with @math_mcp.tool for registration, performs matrix multiplication using NumPy's matmul with input validation for empty matrices and dimension compatibility. Converts inputs to float64 NumPy arrays, computes rounded result, and returns as list of lists.
    @math_mcp.tool
    def matrix_multiplication(
        matrix_1: list[list[int | float]],
        matrix_2: list[list[int | float]],
    ) -> list[list[float]]:
        """Multiplies two matrices with 64 bit floating point precision and returns the result as a matrix.
           You need to provide them in the format of nested lists. For example, [[1, 2], [3, 4]] would represent a 2x2 matrix.
           Each sub list represents a row in the matrix, and each element in the sub list represents a column.
           The matrices must be compatible for multiplication, meaning the number of columns in the first matrix must equal the number of rows in the second matrix.
           You can also use fractions for individual elements if you want to, like [1/2, 1/3, 1/4].
        """
    
        # Convert matrices to numpy arrays for efficient multiplication
        matrix_1 = np.array(matrix_1, dtype=np.float64)
        matrix_2 = np.array(matrix_2, dtype=np.float64)
    
        # This is technically allowed by Fast MCP, but it is an error here
        if not matrix_1.size:
            logging.error("Matrix 1 is empty.")
            raise ValueError("Matrix 1 cannot be empty.")
        if not matrix_2.size:
            logging.error("Matrix 2 is empty.")
            raise ValueError("Matrix 2 cannot be empty.")
        
        # Check if matrices have compatible dimensions for multiplication
        if len(matrix_1[0]) != len(matrix_2):
            logging.error("Incompatible matrix dimensions for multiplication.")
            raise ValueError("Incompatible matrix dimensions.")
        
        
    
        # Use numpy for fast matrix multiplication
        result = np.round(np.matmul(matrix_1, matrix_2), decimals=SIXTY_FOUR_BIT_FLOAT_DECIMAL_PLACES)
        logging.info(f"Multiplying matrices: {matrix_1} * {matrix_2} -> Result: {result}")
    
        # Convert result to list of lists so Fast MCP can serialize it properly
        result = result.tolist()
        return result
Behavior4/5

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

With no annotations provided, the description carries the full burden. It discloses key behavioral traits: precision ('64 bit floating point'), return format ('result as a matrix'), input format requirements ('nested lists'), and compatibility constraints. It doesn't mention error handling, performance limits, or side effects, but covers essential operational behavior adequately.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded with the core purpose. Every sentence adds value: operation definition, format specification, compatibility rule, and data type flexibility. It could be slightly more structured but avoids redundancy and stays focused on essential information.

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 mathematical complexity, 0% schema coverage, and presence of an output schema (which handles return values), the description is quite complete. It covers purpose, input format, constraints, and data types. It doesn't explain edge cases or error responses, but for a tool with output schema and clear mathematical operation, this is sufficient.

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

Parameters5/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 fully. It explicitly explains both parameters: 'matrix_1' and 'matrix_2' are described as matrices in nested list format with detailed examples and formatting rules. It adds crucial meaning beyond the bare schema, including data types (integers, numbers, fractions), structure (rows/columns), and validation rules (compatibility).

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 specific action ('multiplies two matrices') and resource ('matrices'), distinguishing it from sibling tools like 'add', 'subtract', 'multiply' (scalar), and 'divide' by specifying matrix multiplication. It goes beyond just restating the name by detailing the operation and output format.

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

Usage Guidelines3/5

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

The description implies usage through the compatibility requirement ('matrices must be compatible for multiplication'), but doesn't explicitly state when to use this tool versus alternatives like scalar 'multiply' or other matrix operations. It provides necessary context for correct invocation but lacks explicit guidance on tool selection among siblings.

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

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/avanishd-3/math-mcp'

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