Skip to content

Conversation

@jerm-dro
Copy link
Contributor

Stacked on #3278

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
Copy link
Contributor

@github-actions github-actions bot left a 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 transformation

Alternative:

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.

@github-actions github-actions bot added the size/XL Extra large PR: 1000+ lines changed label Jan 14, 2026
@codecov
Copy link

codecov bot commented Jan 14, 2026

Codecov Report

❌ Patch coverage is 64.43515% with 85 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.74%. Comparing base (92b08b7) to head (48340ed).
⚠️ Report is 66 commits behind head on jerm/2026-01-13-optimizer-in-vmcp.

Files with missing lines Patch % Lines
pkg/vmcp/server/server.go 34.61% 46 Missing and 5 partials ⚠️
pkg/vmcp/schema/reflect.go 77.14% 10 Missing and 6 partials ⚠️
pkg/vmcp/server/adapter/optimizer_adapter.go 74.35% 5 Missing and 5 partials ⚠️
pkg/vmcp/optimizer/dummy_optimizer.go 87.50% 3 Missing and 3 partials ⚠️
cmd/vmcp/app/commands.go 0.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@therealnb therealnb force-pushed the jerm/2026-01-13-optimizer-in-vmcp branch 2 times, most recently from 29e89d0 to eb22a40 Compare January 20, 2026 10:39
@jerm-dro
Copy link
Contributor Author

This was a draft. Closing in favor of: #3312

@jerm-dro jerm-dro closed this Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR: 1000+ lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants