fix(mcp): resolve server and async coroutine handling issues

pull/819/head
Pavan Kumar 3 months ago committed by ascender1729
parent fd50b02bfb
commit 58e2f5c009

@ -22,4 +22,4 @@ def divide(a: int, b: int) -> float:
if __name__ == "__main__": if __name__ == "__main__":
print("Starting Mock Math Server on port 8000...") 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)

@ -1861,8 +1861,7 @@ class Agent:
return previous_state, f"Restored to {previous_state}" return previous_state, f"Restored to {previous_state}"
# Response Filtering # Response Filtering
def add_response_filter(self, filter_word: str) -> None: def add_response_filter(self, filter_word: str) -> None:"""
"""
Add a response filter. Add a response filter.
Args: Args:
@ -2777,32 +2776,11 @@ class Agent:
role="Output Cleaner", role="Output Cleaner",
content=response, content=response,
) )
def mcp_execution_flow(self, response: str | dict) -> str | None: def mcp_execution_flow(self, payload: dict) -> str | None:
""" """Forward the tool-call dict to every MCP server in self.mcp_servers"""
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
try: try:
# LLM may give us a JSON string or already-parsed dict result = asyncio.run(batch_mcp_flow(self.mcp_servers, [payload]))
if isinstance(response, str): return any_to_str(result)
call_dict = json.loads(response) except Exception as err:
else: logger.error(f"MCP flow failed: {err}")
call_dict = response return f"[MCP-error] {err}"
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}"
Loading…
Cancel
Save