Skip to content

Commit 71c8bb3

Browse files
committed
Make MCPConnectionManager and MCPAggregator close/exit more resilient to distributed environments
1 parent 7633564 commit 71c8bb3

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/mcp_agent/mcp/mcp_aggregator.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,29 @@ async def close(self):
231231
None, None, None
232232
)
233233
except Exception as e:
234-
logger.error(
235-
f"Error during connection manager __aexit__: {e}"
234+
logger.warning(
235+
f"Error during connection manager cleanup: {e}"
236236
)
237237

238238
# Clean up the connection manager from the context
239239
delattr(self.context, "_mcp_connection_manager")
240240
logger.info(
241241
"Connection manager successfully closed and removed from context"
242242
)
243-
244-
self.initialized = False
243+
else:
244+
logger.debug(
245+
f"Aggregator closing with ref count {current_count}, "
246+
"connection manager will remain active"
247+
)
245248
except Exception as e:
246249
logger.error(
247250
f"Error during connection manager cleanup: {e}", exc_info=True
248251
)
249-
# TODO: saqadri (FA1) - Even if there's an error, we should mark ourselves as uninitialized
250-
self.initialized = False
251252
span.set_status(trace.Status(trace.StatusCode.ERROR))
252253
span.record_exception(e)
254+
finally:
255+
# Always mark as uninitialized regardless of errors
256+
self.initialized = False
253257

254258
@classmethod
255259
async def create(
@@ -289,7 +293,12 @@ async def create(
289293
logger.error(f"Error creating MCPAggregator: {e}")
290294
span.set_status(trace.Status(trace.StatusCode.ERROR))
291295
span.record_exception(e)
292-
await instance.__aexit__(None, None, None)
296+
try:
297+
await instance.__aexit__(None, None, None)
298+
except Exception as cleanup_error:
299+
logger.warning(
300+
f"Error during MCPAggregator cleanup: {cleanup_error}"
301+
)
293302

294303
async def load_server(self, server_name: str):
295304
"""

src/mcp_agent/mcp/mcp_connection_manager.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,19 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
252252

253253
# Then close the task group if it's active
254254
if self._tg_active:
255-
await self._tg.__aexit__(exc_type, exc_val, exc_tb)
256-
self._tg_active = False
257-
self._tg = None
255+
try:
256+
await self._tg.__aexit__(exc_type, exc_val, exc_tb)
257+
except Exception as e:
258+
logger.warning(
259+
f"MCPConnectionManager: Error during task group cleanup: {e}"
260+
)
261+
finally:
262+
self._tg_active = False
263+
self._tg = None
258264
except AttributeError: # Handle missing `_exceptions`
259265
pass
260266
except Exception as e:
261-
logger.error(f"MCPConnectionManager: Error during shutdown: {e}")
267+
logger.warning(f"MCPConnectionManager: Error during shutdown: {e}")
262268

263269
async def launch_server(
264270
self,

0 commit comments

Comments
 (0)