Skip to main content

Python 3.12+ Tests

MCP Kubernetes Server

A Kubernetes management MCP (Model Context Protocol) server that provides interfaces for getting information about Kubernetes clusters, performing operations, monitoring status, and analyzing resources.

Features

  • Cluster Information: Get detailed information about Kubernetes resources (pods, deployments, services, etc.)
  • Cluster Operations: Perform operations on Kubernetes resources (create, update, delete, scale, etc.)
  • Monitoring: Monitor the status of Kubernetes clusters and resources
  • Analysis: Analyze Kubernetes resources and provide recommendations
  • Prompts: Includes prompts for common Kubernetes analysis tasks

Examples

Ex.1

ex1

Ex.2

ex2

Ex.3

ex3

Installation

From Source

git clone https://github.com/yourusername/mcp-k8s-server.git
cd mcp-k8s-server
pip install -e .

Using pip

pip install mcp-k8s-server

Usage

Running Directly

# Run with default settings
mcp-k8s-server

# Specify transport type
mcp-k8s-server --transport sse

# Specify port for SSE transport
mcp-k8s-server --port 8000

# Specify config file
mcp-k8s-server --config /path/to/config.yaml

Using Docker

The Dockerfile uses the command and args pattern to run the server:

CMD ["python", "-m", "mcp_k8s_server.main", \
     "--transport", "sse", \
     "--port", "8000", \
     "--host", "0.0.0.0", \
     "--config", "/etc/rancher/rke2/rke2.yaml", \
     "--debug"]

To build and run the Docker container:

# Build the Docker image
docker build -t mcp-k8s-server .

# Run the Docker container
docker run -p 8000:8000 -v ~/.kube:/home/mcp/.kube mcp-k8s-server

Alternatively, you can use the provided script:

# Make the script executable
chmod +x docker-run.sh

# Run the script
./docker-run.sh

This script builds the Docker image and runs the container with the necessary volume mounts.

Deploying to Kubernetes

# Apply the Kubernetes manifests
kubectl apply -f k8s/

When deploying to Kubernetes, the server will automatically use the in-cluster configuration. The Kubernetes manifests in the k8s/ directory are set up to:

  1. Create a ServiceAccount with appropriate permissions
  2. Mount the service account token and certificate
  3. Configure the server with command-line arguments

You can customize the deployment by editing the manifests:

# k8s/deployment.yaml (example)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcp-k8s-server
  # ...
spec:
  # ...
  template:
    # ...
    spec:
      serviceAccountName: mcp-k8s-server  # Uses the ServiceAccount defined in rbac.yaml
      containers:
      - name: mcp-k8s-server
        # ...
        command:
        - python
        - -m
        - mcp_k8s_server.main
        args:
        - --transport
        - sse
        - --port
        - "8000"
        - --host
        - "0.0.0.0"
        - --config
        - "/app/config/config.yaml"
        - --debug

This approach uses the command and args pattern to configure the server, which is a common pattern in Kubernetes. The command specifies the executable to run, and the args specify the command-line arguments to pass to the executable.

The KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables are automatically set by Kubernetes when the pod is created, so you don't need to specify them in your deployment manifest.

Configuration

The server can be configured using a YAML configuration file, environment variables, or command-line arguments.

In-Cluster Configuration

When running inside a Kubernetes cluster, the server automatically uses the in-cluster configuration. This relies on the following:

  1. Environment variables:

    • KUBERNETES_SERVICE_HOST: Set by Kubernetes to the IP address of the Kubernetes API server
    • KUBERNETES_SERVICE_PORT: Set by Kubernetes to the port of the Kubernetes API server
  2. Service account token and certificate:

    • /var/run/secrets/kubernetes.io/serviceaccount/token: Service account token
    • /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: CA certificate

These are automatically set by Kubernetes when running in a pod. If you're running the server outside a Kubernetes cluster but want to test the in-cluster configuration, you would need to manually set these environment variables and create the token and certificate files.

When running outside a cluster, the server falls back to using the kubeconfig file.

Testing In-Cluster Configuration Locally

