One-MCP / tools /calculator.py
arcticaurora's picture
Update tools/calculator.py
0ed4bc7 verified
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Calculator")
@mcp.tool()
def add(a: float, b: float):
"""Add two numbers together.
Args:
a (float): The first number to add. Can be positive, negative, or decimal.
b (float): The second number to add. Can be positive, negative, or decimal.
Returns:
float: The sum of a and b.
Examples:
add(5, 3) returns 8.0
add(-2, 7) returns 5.0
add(3.14, 2.86) returns 6.0
"""
return a + b
@mcp.tool()
def subtract(a: float, b: float):
"""Subtract the second number from the first number.
Args:
a (float): The number to subtract from (minuend).
b (float): The number to subtract (subtrahend).
Returns:
float: The result of a minus b.
Examples:
subtract(10, 3) returns 7.0
subtract(5, 8) returns -3.0
subtract(3.5, 1.2) returns 2.3
"""
return a - b
@mcp.tool()
def multiply(a: float, b: float):
"""Multiply two numbers together.
Args:
a (float): The first number to multiply.
b (float): The second number to multiply.
Returns:
float: The product of a and b.
Examples:
multiply(4, 5) returns 20.0
multiply(-3, 2) returns -6.0
multiply(2.5, 4) returns 10.0
"""
return a * b
@mcp.tool()
def divide(a: float, b: float):
"""Divide the first number by the second number.
Args:
a (float): The dividend (number to be divided).
b (float): The divisor (number to divide by). Cannot be zero.
Returns:
float: The quotient of a divided by b.
Raises:
ValueError: If b is zero (division by zero is not allowed).
Examples:
divide(10, 2) returns 5.0
divide(15, 4) returns 3.75
divide(-8, 2) returns -4.0
"""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
@mcp.tool()
def power(base: float, exponent: float):
"""Raise a number to the power of another number.
Args:
base (float): The base number to be raised to a power.
exponent (float): The exponent (power) to raise the base to.
Returns:
float: The result of base raised to the exponent power.
Examples:
power(2, 3) returns 8.0 (2^3)
power(5, 2) returns 25.0 (5^2)
power(4, 0.5) returns 2.0 (square root of 4)
power(10, -1) returns 0.1 (1/10)
"""
return base ** exponent
@mcp.tool()
def square_root(number: float):
"""Calculate the square root of a number.
Args:
number (float): The number to find the square root of. Must be non-negative.
Returns:
float: The square root of the input number.
Raises:
ValueError: If number is negative (square root of negative numbers is not supported).
Examples:
square_root(9) returns 3.0
square_root(16) returns 4.0
square_root(2) returns 1.4142135623730951
square_root(0) returns 0.0
"""
if number < 0:
raise ValueError("Cannot calculate square root of negative number")
return number ** 0.5
if __name__ == "__main__":
mcp.run()