-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,71 @@ | ||
package tools | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/strowk/mcp-k8s-go/internal/k8s" | ||
"github.com/strowk/mcp-k8s-go/internal/utils" | ||
|
||
"github.com/strowk/foxy-contexts/pkg/fxctx" | ||
"github.com/strowk/foxy-contexts/pkg/mcp" | ||
"github.com/strowk/foxy-contexts/pkg/toolinput" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func NewListNamespacesTool(pool k8s.ClientPool) fxctx.Tool { | ||
contextProperty := "context" | ||
schema := toolinput.NewToolInputSchema( | ||
toolinput.WithString(contextProperty, "Name of the Kubernetes context to use, defaults to current context"), | ||
) | ||
return fxctx.NewTool( | ||
&mcp.Tool{ | ||
Name: "list-k8s-namespaces", | ||
Description: utils.Ptr("List Kubernetes namespaces using specific context"), | ||
InputSchema: schema.GetMcpToolInputSchema(), | ||
}, | ||
func(args map[string]interface{}) *mcp.CallToolResult { | ||
input, err := schema.Validate(args) | ||
if err != nil { | ||
return errResponse(err) | ||
} | ||
k8sCtx := input.StringOr(contextProperty, "") | ||
|
||
clientset, err := pool.GetClientset(k8sCtx) | ||
|
||
namespace, err := clientset. | ||
CoreV1(). | ||
Namespaces(). | ||
List(context.Background(), metav1.ListOptions{}) | ||
if err != nil { | ||
return errResponse(err) | ||
} | ||
|
||
sort.Slice(namespace.Items, func(i, j int) bool { | ||
return namespace.Items[i].Name < namespace.Items[j].Name | ||
}) | ||
|
||
var contents []interface{} = make([]interface{}, len(namespace.Items)) | ||
for i, namespace := range namespace.Items { | ||
content, err := NewJsonContent(NamespacesInList{ | ||
Name: namespace.Name, | ||
}) | ||
if err != nil { | ||
return errResponse(err) | ||
} | ||
contents[i] = content | ||
} | ||
|
||
return &mcp.CallToolResult{ | ||
Meta: map[string]interface{}{}, | ||
Content: contents, | ||
IsError: utils.Ptr(false), | ||
} | ||
}, | ||
) | ||
} | ||
|
||
type NamespacesInList struct { | ||
Name string `json:"name"` | ||
} |
This file contains 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 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 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 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,44 @@ | ||
in: | ||
{ | ||
"jsonrpc": "2.0", | ||
"method": "tools/call", | ||
"id": 2, | ||
"params": | ||
{ | ||
"name": "list-k8s-namespaces", | ||
"arguments": | ||
{ "context": "k3d-mcp-k8s-integration-test" }, | ||
}, | ||
} | ||
out: | ||
{ | ||
"jsonrpc": "2.0", | ||
"id": 2, | ||
"result": | ||
{ | ||
"content": | ||
[ | ||
{ | ||
"type": "text", | ||
"text": '{"name":"default"}', | ||
}, | ||
{ | ||
"type": "text", | ||
"text": '{"name":"kube-node-lease"}', | ||
}, | ||
{ | ||
"type": "text", | ||
"text": '{"name":"kube-public"}', | ||
}, | ||
{ | ||
"type": "text", | ||
"text": '{"name":"kube-system"}', | ||
}, | ||
{ | ||
"type": "text", | ||
"text": '{"name":"test"}', | ||
}, | ||
], | ||
"isError": false, | ||
}, | ||
} |