feat: set up multiple MCP servers and integrate with multi-agent test system

pull/819/head
Pavan Kumar 2 days ago committed by ascender1729
parent 10e056a05e
commit 6954a9ace4

@ -58,3 +58,24 @@ args = "sleep 2"
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "python examples/mcp_example/test_integration.py"
[[workflows.workflow]]
name = "Run Multiple MCP"
author = 13983571
mode = "sequential"
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "python examples/mcp_example/math_server.py & "
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "python examples/mcp_example/calc_server.py &"
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "sleep 2"
[[workflows.workflow.tasks]]
task = "shell.exec"
args = "python examples/mcp_example/test_integration.py"

@ -0,0 +1,27 @@
from fastmcp import FastMCP
from typing import Dict, Any
import math
# Initialize MCP server
mcp = FastMCP("Calc-Server")
@mcp.tool()
def square_root(x: float) -> float:
"""Calculate square root of a number"""
try:
return math.sqrt(x)
except Exception as e:
return {"error": str(e)}
@mcp.tool()
def power(base: float, exponent: float) -> float:
"""Raise a number to a power"""
try:
return math.pow(base, exponent)
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
print("Starting Calc Server on port 6275...")
mcp.run(port=6275, transport="sse")

@ -1,5 +1,6 @@
from fastmcp import FastMCP
from typing import Dict, Any, Optional
from typing import Dict, Any
# Initialize MCP server
mcp = FastMCP("Math-Server")
@ -20,6 +21,14 @@ def multiply(a: int, b: int) -> int:
except Exception as e:
return {"error": str(e)}
@mcp.tool()
def subtract(a: int, b: int) -> int:
"""Subtract two numbers"""
try:
return a - b
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
print("Starting Math Server...")
mcp.run(transport="sse")
print("Starting Math Server on port 6274...")
mcp.run(port=6274, transport="sse")

@ -5,43 +5,61 @@ from swarms.tools.mcp_integration import MCPServerSseParams
import logging
def main():
# Configure MCP server connection
server = MCPServerSseParams(
# Configure multiple MCP server connections
math_server = MCPServerSseParams(
url="http://0.0.0.0:6274",
headers={"Content-Type": "application/json"},
timeout=10.0,
sse_read_timeout=300.0
)
# Initialize agent with MCP capabilities
agent = Agent(
calc_server = MCPServerSseParams(
url="http://0.0.0.0:6275",
headers={"Content-Type": "application/json"},
timeout=10.0,
sse_read_timeout=300.0
)
# Initialize multiple agents with different MCP capabilities
math_agent = Agent(
agent_name="Math-Agent",
agent_description="Agent that performs math operations",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
mcp_servers=[server],
mcp_servers=[math_server],
streaming_on=True
)
calc_agent = Agent(
agent_name="Calc-Agent",
agent_description="Agent that performs calculations",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
mcp_servers=[calc_server],
streaming_on=True
)
agents = [math_agent, calc_agent]
try:
# First get available tools from server
print("\nDiscovering available tools from MCP server...")
tools = agent.mcp_tool_handling()
print("\nAvailable tools:", tools)
while True:
# Get user input
user_input = input("\nEnter a math operation (or 'exit' to quit): ")
if user_input.lower() == 'exit':
break
# Process user input through agent
try:
result = agent.run(user_input)
print("\nResult:", result)
except Exception as e:
print(f"Error processing request: {e}")
# Test each agent
for agent in agents:
print(f"\nTesting {agent.agent_name}...")
print("\nDiscovering available tools from MCP server...")
tools = agent.mcp_tool_handling()
print(f"\nAvailable tools for {agent.agent_name}:", tools)
while True:
user_input = input(f"\nEnter a math operation for {agent.agent_name} (or 'exit' to switch agent): ")
if user_input.lower() == 'exit':
break
try:
result = agent.run(user_input)
print(f"\nResult from {agent.agent_name}:", result)
except Exception as e:
print(f"Error processing request: {e}")
except Exception as e:
logging.error(f"Test failed: {e}")

Loading…
Cancel
Save