From 6954a9ace42c325c0123da657ef996fa76c7a239 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:25:04 +0000 Subject: [PATCH] feat: set up multiple MCP servers and integrate with multi-agent test system --- .replit | 21 +++++++++ examples/mcp_example/calc_server.py | 27 +++++++++++ examples/mcp_example/math_server.py | 15 ++++-- examples/mcp_example/test_integration.py | 60 +++++++++++++++--------- 4 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 examples/mcp_example/calc_server.py diff --git a/.replit b/.replit index 9b2aec89..a1c69739 100644 --- a/.replit +++ b/.replit @@ -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" diff --git a/examples/mcp_example/calc_server.py b/examples/mcp_example/calc_server.py new file mode 100644 index 00000000..b66323f8 --- /dev/null +++ b/examples/mcp_example/calc_server.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") diff --git a/examples/mcp_example/math_server.py b/examples/mcp_example/math_server.py index af533c90..f3773906 100644 --- a/examples/mcp_example/math_server.py +++ b/examples/mcp_example/math_server.py @@ -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") \ No newline at end of file + print("Starting Math Server on port 6274...") + mcp.run(port=6274, transport="sse") diff --git a/examples/mcp_example/test_integration.py b/examples/mcp_example/test_integration.py index 5429c2ea..fc22a71d 100644 --- a/examples/mcp_example/test_integration.py +++ b/examples/mcp_example/test_integration.py @@ -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): ") + # 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) - if user_input.lower() == 'exit': - break + while True: + user_input = input(f"\nEnter a math operation for {agent.agent_name} (or 'exit' to switch agent): ") - # Process user input through agent - try: - result = agent.run(user_input) - print("\nResult:", result) - except Exception as e: - print(f"Error processing request: {e}") + 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}")