Skip to content

Commit da29a91

Browse files
committed
refactor: make tool mutator generic between cluster/context naming
Signed-off-by: Calum Murray <[email protected]>
1 parent 2778bfc commit da29a91

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

pkg/mcp/tool_mutator.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
type ToolMutator func(tool api.ServerTool) api.ServerTool
1212

13-
const maxClustersInEnum = 15 // TODO: test and validate that this is a reasonable cutoff
13+
const maxTargetsInEnum = 15 // TODO: test and validate that this is a reasonable cutoff
1414

15-
func WithClusterParameter(defaultCluster string, clusters, skipToolNames []string) ToolMutator {
15+
func WithTargetParameter(defaultCluster, targetParameterName string, targets, skipToolNames []string) ToolMutator {
1616
skipNames := make(map[string]struct{}, len(skipToolNames))
1717
for _, n := range skipToolNames {
1818
skipNames[n] = struct{}{}
@@ -31,28 +31,34 @@ func WithClusterParameter(defaultCluster string, clusters, skipToolNames []strin
3131
tool.Tool.InputSchema.Properties = make(map[string]*jsonschema.Schema)
3232
}
3333

34-
if len(clusters) > 1 {
35-
tool.Tool.InputSchema.Properties["cluster"] = createClusterProperty(defaultCluster, clusters)
34+
if len(targets) > 1 {
35+
tool.Tool.InputSchema.Properties[targetParameterName] = createTargetProperty(
36+
defaultCluster,
37+
targetParameterName,
38+
targets,
39+
)
3640
}
3741

3842
return tool
3943
}
4044
}
4145

42-
func createClusterProperty(defaultCluster string, clusters []string) *jsonschema.Schema {
46+
func createTargetProperty(defaultCluster, targetName string, targets []string) *jsonschema.Schema {
4347
baseSchema := &jsonschema.Schema{
4448
Type: "string",
4549
Description: fmt.Sprintf(
46-
"Optional parameter selecting which cluster to run the tool in. Defaults to %s if not set", defaultCluster,
50+
"Optional parameter selecting which %s to run the tool in. Defaults to %s if not set",
51+
targetName,
52+
defaultCluster,
4753
),
4854
}
4955

50-
if len(clusters) <= maxClustersInEnum {
56+
if len(targets) <= maxTargetsInEnum {
5157
// Sort clusters to ensure consistent enum ordering
52-
sort.Strings(clusters)
58+
sort.Strings(targets)
5359

54-
enumValues := make([]any, 0, len(clusters))
55-
for _, c := range clusters {
60+
enumValues := make([]any, 0, len(targets))
61+
for _, c := range targets {
5662
enumValues = append(enumValues, c)
5763
}
5864
baseSchema.Enum = enumValues

pkg/mcp/tool_mutator_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ func createTestToolWithExistingProperties(name string) api.ServerTool {
6666

6767
func TestWithClusterParameter(t *testing.T) {
6868
tests := []struct {
69-
name string
70-
defaultCluster string
71-
clusters []string
72-
skipToolNames []string
73-
toolName string
74-
toolFactory func(string) api.ServerTool
75-
expectCluster bool
76-
expectEnum bool
77-
enumCount int
69+
name string
70+
defaultCluster string
71+
targetParameterName string
72+
clusters []string
73+
skipToolNames []string
74+
toolName string
75+
toolFactory func(string) api.ServerTool
76+
expectCluster bool
77+
expectEnum bool
78+
enumCount int
7879
}{
7980
{
8081
name: "adds cluster parameter when multiple clusters provided",
@@ -146,7 +147,10 @@ func TestWithClusterParameter(t *testing.T) {
146147

147148
for _, tt := range tests {
148149
t.Run(tt.name, func(t *testing.T) {
149-
mutator := WithClusterParameter(tt.defaultCluster, tt.clusters, tt.skipToolNames)
150+
if tt.targetParameterName == "" {
151+
tt.targetParameterName = "cluster"
152+
}
153+
mutator := WithTargetParameter(tt.defaultCluster, tt.targetParameterName, tt.clusters, tt.skipToolNames)
150154
tool := tt.toolFactory(tt.toolName)
151155
originalTool := tool // Keep reference to check if tool was unchanged
152156

@@ -192,41 +196,47 @@ func TestCreateClusterProperty(t *testing.T) {
192196
tests := []struct {
193197
name string
194198
defaultCluster string
199+
targetName string
195200
clusters []string
196201
expectEnum bool
197202
expectedCount int
198203
}{
199204
{
200205
name: "creates property with enum when clusters <= maxClustersInEnum",
201206
defaultCluster: "default",
207+
targetName: "cluster",
202208
clusters: []string{"cluster1", "cluster2", "cluster3"},
203209
expectEnum: true,
204210
expectedCount: 3,
205211
},
206212
{
207213
name: "creates property without enum when clusters > maxClustersInEnum",
208214
defaultCluster: "default",
209-
clusters: make([]string, maxClustersInEnum+5), // 20 clusters
215+
targetName: "cluster",
216+
clusters: make([]string, maxTargetsInEnum+5), // 20 clusters
210217
expectEnum: false,
211218
expectedCount: 0,
212219
},
213220
{
214221
name: "creates property with exact maxClustersInEnum clusters",
215222
defaultCluster: "default",
216-
clusters: make([]string, maxClustersInEnum),
223+
targetName: "cluster",
224+
clusters: make([]string, maxTargetsInEnum),
217225
expectEnum: true,
218-
expectedCount: maxClustersInEnum,
226+
expectedCount: maxTargetsInEnum,
219227
},
220228
{
221229
name: "handles single cluster",
222230
defaultCluster: "default",
231+
targetName: "cluster",
223232
clusters: []string{"single-cluster"},
224233
expectEnum: true,
225234
expectedCount: 1,
226235
},
227236
{
228237
name: "handles empty clusters list",
229238
defaultCluster: "default",
239+
targetName: "cluster",
230240
clusters: []string{},
231241
expectEnum: true,
232242
expectedCount: 0,
@@ -242,7 +252,7 @@ func TestCreateClusterProperty(t *testing.T) {
242252
}
243253
}
244254

245-
property := createClusterProperty(tt.defaultCluster, tt.clusters)
255+
property := createTargetProperty(tt.defaultCluster, tt.targetName, tt.clusters)
246256

247257
assert.Equal(t, "string", property.Type)
248258
assert.Contains(t, property.Description, tt.defaultCluster)
@@ -279,6 +289,6 @@ func TestToolMutatorType(t *testing.T) {
279289

280290
func TestMaxClustersInEnumConstant(t *testing.T) {
281291
t.Run("maxClustersInEnum has expected value", func(t *testing.T) {
282-
assert.Equal(t, 15, maxClustersInEnum, "maxClustersInEnum should be 15")
292+
assert.Equal(t, 15, maxTargetsInEnum, "maxClustersInEnum should be 15")
283293
})
284294
}

0 commit comments

Comments
 (0)