k8s-mcp-server

k8s-mcp-server

Provides read-only Kubernetes cluster operations via MCP, enabling LLMs to query nodes, pods, logs, events, and watch real-time status for troubleshooting.

Category
Visit Server

README

K8s 集群运维 MCP Server

基于 FastMCP 的 Kubernetes 运维查询 MCP 服务。把 kubectl get / describe / logs / events --watch 等日常排查动作封装成 LLM 可调用的只读工具,让 Claude 等客户端直接读集群状态、定位故障第一现场。

⚠️ 本项目只暴露只读工具(无 create/update/delete)。但仍建议配合只读 ServiceAccount 的 RBAC 使用,见下文「安全建议」。

工具清单

工具 说明 对应 kubectl
list_nodes 节点列表概览 kubectl get nodes -o wide
get_node_detail 节点详情 kubectl describe node
get_node_resource_usage 节点资源请求/限制汇总与过载评估 describe node 的 Allocated resources
find_problem_nodes 扫描问题节点(NotReady / 各种 Pressure) —
list_pods Pod 列表(支持 namespace / label / field 过滤) kubectl get pods -o wide
get_pod_detail Pod 详情(支持 label / workload / 名称前缀定位) kubectl describe pod
read_pod_log 读取 Pod 容器日志 kubectl logs
query_events 查询集群事件(去重 + 排序) kubectl get events
watch_events 实时监听事件流 kubectl get events --watch
watch_nodes 实时监听节点状态变化 kubectl get nodes --watch
watch_pods 实时监听 Pod 状态变化 kubectl get pods --watch

另有 @mcp.prompt(节点排查 / 集群巡检引导)和 @mcp.resource(k8s://cluster/summary 集群摘要)两类附加能力。

目录结构

├── app.py              # FastMCP 实例(叶子模块,破循环导入)
├── k8s_server.py       # 入口:导入 Tools 即自动注册全部工具
├── k8s_client.py       # K8s 客户端懒加载 + 单一配置真源
├── config.py           # 12-Factor 环境变量配置
├── prompts.py          # @mcp.prompt 引导
├── resources.py        # @mcp.resource 资源
├── Tools/              # 每个工具一个文件,导入即注册
├── utils/              # 纯解析/辅助模块(无 K8s/MCP 依赖)
└── tests/              # 单元测试

安装与运行

# 1. 创建虚拟环境并安装依赖
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux / macOS
source .venv/bin/activate

pip install -r requirements.txt

# 2. 运行(HTTP transport,默认 127.0.0.1:8081)
python k8s_server.py

运行测试

pip install -r requirements-dev.txt
python -m pytest -q

配置

全部通过环境变量配置,带默认值(见 config.py):

变量 默认值 说明
KUBECONFIG_PATH "" 显式指定 kubeconfig 路径;留空走官方解析链($KUBECONFIG → ~/.kube/config)
K8S_VERIFY_SSL true 是否校验 TLS 证书;仅在调试自签且明确风险时置 false
MCP_SERVER_NAME k8s-node-server MCP 服务名
MCP_TRANSPORT http 传输方式,http 或 stdio
MCP_HOST 127.0.0.1 监听地址
MCP_PORT 8081 监听端口

K8s 连接配置加载优先级:in-cluster(Pod 内 SA)→ 显式 KUBECONFIG_PATH → 官方 kubeconfig 解析链,单一真源,不从多个文件拼装。

接入 MCP 客户端

以 Claude Code 为例(HTTP transport 已在 127.0.0.1:8081 运行时):

{
  "mcpServers": {
    "k8s-node-server": {
      "url": "http://127.0.0.1:8081/mcp"
    }
  }
}

安全建议

  1. 使用只读 RBAC:为运行本服务的 Pod / 用户配置只读 ClusterRole,避免借用高权限凭据。
  2. 默认只绑定本机:MCP_HOST 默认 127.0.0.1。如需对外暴露,务必在反代层加认证(TLS + Token / OIDC),因为 HTTP transport 本身无认证与限流。
  3. 不要关闭证书校验:K8S_VERIFY_SSL=false 仅用于临时调试自签证书,生产环境保持 true。
  4. 凭据不入库:kubeconfig 含密钥,.gitignore 已排除 kubeconfig*,切勿强制提交。

已知边界

  • 所有工具为只读,不含变更操作(这是刻意设计)。
  • watch_* 系列受全局并发锁限制:同一时刻只允许一个 watch 运行,避免给 API Server 加压。
  • 事件/日志输出有长度上限,超大内容会被截断(见 read_pod_log 的截断逻辑与 query_events 的 max_count)。

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured