Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 12, 2025

The update_tools function creates MCP clients but requires callers to manually manage cleanup, leading to verbose finally blocks and potential for resource leaks.

Changes

  • Added MCPToolsManager context manager: Encapsulates client creation, tool fetching, and automatic cleanup on exit
  • Added update_tools_with_cleanup() factory function: Returns configured context manager instance
  • Updated get_servers endpoint: Replaced manual client management with context manager pattern

Before/After

Before:

mcp_stdio_client = MCPStdioClient()
mcp_streamable_http_client = MCPStreamableHttpClient()
try:
    mode, tool_list, _ = await update_tools(
        server_name, server_config,
        mcp_stdio_client=mcp_stdio_client,
        mcp_streamable_http_client=mcp_streamable_http_client,
    )
    # Use tools
finally:
    await mcp_stdio_client.disconnect()
    await mcp_streamable_http_client.disconnect()

After:

async with update_tools_with_cleanup(server_name, server_config) as (mode, tool_list, _):
    # Use tools

The original update_tools function remains unchanged for backward compatibility with components that manage client lifecycle externally (e.g., MCPToolsComponent).


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 12, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 2 commits December 12, 2025 17:07
- Added MCPToolsManager context manager that handles MCP client lifecycle
- Added update_tools_with_cleanup() convenience function
- Updated get_servers endpoint to use the new context manager
- Removed manual client creation and cleanup code
- Clients are now automatically disconnected when exiting the context

Co-authored-by: phact <[email protected]>
Copilot AI changed the title [WIP] Address feedback on mcp-proxy process leak cleanup Refactor update_tools to use context manager for automatic cleanup Dec 12, 2025
Copilot AI requested a review from phact December 12, 2025 17:14
Base automatically changed from fix-mcp-proxy-process-leak to main December 12, 2025 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants