|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/containers/kubernetes-mcp-server/pkg/api" |
| 8 | + "github.com/google/jsonschema-go/jsonschema" |
| 9 | +) |
| 10 | + |
| 11 | +type ToolMutator func(tool api.ServerTool) api.ServerTool |
| 12 | + |
| 13 | +const maxClustersInEnum = 15 // TODO: test and validate that this is a reasonable cutoff |
| 14 | + |
| 15 | +func WithClusterParameter(defaultCluster string, clusters, skipToolNames []string) ToolMutator { |
| 16 | + skipNames := make(map[string]struct{}, len(skipToolNames)) |
| 17 | + for _, n := range skipToolNames { |
| 18 | + skipNames[n] = struct{}{} |
| 19 | + } |
| 20 | + |
| 21 | + return func(tool api.ServerTool) api.ServerTool { |
| 22 | + if _, ok := skipNames[tool.Tool.Name]; ok { |
| 23 | + return tool |
| 24 | + } |
| 25 | + |
| 26 | + if tool.Tool.InputSchema == nil { |
| 27 | + tool.Tool.InputSchema = &jsonschema.Schema{Type: "object"} |
| 28 | + } |
| 29 | + |
| 30 | + if tool.Tool.InputSchema.Properties == nil { |
| 31 | + tool.Tool.InputSchema.Properties = make(map[string]*jsonschema.Schema) |
| 32 | + } |
| 33 | + |
| 34 | + if len(clusters) > 1 { |
| 35 | + tool.Tool.InputSchema.Properties["cluster"] = createClusterProperty(defaultCluster, clusters) |
| 36 | + } |
| 37 | + |
| 38 | + return tool |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func createClusterProperty(defaultCluster string, clusters []string) *jsonschema.Schema { |
| 43 | + baseSchema := &jsonschema.Schema{ |
| 44 | + Type: "string", |
| 45 | + Description: fmt.Sprintf( |
| 46 | + "Optional parameter selecting which cluster to run the tool in. Defaults to %s if not set", defaultCluster, |
| 47 | + ), |
| 48 | + } |
| 49 | + |
| 50 | + if len(clusters) <= maxClustersInEnum { |
| 51 | + // Sort clusters to ensure consistent enum ordering |
| 52 | + sort.Strings(clusters) |
| 53 | + |
| 54 | + enumValues := make([]any, 0, len(clusters)) |
| 55 | + for _, c := range clusters { |
| 56 | + enumValues = append(enumValues, c) |
| 57 | + } |
| 58 | + baseSchema.Enum = enumValues |
| 59 | + } |
| 60 | + |
| 61 | + return baseSchema |
| 62 | +} |
0 commit comments