refactor(math): simplify math operations and improve logging in mcp_client.py and mock_math_server.py
parent
8fd41d24aa
commit
812115a2a6
@ -0,0 +1,66 @@
|
|||||||
|
=== MINIMAL MCP AGENT INTEGRATION TEST ===
|
||||||
|
Testing only the core MCP integration with Agent
|
||||||
|
2025-04-20T18:19:47.080439+0000 | INFO | mcp_client:main:33 - Creating MCP server parameters...
|
||||||
|
2025-04-20T18:19:47.080601+0000 | INFO | mcp_client:main:45 - MCP Server URL: http://0.0.0.0:8000
|
||||||
|
2025-04-20T18:19:47.080686+0000 | INFO | mcp_client:main:46 - MCP Headers: {'Content-Type': 'application/json', 'Accept': 'text/event-stream'}
|
||||||
|
2025-04-20T18:19:47.080763+0000 | INFO | mcp_client:main:49 - Creating Agent with MCP integration...
|
||||||
|
2025-04-20T18:19:47.088927+0000 | WARNING | agent:llm_handling:613 - Model name is not provided, using gpt-4o-mini. You can configure any model from litellm if desired.
|
||||||
|
|
||||||
|
Agent created successfully!
|
||||||
|
Enter a math query or 'exit' to quit
|
||||||
|
|
||||||
|
Math query: what tools you have?
|
||||||
|
2025-04-20T18:20:39.747943+0000 | INFO | mcp_client:main:66 - Processing query: what tools you have?
|
||||||
|
╭────────────────── Agent Name MCP Test Agent [Max Loops: 1 ] ───────────────────╮
|
||||||
|
│ MCP Test Agent: {"tool_name": "none", "description": "I can perform addition, │
|
||||||
|
│ multiplication, and division using specific tools."} │
|
||||||
|
╰────────────────────────────────────────────────────────────────────────────────╯
|
||||||
|
2025-04-20T18:20:41.073474+0000 | ERROR | mcp_integration:connect:89 - Error initializing MCP server: unhandled errors in a TaskGroup (1 sub-exception)
|
||||||
|
2025-04-20T18:20:41.073628+0000 | ERROR | mcp_integration:abatch_mcp_flow:268 - Error in abatch_mcp_flow: unhandled errors in a TaskGroup (1 sub-exception)
|
||||||
|
╭────────── Agent Name MCP Test Agent - Tool Executor [Max Loops: 1 ] ───────────╮
|
||||||
|
│ MCP Test Agent - Tool Executor: Error in batch operation: unhandled errors in │
|
||||||
|
│ a TaskGroup (1 sub-exception) │
|
||||||
|
╰────────────────────────────────────────────────────────────────────────────────╯
|
||||||
|
╭────────── Agent Name MCP Test Agent - Agent Analysis [Max Loops: 1 ] ──────────╮
|
||||||
|
│ MCP Test Agent - Agent Analysis: It seems like you're encountering an error │
|
||||||
|
│ related to a batch operation in a programming or data processing context. │
|
||||||
|
│ However, I can assist you with mathematical calculations. If you have any │
|
||||||
|
│ specific calculations you'd like to perform, please provide the numbers and │
|
||||||
|
│ the operation (addition, multiplication, or division), and I'll format it as │
|
||||||
|
│ JSON for you. │
|
||||||
|
╰────────────────────────────────────────────────────────────────────────────────╯
|
||||||
|
|
||||||
|
Result: System: : Your Name: MCP Test Agent
|
||||||
|
|
||||||
|
Your Description: None
|
||||||
|
|
||||||
|
|
||||||
|
You are a math calculator assistant that uses tools to perform calculations.
|
||||||
|
|
||||||
|
When asked for calculations, determine the operation and numbers, then use one of these tools:
|
||||||
|
- add: Add two numbers
|
||||||
|
- multiply: Multiply two numbers
|
||||||
|
- divide: Divide first number by second
|
||||||
|
|
||||||
|
FORMAT as JSON:
|
||||||
|
{"tool_name": "add", "a": 5, "b": 10}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Human:: what tools you have?
|
||||||
|
|
||||||
|
|
||||||
|
MCP Test Agent: {"tool_name": "none", "description": "I can perform addition, multiplication, and division using specific tools."}
|
||||||
|
|
||||||
|
|
||||||
|
assistant: Tool execution result: ['Error in batch operation: unhandled errors in a TaskGroup (1 sub-exception)']
|
||||||
|
|
||||||
|
|
||||||
|
Tool Executor: Error in batch operation: unhandled errors in a TaskGroup (1 sub-exception)
|
||||||
|
|
||||||
|
|
||||||
|
MCP Test Agent: It seems like you're encountering an error related to a batch operation in a programming or data processing context. However, I can assist you with mathematical calculations. If you have any specific calculations you'd like to perform, please provide the numbers and the operation (addition, multiplication, or division), and I'll format it as JSON for you.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Math query:
|
@ -1,177 +1,86 @@
|
|||||||
from fastmcp import FastMCP
|
from fastmcp import FastMCP
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
import time
|
import time
|
||||||
import json
|
|
||||||
|
|
||||||
# Create the MCP server with detailed debugging
|
# Create the MCP server with all interfaces binding
|
||||||
mcp = FastMCP(
|
mcp = FastMCP(
|
||||||
host="0.0.0.0", # Bind to all interfaces
|
host=
|
||||||
|
"0.0.0.0", # Bind to all interfaces to be accessible from other contexts
|
||||||
port=8000,
|
port=8000,
|
||||||
transport="sse",
|
transport="sse",
|
||||||
require_session_id=False,
|
require_session_id=False,
|
||||||
cors_allowed_origins=["*"], # Allow connections from any origin
|
cors_allowed_origins=["*"], # Allow all origins for testing
|
||||||
debug=True # Enable debug mode for more verbose output
|
debug=True # Enable debug mode
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Add a more flexible parsing approach
|
# Define tools
|
||||||
def parse_input(input_str):
|
|
||||||
"""Parse input that could be JSON or natural language."""
|
|
||||||
try:
|
|
||||||
# First try to parse as JSON
|
|
||||||
return json.loads(input_str)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# If not JSON, try to parse natural language
|
|
||||||
input_lower = input_str.lower()
|
|
||||||
|
|
||||||
# Parse for addition
|
|
||||||
if "add" in input_lower or "plus" in input_lower or "sum" in input_lower:
|
|
||||||
# Extract numbers - very simple approach
|
|
||||||
numbers = [int(s) for s in input_lower.split() if s.isdigit()]
|
|
||||||
if len(numbers) >= 2:
|
|
||||||
return {"a": numbers[0], "b": numbers[1]}
|
|
||||||
|
|
||||||
# Parse for multiplication
|
|
||||||
if "multiply" in input_lower or "times" in input_lower or "product" in input_lower:
|
|
||||||
numbers = [int(s) for s in input_lower.split() if s.isdigit()]
|
|
||||||
if len(numbers) >= 2:
|
|
||||||
return {"a": numbers[0], "b": numbers[1]}
|
|
||||||
|
|
||||||
# Parse for division
|
|
||||||
if "divide" in input_lower or "quotient" in input_lower:
|
|
||||||
numbers = [int(s) for s in input_lower.split() if s.isdigit()]
|
|
||||||
if len(numbers) >= 2:
|
|
||||||
return {"a": numbers[0], "b": numbers[1]}
|
|
||||||
|
|
||||||
# Could not parse successfully
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
# Define tools with more flexible input handling
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def add(input_str=None, a=None, b=None):
|
def add(a: int, b: int) -> str:
|
||||||
"""Add two numbers. Can accept JSON parameters or natural language.
|
"""Add two numbers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
input_str (str, optional): Natural language input to parse
|
a (int): First number
|
||||||
a (int, optional): First number if provided directly
|
b (int): Second number
|
||||||
b (int, optional): Second number if provided directly
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: A message containing the sum
|
str: A message containing the sum
|
||||||
"""
|
"""
|
||||||
logger.info(f"Add tool called with input_str={input_str}, a={a}, b={b}")
|
logger.info(f"Adding {a} and {b}")
|
||||||
|
result = a + b
|
||||||
# If we got a natural language string instead of parameters
|
return f"The sum of {a} and {b} is {result}"
|
||||||
if input_str and not (a is not None and b is not None):
|
|
||||||
parsed = parse_input(input_str)
|
|
||||||
if parsed:
|
|
||||||
a = parsed.get("a")
|
|
||||||
b = parsed.get("b")
|
|
||||||
|
|
||||||
# Validate we have what we need
|
|
||||||
if a is None or b is None:
|
|
||||||
return "Sorry, I couldn't understand the numbers to add"
|
|
||||||
|
|
||||||
try:
|
|
||||||
a = int(a)
|
|
||||||
b = int(b)
|
|
||||||
result = a + b
|
|
||||||
return f"The sum of {a} and {b} is {result}"
|
|
||||||
except ValueError:
|
|
||||||
return "Please provide valid numbers for addition"
|
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def multiply(input_str=None, a=None, b=None):
|
def multiply(a: int, b: int) -> str:
|
||||||
"""Multiply two numbers. Can accept JSON parameters or natural language.
|
"""Multiply two numbers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
input_str (str, optional): Natural language input to parse
|
a (int): First number
|
||||||
a (int, optional): First number if provided directly
|
b (int): Second number
|
||||||
b (int, optional): Second number if provided directly
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: A message containing the product
|
str: A message containing the product
|
||||||
"""
|
"""
|
||||||
logger.info(
|
logger.info(f"Multiplying {a} and {b}")
|
||||||
f"Multiply tool called with input_str={input_str}, a={a}, b={b}")
|
result = a * b
|
||||||
|
return f"The product of {a} and {b} is {result}"
|
||||||
# If we got a natural language string instead of parameters
|
|
||||||
if input_str and not (a is not None and b is not None):
|
|
||||||
parsed = parse_input(input_str)
|
|
||||||
if parsed:
|
|
||||||
a = parsed.get("a")
|
|
||||||
b = parsed.get("b")
|
|
||||||
|
|
||||||
# Validate we have what we need
|
|
||||||
if a is None or b is None:
|
|
||||||
return "Sorry, I couldn't understand the numbers to multiply"
|
|
||||||
|
|
||||||
try:
|
|
||||||
a = int(a)
|
|
||||||
b = int(b)
|
|
||||||
result = a * b
|
|
||||||
return f"The product of {a} and {b} is {result}"
|
|
||||||
except ValueError:
|
|
||||||
return "Please provide valid numbers for multiplication"
|
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def divide(input_str=None, a=None, b=None):
|
def divide(a: int, b: int) -> str:
|
||||||
"""Divide two numbers. Can accept JSON parameters or natural language.
|
"""Divide two numbers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
input_str (str, optional): Natural language input to parse
|
a (int): Numerator
|
||||||
a (int, optional): Numerator if provided directly
|
b (int): Denominator
|
||||||
b (int, optional): Denominator if provided directly
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: A message containing the division result or an error message
|
str: A message containing the division result or an error message
|
||||||
"""
|
"""
|
||||||
logger.info(f"Divide tool called with input_str={input_str}, a={a}, b={b}")
|
logger.info(f"Dividing {a} by {b}")
|
||||||
|
if b == 0:
|
||||||
# If we got a natural language string instead of parameters
|
logger.warning("Division by zero attempted")
|
||||||
if input_str and not (a is not None and b is not None):
|
return "Cannot divide by zero"
|
||||||
parsed = parse_input(input_str)
|
result = a / b
|
||||||
if parsed:
|
return f"{a} divided by {b} is {result}"
|
||||||
a = parsed.get("a")
|
|
||||||
b = parsed.get("b")
|
|
||||||
|
|
||||||
# Validate we have what we need
|
|
||||||
if a is None or b is None:
|
|
||||||
return "Sorry, I couldn't understand the numbers to divide"
|
|
||||||
|
|
||||||
try:
|
|
||||||
a = int(a)
|
|
||||||
b = int(b)
|
|
||||||
|
|
||||||
if b == 0:
|
|
||||||
logger.warning("Division by zero attempted")
|
|
||||||
return "Cannot divide by zero"
|
|
||||||
|
|
||||||
result = a / b
|
|
||||||
return f"{a} divided by {b} is {result}"
|
|
||||||
except ValueError:
|
|
||||||
return "Please provide valid numbers for division"
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
logger.info("Starting math server on http://0.0.0.0:8000")
|
# Log server details
|
||||||
print("Math MCP Server is running. Press Ctrl+C to stop.")
|
logger.info("Starting math server on http://0.0.0.0:8000")
|
||||||
print(
|
print("Math MCP Server is running on http://0.0.0.0:8000")
|
||||||
"Server is configured to accept both JSON and natural language input"
|
print("Press Ctrl+C to stop.")
|
||||||
)
|
|
||||||
|
# List available tools
|
||||||
# Add a small delay to ensure logging is complete before the server starts
|
print("\nAvailable tools:")
|
||||||
time.sleep(0.5)
|
print("- add: Add two numbers")
|
||||||
|
print("- multiply: Multiply two numbers")
|
||||||
# Run the MCP server
|
print("- divide: Divide first number by second number")
|
||||||
mcp.run()
|
|
||||||
except KeyboardInterrupt:
|
# Add a small delay to ensure logging is complete
|
||||||
logger.info("Server shutdown requested")
|
time.sleep(0.5)
|
||||||
print("\nShutting down server...")
|
|
||||||
except Exception as e:
|
# Run the MCP server
|
||||||
logger.error(f"Server error: {e}")
|
mcp.run()
|
||||||
raise
|
except KeyboardInterrupt:
|
||||||
|
logger.info("Server shutdown requested")
|
||||||
|
print("\nShutting down server...")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Server error: {e}")
|
||||||
|
raise
|
||||||
|
Loading…
Reference in new issue