数学计算服务器

Qalc MCP Server 是一个基于 libqalculate 计算引擎的模型上下文协议服务器,为LLM代理提供高级数学计算能力,包括基础算术、符号数学、单位转换、微积分运算和统计分析等。

  • by aturki
  • Python
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 aturki/qalc-mcp

使用说明

Qalc MCP服务器

一个全面的模型上下文协议(MCP)服务器,将强大的libqalculate计算引擎暴露给LLM代理。此服务器提供高级数学计算功能,包括基本算术、符号数学、单位转换、微积分运算、统计计算等等。

特性

核心数学运算

  • 基本算术:标准操作(+、-、*、/、^、mod等)
  • 高级功能:三角函数、对数函数、指数函数、双曲线函数
  • 复数:完全支持复数运算
  • 精确与近似:在精确的符号结果或十进制近似值之间进行选择

单元系统支持

  • 综合单位:所有国际单位制、英制单位和许多专业单位
  • 自动转换:智能单元转换和尺寸分析
  • 货币兑换:实时汇率(可用时)
  • 自定义单位:支持用户自定义单位

编号系统

  • 多基地:二进制、八进制、十进制、十六进制和自定义基数(2-36)
  • 罗马数字:转换为罗马数字符号或从罗马数字符号转换
  • 六十进制:支持时间格式和角度测量
  • 科学记数法:处理非常大和非常小的数字

符号数学

  • 代数运算:展开、分解和简化表达式
  • 方程求解:象征性地求解方程式和不等式
  • 微积分:导数、积分、极限
  • 矩阵运算:矩阵算术、行列式、逆

统计函数

  • 描述性统计:平均值、中位数、众数、标准差、方差
  • 概率分布:正态分布、二项式分布和其他分布
  • 数据分析:支持向量和数据集

高级功能

  • 不确定性传播:处理不确定度(±)的测量
  • 区间运算:根据数值范围进行计算
  • 日期/时间计算:日期算术和时区转换
  • 物理常数:访问大量物理常数库

安装

先决条件

此MCP服务器要求在您的系统上安装libqalculate。

macOS(使用Homebrew)

brew install libqalculate

Ubuntu/Debian

sudo apt-get install qalc

视窗

下载自 libcalculate 版本 或者使用像Chocolatey这样的包管理器。

其他系统

请参阅 libqalculate安装指南 为了你的平台。

安装MCP服务器

cd qalc-mcp
pip install -e .

验证安装

测试一切是否正常工作:

python test_qalc.py

运行交互式演示:

python examples.py

用法

该服务器为不同的计算需求提供了15个全面的工具:

核心评估工具

evaluate_expression(expression, timeout=30)

具有丰富格式的通用数学表达式求值。

evaluate_expression("sqrt(32)")
# Output: "sqrt(32) = 4 × √(2) ≈ 5.656854249"

evaluate_expression("5 ft to m") 
# Output: "5 feet = 1.524 m"

evaluate_expression("solve x^2 = 9")
# Output: "x² = 9 = (x = 3 or x = -3)"

evaluate_terse(expression, timeout=30)

最小输出以获得干净的数值结果。

evaluate_terse("sqrt(32)")
# Output: "5.656854249"

evaluate_terse("pi")
# Output: "3.141592654"

evaluate_with_options(expression, precision=None, approximation=None, base=None, angle_unit=None, timeout=30)

具有精度、近似模式和格式控制的高级评估。

evaluate_with_options("pi", precision=15)
# Output: "π ≈ 3.14159265358979"

evaluate_with_options("sqrt(2)", approximation="exact")
# Output: "sqrt(2) = √(2)"

evaluate_with_options("255", base=16)
# Output: "255 = 0xFF"

专用计算工具

convert_units(value, target_unit, timeout=30)

通过自动单位识别在任何支持的单位之间进行转换。

convert_units("100 km/h", "mph")
# Output: "100 km/h ≈ 62.137119 mph"

convert_units("25 °C", "°F")
# Output: "25 °C = 77 °F"

convert_units("1 atm", "Pa")
# Output: "1 atm = 101325 Pa"

convert_base(number, target_base, source_base=None, timeout=30)

具有自动格式检测功能的数基转换(基数2-36)。

convert_base("255", 16)
# Output: "255 = 0xFF"

convert_base("0xFF", 10)
# Output: "0xFF = 255"

