From a55589febb16f1e50f0c6319954ed8e403880239 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Sun, 20 Apr 2025 10:50:29 +0000 Subject: [PATCH] feat(agent): integrate MCP execution flow and add gpt-4o-mini model support --- swarms/structs/agent.py | 44 ++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index a10af6e0..3c2df969 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -518,7 +518,7 @@ class Agent: self.role = role self.no_print = no_print self.tools_list_dictionary = tools_list_dictionary - self.mcp_servers = mcp_servers + self.mcp_servers = mcp_servers or [] # Initialize mcp_servers to an empty list if None self._cached_llm = ( None # Add this line to cache the LLM instance @@ -1861,16 +1861,7 @@ class Agent: return previous_state, f"Restored to {previous_state}" # Response Filtering - def add_response_filter(self, filter_word: str) -> None: - """ - Add a response filter tofilter out certain words from the response - - Example: - agent.add_response_filter("Trump") - agent.run("Generate a report on Trump") - - - """ + def add_response_filter(self, filter_word: str) -> None: """ logger.info(f"Adding response filter: {filter_word}") self.reponse_filters.append(filter_word) @@ -2774,4 +2765,33 @@ class Agent: self.short_memory.add( role="Output Cleaner", content=response, - ) \ No newline at end of file + ) + 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 + + 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