Skip to content

refactor(mcp): simplify ensure_request_mcp_client and remove McpLoopConfig - #368

Merged
slin1237 merged 2 commits into
mainfrom
chang/mcp-simplify-openai
Feb 7, 2026
Merged

slin1237 merged 2 commits into
mainfrom
chang/mcp-simplify-openai

Conversation

@CatherineSue

@CatherineSue CatherineSue commented Feb 7, 2026 •

Copy link
Copy Markdown
Member

Description

Problem

MCP session setup in the OpenAI path had unnecessary indirection:

  • ensure_request_mcp_client returned a redundant Arc<McpOrchestrator> clone that callers already had.
  • McpLoopConfig was a thin wrapper around max_iterations + mcp_servers with no real value.

Solution

  • Simplify ensure_request_mcp_client to return Option<Vec<(String, String)>> — callers already hold the orchestrator.
  • Delete McpLoopConfig struct and impl Default — callers reference DEFAULT_MAX_ITERATIONS directly and pass mcp_servers as a plain Vec.

Changes

  • mcp_utils.rs: Simplified return type, removed McpLoopConfig
  • openai/responses/non_streaming.rs: Use simplified ensure_request_mcp_client, create McpToolSession directly
  • openai/responses/streaming.rs: Replace McpLoopConfig with direct mcp_servers + DEFAULT_MAX_ITERATIONS
  • openai/responses/mcp.rs: Remove config param from execute_tool_loop, use DEFAULT_MAX_ITERATIONS
  • grpc/common/responses/utils.rs: Update ensure_mcp_connection for new return type

Test Plan

cargo build -p smg
cargo test -p smg --lib   # 340 tests pass
Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • (Optional) Documentation updated

Summary by CodeRabbit

  • Refactor
    • Simplified MCP tool handling by streamlining configuration management and function signatures.
    • Improved code maintainability by consolidating configuration parameters with a constant-based approach.

Change prepare_mcp_tools_as_functions to take &McpToolSession instead of
(&Arc<McpOrchestrator>, &[String]), eliminating redundant server_keys
extraction at call sites since the session already caches this data.
…onfig

Drop the redundant Arc<McpOrchestrator> from ensure_request_mcp_client's
return type (callers already have it) and delete McpLoopConfig — callers
now use DEFAULT_MAX_ITERATIONS directly and pass mcp_servers as a plain
Vec.
@github-actions github-actions Bot added the model-gateway Model gateway crate changes label Feb 7, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @CatherineSue, 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 refactors the MCP (Multi-Cloud Proxy) session setup within the OpenAI path to eliminate unnecessary indirection and simplify the API. The primary goal is to make the code cleaner and more direct by removing a redundant Arc<McpOrchestrator> clone from a function's return type and deprecating a thin wrapper struct. These changes improve code clarity and reduce boilerplate, making the system easier to understand and maintain without altering its core functionality.

Highlights

  • Simplified ensure_request_mcp_client Return Type: The ensure_request_mcp_client function now returns Option<Vec<(String, String)>>, removing the redundant Arc<McpOrchestrator> clone that callers already possessed, streamlining its interface.
  • Removal of McpLoopConfig Struct: The McpLoopConfig struct and its impl Default have been entirely removed, as it was identified as an unnecessary wrapper. Callers now directly use DEFAULT_MAX_ITERATIONS and pass mcp_servers as a plain Vec.
  • Direct Configuration for MCP Tool Loops: Tool loop configurations, previously encapsulated by McpLoopConfig, now directly utilize the DEFAULT_MAX_ITERATIONS constant and a Vec<(String, String)> for MCP servers, reducing indirection across the codebase.
