make calculator into mcp server
This commit is contained in:
@@ -3,6 +3,7 @@ import random
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Type, List
|
from typing import Type, List
|
||||||
import tyro
|
import tyro
|
||||||
|
import time
|
||||||
|
|
||||||
from lang_agent.config import ToolConfig
|
from lang_agent.config import ToolConfig
|
||||||
from lang_agent.base import LangToolBase
|
from lang_agent.base import LangToolBase
|
||||||
@@ -19,9 +20,28 @@ class Calculator(LangToolBase):
|
|||||||
|
|
||||||
def calculator(self, python_expression: str) -> dict:
|
def calculator(self, 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'."""
|
"""For mathamatical calculation, always use this tool to calculate the result of a python expression. You can use 'math' or 'random' directly, without 'import'."""
|
||||||
|
# time.sleep(2)
|
||||||
result = eval(python_expression, {"math": math, "random": random})
|
result = eval(python_expression, {"math": math, "random": random})
|
||||||
return {"success": True, "result": result}
|
return {"success": True, "result": result}
|
||||||
|
|
||||||
def get_tool_fnc(self):
|
def get_tool_fnc(self):
|
||||||
return [self.calculator]
|
return [self.calculator]
|
||||||
|
|
||||||
|
|
||||||
|
# Global calculator instance
|
||||||
|
calculator = Calculator(CalculatorConfig())
|
||||||
|
|
||||||
|
# FastMCP server setup
|
||||||
|
from fastmcp import FastMCP
|
||||||
|
|
||||||
|
mcp = FastMCP("Calculator Server")
|
||||||
|
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def calculate(python_expression: str) -> dict:
|
||||||
|
"""For mathematical calculation, always use this tool to calculate the result of a python expression. You can use 'math' or 'random' directly, without 'import'."""
|
||||||
|
return calculator.calculator(python_expression)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
mcp.run(transport="streamable-http", host="0.0.0.0", port=9000)
|
||||||
|
|||||||
Reference in New Issue
Block a user