Skip to main content
Glama
apetta
by apetta

array_statistics

Calculate statistical measures like mean, median, and standard deviation on 2D arrays. Compute across entire arrays, rows, or columns to analyze data patterns.

Instructions

Calculate statistical measures on arrays using Polars.

Supports computation across entire array, rows, or columns.

Examples:

COLUMN-WISE MEANS: data=[[1,2,3],[4,5,6]], operations=["mean"], axis=0 Result: [2.5, 3.5, 4.5] (average of each column)

ROW-WISE MEANS: data=[[1,2,3],[4,5,6]], operations=["mean"], axis=1 Result: [2.0, 5.0] (average of each row)

OVERALL STATISTICS: data=[[1,2,3],[4,5,6]], operations=["mean","std"], axis=None Result: {mean: 3.5, std: 1.71}

MULTIPLE STATISTICS: data=[[1,2,3],[4,5,6]], operations=["min","max","mean"], axis=0 Result: {min: [1,2,3], max: [4,5,6], mean: [2.5,3.5,4.5]}

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contextNoOptional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification.
output_modeNoOutput format: full (default), compact, minimal, value, or final. See batch_execute tool for details.full
dataYes2D array (e.g., [[1,2,3],[4,5,6]])
operationsYesStatistics to compute (e.g., ['mean','std'])
axisNoAxis: 0=column-wise, 1=row-wise, None=overall

Implementation Reference

  • Full implementation of the array_statistics tool handler, including decorator for registration, input schema via Annotated types, and execution logic using Polars DataFrames and NumPy for various statistics (mean, median, std, min, max, sum) along different axes.
    @mcp.tool( name="array_statistics", description="""Calculate statistical measures on arrays using Polars. Supports computation across entire array, rows, or columns. Examples: COLUMN-WISE MEANS: data=[[1,2,3],[4,5,6]], operations=["mean"], axis=0 Result: [2.5, 3.5, 4.5] (average of each column) ROW-WISE MEANS: data=[[1,2,3],[4,5,6]], operations=["mean"], axis=1 Result: [2.0, 5.0] (average of each row) OVERALL STATISTICS: data=[[1,2,3],[4,5,6]], operations=["mean","std"], axis=None Result: {mean: 3.5, std: 1.71} MULTIPLE STATISTICS: data=[[1,2,3],[4,5,6]], operations=["min","max","mean"], axis=0 Result: {min: [1,2,3], max: [4,5,6], mean: [2.5,3.5,4.5]}""", annotations=ToolAnnotations( title="Array Statistics", readOnlyHint=True, idempotentHint=True, ), ) async def array_statistics( data: Annotated[List[List[float]], Field(description="2D array (e.g., [[1,2,3],[4,5,6]])")], operations: Annotated[ List[Literal["mean", "median", "std", "min", "max", "sum"]], Field(description="Statistics to compute (e.g., ['mean','std'])"), ], axis: Annotated[ int | None, Field(description="Axis: 0=column-wise, 1=row-wise, None=overall") ] = None, ) -> str: """Calculate array statistics.""" try: df = list_to_polars(data) results = {} for op in operations: if axis is None: # Overall statistics across all values all_values = df.to_numpy().flatten() if op == "mean": results[op] = float(np.mean(all_values)) elif op == "median": results[op] = float(np.median(all_values)) elif op == "std": results[op] = float(np.std(all_values, ddof=1)) elif op == "min": results[op] = float(np.min(all_values)) elif op == "max": results[op] = float(np.max(all_values)) elif op == "sum": results[op] = float(np.sum(all_values)) elif axis == 0: # Column-wise statistics if op == "mean": results[op] = df.mean().to_numpy()[0].tolist() elif op == "median": results[op] = df.median().to_numpy()[0].tolist() elif op == "std": results[op] = df.std().to_numpy()[0].tolist() elif op == "min": results[op] = df.min().to_numpy()[0].tolist() elif op == "max": results[op] = df.max().to_numpy()[0].tolist() elif op == "sum": results[op] = df.sum().to_numpy()[0].tolist() elif axis == 1: # Row-wise statistics arr = df.to_numpy() if op == "mean": results[op] = np.mean(arr, axis=1).tolist() elif op == "median": results[op] = np.median(arr, axis=1).tolist() elif op == "std": results[op] = np.std(arr, axis=1, ddof=1).tolist() elif op == "min": results[op] = np.min(arr, axis=1).tolist() elif op == "max": results[op] = np.max(arr, axis=1).tolist() elif op == "sum": results[op] = np.sum(arr, axis=1).tolist() return format_result(results, {"shape": f"{len(data)}×{len(data[0])}", "axis": axis}) except Exception as e: raise ValueError(f"Statistics calculation failed: {str(e)}")
  • Import statement that loads the tools module containing array_statistics, triggering its registration via the @mcp.tool decorator on the CustomMCP server instance.
    from .tools import array, basic, batch, calculus, financial, linalg, statistics # noqa: E402

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/apetta/vibe-math-mcp'

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