If you want to test the in-cluster configuration locally (outside a Kubernetes cluster), you can manually set up the required environment variables and files:

  1. Set the environment variables:

    export KUBERNETES_SERVICE_HOST=<kubernetes-api-server-ip>
    export KUBERNETES_SERVICE_PORT=<kubernetes-api-server-port>
    

    You can get these values by running:

    kubectl cluster-info
    
  2. Create the service account token and certificate directories:

    mkdir -p /var/run/secrets/kubernetes.io/serviceaccount/
    
  3. Copy your Kubernetes certificate and create a token:

    # Copy the CA certificate
    kubectl config view --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 -d > /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    
    # Create a token file (you can use a service account token or generate a temporary one)
    echo "your-service-account-token" > /var/run/secrets/kubernetes.io/serviceaccount/token
    

Note that this approach requires root privileges to create files in /var/run/secrets/. Alternatively, you can modify the code to use different paths for testing purposes.

Configuration File

# config.yaml
server:
  name: mcp-k8s-server
  transport: both  # stdio, sse, or both
  port: 8000
  host: 0.0.0.0

kubernetes:
  config_path: ~/.kube/config
  context: default
  namespace: default

MCP Resources

The server provides access to Kubernetes resources through the Model Context Protocol (MCP). Resources are identified by URI patterns following the k8s:// protocol.

Resource URI Patterns

Resources are organized in a hierarchical structure:

  • k8s://resources - List of all available Kubernetes resources
  • k8s://namespaces - List of all Kubernetes namespaces
  • k8s:///{namespace} - Overview of all resources in a namespace

Namespaced Resources

  • k8s://{namespace}/{resource_type} - List resources of a specific type in a namespace

    • Example: k8s://default/pods - All pods in the 'default' namespace
  • k8s://{namespace}/{resource_type}/{name} - Get a specific namespaced resource

    • Example: k8s://default/deployments/nginx - The 'nginx' deployment in the 'default' namespace

Supported resource types:

  • pods
  • deployments
  • services
  • persistentvolumeclaims
  • events

Cluster-Scoped Resources

  • k8s:///{resource_type} - List cluster-scoped resources of a specific type

    • Example: k8s:///nodes - All nodes in the cluster
  • k8s:///{resource_type}/{name} - Get a specific cluster-scoped resource

    • Example: k8s:///nodes/worker-1 - The 'worker-1' node

Supported resource types:

  • nodes
  • persistentvolumes
  • namespaces

MCP Tools

The server provides the following MCP tools:

Resource Information

  • get_resources: Get a list of resources of a specific type
  • get_resource: Get detailed information about a specific resource
  • get_resource_status: Get the status of a specific resource
  • get_resource_events: Get events related to a specific resource
  • get_resource_logs: Get logs for a specific resource

Resource Operations

  • create_resource: Create a new resource
  • update_resource: Update an existing resource
  • delete_resource: Delete a resource
  • scale_deployment: Scale a deployment
  • restart_deployment: Restart a deployment
  • execute_command: Execute a command in a pod

Monitoring

  • get_cluster_status: Get the overall status of the cluster
  • get_node_status: Get the status of cluster nodes
  • get_resource_metrics: Get metrics for a specific resource
  • get_cluster_metrics: Get metrics for the entire cluster
  • check_cluster_health: Perform a comprehensive health check of the cluster and get a detailed summary

License

MIT

Metadata

Release files for mcp-k8s-server 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mcp-k8s-server 0.1.2
File Size Uploaded Provenance
mcp_k8s_server-0.1.2.tar.gz 35.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mcp-k8s-server 0.1.2
File Interpreter ABI Platform
mcp_k8s_server-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 70.8 kB

Release files / mcp_k8s_server-0.1.2.tar.gz

Download URL mcp_k8s_server-0.1.2.tar.gz
Size 35.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1dde19d385ce9a6fc9ac73a6c74d233cf7a0d38229b983fe6f999506825f807a
BLAKE2b-256 checksum
How to use checksums
cd8bacafe11716f10169cb53374c27966d9d6af4c2c53a5608eb0bef4f5ee81e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / mcp_k8s_server-0.1.2-py3-none-any.whl

Download URL mcp_k8s_server-0.1.2-py3-none-any.whl
Size 35.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f5407f2dafaf8fb04f06b5812f5f3ea22fa004991d5f88f98602610f860ac8b
BLAKE2b-256 checksum
How to use checksums
9d6451814b3a7e510b4949bdfbac58efad3bf1ed7d7c02404ec71d7ad6ac7317
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for mcp-k8s-server 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page