数学计算服务

一个基于Model Context Protocol (MCP)的数学计算服务,提供算术、三角学、代数和数论等工具,适用于AI代理和MCP兼容客户端。

  • by BatoolRubab
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 batoolrubab/mcp-calculator-server

使用说明

MCP计算器服务器

最低限度,生产就绪 模型上下文协议(MCP) 通过Python MCP SDK公开一组数学实用程序(算术、三角学、代数、数论)的服务器 FastMCP (mcp.server.fastmcp.FastMCP).

✨ 特性

  • 使用简单、可读的实现 FastMCP
  • 16+工具,涵盖常见计算器操作
  • 带有有用错误消息的安全输入验证
  • 可与MCP Inspector、Claude Desktop和任何兼容MCP的客户端配合使用
  • 运输: stdio

🧰 可用工具

以下工具可供AI代理调用:

  • add(a: int, b: int) -> int --加两个数字。
  • sub(a: int, b: int) -> int --减去两个数字。
  • mul(a: int, b: int) -> int --将两个数字相乘。
  • div(a: int, b: int) -> float --除以两个数字(浮点结果)。
  • power(base: float, exponent: float) -> float --将数字提高到幂。
  • square_root(x: float) -> float --一个数字的平方根。
  • factorial(n: int) -> int --非负整数的因式分解。
  • log(x: float, base: float = math.e) -> float --带可选底的对数。
  • sin(x: float) -> float --正弦角(弧度)。
  • cos(x: float) -> float --角度的余弦(弧度)。
  • tan(x: float) -> float --角度的切线(弧度)。
  • degrees_to_radians(degrees: float) -> float --将度数转换为弧度。
  • radians_to_degrees(radians: float) -> float --将弧度转换为度数。
  • gcd(a: int, b: int) -> int --最大公约数。
  • lcm(a: int, b: int) -> int --最小公倍数。
  • is_prime(n: int) -> bool --检查一个数字是否为素数。
  • quadratic_roots(a: float, b: float, c: float) -> tuple --解决 ax² + bx + c = 0.

⚠️ 错误处理

  • 除以零→ 提高 ValueError.
  • 负数的平方根→ 提高 ValueError.
  • 负数的因式分解→ 提高 ValueError.
  • 输入无效的对数(非正数或无效基数)→ 提高 ValueError.

🔧 扩展服务器

通过装饰功能添加新工具 @mcp.tool() 并使用清晰的类型提示+文档字符串。 例子:

@mcp.tool()
def nth_fibonacci(n: int) -> int:
    """Return the nth Fibonacci number (n >= 0)"""
    if n < 0:
        raise ValueError("n must be non-negative")
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

重新启动服务器以公开新工具。


由以下材料制成❤️ 作者:Rubab Batool