Built my first MCP Server for Kubernetes

first-mcp

Intro

Built my first MCP Server for read only Kubernetes operations.

This MCP server was created to provide a secure way to interact with Kubernetes clusters without allowing any create, update, or delete operations. It only exposes read-only APIs to ensure your clusters remain safe while still enabling AI assistants to help you monitor and troubleshoot your Kubernetes resources.

Model Context Protocol


Model Context Protocol (MCP) has been very popular lately and you can see the huge adoption as every company is releasing their official MCP server.

MCP server is the new API endpoint.

Why Yet Another MCP Server?

Kubernetes is equally popular and there are many Kubernetes MCP servers out there. Security is still evolving around MCP and I didn’t want to run an MCP server for K8s which can create, update or delete my K8s resources.

Then I got an idea to build my own MCP server where only read only operations are part of the MCP servers tools. If there is no capability written to modify then it cannot modify the resources.

Of course we could achieve it by creating a restrictive RBAC role in K8s cluster but I wanted to learn and what better way than building your own MCP server.

How I built it?

I have used FastMCP framework for building MCP server. For calling Kubernetes API server, I have used the official Kubernetes Python client. Essentially I was plumbing both those things together and building a solution around them.

Each API call is created as a separate MCP tool. For example, there is one K8s API call for get pods which has been implemented as get_pods MCP tool.

There is a common Kubernetes context initiated once at the start which is shared across the MCP tools.

Features

This MCP server provides the following read-only tools:

  • list_pods: List all pods in a namespace or across all namespaces
  • list_deployments: List all deployments in a specified namespace
  • list_services: List all services in a namespace or across all namespaces
  • list_namespaces: List all namespaces in the cluster
  • get_events: Get Kubernetes events from the cluster
  • get_pod_logs: Get logs from a specific pod
  • get_logs: Get logs from pods, deployments, jobs, or resources matching a label selector

TODO

  • Currently I have only implemented the stdio transport. MCP added streamable_http transport recently. For watch or streaming logs such functionality might be needed. I need to check it further.
  • Not all read only Kubernetes APIs are added.

PyPi package

At first, for testing purposes, I was calling the MCP server code from a single python file directly. After that added that command in the MCP config file and tested it. Then I thought it would be better if there is a single command to run the python module and after some research decided to build my own PyPi package.

I only used pip or uv to install python packages and now I got to create one. Now you can install the MCP server using “pip install kubernetes-readonly-mcp@latest”. This was a very fun and learning experience.

Final Remarks

After building, packaging and pushing it to GitHub, I started testing it using VSCode + Cline and installed the MCP server and chat with it.

This project is still in experimental phase. Use it at your own discretion.

Demo Video

Below is the demo of installing and usage of my first MCP server for readonly Kubernetes operations. Do let me know what is your experience with MCP?

If you are new to my posts, I regularly post about AWS, EKS, Kubernetes and Cloud computing related topics. Do follow me in LinkedIn and visit my dev.to posts. You can find all my previous blog posts in my blog