Skip to content

Commit ef4b9c8

Browse files
committed
make cleanToolsets public
1 parent 11c3d70 commit ef4b9c8

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

internal/ghmcp/server.go

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func NewMCPServer(cfg MCPServerConfig) (*server.MCPServer, error) {
106106
},
107107
}
108108

109-
enabledToolsets, invalidToolsets := cleanToolsets(cfg.EnabledToolsets, cfg.DynamicToolsets)
109+
enabledToolsets, invalidToolsets := github.CleanToolsets(cfg.EnabledToolsets, cfg.DynamicToolsets)
110110

111111
if len(invalidToolsets) > 0 {
112112
fmt.Fprintf(os.Stderr, "Invalid toolsets ignored: %s\n", strings.Join(invalidToolsets, ", "))
@@ -465,57 +465,3 @@ func (t *bearerAuthTransport) RoundTrip(req *http.Request) (*http.Response, erro
465465
req.Header.Set("Authorization", "Bearer "+t.token)
466466
return t.transport.RoundTrip(req)
467467
}
468-
469-
// cleanToolsets cleans and handles special toolset keywords:
470-
// - Duplicates are removed from the result
471-
// - Removes whitespaces
472-
// - Validates toolset names and returns invalid ones separately
473-
// - "all": Returns ["all"] immediately, ignoring all other toolsets
474-
// - when dynamicToolsets is true, filters out "all" from the enabled toolsets
475-
// - "default": Replaces with the actual default toolset IDs from GetDefaultToolsetIDs()
476-
// Returns: (validToolsets, invalidToolsets)
477-
func cleanToolsets(enabledToolsets []string, dynamicToolsets bool) ([]string, []string) {
478-
seen := make(map[string]bool)
479-
result := make([]string, 0, len(enabledToolsets))
480-
invalid := make([]string, 0)
481-
validIDs := github.GetValidToolsetIDs()
482-
483-
// Add non-default toolsets, removing duplicates and trimming whitespace
484-
for _, toolset := range enabledToolsets {
485-
trimmed := strings.TrimSpace(toolset)
486-
if trimmed == "" {
487-
continue
488-
}
489-
if !seen[trimmed] {
490-
seen[trimmed] = true
491-
if trimmed != github.ToolsetMetadataDefault.ID && trimmed != github.ToolsetMetadataAll.ID {
492-
// Validate the toolset name
493-
if validIDs[trimmed] {
494-
result = append(result, trimmed)
495-
} else {
496-
invalid = append(invalid, trimmed)
497-
}
498-
}
499-
}
500-
}
501-
502-
hasDefault := seen[github.ToolsetMetadataDefault.ID]
503-
hasAll := seen[github.ToolsetMetadataAll.ID]
504-
505-
// Handle "all" keyword - return early if not in dynamic mode
506-
if hasAll && !dynamicToolsets {
507-
return []string{github.ToolsetMetadataAll.ID}, invalid
508-
}
509-
510-
// Expand "default" keyword to actual default toolsets
511-
if hasDefault {
512-
for _, defaultToolset := range github.GetDefaultToolsetIDs() {
513-
if !seen[defaultToolset] {
514-
result = append(result, defaultToolset)
515-
seen[defaultToolset] = true
516-
}
517-
}
518-
}
519-
520-
return result, invalid
521-
}

pkg/github/tools.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,57 @@ func GenerateToolsetsHelp() string {
445445

446446
return toolsetsHelp
447447
}
448+
449+
// cleanToolsets cleans and handles special toolset keywords:
450+
// - Duplicates are removed from the result
451+
// - Removes whitespaces
452+
// - Validates toolset names and returns invalid ones separately
453+
// - "all": Returns ["all"] immediately, ignoring all other toolsets
454+
// - when dynamicToolsets is true, filters out "all" from the enabled toolsets
455+
// - "default": Replaces with the actual default toolset IDs from GetDefaultToolsetIDs()
456+
// Returns: (validToolsets, invalidToolsets)
457+
func CleanToolsets(enabledToolsets []string, dynamicToolsets bool) ([]string, []string) {
458+
seen := make(map[string]bool)
459+
result := make([]string, 0, len(enabledToolsets))
460+
invalid := make([]string, 0)
461+
validIDs := GetValidToolsetIDs()
462+
463+
// Add non-default toolsets, removing duplicates and trimming whitespace
464+
for _, toolset := range enabledToolsets {
465+
trimmed := strings.TrimSpace(toolset)
466+
if trimmed == "" {
467+
continue
468+
}
469+
if !seen[trimmed] {
470+
seen[trimmed] = true
471+
if trimmed != ToolsetMetadataDefault.ID && trimmed != ToolsetMetadataAll.ID {
472+
// Validate the toolset name
473+
if validIDs[trimmed] {
474+
result = append(result, trimmed)
475+
} else {
476+
invalid = append(invalid, trimmed)
477+
}
478+
}
479+
}
480+
}
481+
482+
hasDefault := seen[ToolsetMetadataDefault.ID]
483+
hasAll := seen[ToolsetMetadataAll.ID]
484+
485+
// Handle "all" keyword - return early if not in dynamic mode
486+
if hasAll && !dynamicToolsets {
487+
return []string{ToolsetMetadataAll.ID}, invalid
488+
}
489+
490+
// Expand "default" keyword to actual default toolsets
491+
if hasDefault {
492+
for _, defaultToolset := range GetDefaultToolsetIDs() {
493+
if !seen[defaultToolset] {
494+
result = append(result, defaultToolset)
495+
seen[defaultToolset] = true
496+
}
497+
}
498+
}
499+
500+
return result, invalid
501+
}

internal/ghmcp/server_test.go renamed to pkg/github/tools_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ghmcp
1+
package github
22

33
import (
44
"testing"
@@ -238,7 +238,7 @@ func TestCleanToolsets(t *testing.T) {
238238

239239
for _, tt := range tests {
240240
t.Run(tt.name, func(t *testing.T) {
241-
result, invalid := cleanToolsets(tt.input, tt.dynamicToolsets)
241+
result, invalid := CleanToolsets(tt.input, tt.dynamicToolsets)
242242

243243
require.Len(t, result, len(tt.expected), "result length should match expected length")
244244

0 commit comments

Comments
 (0)