Skip to content

Conversation

@kevinfeng030
Copy link
Collaborator

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @kevinfeng030, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant optimization to how MCP (Multi-Component Platform) server connections are managed within the sandbox environment. By adding a 'reuse' mode, the system can now cache and reuse server instances for subsequent tool calls, which is expected to reduce connection overhead and improve the efficiency of tool interactions. This change involves new utility functions for tool description transformation and invocation, updates to the sandbox's configuration to enable this feature, and conditional logic in the McpServers class to leverage the new reuse capabilities.

Highlights

  • MCP Server Connection Reuse: Implemented a mechanism to reuse MCP (Multi-Component Platform) server connections, significantly reducing overhead for repeated tool calls within the sandbox environment.
  • New Utility Functions for Reuse: Introduced mcp_tool_desc_transform_v2_reuse and call_mcp_tool_with_reuse in mcp_client/utils.py to support the new reuse logic for tool description transformation and tool invocation, respectively.
  • Sandbox Configuration for Reuse: Added a reuse boolean parameter to the create_sandbox function and the Sandbox base class, allowing users to explicitly enable or disable server connection reuse.
  • Conditional Tool Listing and Calling: Modified the McpServers class to dynamically choose between reuse-enabled and non-reuse methods for listing and calling tools based on the sandbox's reuse setting.
  • Improved Server Cleanup Error Handling: Enhanced the cleanup_server function to specifically log RuntimeError related to 'cancel scope' or 'different task' contexts during server cleanup, preventing failures in such scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@xiudongxu xiudongxu merged commit df1fb82 into main Jan 19, 2026
1 check passed
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a reuse mode for MCP server connections to improve performance by caching and reusing server instances. The changes are implemented across the sandbox and MCP client utilities. While the feature is a good addition, the current implementation has some significant issues. My review highlights a critical performance bug due to a blocking I/O call in an async function, several instances of code duplication that impact maintainability, and violations of best practices such as using global variables and overly broad exception handling. Addressing these points will improve the code's robustness, performance, and long-term maintainability.

except Exception as e:
logger.warning(f"server_name:{server_name} translate failed: {e}")
elif "api" == server_config.get("type", ""):
api_result = requests.get(server_config["url"] + "/list_tools")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The use of requests.get() here is a synchronous, blocking network call within an async function. This will block the event loop and severely degrade the performance of your asynchronous application. You should use an asynchronous HTTP client, such as httpx, for all network requests in async code.

You will need to add import httpx at the top of the file.

Suggested change
api_result = requests.get(server_config["url"] + "/list_tools")
api_result = await httpx.get(server_config["url"] + "/list_tools")

Comment on lines +594 to +600
async def mcp_tool_desc_transform_v2_reuse(
tools: List[str] = None, mcp_config: Dict[str, Any] = None, context: Context = None,
server_instances: Dict[str, Any] = None,
black_tool_actions: Dict[str, List[str]] = None,
sandbox_id: Optional[str] = None,
tool_actions: Optional[List[str]] = None
) -> List[Dict[str, Any]]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new function mcp_tool_desc_transform_v2_reuse is almost a complete duplicate of mcp_tool_desc_transform_v2. This significant code duplication makes maintenance difficult, as any changes or bug fixes will need to be applied in both places.

Please consider refactoring to reduce this duplication. A good approach would be to extract the common logic for parsing server configurations into a shared helper function. This function could be called by both mcp_tool_desc_transform_v2 and mcp_tool_desc_transform_v2_reuse, each then handling their specific logic for server connection management (one using AsyncExitStack, the other using the cache).

Comment on lines +606 to +607
global MCP_SERVERS_CONFIG
MCP_SERVERS_CONFIG = config

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Modifying a global variable (MCP_SERVERS_CONFIG) within a function is a practice that should be avoided. It creates hidden dependencies and side effects, making the code harder to understand, test, and debug. The configuration should be passed explicitly as a parameter to the functions that require it. It appears the functions that might use this global already accept an mcp_config parameter, making this global assignment likely redundant.

Suggested change
global MCP_SERVERS_CONFIG
MCP_SERVERS_CONFIG = config
# global MCP_SERVERS_CONFIG
# MCP_SERVERS_CONFIG = config

