- 
                Notifications
    You must be signed in to change notification settings 
- Fork 134
Implement capability discovery and querying for Virtual MCP Server #2354
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
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      451a15a
              
                Implement capability discovery and querying for Virtual MCP Server
              
              
                JAORMX c7de158
              
                Add comprehensive unit tests for vMCP capability discovery
              
              
                JAORMX 9903e6e
              
                Add comprehensive tests for type conversion logic
              
              
                JAORMX 76dabc2
              
                Add BackendRegistry interface for thread-safe backend access
              
              
                JAORMX 007b354
              
                Address PR feedback on capability discovery implementation
              
              
                JAORMX da50e94
              
                Refactor vmcp tests to reduce verbosity
              
              
                JAORMX 099faaf
              
                Fix linter issues in vmcp test helpers
              
              
                JAORMX c1c8a62
              
                Address PR feedback: Improve error handling and security
              
              
                JAORMX 0af705b
              
                Add HTTP response size limits for DoS protection
              
              
                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
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      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
    
  
  
    
              
  
    
      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,124 @@ | ||
| package aggregator | ||
|  | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
|  | ||
| rt "github.com/stacklok/toolhive/pkg/container/runtime" | ||
| "github.com/stacklok/toolhive/pkg/groups" | ||
| "github.com/stacklok/toolhive/pkg/logger" | ||
| "github.com/stacklok/toolhive/pkg/vmcp" | ||
| "github.com/stacklok/toolhive/pkg/workloads" | ||
| ) | ||
|  | ||
| // cliBackendDiscoverer discovers backend MCP servers from Docker/Podman workloads in a group. | ||
| // This is the CLI version of BackendDiscoverer that uses the workloads.Manager. | ||
| type cliBackendDiscoverer struct { | ||
| workloadsManager workloads.Manager | ||
| groupsManager groups.Manager | ||
| } | ||
|  | ||
| // NewCLIBackendDiscoverer creates a new CLI-based backend discoverer. | ||
| // It discovers workloads from Docker/Podman containers managed by ToolHive. | ||
| func NewCLIBackendDiscoverer(workloadsManager workloads.Manager, groupsManager groups.Manager) BackendDiscoverer { | ||
| return &cliBackendDiscoverer{ | ||
| workloadsManager: workloadsManager, | ||
| groupsManager: groupsManager, | ||
| } | ||
| } | ||
|  | ||
| // Discover finds all backend workloads in the specified group. | ||
| // Returns all accessible backends with their health status marked based on workload status. | ||
| // The groupRef is the group name (e.g., "engineering-team"). | ||
| func (d *cliBackendDiscoverer) Discover(ctx context.Context, groupRef string) ([]vmcp.Backend, error) { | ||
| logger.Infof("Discovering backends in group %s", groupRef) | ||
|  | ||
| // Verify that the group exists | ||
| exists, err := d.groupsManager.Exists(ctx, groupRef) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to check if group exists: %w", err) | ||
| } | ||
| if !exists { | ||
| return nil, fmt.Errorf("group %s not found", groupRef) | ||
| } | ||
|  | ||
| // Get all workload names in the group | ||
| workloadNames, err := d.workloadsManager.ListWorkloadsInGroup(ctx, groupRef) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to list workloads in group: %w", err) | ||
| } | ||
|  | ||
| if len(workloadNames) == 0 { | ||
| logger.Infof("No workloads found in group %s", groupRef) | ||
| return []vmcp.Backend{}, nil | ||
| } | ||
|  | ||
| logger.Debugf("Found %d workloads in group %s, discovering backends", len(workloadNames), groupRef) | ||
|  | ||
| // Query each workload and convert to backend | ||
| var backends []vmcp.Backend | ||
| for _, name := range workloadNames { | ||
| workload, err := d.workloadsManager.GetWorkload(ctx, name) | ||
| if err != nil { | ||
| logger.Warnf("Failed to get workload %s: %v, skipping", name, err) | ||
| continue | ||
| } | ||
|  | ||
| // Skip workloads without a URL (not accessible) | ||
| if workload.URL == "" { | ||
| logger.Debugf("Skipping workload %s without URL", name) | ||
| continue | ||
| } | ||
|  | ||
| // Map workload status to backend health status | ||
| healthStatus := mapWorkloadStatusToHealth(workload.Status) | ||
|  | ||
| // Convert core.Workload to vmcp.Backend | ||
| backend := vmcp.Backend{ | ||
| ID: name, | ||
| Name: name, | ||
| BaseURL: workload.URL, | ||
| TransportType: workload.TransportType.String(), | ||
| HealthStatus: healthStatus, | ||
| Metadata: make(map[string]string), | ||
| } | ||
|  | ||
| // Copy user labels to metadata first | ||
| for k, v := range workload.Labels { | ||
| backend.Metadata[k] = v | ||
| } | ||
|  | ||
| // Set system metadata (these override user labels to prevent conflicts) | ||
| backend.Metadata["group"] = groupRef | ||
| backend.Metadata["tool_type"] = workload.ToolType | ||
| backend.Metadata["workload_status"] = string(workload.Status) | ||
|  | ||
| backends = append(backends, backend) | ||
| logger.Debugf("Discovered backend %s: %s (%s) with health status %s", | ||
| backend.ID, backend.BaseURL, backend.TransportType, backend.HealthStatus) | ||
| } | ||
|  | ||
| if len(backends) == 0 { | ||
| logger.Infof("No accessible backends found in group %s (all workloads lack URLs)", groupRef) | ||
| return []vmcp.Backend{}, nil | ||
| } | ||
|  | ||
| logger.Infof("Discovered %d backends in group %s", len(backends), groupRef) | ||
| return backends, nil | ||
| } | ||
|  | ||
| // mapWorkloadStatusToHealth converts a workload status to a backend health status. | ||
| func mapWorkloadStatusToHealth(status rt.WorkloadStatus) vmcp.BackendHealthStatus { | ||
| switch status { | ||
| case rt.WorkloadStatusRunning: | ||
| return vmcp.BackendHealthy | ||
| case rt.WorkloadStatusUnhealthy: | ||
| return vmcp.BackendUnhealthy | ||
| case rt.WorkloadStatusStopped, rt.WorkloadStatusError, rt.WorkloadStatusStopping, rt.WorkloadStatusRemoving: | ||
| return vmcp.BackendUnhealthy | ||
| case rt.WorkloadStatusStarting, rt.WorkloadStatusUnknown: | ||
| return vmcp.BackendUnknown | ||
| default: | ||
| return vmcp.BackendUnknown | ||
| } | ||
| } | 
      
      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.