187 lines
6.1 KiB
Python
187 lines
6.1 KiB
Python
|
|
"""
|
||
|
|
Mathematical Tools for MCP
|
||
|
|
"""
|
||
|
|
from typing import List
|
||
|
|
from ..core.types import MCPTool
|
||
|
|
|
||
|
|
|
||
|
|
class MathTools:
|
||
|
|
"""Collection of mathematical tools"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_tools() -> List[MCPTool]:
|
||
|
|
"""Get all math tools"""
|
||
|
|
return [
|
||
|
|
MathTools._create_add_tool(),
|
||
|
|
MathTools._create_subtract_tool(),
|
||
|
|
MathTools._create_multiply_tool(),
|
||
|
|
MathTools._create_divide_tool(),
|
||
|
|
MathTools._create_power_tool(),
|
||
|
|
MathTools._create_square_root_tool(),
|
||
|
|
MathTools._create_calculate_bmi_tool(),
|
||
|
|
]
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_add_tool() -> MCPTool:
|
||
|
|
"""Create addition tool"""
|
||
|
|
async def add(a: float, b: float) -> float:
|
||
|
|
"""Add two numbers together"""
|
||
|
|
return a + b
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="add",
|
||
|
|
description="Add two numbers together",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"a": {"type": "number", "description": "First number"},
|
||
|
|
"b": {"type": "number", "description": "Second number"},
|
||
|
|
},
|
||
|
|
"required": ["a", "b"],
|
||
|
|
},
|
||
|
|
handler=add,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_subtract_tool() -> MCPTool:
|
||
|
|
"""Create subtraction tool"""
|
||
|
|
async def subtract(a: float, b: float) -> float:
|
||
|
|
"""Subtract second number from first"""
|
||
|
|
return a - b
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="subtract",
|
||
|
|
description="Subtract second number from first",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"a": {"type": "number", "description": "First number"},
|
||
|
|
"b": {"type": "number", "description": "Number to subtract"},
|
||
|
|
},
|
||
|
|
"required": ["a", "b"],
|
||
|
|
},
|
||
|
|
handler=subtract,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_multiply_tool() -> MCPTool:
|
||
|
|
"""Create multiplication tool"""
|
||
|
|
async def multiply(a: float, b: float) -> float:
|
||
|
|
"""Multiply two numbers"""
|
||
|
|
return a * b
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="multiply",
|
||
|
|
description="Multiply two numbers",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"a": {"type": "number", "description": "First number"},
|
||
|
|
"b": {"type": "number", "description": "Second number"},
|
||
|
|
},
|
||
|
|
"required": ["a", "b"],
|
||
|
|
},
|
||
|
|
handler=multiply,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_divide_tool() -> MCPTool:
|
||
|
|
"""Create division tool"""
|
||
|
|
async def divide(a: float, b: float) -> float:
|
||
|
|
"""Divide first number by second"""
|
||
|
|
if b == 0:
|
||
|
|
raise ValueError("Cannot divide by zero")
|
||
|
|
return a / b
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="divide",
|
||
|
|
description="Divide first number by second",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"a": {"type": "number", "description": "Dividend"},
|
||
|
|
"b": {"type": "number", "description": "Divisor (cannot be zero)"},
|
||
|
|
},
|
||
|
|
"required": ["a", "b"],
|
||
|
|
},
|
||
|
|
handler=divide,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_power_tool() -> MCPTool:
|
||
|
|
"""Create power tool"""
|
||
|
|
async def power(base: float, exponent: float) -> float:
|
||
|
|
"""Calculate base raised to the power of exponent"""
|
||
|
|
return base ** exponent
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="power",
|
||
|
|
description="Calculate base raised to the power of exponent",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"base": {"type": "number", "description": "Base number"},
|
||
|
|
"exponent": {"type": "number", "description": "Exponent"},
|
||
|
|
},
|
||
|
|
"required": ["base", "exponent"],
|
||
|
|
},
|
||
|
|
handler=power,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_square_root_tool() -> MCPTool:
|
||
|
|
"""Create square root tool"""
|
||
|
|
async def square_root(number: float) -> float:
|
||
|
|
"""Calculate square root of a number"""
|
||
|
|
if number < 0:
|
||
|
|
raise ValueError("Cannot calculate square root of negative number")
|
||
|
|
return number ** 0.5
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="square_root",
|
||
|
|
description="Calculate square root of a number",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"number": {"type": "number", "description": "Number to find square root of (must be non-negative)"},
|
||
|
|
},
|
||
|
|
"required": ["number"],
|
||
|
|
},
|
||
|
|
handler=square_root,
|
||
|
|
)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _create_calculate_bmi_tool() -> MCPTool:
|
||
|
|
"""Create BMI calculation tool"""
|
||
|
|
async def calculate_bmi(weight_kg: float, height_m: float) -> str:
|
||
|
|
"""Calculate BMI and provide health category"""
|
||
|
|
if weight_kg <= 0 or height_m <= 0:
|
||
|
|
raise ValueError("Weight and height must be positive numbers")
|
||
|
|
|
||
|
|
bmi = weight_kg / (height_m ** 2)
|
||
|
|
|
||
|
|
if bmi < 18.5:
|
||
|
|
category = "Underweight"
|
||
|
|
elif bmi < 25:
|
||
|
|
category = "Normal weight"
|
||
|
|
elif bmi < 30:
|
||
|
|
category = "Overweight"
|
||
|
|
else:
|
||
|
|
category = "Obese"
|
||
|
|
|
||
|
|
return ".1f"
|
||
|
|
|
||
|
|
return MCPTool(
|
||
|
|
name="calculate_bmi",
|
||
|
|
description="Calculate BMI and provide health assessment",
|
||
|
|
input_schema={
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"weight_kg": {"type": "number", "description": "Weight in kilograms"},
|
||
|
|
"height_m": {"type": "number", "description": "Height in meters"},
|
||
|
|
},
|
||
|
|
"required": ["weight_kg", "height_m"],
|
||
|
|
},
|
||
|
|
handler=calculate_bmi,
|
||
|
|
)
|