if _mcp_openai_tools:
mcp_openai_tools.extend(_mcp_openai_tools)
logger.info(f"✅ server ({server_name}) connected success")
except BaseException as err:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching BaseException is too broad as it includes system-exiting exceptions like KeyboardInterrupt and SystemExit. This can prevent the application from shutting down cleanly. It's better to catch the more specific Exception to handle application-level errors while allowing system exceptions to propagate.

Suggested change
except BaseException as err:
except Exception as err:

# Success, break out of retry loop
break

except (asyncio.TimeoutError, BaseException) as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching BaseException is too broad and can mask critical system-level issues. It includes exceptions like SystemExit and KeyboardInterrupt which should generally not be caught. Please catch Exception instead to handle standard application errors.

Suggested change
except (asyncio.TimeoutError, BaseException) as e:
except (asyncio.TimeoutError, Exception) as e:

Comment on lines +56 to +75
if self._should_reuse():
self.tool_list = await mcp_tool_desc_transform_v2_reuse(
tools=self.mcp_servers,
mcp_config=self.mcp_config,
context=context,
server_instances=self.server_instances,
black_tool_actions=self.black_tool_actions,
sandbox_id=sandbox_id,
tool_actions=self.tool_actions
)
else:
self.tool_list = await mcp_tool_desc_transform_v2(
tools=self.mcp_servers,
mcp_config=self.mcp_config,
context=context,
server_instances=self.server_instances,
black_tool_actions=self.black_tool_actions,
sandbox_id=sandbox_id,
tool_actions=self.tool_actions
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The if/else block contains duplicated code, as the arguments passed to mcp_tool_desc_transform_v2_reuse and mcp_tool_desc_transform_v2 are identical. You can make this more concise and maintainable by selecting the function first and then calling it once.

            transform_func = (
                mcp_tool_desc_transform_v2_reuse
                if self._should_reuse()
                else mcp_tool_desc_transform_v2
            )
            self.tool_list = await transform_func(
                tools=self.mcp_servers,
                mcp_config=self.mcp_config,
                context=context,
                server_instances=self.server_instances,
                black_tool_actions=self.black_tool_actions,
                sandbox_id=sandbox_id,
                tool_actions=self.tool_actions,
            )

Comment on lines +248 to 281
if self._should_reuse():
# Reuse mode: use cached server instances (delegated to utils.py)
call_result_raw = await call_mcp_tool_with_reuse(
server_name=server_name,
tool_name=tool_name,
parameter=parameter,
server_instances=self.server_instances,
mcp_config=self.mcp_config,
context=context,
sandbox_id=sandbox_id,
progress_callback=progress_callback,
max_retry=3,
timeout=120.0
)

if not call_result_raw:
call_mcp_e = Exception("Failed to call tool after all retry attempts")
else:
# Non-reuse mode: use AsyncExitStack (delegated to utils.py)
call_result_raw = await call_mcp_tool_with_exit_stack(
server_name=server_name,
tool_name=tool_name,
parameter=parameter,
mcp_config=self.mcp_config,
context=context,
sandbox_id=sandbox_id,
progress_callback=progress_callback,
max_retry=3,
timeout=120.0
)

if not call_result_raw:
call_mcp_e = Exception("Failed to call tool after all retry attempts")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check for if not call_result_raw: is duplicated in both branches of the if self._should_reuse(): condition. You can refactor this to remove the duplication by moving the check to after the if/else block.

                if self._should_reuse():
                    # Reuse mode: use cached server instances (delegated to utils.py)
                    call_result_raw = await call_mcp_tool_with_reuse(
                        server_name=server_name,
                        tool_name=tool_name,
                        parameter=parameter,
                        server_instances=self.server_instances,
                        mcp_config=self.mcp_config,
                        context=context,
                        sandbox_id=sandbox_id,
                        progress_callback=progress_callback,
                        max_retry=3,
                        timeout=120.0
                    )
                else:
                    # Non-reuse mode: use AsyncExitStack (delegated to utils.py)
                    call_result_raw = await call_mcp_tool_with_exit_stack(
                        server_name=server_name,
                        tool_name=tool_name,
                        parameter=parameter,
                        mcp_config=self.mcp_config,
                        context=context,
                        sandbox_id=sandbox_id,
                        progress_callback=progress_callback,
                        max_retry=3,
                        timeout=120.0
                    )
                
                if not call_result_raw:
                    call_mcp_e = Exception("Failed to call tool after all retry attempts")

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