convert_base("1010", 10, 2)
# Output: "1010₂ = 10"

symbolic_calculation(expression, timeout=30)

代数运算、因式分解、展开和方程求解。

symbolic_calculation("factor x^2-4")
# Output: "x² - 4 = (x - 2)(x + 2)"

symbolic_calculation("expand (x+1)^2")
# Output: "(x + 1)² = x² + 2x + 1"

symbolic_calculation("x^2 + 5x + 6 = 0")
# Output: "x = -2 or x = -3"

calculus_operation(expression, operation, variable="x", timeout=30)

微积分运算:微分和积分。

calculus_operation("x^3", "diff")
# Output: "diff(x³) = 3x²"

calculus_operation("cos(x)", "integrate")
# Output: "integrate(cos(x)) = sin(x) + C"

statistical_calculation(data, operation, timeout=30)

数据集的统计分析。

statistical_calculation("1; 2; 3; 4; 5", "mean")
# Output: "mean([1 2 3 4 5]) = 3"

statistical_calculation("[1, 2, 3, 4, 5]", "stdev")
# Output: "stdev([1 2 3 4 5]) ≈ 1.581"

matrix_vector_operation(expression, timeout=30)

矩阵和向量算术,包括行列式、逆和积。

matrix_vector_operation("det([1, 2; 3, 4])")
# Output: "det([1 2; 3 4]) = -2"

matrix_vector_operation("cross([1, 2, 3], [4, 5, 6])")
# Output: "cross([1 2 3], [4 5 6]) = [-3 6 -3]"

matrix_vector_operation("||[3, 4, 5]||")
# Output: "magnitude([3 4 5]) ≈ 7.071"

programming_mode_calculation(expression, base=16, timeout=30)

使用位运算和基转换进行编程计算。

programming_mode_calculation("0xFF & 0x0F", 16)
# Output: "0xFF & 0xF = 0xF"

programming_mode_calculation("52 << 2", 16)
# Output: "shift(0x52, 0x2) = 0x148"

发现和实用工具

list_functions(search_term=None, timeout=30)

浏览具有可选过滤功能的可用数学函数。

list_functions("sin")
# Lists all trigonometric functions containing "sin"

list_units(search_term=None, timeout=30)

使用可选过滤浏览可用的测量单位。

list_units("meter")
# Lists all units containing "meter"

list_variables(search_term=None, timeout=30)

使用可选筛选浏览可用变量和常量。

list_variables("speed")
# Lists speed-related constants like speed of light

list_prefixes(search_term=None, timeout=30)

浏览可用的单位前缀(千、兆、微等)。

update_exchange_rates(timeout=60)

更新货币汇率以进行准确转换。

表达式语法

基本操作

2 + 3 * 4          # Basic arithmetic
2^3                # Exponentiation  
sqrt(16)           # Square root
sin(pi/2)          # Trigonometric functions
log(100)           # Logarithms

单位

5 ft + 2 m         # Mixed unit arithmetic
100 km/h to mph    # Unit conversion
5 kg * 9.8 m/s^2   # Dimensional analysis

符号数学

(x + y)^2          # Symbolic expressions
solve(x^2 = 9)     # Equation solving
factor(x^2 - 4)    # Factorization
diff(x^3)          # Differentiation

矩阵和向量

[1, 2; 3, 4]       # 2x2 matrix
[1, 2, 3]          # Vector
det([1,2;3,4])     # Determinant
[1,2] × [3,4]      # Vector operations

数字基础

0xFF               # Hexadecimal
0b1010             # Binary
52 to hex          # Base conversion

统计数据

mean(1; 2; 3; 4; 5)        # Semicolon-separated
stdev([1, 2, 3, 4, 5])     # Vector format

错误处理

服务器包括全面的错误处理:

  • 超时保护:30秒后计算超时
  • 依赖检查:验证libqaculate安装
  • 输入验证:验证参数和表达式
  • 故障弱化:提供有用的错误消息

高级用法

精度管理

使用 evaluate_with_options 控制计算精度和格式:

# High precision calculations
evaluate_with_options("pi", precision=20)

# Exact symbolic results
evaluate_with_options("sqrt(2)", approximation="exact")

# Base conversions
evaluate_with_options("255", base=16)

# Angle unit control
evaluate_with_options("sin(90)", angle_unit="degrees")

