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]] [[workflows.workflow.tasks]]
task = "shell.exec" task = "shell.exec"
args = "python examples/mcp_example/test_integration.py" 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 fastmcp import FastMCP
from typing import Dict, Any, Optional from typing import Dict, Any
# Initialize MCP server # Initialize MCP server
mcp = FastMCP("Math-Server") mcp = FastMCP("Math-Server")
@ -20,6 +21,14 @@ def multiply(a: int, b: int) -> int:
except Exception as e: except Exception as e:
return {"error": str(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__": if __name__ == "__main__":
print("Starting Math Server...") print("Starting Math Server on port 6274...")
mcp.run(transport="sse") mcp.run(port=6274, transport="sse")

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

Loading…
Cancel
Save