-
Notifications
You must be signed in to change notification settings - Fork 143
feat: Manage tool filters in working sets #226
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
Open
bobbyhouse
wants to merge
4
commits into
main
Choose a base branch
from
manage-tool-filters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+859
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,127 @@ | ||
| package workingset | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/docker/mcp-gateway/pkg/db" | ||
| ) | ||
|
|
||
| func UpdateTools(ctx context.Context, dao db.DAO, id string, enable, disable, enableAll, disableAll []string) error { | ||
| if len(enable) == 0 && len(disable) == 0 && len(enableAll) == 0 && len(disableAll) == 0 { | ||
| return fmt.Errorf("must provide at least one flag: --enable, --disable, --enable-all, or --disable-all") | ||
| } | ||
| dbWorkingSet, err := dao.GetWorkingSet(ctx, id) | ||
| if err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return fmt.Errorf("working set %s not found", id) | ||
| } | ||
| return fmt.Errorf("failed to get working set: %w", err) | ||
| } | ||
| workingSet := NewFromDb(dbWorkingSet) | ||
|
|
||
| // Handle enable-all for specified servers | ||
| enableAllCount := 0 | ||
| for _, serverName := range enableAll { | ||
| server := workingSet.FindServer(serverName) | ||
| if server == nil { | ||
| return fmt.Errorf("server %s not found in working set", serverName) | ||
| } | ||
| if server.Tools != nil { | ||
| server.Tools = nil | ||
| enableAllCount++ | ||
| } | ||
| } | ||
|
|
||
| // Handle disable-all for specified servers | ||
| disableAllCount := 0 | ||
| for _, serverName := range disableAll { | ||
| server := workingSet.FindServer(serverName) | ||
| if server == nil { | ||
| return fmt.Errorf("server %s not found in working set", serverName) | ||
| } | ||
| if server.Tools == nil || len(server.Tools) > 0 { | ||
| server.Tools = []string{} | ||
| disableAllCount++ | ||
| } | ||
| } | ||
|
|
||
| // Check for overlap between enable and disable sets | ||
| enableSet := make(map[string]bool) | ||
| for _, toolArg := range enable { | ||
| enableSet[toolArg] = true | ||
| } | ||
| disableSet := make(map[string]bool) | ||
| for _, toolArg := range disable { | ||
| disableSet[toolArg] = true | ||
| } | ||
|
|
||
| var overlapping []string | ||
| for tool := range enableSet { | ||
| if disableSet[tool] { | ||
| overlapping = append(overlapping, tool) | ||
| } | ||
| } | ||
|
|
||
| enabledCount := 0 | ||
| for _, toolArg := range enable { | ||
| serverName, toolName, found := strings.Cut(toolArg, ".") | ||
| if !found { | ||
| return fmt.Errorf("invalid tool argument: %s, expected <serverName>.<toolName>", toolArg) | ||
| } | ||
| server := workingSet.FindServer(serverName) | ||
| if server == nil { | ||
| return fmt.Errorf("server %s not found in working set for argument %s", serverName, toolArg) | ||
| } | ||
| if !slices.Contains(server.Tools, toolName) { | ||
| server.Tools = append(server.Tools, toolName) | ||
| enabledCount++ | ||
| } | ||
| } | ||
|
|
||
| disabledCount := 0 | ||
| for _, toolArg := range disable { | ||
| serverName, toolName, found := strings.Cut(toolArg, ".") | ||
| if !found { | ||
| return fmt.Errorf("invalid tool argument: %s, expected <serverName>.<toolName>", toolArg) | ||
| } | ||
| server := workingSet.FindServer(serverName) | ||
| if server == nil { | ||
| return fmt.Errorf("server %s not found in working set for argument %s", serverName, toolArg) | ||
| } | ||
| if idx := slices.Index(server.Tools, toolName); idx != -1 { | ||
| server.Tools = slices.Delete(server.Tools, idx, idx+1) | ||
| disabledCount++ | ||
| } | ||
| } | ||
|
|
||
| err = dao.UpdateWorkingSet(ctx, workingSet.ToDb()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to update working set: %w", err) | ||
| } | ||
|
|
||
| if enabledCount == 0 && disabledCount == 0 && enableAllCount == 0 && disableAllCount == 0 { | ||
| fmt.Printf("No changes made to working set %s\n", id) | ||
| } else { | ||
| if enableAllCount > 0 { | ||
| fmt.Printf("Enabled all tools for %d server(s) in working set %s\n", enableAllCount, id) | ||
| } | ||
| if disableAllCount > 0 { | ||
| fmt.Printf("Disabled all tools for %d server(s) in working set %s\n", disableAllCount, id) | ||
| } | ||
| if enabledCount > 0 || disabledCount > 0 { | ||
| fmt.Printf("Updated working set %s: %d tool(s) enabled, %d tool(s) disabled\n", id, enabledCount, disabledCount) | ||
| } | ||
| } | ||
|
|
||
| if len(overlapping) > 0 { | ||
| slices.Sort(overlapping) | ||
| fmt.Printf("Warning: The following tool(s) were both enabled and disabled in the same operation: %s\n", strings.Join(overlapping, ", ")) | ||
| } | ||
|
|
||
| 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.