Skip to content

Commit eab2431

Browse files
committed
added: list namespaces tool
1 parent 99fd9cb commit eab2431

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project is intended as a both MCP server connecting to Kubernetes and a lib
55
Currently available:
66
- resource: K8S contexts as read from kubeconfig configurations
77
- tool: list-k8s-contexts
8+
- tool: list-k8s-namespaces in a given context
89
- tool: list-k8s-pods in a given context and namespace
910
- tool: list-k8s-events in a given context and namespace
1011
- tool: list-k8s-services in a given context and namespace

internal/tools/namespaces.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package tools
2+
3+
import (
4+
"context"
5+
"sort"
6+
7+
"github.com/strowk/mcp-k8s-go/internal/k8s"
8+
"github.com/strowk/mcp-k8s-go/internal/utils"
9+
10+
"github.com/strowk/foxy-contexts/pkg/fxctx"
11+
"github.com/strowk/foxy-contexts/pkg/mcp"
12+
"github.com/strowk/foxy-contexts/pkg/toolinput"
13+
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
)
16+
17+
func NewListNamespacesTool(pool k8s.ClientPool) fxctx.Tool {
18+
contextProperty := "context"
19+
schema := toolinput.NewToolInputSchema(
20+
toolinput.WithString(contextProperty, "Name of the Kubernetes context to use, defaults to current context"),
21+
)
22+
return fxctx.NewTool(
23+
&mcp.Tool{
24+
Name: "list-k8s-namespaces",
25+
Description: utils.Ptr("List Kubernetes namespaces using specific context"),
26+
InputSchema: schema.GetMcpToolInputSchema(),
27+
},
28+
func(args map[string]interface{}) *mcp.CallToolResult {
29+
input, err := schema.Validate(args)
30+
if err != nil {
31+
return errResponse(err)
32+
}
33+
k8sCtx := input.StringOr(contextProperty, "")
34+
35+
clientset, err := pool.GetClientset(k8sCtx)
36+
37+
namespace, err := clientset.
38+
CoreV1().
39+
Namespaces().
40+
List(context.Background(), metav1.ListOptions{})
41+
if err != nil {
42+
return errResponse(err)
43+
}
44+
45+
sort.Slice(namespace.Items, func(i, j int) bool {
46+
return namespace.Items[i].Name < namespace.Items[j].Name
47+
})
48+
49+
var contents []interface{} = make([]interface{}, len(namespace.Items))
50+
for i, namespace := range namespace.Items {
51+
content, err := NewJsonContent(NamespacesInList{
52+
Name: namespace.Name,
53+
})
54+
if err != nil {
55+
return errResponse(err)
56+
}
57+
contents[i] = content
58+
}
59+
60+
return &mcp.CallToolResult{
61+
Meta: map[string]interface{}{},
62+
Content: contents,
63+
IsError: utils.Ptr(false),
64+
}
65+
},
66+
)
67+
}
68+
69+
type NamespacesInList struct {
70+
Name string `json:"name"`
71+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func main() {
6969
).
7070
WithTool(tools.NewPodLogsTool).
7171
WithTool(tools.NewListContextsTool).
72+
WithTool(tools.NewListNamespacesTool).
7273
WithTool(tools.NewListEventsTool).
7374
WithTool(tools.NewListPodsTool).
7475
WithTool(tools.NewListServicesTool).

packages/npm-mcp-k8s/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is a distribution of MCP server connecting to Kubernetes written in Golang
55
Currently available:
66
- resource: K8S contexts as read from kubeconfig configurations
77
- tool: list-k8s-contexts
8+
- tool: list-k8s-namespaces in a given context
89
- tool: list-k8s-pods in a given context and namespace
910
- tool: list-k8s-events in a given context and namespace
1011
- tool: list-k8s-services in a given context and namespace

testdata/list_tools_test.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ out:
8181
"required": ["context", "namespace"],
8282
},
8383
},
84+
{
85+
"name": "list-k8s-namespaces",
86+
"description": "List Kubernetes namespaces using specific context",
87+
"inputSchema":
88+
{
89+
"type": "object",
90+
"properties":
91+
{
92+
"context":
93+
{
94+
"type": "string",
95+
"description": "Name of the Kubernetes context to use, defaults to current context",
96+
},
97+
},
98+
},
99+
},
84100
{
85101
"name": "list-k8s-pods",
86102
"description": "List Kubernetes pods using specific context in a specified namespace",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
in:
2+
{
3+
"jsonrpc": "2.0",
4+
"method": "tools/call",
5+
"id": 2,
6+
"params":
7+
{
8+
"name": "list-k8s-namespaces",
9+
"arguments":
10+
{ "context": "k3d-mcp-k8s-integration-test" },
11+
},
12+
}
13+
out:
14+
{
15+
"jsonrpc": "2.0",
16+
"id": 2,
17+
"result":
18+
{
19+
"content":
20+
[
21+
{
22+
"type": "text",
23+
"text": '{"name":"default"}',
24+
},
25+
{
26+
"type": "text",
27+
"text": '{"name":"kube-node-lease"}',
28+
},
29+
{
30+
"type": "text",
31+
"text": '{"name":"kube-public"}',
32+
},
33+
{
34+
"type": "text",
35+
"text": '{"name":"kube-system"}',
36+
},
37+
{
38+
"type": "text",
39+
"text": '{"name":"test"}',
40+
},
41+
],
42+
"isError": false,
43+
},
44+
}

0 commit comments

Comments
 (0)