近似模式

  • exact:强制精确的符号结果(例如,√2,π/4)
  • try_exact:如果可能的话,更喜欢精确,回到小数点
  • approximate:强制十进制近似

数字基础支持

支持从2到36的任何基数,并具有自动格式检测功能:

convert_base("255", 16)        # Decimal to hex: "0xFF"
convert_base("0xFF", 10)       # Hex to decimal: "255"
convert_base("1010", 10, 2)    # Binary to decimal: "10"

编程模式功能

  • 比特操作: &, |, ^, ~, <<, >>
  • 多种基本显示格式
  • 布尔逻辑运算
  • 十六进制、二进制和八进制文字

错误处理和超时

所有工具都包括全面的错误处理:

  • 超时保护:计算默认超时30秒
  • 依赖性验证:自动libqaculate可用性检查
  • 输入验证:参数验证,并显示有用的错误消息
  • 故障弱化:无效表达式的有意义错误消息

物理常数和变量

访问以下广泛的图书馆:

  • 物理常数: c (光速), h (普朗克常数), k_e (库仑常数)
  • 数学常量: pi, e, golden_ratio
  • 化学元素: atom(H; weight) 原子量

例子

科学计算

# Kinetic energy calculation
0.5 * 2 kg * (10 m/s)^2 to J

# Gravitational force
G * (mass_earth * mass_moon) / (384400 km)^2

# Wave frequency
c / (550 nm) to Hz

工程应用

# Electrical power
12 V * 2 A to W

# Mechanical advantage
100 lbf * 5 ft to Nm

# Fluid dynamics
0.5 * 1.225 kg/m^3 * (25 m/s)^2 * 2 m^2 to N

财务计算

# Compound interest (if exchange rates available)
1000 USD * (1.05)^10

# Currency conversion
500 EUR to USD

配置

服务器会自动加载libqalculate的默认配置,包括:

  • 标准数学函数
  • 国际单位制和英制单位
  • 物理常数
  • 货币汇率(如有)

测试和验证

运行测试套件

使用全面的测试套件验证所有功能:

python test_qalc.py

互动示例

使用演示脚本探索功能:

python examples.py

故障排除

常见问题

  1. “找不到qalc命令”

    • 安装libqalculate: brew install libqalculate (macOS)或 apt-get install qalc (Ubuntu)
    • 确保qalc在您的系统PATH中
    • 测试: qalc "2+2"
  2. 超时错误

    • 30秒后复杂计算超时(可配置)
    • 将复杂表达式分解为更简单的部分
    • 代数表达式使用符号模式
  3. 单位转换错误

    • 验证单元名称: list_units("search_term")
    • 检查单位拼写和缩写
    • 使用正确的单位语法: 5 ft 不 5ft
  4. 表达式分析错误

    • 使用括号表示运算符优先级
    • 检查函数名称: list_functions("function_name")
    • 使用正确的数学语法
  5. 基础转换问题

    • 确保底座在2-36之间
    • 使用正确的前缀: 0x 对于十六进制, 0b 二进位
    • 检查非十进制输入的源库规范

业绩说明

  • 简单计算:<100ms
  • 单位转换:<200ms
  • 符号操作:1-5秒
  • 复杂集成:5-30秒
  • 矩阵运算:取决于大小和复杂性

MCP集成

此服务器旨在与MCP兼容的应用程序无缝集成:

  • 克劳德桌面:添加到MCP配置中
  • 其他MCP客户端:使用标准MCP协议
  • 自定义应用程序:通过MCP客户端库集成

MCP配置示例

{
  "mcpServers": {
    "qalc": {
      "command": "python",
      "args": ["/path/to/qalc-mcp/qalc.py"],
      "env": {}
    }
  }
}

建筑

服务器在libqalculate上提供了一个干净的抽象层:

  • 输入验证:所有参数在处理前都经过验证
  • 错误处理:全面的错误捕捉和用户友好的消息
  • 超时管理:防止无限计算
  • 输出格式化:所有工具的结果一致、可读

贡献

欢迎投稿!需要改进的地方:

  • 附加统计功能
  • 增强矩阵运算
  • 自定义单位定义
  • 绘图功能(如果gnuplot可用)

有关计算问题,请参阅 libqalculate文档.

许可证

此MCP服务器实现是在MIT许可证下提供的。Libqalculate本身是根据GPL许可的。


准备好开始了吗? 安装libqalculate,运行测试套件,并探索示例!