Kubernetes MCP Server: Setup, Configuration, and Production Usage
Quick Answer
- What it is:
mcp-server-kubernetesis an MCP (Model Context Protocol) server that lets AI assistants like Claude Desktop manage Kubernetes clusters using natural language commands. - First check: Ensure
kubectl get nodesworks from your terminal before installing the MCP server — it uses your existing kubeconfig. - Minimal install: Run
npm install -g mcp-server-kubernetesglobally, then configure Claude Desktop with thenpxcommand shown below. - Production boundary: Set
ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS=truefor read-only access; never use this in production without a dedicated read-only ServiceAccount and isolated namespace. - Version note: Requires Node.js 18+ and access to a Kubernetes API server (any version compatible with your kubeconfig context).
What Problem It Solves
DevOps engineers, platform engineers, and SRE teams often need to perform routine Kubernetes operations — checking pod status, viewing logs, creating resources, deploying Helm charts — without switching contexts between a terminal and an AI assistant. The Kubernetes MCP Server bridges this gap by exposing Kubernetes API operations as MCP tools that AI assistants can call.
This is particularly useful for:
- Daily cluster巡检 (health checks, resource monitoring)
- Quick resource creation from natural language descriptions
- Log viewing and troubleshooting
- Helm chart deployment and management
- Reducing context switching during incident response
It is not suitable for high-throughput automation, low-latency operations, or environments requiring strict audit trails.
Installation and Quick Start
Prerequisites
- Node.js 18 or later
- A working Kubernetes cluster with
kubectlconfigured - Claude Desktop (or any MCP-compatible client)
Install the Server
BASHnpm install -g mcp-server-kubernetes
This installs the server globally. Verify with:
BASHwhich mcp-server-kubernetes
Configure Claude Desktop
Add the following to your Claude Desktop configuration file (claude_desktop_config.json):
JSON{ "mcpServers": { "kubernetes": { "command": "npx", "args": [ "mcp-server-kubernetes" ], "env": { "KUBECONFIG": "/path/to/.kube/config", "ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS": "false" } } } }
Replace /path/to/.kube/config with the actual path to your kubeconfig file. Set ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS to "true" for read-only mode.
Test the Connection
After restarting Claude Desktop, ask: "List all pods in the default namespace." If you see pod information, the setup is working.
Parameters and Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
KUBECONFIG | No | ~/.kube/config | Path to the kubeconfig file for cluster authentication |
ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS | No | false | When true, restricts operations to read-only and create-only actions. Recommended for production environments |
The ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS environment variable is the primary safety mechanism. When enabled, the server will reject destructive operations like delete, scale down, or rollback. However, note that "create" operations are still permitted — so a user could still create a pod that consumes cluster resources.
Common Errors and Fixes
Error: connect ECONNREFUSED 127.0.0.1:6443
Cause: The Kubernetes API server is not running, or the kubeconfig points to an incorrect address.
Solution:
- Verify the API server is accessible:
kubectl get nodes - Check your kubeconfig:
kubectl config view --minify - Ensure the
KUBECONFIGenvironment variable in your MCP configuration points to the correct file
Error: Forbidden: user cannot list pods in namespace default
Cause: The kubeconfig user lacks sufficient RBAC permissions.
Solution:
- Check current permissions:
kubectl auth can-i list pods --namespace default - Switch to a context with appropriate permissions:
kubectl config use-context <context-name> - Create a dedicated ServiceAccount with a read-only ClusterRole for production use
Error: Helm release already exists
Cause: A Helm release with the same name already exists in the target namespace.
Solution:
- List existing releases:
helm list -n <namespace> - Use a different release name in your request
- Uninstall the existing release first if you want to replace it
Error: timeout waiting for condition
Cause: An operation (typically resource creation or deletion) timed out, often due to insufficient cluster resources.
Solution:
- Check for pending pods:
kubectl get pods --all-namespaces | grep Pending - Verify cluster resource availability:
kubectl describe nodes - Increase the MCP server timeout by setting
MCP_TIMEOUTenvironment variable (if supported by your client)
Production Notes and Security Checks
Running the Kubernetes MCP Server in production requires careful security considerations:
Critical Limitations
| Limitation | Impact | Mitigation |
|---|---|---|
| Uses current user's kubeconfig | Inherits all permissions of the configured user | Create a dedicated, minimal-permission ServiceAccount |
| Non-destructive mode is not comprehensive | Still allows resource creation and some mutations | Combine with RBAC restrictions |
| No built-in audit logging | Operations are not traceable | Enable Kubernetes API audit logging separately |
| Single cluster per instance | Cannot switch clusters dynamically | Run multiple MCP Server instances with different kubeconfigs |
| No concurrency protection | Concurrent requests may cause kubeconfig conflicts | Use a single-threaded client or queue requests |
| No TLS enforcement | Traffic between MCP Server and API server may be unencrypted | Ensure the kubeconfig uses HTTPS endpoints |
Recommended Production Configuration
JSON{ "mcpServers": { "kubernetes": { "command": "npx", "args": [ "mcp-server-kubernetes" ], "env": { "KUBECONFIG": "/path/to/readonly-kubeconfig", "ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS": "true" } } } }
Create a dedicated ServiceAccount with a read-only ClusterRole:
BASHkubectl create serviceaccount mcp-readonly -n mcp-system kubectl create clusterrolebinding mcp-readonly-binding \ --clusterrole=view \ --serviceaccount=mcp-system:mcp-readonly
Export the ServiceAccount's kubeconfig and use that file as the KUBECONFIG value.
FAQ
Q: How do I ensure read-only access to a production cluster?
A: Set ALLOW_ONLY_NON_DESTRUCTIVE_TOOLS=true and use a kubeconfig that only has get, list, and watch permissions. Create a dedicated ServiceAccount bound to the view ClusterRole, and export its kubeconfig for use with the MCP server.
Q: Can I connect to multiple Kubernetes clusters simultaneously?
A: Each MCP Server instance uses a single kubeconfig context. To work with multiple clusters, start separate MCP Server instances with different KUBECONFIG environment variables, and register each as a separate MCP server in your Claude Desktop configuration.
Q: How do I audit operations performed through the MCP server?
A: The MCP Server itself does not provide audit logging. Enable Kubernetes API audit logging on your cluster using an audit policy. Alternatively, wrap the MCP Server process with a logging proxy that records all API calls. For critical environments, consider using a separate monitoring tool to capture all kubectl-equivalent operations.