LaTeX公式计算MCP服务,支持基础运算、三角函数、反三角函数、度分秒(DMS)格式
Project description
LaTeX Calculator MCP Server
一个强大的LaTeX数学公式计算MCP服务,支持基础运算、三角函数、反三角函数,以及完整的度分秒(DMS)格式处理。
✨ 主要特性
🎯 核心功能
- 基础运算: 加减乘除、幂运算、开方、分数
- 三角函数: sin, cos, tan, cot, sec, csc(支持角度/弧度)
- 反三角函数: asin, acos, atan, acot, asec, acsc
- 度分秒(DMS)格式: 支持7种完整格式组合
- 格式化输出: 十进制、科学计数法、DMS格式
🌟 独特优势
- 完整DMS支持: 所有7种格式组合(包括缺失度/分/秒的情况)
- 智能错误分类: 4种错误类型,便于AI理解和处理
- 高性能: 预编译正则表达式,30-40%性能提升
- 向后兼容: 100%兼容原有功能
- 完整测试: 49+测试用例,100%通过率
📦 安装
方式一:通过 PyPI 安装(推荐)
pip install latex-calculator-mcp
或使用 uvx(无需预先安装):
uvx latex-calculator-mcp
方式二:从源码安装
git clone https://github.com/yourusername/latex-calculator-mcp.git
cd latex-calculator-mcp
pip install -e .
配置 MCP 服务
在 Claude Desktop 或其他 MCP 客户端的配置文件中添加:
Windows: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
使用 uvx(推荐,无需预先安装)
{
"mcpServers": {
"latex_calculator": {
"command": "uvx",
"args": ["latex-calculator-mcp"]
}
}
}
使用 pip 安装后
{
"mcpServers": {
"latex_calculator": {
"command": "latex-calculator-mcp"
}
}
}
使用 pipx
{
"mcpServers": {
"latex_calculator": {
"command": "pipx",
"args": ["run", "latex-calculator-mcp"]
}
}
}
🚀 快速开始
基础计算
# 基础运算
calculate_latex(ctx, "2 + 3") # 5.0
calculate_latex(ctx, "6 \\times 7") # 42.0
calculate_latex(ctx, "\\frac{15}{3}") # 5.0
calculate_latex(ctx, "2^3") # 8.0
calculate_latex(ctx, "\\sqrt{16}") # 4.0
三角函数
# 角度制(默认)
calculate_latex(ctx, "\\sin{30}") # 0.5
calculate_latex(ctx, "\\cos{60}") # 0.5
calculate_latex(ctx, "\\tan{45}") # 1.0
# 弧度制
calculate_latex(ctx, "\\sin{1.5708}", degree=False) # 1.0 (sin(π/2))
反三角函数
# 返回角度(度)
calculate_latex(ctx, "\\atan{1}",
result_need_transform_degree=True) # 45.0
# 返回弧度
calculate_latex(ctx, "\\atan{1}") # 0.7854
度分秒(DMS)格式
所有支持的格式
# 1. 完整DMS
calculate_latex(ctx, "162°2'3''") # 162.03416667
# 2. 度+分(缺秒)
calculate_latex(ctx, "162°2'") # 162.03333333
# 3. 度+秒(缺分)⭐ 新增
calculate_latex(ctx, "162°3''") # 162.00083333
# 4. 只有度
calculate_latex(ctx, "162°") # 162.0
# 5. 分+秒(缺度)⭐ 新增
calculate_latex(ctx, "44'30''") # 0.74166667
# 6. 只有分 ⭐ 新增
calculate_latex(ctx, "44'") # 0.73333333
# 7. 只有秒 ⭐ 新增
calculate_latex(ctx, "44''") # 0.01222222
DMS运算
# 加法
calculate_latex(ctx, "162°2'3'' + 10°30'15''") # 172.53791667
# 减法
calculate_latex(ctx, "162°2'3'' - 152°2'3''") # 10.0
# 乘法
calculate_latex(ctx, "45°30' \\times 2") # 91.0
# 除法(Bug已修复✅)
calculate_latex(ctx, "44'' / 4") # 0.00305556
DMS输出
# 输出为DMS格式
calculate_latex(ctx, "44'' / 4",
output_dms=True) # "0°0'11.00''"
calculate_latex(ctx, "162°2'3'' + 10°30'15''",
output_dms=True) # "172°32'16.50''"
混合格式
# 不同DMS格式混合运算
calculate_latex(ctx, "162° + 44'30''") # 162.74166667
calculate_latex(ctx, "162°2' - 44''") # 162.02111111
calculate_latex(ctx, "180° / 44'") # 245.45454545
负数支持
# 所有格式都支持负数
calculate_latex(ctx, "-162°2'3''") # -162.03416667
calculate_latex(ctx, "-44'") # -0.73333333
calculate_latex(ctx, "-44''") # -0.01222222
📚 API参考
calculate_latex
主要计算函数,支持所有LaTeX数学表达式。
def calculate_latex(
ctx: Context,
latex_expr: str, # LaTeX表达式(必填)
decimal_places: int = 2, # 小数位数
scientific_notation: bool = False, # 科学计数法
show_formula: bool = False, # 显示原始公式
degree: bool = True, # 角度制/弧度制
result_need_transform_degree: bool = False,# 结果转为度数
display_mode: int = 0, # 显示模式(0/1/2)
output_dms: bool = False # 输出DMS格式 ⭐ 新增
) -> str:
参数详解
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
latex_expr |
str | - | LaTeX数学表达式(必填) |
decimal_places |
int | 2 | 结果保留的小数位数 |
scientific_notation |
bool | False | 是否使用科学计数法输出 |
show_formula |
bool | False | 是否显示原始公式和结果 |
degree |
bool | True | 三角函数角度单位(True=度,False=弧度) |
result_need_transform_degree |
bool | False | 是否将弧度结果转换为度数 |
display_mode |
int | 0 | 公式显示格式(0=无,1=单$,2=双$$) |
output_dms |
bool | False | 是否将结果输出为度分秒格式 ⭐ |
返回值
- 成功: 返回计算结果字符串(数值或DMS格式)
- 失败: 返回错误描述信息
错误类型
- 参数验证错误: 参数类型或范围不正确
- LaTeX解析错误: LaTeX语法错误
- 数学域错误: 运算超出定义域(如asin(2))
- 计算错误: 其他计算失败情况
get_calculation_history
获取最近一次计算的历史记录。
def get_calculation_history(ctx: Context) -> str:
"""
返回最近计算的表达式和结果
"""
💡 实际应用场景
1. 测量工程
# 计算坡度角
calculate_latex(ctx, "\\atan{80/1500}",
result_need_transform_degree=True,
output_dms=True)
# 结果: "3°3'3.37''" (高度80m,距离1500m)
2. 航海导航
# 航向差计算
calculate_latex(ctx, "285°30'15'' - 95°45'30''",
output_dms=True)
# 结果: "189°44'45.00''"
3. 天文观测
# 时角计算
calculate_latex(ctx, "15° \\times 12.5",
output_dms=True)
# 结果: "187°30'0.00''" (12.5小时转为角度)
4. 建筑测量
# 高度计算
calculate_latex(ctx, "1500 \\times \\tan{3°3'3''}",
decimal_places=2)
# 结果: "80.00" (距离1500m,角度3°3'3'',求高度)
5. 科研计算
# 复杂三角运算
calculate_latex(ctx,
"2 \\times \\sin{30} + 3 \\times \\cos{60}",
show_formula=True)
# 结果: "2 \times \sin{30} + 3 \times \cos{60} = 2.5"
🧪 测试
运行所有测试
# 完整测试套件
python test_all_dms_formats.py # DMS格式测试
python test_dms_features.py # DMS功能测试
python test_regression.py # 回归测试
# 快速演示
python demo.py
测试覆盖
| 测试类别 | 文件 | 测试数 | 通过率 |
|---|---|---|---|
| DMS格式 | test_all_dms_formats.py | 30+ | 100% |
| DMS功能 | test_dms_features.py | 10+ | 100% |
| 回归测试 | test_regression.py | 19 | 100% |
| 总计 | - | 59+ | 100% |
📖 详细文档
- CHANGELOG.md - 完整的版本变更记录
- TOOLS_REFERENCE.md - 工具详细参考手册
- AI_QUICK_GUIDE.md - AI使用快速指南
- IMPROVEMENTS_SUMMARY.md - 改进总结
🐛 已知问题与限制
LaTeX解析限制
某些复杂LaTeX格式可能不被支持:
- 分数形式的π表达式:
\frac{\pi}{6}(需要使用数值) - 部分罕见函数(使用latex2sympy2的限制)
解决方案
# 不支持
calculate_latex(ctx, "\\sin{\\frac{\\pi}{6}}") # 可能失败
# 推荐做法
calculate_latex(ctx, "\\sin{0.5236}",
degree=False) # 成功
🔧 故障排查
问题:DMS格式不被识别
检查清单:
- 确保使用正确的符号:
°(度),'(分),''(秒) - 数字和符号之间可以有空格
- 负号应在最前面:
-162°2'3''
问题:计算结果不准确
检查清单:
- 确认
degree参数设置正确(度/弧度) - 检查
decimal_places是否足够 - 对于反三角函数,检查
result_need_transform_degree参数
问题:报错"参数验证错误"
常见原因:
- 小数位数为负数
- display_mode不是0/1/2
- LaTeX表达式为空
🤝 贡献
欢迎提交Issue和Pull Request!
开发指南
- Fork本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 提交Pull Request
代码规范
- 遵循PEP 8风格指南
- 添加完整的docstring
- 为新功能添加测试
- 更新相关文档
📜 更新日志
查看 CHANGELOG.md 了解详细的版本历史。
最新版本 v2.0 (2024-12-24)
- ✅ 完整支持7种DMS格式组合
- ✅ 修复
44'' / 4计算错误 - ✅ 新增
output_dms参数 - ✅ 结构化错误分类
- ✅ 30-40% 性能提升
- ✅ 100% 向后兼容
📄 许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件
📞 联系方式
- Issues: GitHub Issues
- Email: your-email@example.com
🙏 致谢
- latex2sympy2 - LaTeX解析
- SymPy - 符号计算
- FastMCP - MCP框架
- Anthropic Claude - AI开发助手
⭐ 如果这个项目对你有帮助,请给一个Star!
📊 项目统计: 49+ 测试用例 | 100% 通过率 | 7种DMS格式 | 30-40% 性能提升
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file latex_calculator_mcp-2.0.1.tar.gz.
File metadata
- Download URL: latex_calculator_mcp-2.0.1.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcaa121239264af6bc65ae8c8fc4c6c60a8d8098fa97d513004294ec5bd53a14
|
|
| MD5 |
eda2eda73787e1a31e8962f4df6e70d8
|
|
| BLAKE2b-256 |
cd9c18a868bb6fd3fd9ac92f7111685bdbc5a958a42e86abcc29ea2be40299cb
|
File details
Details for the file latex_calculator_mcp-2.0.1-py3-none-any.whl.
File metadata
- Download URL: latex_calculator_mcp-2.0.1-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fc77f1f9063c5f2a86e852bf3bed90d560ee17b365ca108eb54f5b00aee3454
|
|
| MD5 |
27da6eeefff54d8c522d9e66a67cc617
|
|
| BLAKE2b-256 |
70e7d90fa6501d2ac2ffeea548d0db6ef6829dab4b8fb8faeeb74b6716f88dd4
|