another tool

This commit is contained in:
2025-10-14 20:17:01 +08:00
parent 783864a3f8
commit 0935c0ff0b

View File

@@ -1,45 +1,27 @@
# NOTE: dummy mcp for developement
# server.py
from mcp.server.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 math
import random import random
from dataclasses import dataclass, field
from typing import Type, List
import tyro
# Create an MCP server from lang_agent.config import ToolConfig
mcp = FastMCP("Calculator") from lang_agent.base import LangToolBase
# Add an addition tool @tyro.conf.configure(tyro.conf.SuppressFixed)
# @mcp.tool() @dataclass
# def calculator(python_expression: str) -> dict: class CalculatorConfig(ToolConfig):
# """For mathamatical calculation, always use this tool to calculate the result of a python expression. You can use 'math' or 'random' directly, without 'import'.""" _target:Type = field(default_factory=lambda: Calculator)
# result = eval(python_expression, {"math": math, "random": random})
# logger.info(f"Calculating formula: {python_expression}, result: {result}")
# return {"success": True, "result": result}
class Calculator:
def __init__(self):
pass
@mcp.tool() class Calculator(LangToolBase):
def __init__(self, config: CalculatorConfig):
self.config = config
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'."""
result = eval(python_expression, {"math": math, "random": random}) result = eval(python_expression, {"math": math, "random": random})
logger.info(f"Calculating formula: {python_expression}, result: {result}")
return {"success": True, "result": result} return {"success": True, "result": result}
# Start the server
if __name__ == "__main__":
mcp.run(transport="stdio") def get_tool_fnc(self):
return [self.calculator]