Skip to main content
Glama
mcp-math-calculator

mcp-math-calculator

by Cosmiumx

mcp-math-calculator

🧮 精确数学计算的 MCP 服务器 - 为 AI 提供 100% 准确的计算能力

npm version License: MIT

🌟 特性

  • ✅ 精确算术运算 - 解决浮点数精度问题(如 0.1 + 0.2 = 0.3)

  • ✅ 大数支持 - 处理超出 JavaScript 安全整数范围的数字

  • ✅ 复杂表达式 - 支持括号、运算符优先级

  • ✅ 科学函数 - sqrt, pow, sin, cos, log, exp 等 300+ 函数

  • ✅ 安全求值 - 无代码注入风险,沙箱隔离执行

  • ✅ 超时保护 - 5秒自动超时,防止恶意计算

Related MCP server: Math MCP Server

📦 安装

快速开始(推荐)

在你的 MCP 客户端配置中添加(如 Claude Desktop):

macOS/Linux: ~/Library/Application Support/Claude/claude_desktop_config.json

Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "math-calculator": {
      "command": "npx",
      "args": ["-y", "mcp-math-calculator"]
    }
  }
}

方式 2: 全局安装

npm install -g mcp-math-calculator
# 或
pnpm add -g mcp-math-calculator

然后配置:

{
  "mcpServers": {
    "math-calculator": {
      "command": "mcp-math-calculator"
    }
  }
}

🚀 使用方法

配置完成后,重启你的 AI 客户端,然后可以这样提问:

  • "精确计算 0.1 + 0.2"

  • "123456789 * 987654321 等于多少?"

  • "计算 (10 + 20) * 3 / 2"

  • "sqrt(16) + pow(2, 3) 的值是多少?"

  • "帮我算一下 sin(pi/2)"

AI 会自动调用此工具进行精确计算。

🛠️ 可用工具

calculate

精确计算数学表达式。

参数:

  • expression (string, 必需): 要计算的数学表达式

  • precision (number, 可选): 小数精度位数(默认: 15,范围: 1-64)

支持的运算:

类型

示例

基础运算

+, -, *, /, ^, %

括号

(, )

函数

sqrt(), pow(), abs(), round(), floor(), ceil()

三角函数

sin(), cos(), tan(), asin(), acos(), atan()

对数指数

log(), log10(), log2(), exp()

常数

pi, e, tau, phi

示例:

// 基础运算
"2 + 2"                    // 4
"10 - 3"                   // 7
"6 * 7"                    // 42
"20 / 4"                   // 5

// 浮点数精度
"0.1 + 0.2"                // 0.3 (不是 0.30000000000000004)

// 复杂表达式
"(10 + 20) * 3"            // 90
"2 + 3 * 4 - 5 / 5"        // 13

// 科学函数
"sqrt(16)"                 // 4
"pow(2, 3)"                // 8
"sin(pi / 2)"              // 1

// 大数运算
"123456789 * 987654321"    // 121932631112635269
"999999999999999999 + 1"   // 1000000000000000000

🔒 安全性

本工具采用多层安全机制:

  • ✅ 不使用 eval() - 使用 mathjs 安全解析器

  • ✅ 沙箱执行 - 隔离计算环境

  • ✅ 函数白名单 - 仅允许安全的数学函数

  • ✅ 输入验证 - 拒绝危险模式(import, require, eval 等)

  • ✅ 超时保护 - 5秒自动终止

  • ✅ 结果大小限制 - 防止内存溢出

🧪 开发

# 克隆仓库
git clone https://github.com/Cosmiumx/mcp-math-calculator.git
cd mcp-math-calculator

# 安装依赖
pnpm install

# 构建
pnpm build

# 运行测试
pnpm test

# 监听模式
pnpm watch

📊 测试覆盖

项目包含 37 个测试用例,覆盖:

  • ✅ 基础算术运算

  • ✅ 浮点数精度问题

  • ✅ 复杂表达式计算

  • ✅ 科学函数

  • ✅ 数学常数

  • ✅ 大数运算

  • ✅ 错误处理

  • ✅ 安全性验证

  • ✅ 精度控制

🤝 贡献

欢迎贡献!请随时提交 Issue 或 Pull Request。

  1. Fork 本仓库

  2. 创建你的特性分支 (git checkout -b feature/AmazingFeature)

  3. 提交你的改动 (git commit -m 'Add some AmazingFeature')

  4. 推送到分支 (git push origin feature/AmazingFeature)

  5. 开启一个 Pull Request

📝 License

MIT License - 详见 LICENSE 文件

🐛 问题反馈

遇到问题?请在 GitHub Issues 中报告。

📚 相关资源

⭐ Star History

如果这个项目对你有帮助,请给它一个 ⭐️!


Made with ❤️ by Cosmiumx

Available Tools

1 tool
calculateA

⚠️ 精确数学计算工具 - 请在任何涉及数字运算时使用

AI语言模型在数学计算上存在已知缺陷:

  • 大数计算错误率高

  • 浮点数精度问题(0.1+0.2≠0.3)

  • 复杂表达式容易出错

  • 即使简单运算也可能失误

为确保用户获得100%准确的结果,请在以下情况使用此工具: ✓ 任何数学表达式(包括简单的 1+1) ✓ 金融、科学、工程计算 ✓ 用户明确要求计算 ✓ 不确定自己能否算对时

支持的运算:

  • 基础运算: +, -, *, /, ^, %

  • 括号: ( )

  • 函数: sqrt, pow, abs, sin, cos, tan, log, exp 等

  • 常数: pi, e

输入格式:标准数学表达式 示例: "2+2", "(10+20)*3", "sqrt(16)", "sin(pi/2)", "0.1 + 0.2"

Inputs: expression (required), precision.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4.1/5.0

    Scored across 1 tool

    Disambiguation5/5

    There is only one tool, so there is zero possibility of misselection or overlap. Its purpose (evaluating a mathematical expression) is unambiguous and clearly stated.

    Naming Consistency5/5

    The single tool name 'calculate' is a clear, conventional verb with no casing or style conflicts to speak of. Nothing in the set deviates from a pattern.

    Tool Count4/5

    One tool is defensible for a narrow expression-evaluation domain, since a single calculate entry point can handle all arithmetic. It is on the thin side—an agent gets no batch, variable-assignment, or unit-conversion entry points—but nothing is padded.

    Completeness4/5

    The tool covers arithmetic, parentheses, common functions, and constants, which is the full core lifecycle of expression evaluation. Minor gaps exist for things like rounding/precision control, variable binding across calls, or unit/currency conversion, but agents can work around these.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      A
      quality
      D
      maintenance
      Enables mathematical calculations through basic arithmetic operations including addition, subtraction, multiplication, division, exponentiation, and logarithms. Provides a simple interface for AI agents to perform mathematical computations.
      6
      3
      -
    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables LLMs to perform accurate mathematical calculations by evaluating expressions using mathjs. Supports basic arithmetic, functions, constants, and complex mathematical operations through natural language requests.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides LLMs with accurate mathematical computation through a real calculator powered by math.js, offering tools for arithmetic, algebra, calculus, unit conversion, and more.
      51 npm
      3
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Enables safe math expression evaluation, unit conversion, statistical analysis, number formatting, and percentage calculations for AI agents via MCP.
      5
      47 npm
      MIT