Changelog
  • model_gateway/src/routers/grpc/common/responses/utils.rs
    • Updated ensure_mcp_connection to align with the simplified return type of ensure_request_mcp_client, no longer expecting a redundant orchestrator clone.
  • model_gateway/src/routers/mcp_utils.rs
    • Removed the McpLoopConfig struct and its Default implementation.
    • Modified ensure_request_mcp_client to return Option<Vec<(String, String)>>, removing the Arc<McpOrchestrator> from its return tuple.
    • Adjusted internal logic and test cases for ensure_request_mcp_client to reflect the new return signature.
  • model_gateway/src/routers/openai/responses/mcp.rs
    • Removed Arc import where it was no longer needed due to McpOrchestrator not being passed as a clone.
    • Updated imports to replace McpLoopConfig with DEFAULT_MAX_ITERATIONS.
    • Refactored prepare_mcp_tools_as_functions to accept &McpToolSession<'_> directly, simplifying its parameters.
    • Removed the config: &McpLoopConfig parameter from execute_tool_loop.
    • Replaced all instances of config.max_iterations with DEFAULT_MAX_ITERATIONS within execute_tool_loop.
  • model_gateway/src/routers/openai/responses/non_streaming.rs
    • Removed McpLoopConfig from mcp_utils imports.
    • Updated the call to ensure_request_mcp_client to handle its new return type.
    • Eliminated the creation and passing of the config variable to execute_tool_loop.
    • Adjusted McpToolSession::new and prepare_mcp_tools_as_functions calls to match the updated function signatures.
  • model_gateway/src/routers/openai/responses/streaming.rs
    • Replaced McpLoopConfig with DEFAULT_MAX_ITERATIONS in mcp_utils imports.
    • Modified handle_streaming_with_tool_interception to directly accept mcp_servers: Vec<(String, String)> instead of McpLoopConfig.
    • Updated the logic for prepare_mcp_tools_as_functions and McpToolSession::new to use the direct mcp_servers vector.
    • Replaced loop_config.max_iterations with DEFAULT_MAX_ITERATIONS in tool loop limit calculations.
    • Adjusted handle_streaming_response to pass mcp_orchestrator and mcp_servers directly to handle_streaming_with_tool_interception.
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. ↩

@coderabbitai

coderabbitai Bot commented Feb 7, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This PR refactors MCP (Model Control Protocol) architecture by removing the public McpLoopConfig struct and narrowing the return type of ensure_request_mcp_client to exclude orchestrator references. All dependent modules are updated to use McpToolSession directly and replace config-based iteration limits with a constant.

Changes

Cohort / File(s) Summary
Core MCP utilities refactoring
model_gateway/src/routers/mcp_utils.rs
Removed public McpLoopConfig struct and refactored ensure_request_mcp_client to return Option<Vec<(String, String)>> instead of Option<(Arc<McpOrchestrator>, Vec<...>)>. Updated all internal logic and tests to work with the narrower return type.
MCP response handler updates
model_gateway/src/routers/openai/responses/mcp.rs
Updated function signatures to accept McpToolSession instead of separate orchestrator and config parameters. Replaced config.max_iterations with DEFAULT_MAX_ITERATIONS constant. Changed prepare_mcp_tools_as_functions and execute_tool_loop to use session-based tool retrieval.
Streaming and non-streaming response handlers
model_gateway/src/routers/openai/responses/non_streaming.rs, model_gateway/src/routers/openai/responses/streaming.rs
Refactored to construct McpToolSession directly from orchestrator and server list. Updated handle_streaming_with_tool_interception signature to accept mcp_servers directly. Replaced McpLoopConfig references with DEFAULT_MAX_ITERATIONS for iteration limiting.
GRPC response utility
model_gateway/src/routers/grpc/common/responses/utils.rs
Updated pattern matching in ensure_mcp_connection to destructure result as Some(mcp_servers) instead of Some((_orchestrator, mcp_servers)), accommodating the narrower return type from ensure_request_mcp_client.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

mcp, model-gateway

Suggested reviewers

  • key4ng
  • slin1237

Poem

🐰 A config grows too wide and round,
We trim McpLoop—less to unwound!
Sessions now dance with orchestrated grace,
One constant rhythm sets the pace. ✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chang/mcp-simplify-openai

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

@CatherineSue CatherineSue reopened this Feb 7, 2026

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

Copy link
Copy Markdown
Contributor

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 refactors the MCP session setup, simplifying it by changing ensure_request_mcp_client to return only mcp_servers and removing the McpLoopConfig wrapper, which cleans up the code and reduces indirection. While this is a solid improvement, it introduces a significant security risk: the gateway allows users to specify arbitrary MCP server URLs without sufficient validation, leading to a potential Server-Side Request Forgery (SSRF) vulnerability. As this is a cross-cutting security concern requiring design decisions, it should be addressed in a dedicated pull request rather than being patched within this feature-specific PR.

Comment thread model_gateway/src/routers/mcp_utils.rs
@slin1237
slin1237 merged commit b26de5f into main Feb 7, 2026
42 of 52 checks passed
@slin1237
slin1237 deleted the chang/mcp-simplify-openai branch February 7, 2026 20:55
ppraneth pushed a commit that referenced this pull request Feb 18, 2026
…onfig (#368)

Signed-off-by: ppraneth <pranethparuchuri@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model-gateway Model gateway crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants