Giving Claude or Cursor access to your clusters, with an approval step in the middle
The quickest way to let an AI agent work on a cluster is to give it a shell and let it run kubectl. It works well until you look at what you have actually granted: the agent now acts with your kubeconfig, which is usually your most powerful credential, through a free-form command line that nothing checks before it runs.
The Model Context Protocol offers a better shape for this. Instead of a shell, the agent gets a list of named tools with typed arguments, and the application serving them can decide what each one is allowed to do and when a human has to say yes.
The setup
In Kubexer for Kubernetes, open Settings → AI Agents and switch on Allow AI agents to use Kubexer. The MCP server listens on 127.0.0.1 only, on port 47822 by default, and every request needs a bearer token. The page gives you a ready claude mcp add --transport http ... command for Claude Code and a JSON snippet for other clients such as Claude Desktop or Cursor. Regenerating the token disconnects every agent that used the old one.
The agent works with the clusters you have already added in Kubexer, and every tool takes a cluster argument (an id or kubeconfig context name from list_clusters). A cluster that is not connected yet is connected on first use.
What an agent can read
Most of what an agent wants is diagnosis, and that is the larger half of the toolset. The same read tools that power Kubexy, the in-app assistant, are exposed:
- Browsing: list namespaces, list resources, get one resource's full YAML, pod logs (including previous container logs), and events.
- Health and usage: namespace and cluster health, top pods, top nodes, node allocation, quota status, pod resources, top restarting pods.
- Problem finding: diagnose a pod, find failing pods, explain why pods are unschedulable, find image pull errors, list warning events.
- Networking and storage: service endpoints, ingress status, network policies, PVC status, and pods without resource limits.
- Cluster info: Kubernetes version, whether it is Kubernetes or OpenShift, and detected add-ons such as Argo CD and Flux.
Four more tools sit behind Kubexer Pro features and follow your plan: search_resources (Global Search across all kinds), rbac_who_can (the Security Matrix: who is granted a verb on a resource, through which binding and role), list_cluster_alerts (your Cluster Alerts rules and recent firings) and list_image_scans with get_image_scan (Security Scanner results). On a plan without them, the agent gets a clear message saying so, which is exactly what the app would show you.
What it can change
The write tools are deliberately specific rather than "run any kubectl":
- restart a workload, scale a workload, suspend or resume (CronJobs and similar), trigger a CronJob;
- cordon and uncordon a node;
- apply edited YAML to a resource, and delete a resource;
- run one command in a pod container.
There is also a preview_resource_yaml tool, which is a dry run of apply: the API server validates the manifest and runs admission webhooks, but nothing is saved. A well-behaved agent checks an edit with it before proposing to apply it.
Three switches that decide how much it may change
Under Cluster changes there are three settings, and they are worth reading before you connect anything.
- Allow agents to change clusters. When off, the server is read-only. Every write tool is refused with a message telling the agent to describe the change for you instead.
- Ask me before each change. When on, every write waits in the app. You see what the agent wants to do and approve or decline. For YAML edits the dialog shows the resource before and after. Nothing changes until you approve, and a request you do not answer within two minutes is declined.
- Allow agents to run commands in pods. A separate switch for
exec_in_pod, on top of the first one. It runs one non-interactive command, given as an argument list, with a timeout and output capped per stream.
These are enforced inside the server, not by asking the agent politely. A refused call never reaches the cluster.
It is worth saying plainly that the defaults are permissive: changes are allowed, per-change approval is off, and exec is allowed. If you want a human in the loop, turn on Ask me before each change yourself. Exec is treated as a change, because a command can alter the container, so with approval on it asks first too; and it is refused before any prompt if the exec switch is off.
What an agent never gets
The settings page states it directly: kubeconfig credentials and Secret values are never exposed. Kubeconfigs only surface as context and cluster names. When the agent fetches a resource, Secret data is redacted, including in the YAML echoed back after an apply. The agent can know a Secret exists and what keys it has; it cannot read the values through Kubexer.
Two caveats belong next to that statement. First, exec_in_pod is a command run inside a container, so anything that container can read or print, including its own environment variables or mounted files, can come back in the output. If that matters, keep exec off. Second, the tools run with whatever the connected context is allowed to do. If your kubeconfig is cluster-admin, the agent's approved changes are cluster-admin changes. A scoped kubeconfig or a narrower context is the better credential to add for this purpose.
Keeping track of what happened
Every change an agent makes raises a notification in the app, and the AI Agents page keeps a list of recent agent activity, for example "AI agent: scale deployment api to 3 on staging". When the agent connects to a cluster that was not yet connected, that is logged too. Approvals themselves appear in a dialog that brings the Kubexer window forward, so a request is hard to miss even if the app was in the background.
A sensible way to start
- Add one non-production cluster and connect an agent to it.
- Leave changes on but switch on Ask me before each change.
- Ask the agent something diagnostic ("why is the checkout pod restarting?") and watch it use the read tools.
- Only when you have seen how it proposes changes, consider whether you ever want to turn approval off, and for which clusters.
Limits
- The server only runs while Kubexer is open.
- Approval is all-or-nothing per change. You approve what the tool describes, and a YAML diff is shown for YAML edits; other changes show a one-line summary, such as scaling a named workload to a number.
- Tool output goes to your model provider. Logs and events are text, and whatever they contain becomes part of the agent's context. Secret redaction covers Secret resources, not log lines that happen to print a token.
- Pods exec is non-interactive. No terminal and no input, so interactive programs just wait until the timeout.
In short
An MCP server turns "agent with kubectl" into "agent with a short list of tools, one read-only switch, one approval step, and a separate lock on exec". Kubexer Terminal has the same idea for SSH, with per-tab approval instead of per-change; see the Terminal post.