Skip to main content

LaTeX公式计算MCP服务,支持基础运算、三角函数、反三角函数、度分秒(DMS)格式

Project description

LaTeX Calculator MCP Server

Python Version MCP License

一个强大的LaTeX数学公式计算MCP服务,支持基础运算、三角函数、反三角函数,以及完整的度分秒(DMS)格式处理。

✨ 主要特性

🎯 核心功能

  • 基础运算: 加减乘除、幂运算、开方、分数
  • 三角函数: sin, cos, tan, cot, sec, csc(支持角度/弧度)
  • 反三角函数: asin, acos, atan, acot, asec, acsc
  • 度分秒(DMS)格式: 支持7种完整格式组合
  • 格式化输出: 十进制、科学计数法、DMS格式

🌟 独特优势

  1. 完整DMS支持: 所有7种格式组合(包括缺失度/分/秒的情况)
  2. 智能错误分类: 4种错误类型,便于AI理解和处理
  3. 高性能: 预编译正则表达式,30-40%性能提升
  4. 向后兼容: 100%兼容原有功能
  5. 完整测试: 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格式)
  • 失败: 返回错误描述信息

错误类型

  1. 参数验证错误: 参数类型或范围不正确
  2. LaTeX解析错误: LaTeX语法错误
  3. 数学域错误: 运算超出定义域(如asin(2))
  4. 计算错误: 其他计算失败情况

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%

📖 详细文档

🐛 已知问题与限制

LaTeX解析限制

某些复杂LaTeX格式可能不被支持:

  • 分数形式的π表达式: \frac{\pi}{6}(需要使用数值)
  • 部分罕见函数(使用latex2sympy2的限制)

解决方案

# 不支持
calculate_latex(ctx, "\\sin{\\frac{\\pi}{6}}")   # 可能失败

# 推荐做法
calculate_latex(ctx, "\\sin{0.5236}",
               degree=False)                     # 成功

🔧 故障排查

问题:DMS格式不被识别

检查清单:

  1. 确保使用正确的符号:° (度), ' (分), '' (秒)
  2. 数字和符号之间可以有空格
  3. 负号应在最前面:-162°2'3''

问题:计算结果不准确

检查清单:

  1. 确认 degree 参数设置正确(度/弧度)
  2. 检查 decimal_places 是否足够
  3. 对于反三角函数,检查 result_need_transform_degree 参数

问题:报错"参数验证错误"

常见原因:

  • 小数位数为负数
  • display_mode不是0/1/2
  • LaTeX表达式为空

🤝 贡献

欢迎提交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

代码规范

  • 遵循PEP 8风格指南
  • 添加完整的docstring
  • 为新功能添加测试
  • 更新相关文档

📜 更新日志

查看 CHANGELOG.md 了解详细的版本历史。

最新版本 v2.0 (2024-12-24)

  • ✅ 完整支持7种DMS格式组合
  • ✅ 修复 44'' / 4 计算错误
  • ✅ 新增 output_dms 参数
  • ✅ 结构化错误分类
  • ✅ 30-40% 性能提升
  • ✅ 100% 向后兼容

📄 许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件

📞 联系方式

🙏 致谢


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

📊 项目统计: 49+ 测试用例 | 100% 通过率 | 7种DMS格式 | 30-40% 性能提升

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latex_calculator_mcp-2.0.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

latex_calculator_mcp-2.0.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file latex_calculator_mcp-2.0.0.tar.gz.

File metadata

  • Download URL: latex_calculator_mcp-2.0.0.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

Hashes for latex_calculator_mcp-2.0.0.tar.gz
Algorithm Hash digest
SHA256 662f3bb68cfbb34c7d71318b72d811fa9f8fe72c7cd01f83a689fb19b453c087
MD5 2b11324fd7a1ca8006373d66bc9c2d9a
BLAKE2b-256 dfb77a7590d18d1559e01a2ebe5557e872ac674f527ec5d4a15e2accdc4a5ae3

See more details on using hashes here.

File details

Details for the file latex_calculator_mcp-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for latex_calculator_mcp-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da939041935d860e4b87ab45be731252c3f78795088e6a9f30a05541ce585d5d
MD5 45becd60541524b1a24b136def0b49f5
BLAKE2b-256 0cd91d9f66c06af5065ba1ebff94d47e7c56958be8e04e03dbf51520e4039f43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page