Qalc MCP服务器
一个全面的模型上下文协议(MCP)服务器,将强大的libqalculate计算引擎暴露给LLM代理。此服务器提供高级数学计算功能,包括基本算术、符号数学、单位转换、微积分运算、统计计算等等。
特性
核心数学运算
- 基本算术:标准操作(+、-、\*、/、^、mod等)
- 高级功能:三角函数、对数函数、指数函数、双曲线函数
- 复数:完全支持复数运算
- 精确与近似:在精确的符号结果或十进制近似值之间进行选择
单元系统支持
- 综合单位:所有国际单位制、英制单位和许多专业单位
- 自动转换:智能单元转换和尺寸分析
- 货币兑换:实时汇率(可用时)
- 自定义单位:支持用户自定义单位
编号系统
- 多基地:二进制、八进制、十进制、十六进制和自定义基数(2-36)
- 罗马数字:转换为罗马数字符号或从罗马数字符号转换
- 六十进制:支持时间格式和角度测量
- 科学记数法:处理非常大和非常小的数字
符号数学
- 代数运算:展开、分解和简化表达式
- 方程求解:象征性地求解方程式和不等式
- 微积分:导数、积分、极限
- 矩阵运算:矩阵算术、行列式、逆
统计函数
- 描述性统计:平均值、中位数、众数、标准差、方差
- 概率分布:正态分布、二项式分布和其他分布
- 数据分析:支持向量和数据集
高级功能
- 不确定性传播:处理不确定度(±)的测量
- 区间运算:根据数值范围进行计算
- 日期/时间计算:日期算术和时区转换
- 物理常数:访问大量物理常数库
安装
先决条件
此MCP服务器要求在您的系统上安装libqalculate。
macOS(使用Homebrew)
brew install libqalculateUbuntu/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 >`
- 多种基本显示格式
- 布尔逻辑运算
- 十六进制、二进制和八进制文字
### 错误处理和超时
所有工具都包括全面的错误处理:
- **超时保护**:计算默认超时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"`
1. **超时错误**
- 30秒后复杂计算超时(可配置)
- 将复杂表达式分解为更简单的部分
- 代数表达式使用符号模式
1. **单位转换错误**
- 验证单元名称: `list_units("search_term")`
- 检查单位拼写和缩写
- 使用正确的单位语法: `5 ft` 不 `5ft`
1. **表达式分析错误**
- 使用括号表示运算符优先级
- 检查函数名称: `list_functions("function_name")`
- 使用正确的数学语法
1. **基础转换问题**
- 确保底座在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文档](https://qalculate.github.io/manual/index.html).
## 许可证
此MCP服务器实现是在MIT许可证下提供的。Libqalculate本身是根据GPL许可的。
______________________________________________________________________
**准备好开始了吗?** 安装libqalculate,运行测试套件,并探索示例!