k8s-mcp-server
Supports connecting to and managing K3s clusters (both local and cloud) for the same Kubernetes operations, such as listing pods, nodes, namespaces, events, and fetching logs.
Provides tools for querying and troubleshooting Kubernetes clusters, including listing namespaces, pods, nodes, and events, describing pods, and retrieving pod logs.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@k8s-mcp-server list pods in the kube-system namespace"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
k8s-mcp-server
一个基于 MCP(Model Context Protocol) 的 Kubernetes 运维助手。它将 K8s 集群的查询与诊断能力封装成标准化的 MCP 工具,让 LLM 客户端(如 ZCode、Claude Desktop、Cursor)能够通过自然语言查看和排查 Kubernetes 集群。
功能特性
基于 MCP Python SDK,使用 stdio 传输
支持多集群切换(本地 k3s / 阿里云 k3s)
8 个工具:健康检查、资源查询、Pod 一键诊断
diagnose_pod组合工具:一次调用聚合 Pod 状态、事件、日志,并通过规则引擎给出初步诊断建议完善的异常处理,错误信息对 LLM 友好
输出经过精简格式化,避免返回冗余的 K8s 原始对象
所有工具均为只读操作,不会修改集群状态
28 个 pytest 单元测试,ruff 代码检查
Docker 多阶段构建,GitHub Actions CI/CD 自动推送镜像至阿里云 ACR
Related MCP server: Multi Cluster Kubernetes MCP Server
工具列表
工具 | 说明 |
| 健康检查,返回 pong |
| 列出所有 namespace 及其状态 |
| 列出指定 namespace 下的 Pod |
| 查看 Pod 详情(状态、IP、节点、容器、重启次数、资源) |
| 获取 Pod 日志(支持 tail_lines 限制行数) |
| 列出集群节点及状态、OS、K8s 版本 |
| 列出所有 namespace 的最近事件(按时间倒序) |
| 一键诊断:聚合基本信息、容器状态、Warning 事件、日志和规则引擎建议 |
diagnose_pod 规则引擎
diagnose_pod 不依赖 LLM 做确定性判断,而是用 Python 规则引擎检测常见问题:
检测项 | 判断依据 | 诊断建议 |
CrashLoopBackOff |
| 检查日志中的异常堆栈、启动命令和配置 |
ImagePullBackOff |
| 检查镜像名、标签和仓库凭证 |
OOMKilled |
| 增加 memory limit 或排查内存泄漏 |
频繁重启 |
| 查看日志和上次终止原因 |
调度失败 |
| 检查节点资源、亲和性和污点容忍 |
规则引擎负责确定性模式匹配(零延迟、100% 一致),LLM 在此基础上做上下文关联和深度分析,将多次工具调用压缩为一次。
环境要求
Python 3.12+
uv 包管理器
可访问的 Kubernetes 集群及对应的 kubeconfig
快速开始
安装
git clone git@github.com:AmazingYe-oss/k8s-mcp-server.git
cd k8s-mcp-server
uv sync配置 kubeconfig
默认从以下路径加载 kubeconfig(在 src/k8s_mcp_server/k8s_client.py 中配置):
local→~/.kube/configcloud→~/.kube/config-k3s-cloud
如需修改集群映射,编辑 KUBECONFIGS 字典即可。
运行
uv run k8s-mcp-server服务器以 stdio 模式启动,等待 MCP 客户端连接。
MCP 客户端配置
ZCode / Claude Desktop(Windows 连接 WSL 中的 Server)
{
"mcp": {
"servers": {
"k8s": {
"type": "stdio",
"command": "wsl",
"args": [
"bash",
"-lc",
"cd /home/<your-user>/k8s-mcp-server && uv run k8s-mcp-server"
]
}
}
}
}原生 Linux/macOS
{
"mcp": {
"servers": {
"k8s": {
"type": "stdio",
"command": "uv",
"args": ["--directory", "/path/to/k8s-mcp-server", "run", "k8s-mcp-server"]
}
}
}
}Docker
构建镜像
docker build -t k8s-mcp-server:latest .运行(挂载 kubeconfig)
docker run --rm -v ~/.kube:/home/appuser/.kube:ro k8s-mcp-server:latest项目结构
src/k8s_mcp_server/
├── __init__.py
├── server.py # MCP Server 入口,注册 8 个工具
├── k8s_client.py # K8s API 客户端封装与异常处理
├── formatters.py # K8s 对象到精简文本的格式化
└── diagnose.py # diagnose_pod 组合工具与规则引擎
tests/
├── conftest.py # 共享 mock fixtures(5 种 Pod 场景)
├── test_formatters.py # formatter 单元测试
└── test_diagnose.py # 规则引擎与报告组装测试开发
# 代码检查
uv run ruff check .
uv run ruff format --check .
# 运行测试
uv run pytest
# 代码格式化
uv run ruff format .CI/CD
GitHub Actions 流水线(.github/workflows/docker-ci-cd.yml):
PR / push 时:运行 ruff 检查和 pytest
合并到 main 后:构建 Docker 镜像并推送至阿里云 ACR
标签:
latest、<git-sha>、语义化版本 tag
需要在 GitHub 仓库配置以下 Secrets:
Secret | 说明 |
| ACR 地址,如 |
| ACR 用户名 |
| ACR 密码 |
技术栈
Python 3.12
uv(依赖管理)
pytest + unittest.mock(单元测试)
ruff(代码检查与格式化)
Docker + GitHub Actions + 阿里云 ACR
Available Tools
8 toolsdescribe_podA
获取指定 pod 的详细信息,包括状态、IP、节点、容器状态、重启次数、镜像和资源配置。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
namespace: 指定的 namespace,默认 "default"
pod_name: 指定的 pod 名称
Inputs: cluster, pod_name, namespace. Returns: result
diagnose_podA
诊断指定 Pod 的健康状态。
一次调用返回基本信息、容器状态、Warning 事件、日志和基于规则的初步诊断建议。
当用户需要排查 Pod 异常、崩溃、无法启动等问题时,优先使用此工具。
Args:
namespace: Pod 所在的 namespace
pod_name: Pod 名称
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
Inputs: pod_name (required), namespace (required), cluster. Returns: result
get_pod_logsA
获取指定 pod 的日志。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
namespace: 指定的 namespace,默认 "default"
pod_name: 指定的 pod 名称
tail_lines: 返回最后多少行日志,默认 100
Inputs: cluster, pod_name, namespace, tail_lines. Returns: result
list_eventsA
列出集群中所有 namespace 下的最近事件,按时间倒序排列,最多显示 20 条。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
Inputs: cluster. Returns: result
list_namespacesA
列出集群中所有 namespace 及其状态。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
Inputs: cluster. Returns: result
list_nodesA
列出集群中的所有节点及其状态、操作系统和 K8s 版本。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
Inputs: cluster. Returns: result
list_podsA
列出指定 namespace 下的所有 pod 及其状态。
Args:
cluster: 目标集群,可选 "local"(本地 k3s)或 "cloud"(阿里云 k3s),默认 "local"
namespace: 指定的 namespace,默认 "default"
Inputs: cluster, namespace. Returns: result
pingA
健康检查工具,返回 pong 表示服务器正常运行。
Inputs: no named parameters. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 8 tools
Each tool targets a distinct resource or action: health check, namespaces, pods, logs, pod details, nodes, events, and a comprehensive diagnostic. The diagnose_pod tool is clearly positioned as a higher-level troubleshooting action, so there is no ambiguity.
Tool names consistently follow the verb_noun pattern (list_*, get_*, describe_*, diagnose_*), with only 'ping' as a standard health-check verb. All names use lowercase with underscores, providing a predictable naming scheme.
Eight tools is well within the ideal range for a focused Kubernetes inspection server. Each tool covers a distinct aspect of cluster and pod monitoring, and there is no redundancy or unnecessary bloat.
The tool set provides solid coverage for cluster health, namespace listing, pod logs, pod details, node information, events, and a diagnostic workflow. However, it lacks tools for other common Kubernetes resources like deployments, services, and configmaps, which would be expected from a general-purpose k8s server.
Related MCP Connectors
The Google GKE MCP server is a managed Model Context Protocol server that provides AI applications with tools to manage Google Kubernetes Engine (GKE) clusters and Kubernetes resources. It exposes a structured, discoverable interface that allows AI agents to interact with GKE and Kubernetes APIs, enabling them to inspect cluster configurations, retrieve Kubernetes resource YAMLs, monitor operations like cluster upgrades, diagnose issues, and optimize costs—all without needing to parse text output or use complex kubectl commands.
MCP server for building and testing AI agents with multi-model experimentation and insights.
Official Microsoft MCP Server to query Microsoft Entra data using natural language
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA read-only MCP server for Kubernetes that allows querying cluster information and diagnosing issues through natural language interfaces like Claude.7MIT
- FlicenseNot gradedqualityDmaintenanceAn MCP server that enables interaction with multiple Kubernetes clusters simultaneously, providing comprehensive tools for cluster management, resource operations, and diagnostics across different environments.-
- AlicenseNot gradedqualityDmaintenanceAn MCP server that enables AI assistants to interact with Kubernetes clusters by translating natural language into kubectl and Helm operations. It allows users to query, manage, and diagnose Kubernetes resources and cluster states through a seamless integration.20Apache 2.0
- AlicenseNot gradedqualityDmaintenanceA lightweight MCP server that enables natural language interaction with Kubernetes clusters, allowing management of pods, deployments, namespaces, and cluster resources through conversational queries or API endpoints.1MIT