Kubernetes MCP logo

Kubernetes MCP

by HSn0918

Kubernetes MCP is a Model Context Protocol (MCP) server implementation in Go for interacting with Kubernetes clusters. It allows MCP-compatible clients to perform Kubernetes operations through defined tools.

View on GitHub

Last updated: N/A

Kubernetes MCP

<div align="center"> <img src="logo.png" alt="Kubernetes MCP Logo" width="200"> </div>

English | δΈ­ζ–‡

✨ A Model Context Protocol (MCP) server implementation designed with Go for interacting with Kubernetes clusters. This server allows MCP-compatible clients to perform Kubernetes operations through defined tools.

πŸ“Œ Core Features

  • πŸ”Ή MCP Server: Implements the mcp-go library to provide MCP functionality
  • πŸ”Ή Kubernetes Interaction: Uses controller-runtime client to interact with clusters
  • πŸ”Ή Transport Methods: Supports standard I/O (stdio) or Server-Sent Events (sse)// but the stdio is not implement

πŸ› οΈ Resource Management Tools

πŸ“Š Implemented API Groups

πŸ”Έ Core API Group (v1)

  • List, get, describe, create, update, delete operations
  • Cluster-scoped: list namespaces, list nodes
  • Get Pod logs functionality

πŸ”Έ Apps API Group (apps/v1)

  • Full support for Deployment, ReplicaSet, StatefulSet, DaemonSet

πŸ”Έ Batch API Group (batch/v1)

  • Full support for Job, CronJob

πŸ”Έ Networking API Group (networking.k8s.io/v1)

  • Full support for Ingress, NetworkPolicy

πŸ”Έ RBAC API Group (rbac.authorization.k8s.io/v1)

  • Full support for Role, RoleBinding, ClusterRole, ClusterRoleBinding

πŸ”Έ Storage API Group (storage.k8s.io/v1)

  • Full support for StorageClass, VolumeAttachment

πŸ”Έ Policy API Group (policy/v1beta1)

  • Full support for PodSecurityPolicy, PodDisruptionBudget

πŸ”Έ API Extensions API Group (apiextensions.k8s.io/v1)

  • Full support for CustomResourceDefinition

πŸ”Έ Autoscaling API Group (autoscaling/v1)

  • Full support for HorizontalPodAutoscaler

πŸ“‹ Requirements

  • πŸ“Œ Go 1.24
  • πŸ“Œ Kubernetes cluster access (via kubeconfig or in-cluster service account)

πŸ“¦ Key Dependencies

🧩 Core Libraries:

  • github.com/mark3labs/mcp-go - MCP protocol implementation
  • sigs.k8s.io/controller-runtime - Kubernetes client
  • k8s.io/client-go - Core Kubernetes libraries
  • github.com/spf13/cobra - CLI structure
  • go.uber.org/zap - Logging
  • sigs.k8s.io/yaml - YAML processing

πŸ”¨ Build Methods

πŸ“₯ Build from Source

git clone https://github.com/HSn0918/kubernetes-mcp.git
cd kubernetes-mcp
go build -o kubernetes-mcp ./cmd/kubernetes-mcp
./kubernetes-mcp server --transport=sse --port 8080

🐳 Docker Build

# Build image
docker build -t kubernetes-mcp:latest \
  --build-arg VERSION=$(git describe --tags --always) \
  --build-arg COMMIT=$(git rev-parse HEAD) \
  --build-arg BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") .

# Run with stdio transport
docker run -v ~/.kube:/root/.kube kubernetes-mcp:latest server transport stdio

# Run with SSE transport
docker run -p 8080:8080 -v ~/.kube:/root/.kube kubernetes-mcp:latest server transport sse

# Run with SSE transport and custom base URL
docker run -p 8080:8080 -v ~/.kube:/root/.kube kubernetes-mcp:latest server transport sse --base-url="http://your-host:8080"

# View version info
docker run kubernetes-mcp:latest version

# Specify custom kubeconfig
docker run -v /path/to/config:/config kubernetes-mcp:latest server transport sse --kubeconfig=/config

πŸš€ Usage

πŸ”„ Starting the Server

# Using standard I/O
./kubernetes-mcp server transport stdio

# Using SSE (Server-Sent Events)
./kubernetes-mcp server transport sse --port 8080

# Specifying custom base URL for SSE connections
./kubernetes-mcp server transport sse --port 8080 --base-url="http://your-server-address:8080"

# Setting CORS allowed origins
./kubernetes-mcp server transport sse --allow-origins="*"

# Specifying Kubeconfig
./kubernetes-mcp server transport sse --kubeconfig /path/to/your/kubeconfig

# View version
./kubernetes-mcp version

βš™οΈ Command Structure

The application uses a hierarchical command structure:

