Multi-MCP / tools /calculator.py
Zoro-147's picture
Upload 16 files
c4070e8 verified
def calculate(prompt, operation):
try:
# Extract numbers from the prompt
numbers = [int(s) for s in prompt.split() if s.isdigit()]
if len(numbers) < 2:
return "Please provide at least two numbers for calculation."
num1 = numbers[0]
num2 = numbers[1]
if operation == "sum":
result = num1 + num2
return f"The sum of {num1} and {num2} is {result}."
elif operation == "multiply":
result = num1 * num2
return f"The product of {num1} and {num2} is {result}."
elif operation == "subtract":
result = num1 - num2
return f"The difference between {num1} and {num2} is {result}."
elif operation == "divide":
if num2 == 0:
return "Cannot divide by zero."
result = num1 / num2
return f"The division of {num1} by {num2} is {result}."
else:
return "Invalid operation."
except Exception as e:
return f"Error during calculation: {e}"