- 
                Notifications
    
You must be signed in to change notification settings  - Fork 135
 
vMCP: Implement capability merging and routing integration #2376
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      1cd8990
              
                Implement Virtual MCP Server with capability merging and routing
              
              
                JAORMX bd9dfe4
              
                Address review feedback: timeouts and MIME type handling
              
              
                JAORMX 5b97270
              
                Add defensive nil map checks to router
              
              
                JAORMX 0bb2849
              
                Add HTTP server timeouts and startup validation
              
              
                JAORMX File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package router | ||
| 
     | 
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| 
     | 
||
| "github.com/stacklok/toolhive/pkg/logger" | ||
| "github.com/stacklok/toolhive/pkg/vmcp" | ||
| ) | ||
| 
     | 
||
| // defaultRouter is a simple router implementation that uses a RoutingTable | ||
| // to map capability names to backend targets. | ||
| // | ||
| // It is safe for concurrent use through RWMutex locking. | ||
| // The RWMutex provides flexibility for both wholesale table replacement | ||
| // and future fine-grained updates (e.g., adding/removing individual backends). | ||
| type defaultRouter struct { | ||
| mu sync.RWMutex | ||
| routingTable *vmcp.RoutingTable | ||
| } | ||
| 
     | 
||
| // NewDefaultRouter creates a new default router instance. | ||
| // The router initially has no routing table and will return errors | ||
| // until UpdateRoutingTable is called. | ||
| func NewDefaultRouter() Router { | ||
| return &defaultRouter{} | ||
| } | ||
| 
     | 
||
| // RouteTool resolves a tool name to its backend target. | ||
| func (r *defaultRouter) RouteTool(_ context.Context, toolName string) (*vmcp.BackendTarget, error) { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| 
     | 
||
| if r.routingTable == nil { | ||
| return nil, fmt.Errorf("routing table not initialized") | ||
| } | ||
| 
     | 
||
| if r.routingTable.Tools == nil { | ||
| return nil, fmt.Errorf("routing table tools map not initialized") | ||
| } | ||
| 
     | 
||
| target, exists := r.routingTable.Tools[toolName] | ||
| if !exists { | ||
| logger.Debugf("Tool not found in routing table: %s", toolName) | ||
| return nil, fmt.Errorf("%w: %s", ErrToolNotFound, toolName) | ||
| } | ||
| 
     | 
||
| logger.Debugf("Routed tool %s to backend %s", toolName, target.WorkloadID) | ||
| return target, nil | ||
| } | ||
| 
     | 
||
| // RouteResource resolves a resource URI to its backend target. | ||
| func (r *defaultRouter) RouteResource(_ context.Context, uri string) (*vmcp.BackendTarget, error) { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| 
     | 
||
| if r.routingTable == nil { | ||
| return nil, fmt.Errorf("routing table not initialized") | ||
| } | ||
| 
     | 
||
| if r.routingTable.Resources == nil { | ||
| return nil, fmt.Errorf("routing table resources map not initialized") | ||
| } | ||
| 
     | 
||
| target, exists := r.routingTable.Resources[uri] | ||
| if !exists { | ||
| logger.Debugf("Resource not found in routing table: %s", uri) | ||
| return nil, fmt.Errorf("%w: %s", ErrResourceNotFound, uri) | ||
| } | ||
| 
     | 
||
| logger.Debugf("Routed resource %s to backend %s", uri, target.WorkloadID) | ||
| return target, nil | ||
| } | ||
| 
     | 
||
| // RoutePrompt resolves a prompt name to its backend target. | ||
| func (r *defaultRouter) RoutePrompt(_ context.Context, name string) (*vmcp.BackendTarget, error) { | ||
| r.mu.RLock() | ||
| defer r.mu.RUnlock() | ||
| 
     | 
||
| if r.routingTable == nil { | ||
| return nil, fmt.Errorf("routing table not initialized") | ||
| } | ||
| 
     | 
||
| if r.routingTable.Prompts == nil { | ||
| return nil, fmt.Errorf("routing table prompts map not initialized") | ||
| } | ||
| 
     | 
||
| target, exists := r.routingTable.Prompts[name] | ||
| if !exists { | ||
| logger.Debugf("Prompt not found in routing table: %s", name) | ||
| return nil, fmt.Errorf("%w: %s", ErrPromptNotFound, name) | ||
| } | ||
| 
     | 
||
| logger.Debugf("Routed prompt %s to backend %s", name, target.WorkloadID) | ||
| return target, nil | ||
| } | ||
| 
     | 
||
| // UpdateRoutingTable updates the router's internal routing table. | ||
| // This is called after capability aggregation completes with the | ||
| // merged routing information. | ||
| // | ||
| // The update is atomic - all lookups see either the old table or the new table. | ||
| func (r *defaultRouter) UpdateRoutingTable(_ context.Context, table *vmcp.RoutingTable) error { | ||
| if table == nil { | ||
| return fmt.Errorf("routing table cannot be nil") | ||
| } | ||
| 
     | 
||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
| 
     | 
||
| r.routingTable = table | ||
| 
     | 
||
| logger.Infof("Updated routing table: %d tools, %d resources, %d prompts", | ||
| len(table.Tools), len(table.Resources), len(table.Prompts)) | ||
| 
     | 
||
| return nil | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.