From 58e2f5c00924ac92eb051dc0772f5b6d31dfc4a2 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sun, 20 Apr 2025 11:14:53 +0000 Subject: [PATCH] fix(mcp): resolve server and async coroutine handling issues --- examples/mcp_example/mock_math_server.py | 2 +- swarms/structs/agent.py | 38 +++++------------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/examples/mcp_example/mock_math_server.py b/examples/mcp_example/mock_math_server.py index 383a4afc..07803d84 100644 --- a/examples/mcp_example/mock_math_server.py +++ b/examples/mcp_example/mock_math_server.py @@ -22,4 +22,4 @@ def divide(a: int, b: int) -> float: if __name__ == "__main__": print("Starting Mock Math Server on port 8000...") - mcp.run(transport="sse", host="0.0.0.0", port=8000) + mcp.run(transport="sse", port=8000) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 6a1cf265..68e505a1 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -1861,8 +1861,7 @@ class Agent: return previous_state, f"Restored to {previous_state}" # Response Filtering - def add_response_filter(self, filter_word: str) -> None: - """ + def add_response_filter(self, filter_word: str) -> None:""" Add a response filter. Args: @@ -2777,32 +2776,11 @@ class Agent: role="Output Cleaner", content=response, ) - def mcp_execution_flow(self, response: str | dict) -> str | None: - """ - Detect an LLM function-call style response and proxy the call to the - configured MCP servers. Returns the tool output as a string so it can - be fed back into the conversation. - """ - if not self.mcp_servers: - return None - + def mcp_execution_flow(self, payload: dict) -> str | None: + """Forward the tool-call dict to every MCP server in self.mcp_servers""" try: - # LLM may give us a JSON string or already-parsed dict - if isinstance(response, str): - call_dict = json.loads(response) - else: - call_dict = response - - if not isinstance(call_dict, dict): - return None # nothing to do - - if "tool_name" not in call_dict and "name" not in call_dict: - return None # not a tool call - - from swarms.tools.mcp_integration import batch_mcp_flow - out = batch_mcp_flow(self.mcp_servers, call_dict) - return any_to_str(out) - - except Exception as e: - logger.error(f"MCP flow failed: {e}") - return f"[MCP-error] {e}" \ No newline at end of file + result = asyncio.run(batch_mcp_flow(self.mcp_servers, [payload])) + return any_to_str(result) + except Exception as err: + logger.error(f"MCP flow failed: {err}") + return f"[MCP-error] {err}" \ No newline at end of file