refactor(mcp): introduce McpServerBinding, unify ensure functions, fixethrowaway session - #482
Conversation
📝 WalkthroughWalkthroughThis PR introduces a new public struct Changes
Sequence Diagram(s)(Skipped — changes are refactors of data representation and signatures, not a new multi-component control-flow feature requiring a sequence diagram.) Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Comment |
Summary of ChangesHello @key4ng, 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 significantly refactors the Multi-Cloud Proxy (MCP) integration by introducing a dedicated struct for server bindings, unifying the server connection logic across different routers, and optimizing the tool injection process within the Anthropic router. These changes aim to enhance code clarity, reduce redundancy, and improve the overall robustness of MCP server interactions. Highlights
Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request is a significant and well-executed refactoring. It introduces the McpServerBinding struct to replace opaque (String, String) tuples, which greatly improves code readability and type safety. The unification of the various ensure_* connection functions into a single ensure_mcp_servers is a great architectural improvement that reduces code duplication and centralizes connection logic. Additionally, removing the throwaway McpToolSession in the Anthropic router simplifies the control flow.
I've made a couple of minor suggestions to use collect for more idiomatic and concise code. Overall, this is a high-quality change that improves the maintainability of the MCP codebase.
79acb76 to
a542f65
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@mcp/src/core/session.rs`:
- Around line 27-37: Add `PartialEq` and `Eq` derives to the McpServerBinding
struct so it can be directly compared; update the derive attribute on the
McpServerBinding declaration (currently #[derive(Debug, Clone)]) to include
PartialEq and Eq (e.g., #[derive(Debug, Clone, PartialEq, Eq)]) so tests and
duplicate-checking can use direct equality and collection helpers like contains.
…improved readability and safety This change introduces the McpServerBinding struct to replace the previous (String, String) tuple used for representing MCP server connections. The new struct enhances code clarity and reduces the risk of field-swap bugs across multiple call sites. Updates include changes in the core session, orchestrator, and various router files to accommodate the new structure. Signed-off-by: key4ng <rukeyang@gmail.com>
This update promotes the `inject_mcp_tools_into_request` and `collect_allowed_tools_from_toolsets` functions to public visibility, allowing for better integration and reuse in other modules. Additionally, the Anthropic router has been updated to utilize a new MCP server connection method, improving error handling and logging for server connection failures. Signed-off-by: key4ng <rukeyang@gmail.com>
This commit enhances code readability by standardizing formatting, particularly in the MCP-related files. Changes include consistent indentation and line breaks in the `McpServerBinding` struct and various function calls. Additionally, unnecessary imports have been removed, and comments have been refined for clarity. These adjustments aim to improve maintainability and facilitate future development. Signed-off-by: key4ng <rukeyang@gmail.com>
…key mapping This commit updates the McpServerBinding struct to derive PartialEq and Eq traits, improving its usability in comparisons. Additionally, it refactors the code for collecting server keys and mapping server labels to keys, enhancing readability and performance by utilizing iterator methods. These changes contribute to cleaner and more efficient code in the MCP module. Signed-off-by: key4ng <rukeyang@gmail.com>
e4b7fd8 to
6be0ae8
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@model_gateway/src/routers/mcp_utils.rs`:
- Around line 188-197: extract_builtin_types currently collects BuiltinToolType
entries directly and can return duplicates when multiple ResponseTool items
share the same builtin type; change the implementation to deduplicate before
returning (e.g., map ResponseTool -> BuiltinToolType, insert into a
HashSet<BuiltinToolType> or use iterator.collect::<HashSet<_>>(), then convert
to Vec), keeping the same function signature and matching arms for
ResponseToolType::WebSearchPreview and ::CodeInterpreter so callers (and
ensure_mcp_servers) receive only unique builtin types.
---
Duplicate comments:
In `@mcp/src/core/session.rs`:
- Around line 27-37: No changes required—McpServerBinding is correctly defined
and documented with derives PartialEq and Eq; keep the struct as-is
(McpServerBinding with fields label and server_key) and proceed with the
approved change.
Description
Problem
Solution
Changes
Test Plan
Checklist
cargo +nightly fmtpassescargo clippy --all-targets --all-features -- -D warningspassesSummary by CodeRabbit
New Features
Refactor