array_aggregate
Calculate sumproduct, weighted average, or dot product operations on 1D arrays for mathematical analysis and data processing.
Instructions
Perform aggregation operations on 1D arrays.
Examples:
SUMPRODUCT: operation="sumproduct", array1=[1,2,3], array2=[4,5,6] Result: 32 (1×4 + 2×5 + 3×6)
WEIGHTED AVERAGE: operation="weighted_average", array1=[10,20,30], weights=[1,2,3] Result: 23.33... ((10×1 + 20×2 + 30×3) / (1+2+3))
DOT PRODUCT: operation="dot_product", array1=[1,2], array2=[3,4] Result: 11 (1×3 + 2×4)
GRADE CALCULATION: operation="weighted_average", array1=[85,92,78], weights=[0.3,0.5,0.2] Result: 86.5
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification. | |
| output_mode | No | Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details. | full |
| operation | Yes | Aggregation operation | |
| array1 | Yes | First 1D array (e.g., [1,2,3]) | |
| array2 | No | Second 1D array for sumproduct/dot_product | |
| weights | No | Weights for weighted_average (e.g., [1,2,3]) |
Implementation Reference
- src/vibe_math_mcp/tools/array.py:220-269 (handler)The main handler function that executes the array_aggregate tool. It parses inputs, performs NumPy-based aggregations (sumproduct/dot_product via np.dot, weighted_average via np.average), handles errors, and formats the result.async def array_aggregate( operation: Annotated[ Literal["sumproduct", "weighted_average", "dot_product"], Field(description="Aggregation operation"), ], array1: Annotated[List[float], Field(description="First 1D array (e.g., [1,2,3])")], array2: Annotated[ Union[str, List[float], None], Field(description="Second 1D array for sumproduct/dot_product"), ] = None, weights: Annotated[ Union[str, List[float], None], Field(description="Weights for weighted_average (e.g., [1,2,3])"), ] = None, ) -> str: """Aggregate 1D arrays.""" try: # Parse stringified JSON from XML serialization if isinstance(array2, str): array2 = cast(List[float], json.loads(array2)) if isinstance(weights, str): weights = cast(List[float], json.loads(weights)) arr1 = np.array(array1, dtype=float) if operation == "sumproduct" or operation == "dot_product": if array2 is None: raise ValueError(f"{operation} requires array2") arr2 = np.array(array2, dtype=float) if len(arr1) != len(arr2): raise ValueError(f"Arrays must have same length. Got {len(arr1)} and {len(arr2)}") result = float(np.dot(arr1, arr2)) elif operation == "weighted_average": if weights is None: raise ValueError("weighted_average requires weights") w = np.array(weights, dtype=float) if len(arr1) != len(w): raise ValueError( f"Array and weights must have same length. Got {len(arr1)} and {len(w)}" ) result = float(np.average(arr1, weights=w)) else: raise ValueError(f"Unknown operation: {operation}") return format_result(result, {"operation": operation}) except Exception as e: raise ValueError(f"Aggregation failed: {str(e)}")
- Input schema defined using Pydantic Annotated types for the tool parameters: operation (enum), array1 (required list), array2 (optional for sumproduct/dot), weights (optional for weighted_avg).operation: Annotated[ Literal["sumproduct", "weighted_average", "dot_product"], Field(description="Aggregation operation"), ], array1: Annotated[List[float], Field(description="First 1D array (e.g., [1,2,3])")], array2: Annotated[ Union[str, List[float], None], Field(description="Second 1D array for sumproduct/dot_product"), ] = None, weights: Annotated[ Union[str, List[float], None], Field(description="Weights for weighted_average (e.g., [1,2,3])"), ] = None, ) -> str:
- src/vibe_math_mcp/tools/array.py:193-219 (registration)Tool registration using @mcp.tool decorator, specifying name, description with examples, and annotations for read-only and idempotent hints.@mcp.tool( name="array_aggregate", description="""Perform aggregation operations on 1D arrays. Examples: SUMPRODUCT: operation="sumproduct", array1=[1,2,3], array2=[4,5,6] Result: 32 (1×4 + 2×5 + 3×6) WEIGHTED AVERAGE: operation="weighted_average", array1=[10,20,30], weights=[1,2,3] Result: 23.33... ((10×1 + 20×2 + 30×3) / (1+2+3)) DOT PRODUCT: operation="dot_product", array1=[1,2], array2=[3,4] Result: 11 (1×3 + 2×4) GRADE CALCULATION: operation="weighted_average", array1=[85,92,78], weights=[0.3,0.5,0.2] Result: 86.5""", annotations=ToolAnnotations( title="Array Aggregation", readOnlyHint=True, idempotentHint=True, ), )