feat(mcp): integrate natural language interface for improved agent interaction

pull/819/head
Pavan Kumar 3 months ago committed by ascender1729
parent c7206baf1d
commit 4b6f9a529f

@ -1,3 +1,4 @@
from swarms import Agent from swarms import Agent
from swarms.tools.mcp_integration import MCPServerSseParams from swarms.tools.mcp_integration import MCPServerSseParams
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
@ -13,11 +14,12 @@ def initialize_math_system():
) )
math_agent = Agent( math_agent = Agent(
agent_name="Math Agent", agent_name="Math Assistant",
agent_description="Basic math calculator", agent_description="Friendly math calculator",
system_prompt=MATH_AGENT_PROMPT, system_prompt=MATH_AGENT_PROMPT,
max_loops=1, max_loops=1,
mcp_servers=[math_server] mcp_servers=[math_server],
model_name="gpt-3.5-turbo"
) )
return math_agent return math_agent
@ -26,26 +28,26 @@ def main():
math_agent = initialize_math_system() math_agent = initialize_math_system()
print("\nMath Calculator Ready!") print("\nMath Calculator Ready!")
print("Available operations: add, multiply, divide") print("Ask me any math question!")
print("Example: 'add 5 and 3' or 'multiply 4 by 6'") print("Examples: 'what is 5 plus 3?' or 'can you multiply 4 and 6?'")
print("Type 'exit' to quit\n") print("Type 'exit' to quit\n")
while True: while True:
try: try:
query = input("Enter math operation: ").strip() query = input("What would you like to calculate? ").strip()
if not query: if not query:
continue continue
if query.lower() == 'exit': if query.lower() == 'exit':
break break
result = math_agent.run(query) result = math_agent.run(query)
print(f"Result: {result}") print(f"\nResult: {result}\n")
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nExiting...") print("\nGoodbye!")
break break
except Exception as e: except Exception as e:
logger.error(f"Error: {e}") logger.error(f"Error: {e}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

@ -1,3 +1,4 @@
from fastmcp import FastMCP from fastmcp import FastMCP
from loguru import logger from loguru import logger
@ -5,26 +6,28 @@ mcp = FastMCP(
host="0.0.0.0", host="0.0.0.0",
port=8000, port=8000,
transport="sse", transport="sse",
require_session_id=False, require_session_id=False
timeout=30.0
) )
@mcp.tool() @mcp.tool()
def add(a: int, b: int) -> int: def add(a: int, b: int) -> str:
"""Add two numbers.""" """Add two numbers."""
return a + b result = a + b
return f"The sum of {a} and {b} is {result}"
@mcp.tool() @mcp.tool()
def multiply(a: int, b: int) -> int: def multiply(a: int, b: int) -> str:
"""Multiply two numbers.""" """Multiply two numbers."""
return a * b result = a * b
return f"The product of {a} and {b} is {result}"
@mcp.tool() @mcp.tool()
def divide(a: int, b: int) -> float: def divide(a: int, b: int) -> str:
"""Divide two numbers.""" """Divide two numbers."""
if b == 0: if b == 0:
raise ValueError("Cannot divide by zero") return "Cannot divide by zero"
return a / b result = a / b
return f"{a} divided by {b} is {result}"
if __name__ == "__main__": if __name__ == "__main__":
try: try:
@ -32,4 +35,4 @@ if __name__ == "__main__":
mcp.run() mcp.run()
except Exception as e: except Exception as e:
logger.error(f"Server error: {e}") logger.error(f"Server error: {e}")
raise raise

@ -1,20 +1,23 @@
# Agent prompts for MCP testing and interactions # Agent prompts for MCP testing and interactions
MATH_AGENT_PROMPT = '''You are a math calculator agent that performs basic arithmetic operations. MATH_AGENT_PROMPT = '''You are a helpful math calculator assistant.
Your role is to understand natural language math requests and perform calculations.
Available operations: When asked to perform calculations:
- Addition: add numbers together 1. Determine the operation (add, multiply, or divide)
- Multiplication: multiply numbers 2. Extract the numbers from the request
- Division: divide numbers (checks for division by zero) 3. Use the appropriate math operation tool
You must respond with the operation and numbers in this exact format: Respond conversationally but be concise.
{"tool_name": "<operation>", "a": <first_number>, "b": <second_number>}
Example: Example:
User: "add 5 and 3" User: "what is 5 plus 3?"
You: {"tool_name": "add", "a": 5, "b": 3} You: Using the add operation for 5 and 3
{"tool_name": "add", "a": 5, "b": 3}
Parse the numbers as integers. Only return the operation format, no other text.''' User: "multiply 4 times 6"
You: Using multiply for 4 and 6
{"tool_name": "multiply", "a": 4, "b": 6}'''
FINANCE_AGENT_PROMPT = """You are a financial analysis agent with access to stock market data services. FINANCE_AGENT_PROMPT = """You are a financial analysis agent with access to stock market data services.
Key responsibilities: Key responsibilities:
@ -177,4 +180,4 @@ def get_report_by_type(report_type):
"resource_report": generate_resource_report_prompt, "resource_report": generate_resource_report_prompt,
"outline_report": generate_outline_report_prompt, "outline_report": generate_outline_report_prompt,
} }
return report_type_mapping[report_type] return report_type_mapping[report_type]
Loading…
Cancel
Save