- Enhanced `math_server.py` to handle invalid tool requests and unknown inputs gracefully - Updated `test_integration.py` to include edge case scenarios for validation - Ensured agents dynamically discover available tools and respond accordinglypull/819/head
parent
6e2d79dd32
commit
d71030859d
@ -1,17 +1,77 @@
|
||||
|
||||
from fastmcp import FastMCP
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
# Initialize MCP server
|
||||
mcp = FastMCP("Math-Server")
|
||||
|
||||
# Add tool documentation and type hints
|
||||
@mcp.tool()
|
||||
def add(a: int, b: int) -> int:
|
||||
"""Add two numbers"""
|
||||
return a + b
|
||||
def add(a: int, b: int) -> Dict[str, Any]:
|
||||
"""Add two numbers together
|
||||
|
||||
Args:
|
||||
a (int): First number to add
|
||||
b (int): Second number to add
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: Result dictionary containing sum and metadata
|
||||
"""
|
||||
try:
|
||||
result = a + b
|
||||
return {
|
||||
"status": "success",
|
||||
"result": result,
|
||||
"message": f"Successfully added {a} and {b}"
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "error",
|
||||
"error": str(e),
|
||||
"message": "Failed to perform addition"
|
||||
}
|
||||
|
||||
@mcp.tool()
|
||||
def multiply(a: int, b: int) -> int:
|
||||
"""Multiply two numbers"""
|
||||
return a * b
|
||||
def multiply(a: int, b: int) -> Dict[str, Any]:
|
||||
"""Multiply two numbers together
|
||||
|
||||
Args:
|
||||
a (int): First number to multiply
|
||||
b (int): Second number to multiply
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: Result dictionary containing product and metadata
|
||||
"""
|
||||
try:
|
||||
result = a * b
|
||||
return {
|
||||
"status": "success",
|
||||
"result": result,
|
||||
"message": f"Successfully multiplied {a} and {b}"
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "error",
|
||||
"error": str(e),
|
||||
"message": "Failed to perform multiplication"
|
||||
}
|
||||
|
||||
@mcp.tool()
|
||||
def get_available_operations() -> Dict[str, Any]:
|
||||
"""Get list of available mathematical operations
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: Dictionary containing available operations and their descriptions
|
||||
"""
|
||||
return {
|
||||
"status": "success",
|
||||
"operations": {
|
||||
"add": "Add two numbers together",
|
||||
"multiply": "Multiply two numbers together"
|
||||
}
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting Math Server...")
|
||||
print("Available operations:", get_available_operations())
|
||||
mcp.run(host="0.0.0.0", port=6274, transport="sse")
|
||||
|
Loading…
Reference in new issue