kubernetes-mcp
β”œβ”€β”€ server
β”‚   └── transport
β”‚       β”œβ”€β”€ sse
β”‚       β”‚   β”œβ”€β”€ --port=8080
β”‚       β”‚   β”œβ”€β”€ --health-port=8081
β”‚       β”‚   β”œβ”€β”€ --base-url="http://example.com:8080"
β”‚       β”‚   └── --allow-origins="*"
β”‚       └── stdio
└── version

βš™οΈ Configuration Options

Global options that can be used with any command:

  • πŸ”§ Config file: --kubeconfig (path to Kubernetes configuration)
  • πŸ”§ Log level: --log-level (debug/info/warn/error)
  • πŸ”§ Log format: --log-format (console/json)

SSE transport specific options:

  • πŸ”§ Port: --port (default 8080)
  • πŸ”§ Health check port: --health-port (default 8081)
  • πŸ”§ Base URL: --base-url (URL clients will use to connect to the server)
  • πŸ”§ CORS allowed origins: --allow-origins (comma-separated list or "*" for all)

🧩 Advanced Features

πŸ” Structured Tools

  • πŸ” GET_CLUSTER_INFO: Get cluster information and version details
  • πŸ” GET_API_RESOURCES: List available API resources in the cluster
  • πŸ” SEARCH_RESOURCES: Search across namespaces and resource types
  • πŸ” EXPLAIN_RESOURCE: Get resource structure and field details
  • πŸ” APPLY_MANIFEST: Apply YAML manifests to the cluster
  • πŸ” VALIDATE_MANIFEST: Validate YAML manifest format
  • πŸ” DIFF_MANIFEST: Compare YAML with existing cluster resources
  • πŸ” GET_EVENTS: Get events related to specific resources

πŸ’‘ Prompt System

  • πŸ”– KUBERNETES_YAML_PROMPT: Generate standard Kubernetes YAML
  • πŸ”– KUBERNETES_QUERY_PROMPT: Kubernetes operation guidance
  • πŸ”– TROUBLESHOOT_PODS_PROMPT: Pod troubleshooting guide
  • πŸ”– TROUBLESHOOT_NODES_PROMPT: Node troubleshooting guide

πŸ”„ Standard Resource Operations

Each API group supports the following operations:

  • List resources: Get resource lists, filterable by namespace and labels
  • Get resource: Retrieve specific resources in YAML format
  • Describe resource: Get detailed readable descriptions of resources
  • Create resource: Create new resources from YAML
  • Update resource: Update existing resources using YAML
  • Delete resource: Remove specific resources

🌟 Core API Group Special Operations

  • Get Pod logs: Retrieve logs from specific Pod containers
  • List namespaces: View all available namespaces in the cluster
  • List nodes: View all nodes and their status in the cluster

πŸ“Š Log Analysis Features

  • Error Pattern Recognition: Identifies common error patterns and frequencies
  • Time-based Distribution Analysis: Analyzes error occurrence patterns over time
  • HTTP Status Code Tracking: Monitors and categorizes HTTP response codes
  • Performance Metrics: Tracks response times and resource usage statistics

πŸ“Š Cluster Metrics Features

  • πŸ” GET_NODE_METRICS: Retrieve node resource usage metrics, including CPU and memory utilization
  • πŸ” GET_POD_METRICS: Get Pod resource usage metrics to monitor container CPU and memory consumption
  • πŸ” GET_RESOURCE_METRICS: Obtain overall cluster resource usage including CPU, memory, storage, and Pod count statistics
  • πŸ” GET_TOP_CONSUMERS: Identify Pods with highest resource consumption to pinpoint resource bottlenecks

All metrics APIs support:

  • Flexible sorting: Sort by CPU, memory consumption or utilization percentage
  • Detailed filtering: Use field selectors and label selectors to target resources precisely
  • Result limitation: Control the number of returned results
  • JSON formatting: All responses are returned in structured JSON format for easy processing

πŸ“ Cluster Metrics Prompt System

  • πŸ”– CLUSTER_RESOURCE_USAGE: Guidance for retrieving cluster resource usage
  • πŸ”– NODE_RESOURCE_USAGE: Guidance for retrieving node resource usage
  • πŸ”– POD_RESOURCE_USAGE: Guidance for retrieving Pod resource usage

πŸ“‹ API Response Formatting

All API responses are now standardized in JSON format:

  • πŸ”Έ Structured Responses: All API responses are returned in consistent JSON structures
  • πŸ”Έ Node Lists: Include detailed information such as node name, status, roles, labels, taints, and allocatable resources
  • πŸ”Έ Namespace Lists: Include namespace name, status, labels, annotations, and other details
  • πŸ”Έ Logs and Log Analysis: Log content and analysis results are returned in structured format for easy processing
  • πŸ”Έ Resource Metrics: CPU, memory, storage metrics are returned in structured format including raw values and percentages
  • πŸ”Έ Time Formatting: Supports human-readable time formats in both English and Chinese, such as "5 minutes ago"/"5εˆ†ι’Ÿε‰"