Skip to content

Commit

Permalink
added: list namespaces tool
Browse files Browse the repository at this point in the history
  • Loading branch information
strowk committed Dec 13, 2024
1 parent 99fd9cb commit eab2431
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project is intended as a both MCP server connecting to Kubernetes and a lib
Currently available:
- resource: K8S contexts as read from kubeconfig configurations
- tool: list-k8s-contexts
- tool: list-k8s-namespaces in a given context
- tool: list-k8s-pods in a given context and namespace
- tool: list-k8s-events in a given context and namespace
- tool: list-k8s-services in a given context and namespace
Expand Down
71 changes: 71 additions & 0 deletions internal/tools/namespaces.go
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"`
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
).
WithTool(tools.NewPodLogsTool).
WithTool(tools.NewListContextsTool).
WithTool(tools.NewListNamespacesTool).
WithTool(tools.NewListEventsTool).
WithTool(tools.NewListPodsTool).
WithTool(tools.NewListServicesTool).
Expand Down
1 change: 1 addition & 0 deletions packages/npm-mcp-k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is a distribution of MCP server connecting to Kubernetes written in Golang
Currently available:
- resource: K8S contexts as read from kubeconfig configurations
- tool: list-k8s-contexts
- tool: list-k8s-namespaces in a given context
- tool: list-k8s-pods in a given context and namespace
- tool: list-k8s-events in a given context and namespace
- tool: list-k8s-services in a given context and namespace
Expand Down
16 changes: 16 additions & 0 deletions testdata/list_tools_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ out:
"required": ["context", "namespace"],
},
},
{
"name": "list-k8s-namespaces",
"description": "List Kubernetes namespaces using specific context",
"inputSchema":
{
"type": "object",
"properties":
{
"context":
{
"type": "string",
"description": "Name of the Kubernetes context to use, defaults to current context",
},
},
},
},
{
"name": "list-k8s-pods",
"description": "List Kubernetes pods using specific context in a specified namespace",
Expand Down
44 changes: 44 additions & 0 deletions testdata/with_k3d/list_k8s_namespaces_test.yaml
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,
},
}

0 comments on commit eab2431

Please sign in to comment.