BUG-HUNT: [resource-management] Potential resource leak in MCPToolAdapter #1319

Open
opened 2026-04-02 16:49:13 +00:00 by freemo · 0 comments
Owner

Bug Report: Resource Management — Potential resource leak in MCPToolAdapter

Severity Assessment

  • Impact: If the disconnect method is not called, the MCP server process (for stdio transport) might not be terminated, leading to orphaned processes and resource leaks.
  • Likelihood: Medium, as it depends on how the MCPToolAdapter is used. If it's used in a long-running process without proper cleanup, the likelihood is high.
  • Priority: High

Location

  • File: src/cleveragents/mcp/adapter.py
  • Function/Class: MCPToolAdapter
  • Lines: 142-167

Description

The MCPToolAdapter class manages the connection to an MCP server, which may involve spawning a subprocess (for the stdio transport). The class provides a disconnect method to clean up the connection and terminate the server process. However, there is no mechanism to guarantee that disconnect is called. If a consumer of this class forgets to call disconnect, the underlying transport and server process may not be cleaned up, leading to resource leaks.

Evidence

The MCPToolAdapter class does not implement the context manager protocol (__enter__ and __exit__), nor does it have a __del__ method to ensure cleanup. This makes it easy to misuse the class and forget to call disconnect.

Expected Behavior

The MCPToolAdapter should ensure that its disconnect method is called when it is no longer in use.

Suggested Fix

Implement the context manager protocol for the MCPToolAdapter class. This would allow it to be used in a with statement, which would guarantee that disconnect is called.

    def __enter__(self) -> "MCPToolAdapter":
        self.connect()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.disconnect()

Category

resource-management

## Bug Report: Resource Management — Potential resource leak in MCPToolAdapter ### Severity Assessment - **Impact**: If the `disconnect` method is not called, the MCP server process (for `stdio` transport) might not be terminated, leading to orphaned processes and resource leaks. - **Likelihood**: Medium, as it depends on how the `MCPToolAdapter` is used. If it's used in a long-running process without proper cleanup, the likelihood is high. - **Priority**: High ### Location - **File**: `src/cleveragents/mcp/adapter.py` - **Function/Class**: `MCPToolAdapter` - **Lines**: 142-167 ### Description The `MCPToolAdapter` class manages the connection to an MCP server, which may involve spawning a subprocess (for the `stdio` transport). The class provides a `disconnect` method to clean up the connection and terminate the server process. However, there is no mechanism to guarantee that `disconnect` is called. If a consumer of this class forgets to call `disconnect`, the underlying transport and server process may not be cleaned up, leading to resource leaks. ### Evidence The `MCPToolAdapter` class does not implement the context manager protocol (`__enter__` and `__exit__`), nor does it have a `__del__` method to ensure cleanup. This makes it easy to misuse the class and forget to call `disconnect`. ### Expected Behavior The `MCPToolAdapter` should ensure that its `disconnect` method is called when it is no longer in use. ### Suggested Fix Implement the context manager protocol for the `MCPToolAdapter` class. This would allow it to be used in a `with` statement, which would guarantee that `disconnect` is called. ```python def __enter__(self) -> "MCPToolAdapter": self.connect() return self def __exit__(self, exc_type, exc_val, exc_tb): self.disconnect() ``` ### Category resource-management
freemo self-assigned this 2026-04-02 18:45:23 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#1319
No description provided.