@yasogan/math-mcp - Mathematical Model Context Protocol Server
MCP server exposing arithmetic, algebra, calculus, matrices, statistics, probability, and unit conversion tools to AI agents.
Problem Statement
AI agents (Claude, OpenCode, Cursor, etc.) often need to perform mathematical calculations but lack built-in mathematical capabilities. Users must either rely on the agent’s limited math knowledge or manually perform calculations outside the agent environment. There’s a need for a standardized way to give mathematical superpowers to any MCP-compatible AI agent without requiring custom code from the agent side.
Solution Architecture
@yasogan/math-mcp implements a comprehensive Model Context Protocol server that exposes 8 mathematical tools through a standardized interface. The server bridges the gap between AI agents and mathematical computation libraries, providing:
- Unified Interface: Single MCP server exposing 8 distinct mathematical tools
- Symbolic Computation: Support for both numeric (mathjs) and symbolic (Algebrite) evaluation modes
- Comprehensive Coverage: Arithmetic, algebra, calculus, linear algebra, statistics, probability, and unit conversions
- Zero-Configuration: Agents can use tools immediately without writing mathematical code
Technical Implementation
Core Components
- MCP Server Foundation: Built on
@modelcontextprotocol/sdkv1.29.0 - Tool Registry: 8 distinct mathematical tools with typed interfaces
- Expression Parser: Supports LaTeX-like mathematical syntax via
@scicave/math-latex-parser - Computation Backends:
mathjsv15.2.0 for numeric computationsalgebritev1.4.0 for symbolic algebra
- Error Handling: Comprehensive validation and error reporting for malformed inputs
- Type Safety: Full TypeScript implementation with strict type checking
Tool Categories
1. Basic Arithmetic & Evaluation
evaluate: Computes mathematical expressions withmode: "numeric" | "symbolic"- Examples:
2^10,sin(pi/4),derivative("x^3", "x"),integrate(x^2, x, 0, 1)
2. Algebraic Operations
solve: Solves equations for variables (returns all solutions including complex)simplify: Simplifies expressions using mathematical identitiesfactor: Factors polynomials into irreducible componentsexpand: Expands factored expressions into polynomial form
3. Linear Algebra
matrix: 13 matrix operations including:- Determinant, inverse, transpose, eigenvalues, eigenvectors
- Matrix arithmetic (add, subtract, multiply)
- Vector operations (dot product, cross product)
- Matrix properties (rank, norm, trace)
4. Statistics & Probability
statistics: 24 statistical operations including:- Descriptive statistics (mean, median, mode, std, variance)
- Probability distributions (normal, binomial, Poisson, t, chi-squared)
- Linear regression with auto-indexing
5. Unit Conversions
units: Converts between physical units:- Length (km, miles, m, ft, in)
- Mass (kg, lb, g, oz)
- Temperature (degC, degF, K)
- Pressure, energy, speed, area
Key Implementation Details
Expression Syntax
- Arithmetic:
2^10,3 + 4 * 2 - Trigonometry:
sin(pi/4),cos(30 deg) - Algebra:
x^2 - 4 = 0,(x+1)^3 - Calculus:
derivative("x^3", "x"),integrate(x^2, x, 0, 1) - Complex Numbers:
2+3i,sqrt(-1) - Matrices:
det([[1,2],[3,4]]),inv([[4,7],[2,6]])
Tool Integration Pattern
// Agent-side usage (no code required)
matrix({
op: "determinant",
a: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
});
// Returns: 0
solve({ equation: "x^2 - 4 = 0", variable: "x" });
// Returns: ["2", "-2"]
Configuration Simplicity
{
"mcpServers": {
"math": {
"command": "npx",
"args": ["-y", "@yasogan/math-mcp"]
}
}
}
Dependencies
Production Dependencies
@modelcontextprotocol/sdk: MCP server implementationmathjs: Comprehensive mathematics libraryalgebrite: Computer algebra system@scicave/math-latex-parser: Mathematical expression parsing
Development Dependencies
typescript: Type safety and compilationvitest: Testing frameworktsx: TypeScript execution
Technical Limitations & Considerations
Expression Parsing
- Syntax Restrictions: Requires well-formed mathematical expressions
- LaTeX Support: Partial LaTeX syntax via math-latex-parser
- Error Handling: Malformed expressions return clear error messages
Computational Limits
- Matrix Size: Large matrices may impact performance
- Symbolic Complexity: Highly complex symbolic expressions may timeout
- Numerical Precision: Double-precision floating point (IEEE 754)
MCP Protocol
- Tool Count: Limited to 8 tools (MCP design constraint)
- Input Validation: Strict schema validation for all parameters
- Error Reporting: Structured error responses per MCP specification
Use Cases
AI Agent Enhancement
- Mathematical Reasoning: Agents can solve equations, factor polynomials, compute derivatives
- Data Analysis: Statistical calculations, probability distributions, linear regression
- Engineering Calculations: Unit conversions, matrix operations, symbolic algebra
Education & Research
- Mathematics Education: Interactive problem solving with AI tutors
- Research Computation: Statistical analysis, linear algebra operations
- Scientific Computing: Unit conversions, physical calculations
Development Workflow
- Code Review: Mathematical verification during code reviews
- Documentation: Generating mathematical examples in documentation
- Testing: Mathematical validation of algorithm implementations
Architecture Deep Dive
Server Initialization
const server = new Server(
{
name: "@yasogan/math-mcp",
version: "0.1.0",
},
{
capabilities: {
tools: {
// 8 tool definitions with schemas
},
},
},
);
Tool Handler Pattern
const matrixTool: Tool = {
name: "matrix",
description: "Perform matrix operations",
inputSchema: {
type: "object",
properties: {
op: { type: "string", enum: MATRIX_OPS },
a: { type: "array", items: { type: "array", items: { type: "number" } } },
b: { type: "array", items: { type: "array", items: { type: "number" } } },
},
required: ["op", "a"],
},
};
Error Handling Strategy
- Validation Errors: Invalid inputs return structured error messages
- Computation Errors: Mathematical errors (division by zero, singular matrices) return meaningful error codes
- Protocol Errors: MCP protocol violations handled gracefully
Performance Characteristics
Computational Performance
- Numeric Operations: Near-native speed via mathjs optimized routines
- Symbolic Operations: Variable performance based on expression complexity
- Matrix Operations: Optimized linear algebra routines
Memory Usage
- Server Footprint: Minimal memory overhead
- Expression Parsing: Efficient AST representation
- Result Serialization: Compact JSON output
Startup Time
- Cold Start: ~100-200ms (TypeScript compilation + dependency loading)
- Warm Start: <50ms (cached computation)
Testing & Reliability
Test Strategy
- Unit Tests: Individual tool functionality
- Integration Tests: Full MCP server operation
- Edge Cases: Invalid inputs, boundary conditions
- Performance Tests: Large matrix operations, complex expressions
Reliability Features
- Input Validation: All parameters validated against schemas
- Error Boundaries: Graceful degradation on computation failures
- Type Safety: Full TypeScript coverage prevents runtime type errors
License
This project is licensed under the MIT License - see the LICENSE file for details.
Community Contributions
- Open to community tool suggestions
- Extensible architecture for domain-specific mathematical tools
- Plugin system for custom computation backends
Developed as part of the MCP ecosystem to enhance AI agent capabilities. The server follows MCP specification v1.0 and is compatible with all MCP-compatible clients including Claude Desktop, OpenCode, Cursor, and Zed.