Skip to content

Commit 2778bfc

Browse files
committed
feat: add contexts_list tool
Signed-off-by: Calum Murray <[email protected]>
1 parent 3e4af46 commit 2778bfc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

pkg/api/toolsets.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func NewToolCallResult(content string, err error) *ToolCallResult {
4444
type ToolHandlerParams struct {
4545
context.Context
4646
*internalk8s.Kubernetes
47+
internalk8s.ManagerProvider
4748
ToolCallRequest
4849
ListOutput output.Output
4950
}

pkg/toolsets/config/configuration.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import (
1212

1313
func initConfiguration() []api.ServerTool {
1414
tools := []api.ServerTool{
15+
{Tool: api.Tool{
16+
Name: "contexts_list",
17+
Description: "List all available contexts from the kubeconfig file. Shows context names for all available contexts",
18+
InputSchema: &jsonschema.Schema{
19+
Type: "object",
20+
},
21+
Annotations: api.ToolAnnotations{
22+
Title: "Contexts: List",
23+
ReadOnlyHint: ptr.To(true),
24+
DestructiveHint: ptr.To(false),
25+
IdempotentHint: ptr.To(true),
26+
OpenWorldHint: ptr.To(false),
27+
},
28+
}, Handler: contextsList},
1529
{Tool: api.Tool{
1630
Name: "configuration_view",
1731
Description: "Get the current Kubernetes configuration content as a kubeconfig YAML",
@@ -39,6 +53,37 @@ func initConfiguration() []api.ServerTool {
3953
return tools
4054
}
4155

56+
func contextsList(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
57+
contexts, err := params.GetTargets(params.Context)
58+
if err != nil {
59+
return api.NewToolCallResult("", fmt.Errorf("failed to list contexts: %v", err)), nil
60+
}
61+
62+
if len(contexts) == 0 {
63+
return api.NewToolCallResult("No contexts found in kubeconfig", nil), nil
64+
}
65+
66+
defaultContext := params.GetDefaultTarget()
67+
68+
result := fmt.Sprintf("Available Kubernetes contexts (%d total, default: %s):\n\n", len(contexts), defaultContext)
69+
result += "Format: [*] CONTEXT_NAME\n"
70+
result += " (* indicates the default context used in tools if context is not set)\n\n"
71+
result += "Contexts:\n---------\n"
72+
for _, context := range contexts {
73+
marker := " "
74+
if context == defaultContext {
75+
marker = "*"
76+
}
77+
78+
result += fmt.Sprintf("%s%s\n", marker, context)
79+
}
80+
result += "---------\n\n"
81+
82+
result += "To use a specific context with any tool, set the 'context' parameter in the tool call arguments"
83+
84+
return api.NewToolCallResult(result, nil), nil
85+
}
86+
4287
func configurationView(params api.ToolHandlerParams) (*api.ToolCallResult, error) {
4388
minify := true
4489
minified := params.GetArguments()["minified"]

0 commit comments

Comments
 (0)