Skip to content

Commit 6a380d5

Browse files
committed
fix: support list resource from all namespaces
1 parent b76bb9a commit 6a380d5

File tree

7 files changed

+85
-15
lines changed

7 files changed

+85
-15
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ FROM python:3.12-bullseye
1313

1414
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
1515
chmod +x kubectl && mv kubectl /usr/local/bin/kubectl && \
16+
curl -sSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash && \
1617
useradd --create-home --shell /bin/bash mcp
1718

1819
COPY --from=builder /app/dist/*.whl /tmp

README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# mcp-kubernetes-server
22

3-
A Model Context Protocol (MCP) server to enable Claude, Cursor and other AI tools to interact with Kubernetes cluster via kubectl.
3+
The mcp-kubernetes-server is a Model Context Protocol (MCP) server that enables AI assistants to interact with Kubernetes clusters. It serves as a bridge between AI tools (like Claude, Cursor, and GitHub Copilot) and Kubernetes, translating natural language requests into Kubernetes operations and returning the results in a format the AI tools can understand.
4+
5+
It allows AI tools to:
6+
7+
- Query Kubernetes resources
8+
- Execute kubectl commands
9+
- Manage Kubernetes clusters through natural language interactions
10+
- Diagnose and interpret the states of Kubernetes resources
11+
12+
## How it works
13+
14+
![](assets/mcp-kubernetes-server.png)
415

516
## How to install
617

@@ -27,14 +38,24 @@ Get your kubeconfig file for your Kubernetes cluster and setup in the mcpServers
2738

2839
### UVX
2940

30-
1. Install [uv](https://docs.astral.sh/uv/getting-started/installation/#installation-methods) and add it to your PATH, e.g. using curl:
41+
<details>
42+
43+
<summary>Install uv</summary>
44+
45+
Install [uv](https://docs.astral.sh/uv/getting-started/installation/#installation-methods) if it's not installed yet and add it to your PATH, e.g. using curl:
3146

3247
```bash
3348
# For Linux and MacOS
3449
curl -LsSf https://astral.sh/uv/install.sh | sh
3550
```
3651

37-
2. Install [kubectl](https://kubernetes.io/docs/tasks/tools/) and add it to your PATH, e.g.
52+
</details>
53+
54+
<details>
55+
56+
<summary>Install kubectl</summary>
57+
58+
Install [kubectl](https://kubernetes.io/docs/tasks/tools/) if it's not installed yet and add it to your PATH, e.g.
3859

3960
```bash
4061
# For Linux
@@ -44,7 +65,22 @@ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stabl
4465
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
4566
```
4667

47-
3. Config your MCP servers in [Claude Desktop](https://claude.ai/download), [Cursor](https://www.cursor.com/), [ChatGPT Copilot](https://marketplace.visualstudio.com/items?itemName=feiskyer.chatgpt-copilot), [Github Copilot](https://github.com/features/copilot) and other supported AI clients, e.g.
68+
</details>
69+
70+
<details>
71+
<summary>Install helm</summary>
72+
73+
Install [helm](https://helm.sh/docs/intro/install/) if it's not installed yet and add it to your PATH, e.g.
74+
75+
```bash
76+
curl -sSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
77+
```
78+
79+
</details>
80+
81+
<br/>
82+
83+
Config your MCP servers in [Claude Desktop](https://claude.ai/download), [Cursor](https://www.cursor.com/), [ChatGPT Copilot](https://marketplace.visualstudio.com/items?itemName=feiskyer.chatgpt-copilot), [Github Copilot](https://github.com/features/copilot) and other supported AI clients, e.g.
4884

4985
```json
5086
{
@@ -62,6 +98,38 @@ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stabl
6298
}
6399
```
64100

101+
### Options
102+
103+
Environment variables:
104+
105+
- `KUBECONFIG`: Path to your kubeconfig file, e.g. `/home/<username>/.kube/config`.
106+
107+
Command line arguments:
108+
109+
```sh
110+
usage: mcp-kubernetes-server [-h] [--disable-kubectl] [--disable-helm] [--transport {stdio,sse}] [--port PORT]
111+
112+
MCP Kubernetes Server
113+
114+
options:
115+
-h, --help show this help message and exit
116+
--disable-kubectl Disable kubectl command execution
117+
--disable-helm Disable helm command execution
118+
--transport {stdio,sse}
119+
Transport mechanism to use (stdio or sse)
120+
--port PORT Port to use for the server (only used with sse transport)
121+
```
122+
123+
## Usage
124+
125+
Ask any questions about Kubernetes cluster in your AI client, e.g.
126+
127+
```txt
128+
What is the status of my Kubernetes cluster?
129+
130+
What is wrong with my nginx pod?
131+
```
132+
65133
## Development
66134

67135
How to run the project locally:

assets/mcp-kubernetes-server.png

242 KB
Loading

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-kubernetes-server"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "MCP server to manage Kubernetes clusters via kubectl"
55
readme = "README.md"
66
requires-python = ">=3.11"

src/mcp_kubernetes_server/kubeclient.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ def _get_group_versions(api_client):
124124
yield g["name"], v["version"]
125125

126126

127-
async def get(resource, name, namespace="default"):
127+
async def get(resource, name, namespace):
128128
"""
129-
Fetch any Kubernetes object (or list) as JSON string. Pass name="" to list the collection.
129+
Fetch any Kubernetes object (or list) as JSON string. Pass name="" to list the collection and namespace="" to get the resource in all namespaces.
130130
131131
:param resource: The resource type (e.g., pods, deployments).
132132
:param name: The name of the resource.
133-
:param namespace: The namespace of the resource. Defaults to "default".
133+
:param namespace: The namespace of the resource.
134134
:return: The JSON representation of the resource.
135135
"""
136136
try:
@@ -163,11 +163,13 @@ async def get(resource, name, namespace="default"):
163163

164164
# 3. GET the object or list
165165
if rc.namespaced:
166-
fetched = (
167-
rc.get(name=name, namespace=namespace)
168-
if name
169-
else rc.get(namespace=namespace)
170-
)
166+
if name:
167+
fetched = rc.get(name=name, namespace=namespace or "default")
168+
else:
169+
if namespace == "" or namespace is None:
170+
fetched = rc.get(all_namespaces=True)
171+
else:
172+
fetched = rc.get(namespace=namespace)
171173
else:
172174
fetched = rc.get(name=name) if name else rc.get()
173175

src/mcp_kubernetes_server/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
import argparse
3-
import json
43
from fastmcp import FastMCP
54
from .kubeclient import setup_client, apis,crds, get
65
from .command import kubectl, helm

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)