You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/examples/mcp_example/math_server.py

78 lines
2.0 KiB

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) -> 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) -> 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")