How to Configure Mastra Kubernetes MCP Server

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mastra kubernetes mcp server by linking your agent to a cluster role. The server reads your cluster file and runs commands for the AI agent. Most teams start by giving the agent a broad admin role to test fast. This wide role lets the agent read secret keys or delete live pods across namespaces. A safe setup needs tight role rules before the agent runs a single command. How a mastra kubernetes mcp server works with cluster roles The agent framework uses the Model Context Protocol

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up a mastra kubernetes mcp server by linking your agent to a cluster role. The server reads your cluster file and runs commands for the AI agent. Most teams start by giving the agent a broad admin role to test fast. This wide role lets the agent read secret keys or delete live pods across namespaces. A safe setup needs tight role rules before the agent runs a single command.

How a mastra kubernetes mcp server works with cluster roles

The agent framework uses the Model Context Protocol to talk to external tools. The MCP server acts as a bridge between the Mastra agent and the Kubernetes API. When the agent needs cluster data, it sends a tool request to the MCP server. The server then calls the Kubernetes API using a local kubeconfig or service account.

Kubernetes controls these calls through Role-Based Access Control on the API server. In Kubernetes RBAC, a Role sets permissions inside one single namespace. A ClusterRole sets permissions across the whole cluster and covers cluster-scoped items like nodes. Permissions in Kubernetes are purely additive, which means there are no deny rules. You grant permissions by linking a role to an account with a RoleBinding or ClusterRoleBinding.

To keep the mastra kubernetes mcp server safe, you should start with a basic Role. You bind that Role to a dedicated service account inside one target namespace. If the agent needs to check pod logs, you must grant access to the log subresource. Kubernetes RBAC names subresources with a slash, such as pods and log or pods and exec. If you skip subresource rules, the agent can list pods but cannot read logs.

What native RBAC solves and what it leaves open

Native Kubernetes RBAC is great at controlling which API endpoints a service account can reach. A tight Role stops an agent from touching other namespaces or editing cluster nodes. It ensures the MCP server only calls the resource types you allow in your rules.

Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Still, RBAC only checks who logs in and which API verb they use. It does not inspect what happens inside an active command or log stream. For example, granting the exec subresource lets the agent run commands inside a container. Kubernetes RBAC cannot block dangerous shell commands once that exec permission is granted.

Also, pod logs and cluster objects often hold sensitive user data. When the agent reads pod logs, the raw text flows back to the language model. Native RBAC cannot rewrite emails, phone numbers, card numbers, or national IDs in that output. Teams also struggle to link shared service account actions back to a specific user prompt. You need runtime controls to check individual commands and mask sensitive fields in flight.

Govern kubectl and API calls in flight

Identity providers and Kubernetes RBAC decide who logs into a cluster. They cannot redact keys inside returned JSON objects or hold a risky delete call for review. By design, hoop.dev runs as an open-source proxy, a sidecar for your Kubernetes API, checking requests and responses without replacing RBAC.

First, hoop.dev guardrails refuse banned methods, paths, or headers before they run. Next, hoop.dev data masking rewrites sensitive keys in returned Secret, ConfigMap, and log responses in memory, and writes each decision to the audit trail. Explore the repo on GitHub.

Frequently asked questions

Should I use a Role or a ClusterRole for the MCP server?

Use a Role with a RoleBinding whenever the agent works in one namespace. Use a ClusterRole only when the agent must inspect nodes or check all namespaces.

How do I let the Mastra agent read pod logs without full pod control?

Create a Role that grants get and list verbs on pods and the log subresource. Leave out the exec and delete verbs so the agent cannot alter running containers.

Why are deny rules missing when I set up Kubernetes RBAC for agents?

Kubernetes RBAC rules are purely additive by design and do not support deny rules. You must start with zero permissions and add only the specific resources your agent needs.

The query runs. The emails stay hidden.