-
Notifications
You must be signed in to change notification settings - Fork 170
Integration and Config for Optimizer in vMCP #3282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integration and Config for Optimizer in vMCP #3282
Conversation
Extract the inline AddOnRegisterSession hook to a dedicated handleSessionRegistration method for better testability and readability. This is a pure refactor with no functional changes - the same logic is executed, just moved to a named method on *Server. This prepares the codebase for the optimizer feature which will need to conditionally modify session registration behavior.
Add the pkg/vmcp/optimizer package with: - Optimizer interface defining FindTool and CallTool methods - FindToolInput/FindToolOutput types for tool discovery - CallToolInput/CallToolResult types for tool invocation - ToolMatch for search results with relevance scoring - TokenMetrics for tracking token usage optimization This interface will be implemented by: - DummyOptimizer: exact string matching (testing) - EmbeddingOptimizer: semantic similarity (production)
Add the pkg/vmcp/optimizer package with: Optimizer interface: - FindTool: Search for tools by description (semantic search) - CallTool: Invoke a tool by name, returns map[string]any directly Types: - FindToolInput/FindToolOutput for tool discovery - CallToolInput for tool invocation - ToolMatch for search results with relevance scoring - TokenMetrics for tracking optimization (placeholder for now) DummyOptimizer implementation: - Case-insensitive substring matching on tool name and description - Routes tool calls via router interface to backend client - Backend tools only (composite tools not supported in v1) - Intended for testing; production will use EmbeddingOptimizer Note: Composite tools are excluded as they require the composer for execution, not the router/backendClient pipeline. This will be addressed in a future iteration. Signed-off-by: Jeremy Drouillard <[email protected]>
Add OptimizerConfig to pkg/vmcp/config/config.go to enable the MCP optimizer feature. When configured, vMCP exposes only find_tool and call_tool operations instead of all backend tools directly. OptimizerConfig fields: - EmbeddingService: Name of a K8s Service providing the embedding API for semantic tool discovery Generated artifacts updated: - CRD manifests (VirtualMCPServer) - DeepCopy implementations - CRD API documentation The config is automatically included in the VirtualMCPServer CRD through the embedded config.Config field.
Refactor the VirtualMCPServer config converter to use DeepCopy() for initializing the vmcp.Config from the embedded config.Config. This ensures that new fields added to config.Config (like Optimizer) are automatically passed through without requiring explicit mapping in the converter. Only fields requiring special handling (auth, aggregation, composite tools, telemetry) are explicitly converted. Benefits: - New config fields are automatically included - Reduces maintenance burden when adding config options - Less code duplication between CRD and config types
Add GenerateSchema[T]() and Translate[T]() functions to pkg/vmcp/schema: GenerateSchema[T](): - Generates JSON Schema from Go struct using reflection - Uses json tags for field names - Uses description tags for field descriptions - Uses omitempty to determine required vs optional fields - Supports string, integer, number, boolean, array, object types Translate[T](): - Converts untyped map[string]any to typed structs - Uses JSON marshal/unmarshal for reliable conversion - Simplifies MCP tool argument handling These functions ensure optimizer tool schemas (find_tool, call_tool) stay in sync with their Go struct definitions.
Refactor DummyOptimizer to use server.ServerTool: - Constructor takes []server.ServerTool instead of separate router/client - CallTool invokes the tool handler directly, returns *mcp.CallToolResult - FindTool returns tool schemas as json.RawMessage (preserves original format) - getToolSchema helper handles RawInputSchema vs InputSchema fallback Add optimizer tool handlers in adapter package: - CreateOptimizerTools creates find_tool and call_tool SDK tools - find_tool handler uses mcp.NewToolResultStructuredOnly for output - call_tool handler delegates to optimizer and returns result directly - Schemas are pre-generated at package init for startup-time validation Uses schema.GenerateSchema and schema.Translate for type-safe argument handling and schema generation.
Add OptimizerFactory to server.Config to enable optimizer mode: - OptimizerFactory is a function that creates an Optimizer from tools - When set, session registration calls injectOptimizerCapabilities - injectOptimizerCapabilities wraps all backend/composite tools in optimizer - Only find_tool and call_tool are exposed to clients in optimizer mode - Resources and prompts are still injected normally Wire up in vmcp serve command: - When cfg.Optimizer is set, configure OptimizerFactory with DummyOptimizer - TODO comment for replacing with real optimizer implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details.
This review will be automatically dismissed once you add the justification section.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## jerm/2026-01-13-optimizer-in-vmcp #3282 +/- ##
=====================================================================
+ Coverage 63.66% 63.74% +0.08%
=====================================================================
Files 362 365 +3
Lines 35521 35714 +193
=====================================================================
+ Hits 22613 22765 +152
- Misses 11089 11115 +26
- Partials 1819 1834 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
29e89d0 to
eb22a40
Compare
|
This was a draft. Closing in favor of: #3312 |
Stacked on #3278