-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathcalculator.py
More file actions
29 lines (23 loc) · 874 Bytes
/
calculator.py
File metadata and controls
29 lines (23 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# server.py
from fastmcp import FastMCP
import sys
import logging
logger = logging.getLogger('Calculator')
# Fix UTF-8 encoding for Windows console
if sys.platform == 'win32':
sys.stderr.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')
import math
import random
# Create an MCP server
mcp = FastMCP("Calculator")
# Add an addition tool
@mcp.tool()
def calculator(python_expression: str) -> dict:
"""For mathamatical calculation, always use this tool to calculate the result of a python expression. You can use 'math' or 'random' directly, without 'import'."""
result = eval(python_expression, {"math": math, "random": random})
logger.info(f"Calculating formula: {python_expression}, result: {result}")
return {"success": True, "result": result}
# Start the server
if __name__ == "__main__":
mcp.run(transport="stdio")