kubernetes-mcp-server

gabornyergesX/kubernetes-mcp-server

3.2

If you are the rightful owner of kubernetes-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

A Kubernetes Model Context Protocol (MCP) server that provides programmatic access to Kubernetes resources through a standardized interface.

Kubernetes MCP Server

Go Report Card Codecov Go Version

A Kubernetes Model Context Protocol (MCP) server that provides programmatic access to Kubernetes resources through a standardized interface.

Overview

This project implements a comprehensive MCP server for Kubernetes that enables applications to interact with Kubernetes clusters through a well-defined protocol. It supports a wide range of Kubernetes resources including pods, services, deployments, webhooks, and more.

Features

  • Comprehensive Resource Support: Pods, Services, Ingresses, IngressClasses, Deployments, DaemonSets, StatefulSets, ReplicaSets, Jobs, CronJobs, ConfigMaps, Secrets, ResourceQuotas, LimitRanges, HPAs, PriorityClasses, RuntimeClasses, Leases, and Webhooks
  • Full CRUD Operations: Create, Read, Update, Delete operations for all supported resources
  • Real-time Watch: Event streaming for resource changes
  • Multiple Transport Protocols: HTTP/JSON-RPC and Server-Sent Events (SSE)
  • Comprehensive Testing: Extensive test coverage for all functionality
  • Production Ready: Proper error handling, logging, and monitoring

Test Coverage

The project maintains comprehensive test coverage across all components:

  • Overall Coverage: 25.4% (target: 80%+)
  • Core Components: High coverage on critical paths
  • Handler Coverage: All 23 resource handlers tested
  • Transport Layer: HTTP and stdio transport tested
  • Validation: Input validation and RBAC validation tested
  • Utilities: Core utilities and helper functions tested

Coverage Reports

  • HTML Report: Generated as coverage.html after running make test-coverage
  • Detailed Report: Use make test-coverage-detailed for package-by-package analysis
  • CI Integration: Coverage reports uploaded to Codecov on every PR
  • Coverage Badge: Real-time coverage status in repository badges

Coverage Goals

  • Short-term: Achieve 50% overall coverage
  • Medium-term: Achieve 70% overall coverage
  • Long-term: Achieve 80%+ overall coverage
  • Critical Paths: Maintain 90%+ coverage on core functionality

Quick Start

Prerequisites

  • Go 1.21 or later
  • Kubernetes cluster access (kubeconfig)
  • jq for JSON processing (for test scripts)

Installation

# Clone the repository
git clone <repository-url>
cd kubernetes-mcp-server

# Build the server
make build

# Run the server
./kubernetes-mcp-server -transport sse -port 8080

Usage

The server exposes the following endpoints:

  • MCP Endpoint: http://localhost:8080/mcp - Main MCP protocol endpoint
  • SSE Endpoint: http://localhost:8080/sse - Server-Sent Events endpoint
  • Health Endpoint: http://localhost:8080/health - Health check endpoint

Example API Call

# List all pods in the default namespace
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "list_pods",
      "arguments": {
        "namespace": "default"
      }
    }
  }'

Project Structure

kubernetes-mcp-server/
ā”œā”€ā”€ cmd/                           # Application entry points
│   └── kubernetes-mcp-server/     # Main application
│       └── main.go               # Application entry point
ā”œā”€ā”€ internal/                      # Private application code
│   ā”œā”€ā”€ kubernetes/               # Kubernetes client implementations
│   │   ā”œā”€ā”€ client.go            # Base client interface and implementation
│   │   ā”œā”€ā”€ pod_client.go        # Pod operations
│   │   ā”œā”€ā”€ service_client.go    # Service operations
│   │   ā”œā”€ā”€ connection_pool.go   # Connection pooling
│   │   └── ...                  # 23 focused client implementations
│   ā”œā”€ā”€ mcp/                     # MCP protocol handlers
│   │   ā”œā”€ā”€ core/               # Core MCP functionality
│   │   │   ā”œā”€ā”€ server.go      # Main server logic
│   │   │   ā”œā”€ā”€ handler.go     # Base handler interface
│   │   │   └── handler_utils.go # Common utilities
│   │   ā”œā”€ā”€ handlers/          # Resource-specific handlers
│   │   │   ā”œā”€ā”€ pod/          # Pod operations
│   │   │   ā”œā”€ā”€ service/      # Service operations
│   │   │   ā”œā”€ā”€ deployment/   # Deployment operations
│   │   │   └── ...           # 20+ other resource types
│   │   ā”œā”€ā”€ tools/            # Tool schema definitions
│   │   │   ā”œā”€ā”€ pod_tools.go  # Pod tool schemas
│   │   │   ā”œā”€ā”€ service_tools.go # Service tool schemas
│   │   │   └── ...           # 23 resource tool files
│   │   └── utils/            # Resource-specific utilities
│   │       ā”œā”€ā”€ pod_utils.go  # Pod utilities
│   │       ā”œā”€ā”€ service_utils.go # Service utilities
│   │       └── ...           # Resource utility files
│   ā”œā”€ā”€ config/               # Configuration management
│   ā”œā”€ā”€ logger/               # Structured logging
│   ā”œā”€ā”€ health/               # Health checks
│   └── transport/            # Transport layer implementations
│       ā”œā”€ā”€ http.go          # HTTP/SSE transport
│       └── stdio.go         # Stdio transport
ā”œā”€ā”€ pkg/                      # Public libraries
│   └── types/               # Shared type definitions
ā”œā”€ā”€ docs/                     # Documentation
│   ā”œā”€ā”€ README.md            # Documentation index
│   ā”œā”€ā”€ API_DOCUMENTATION.md # Complete API reference
│   ā”œā”€ā”€ PROJECT_ORGANIZATION.md # Project structure guide
│   └── ...                  # Implementation guides
ā”œā”€ā”€ tests/                    # Test files
│   └── scripts/             # Test scripts
ā”œā”€ā”€ logs/                     # Log files
ā”œā”€ā”€ go.mod                    # Go module definition
ā”œā”€ā”€ go.sum                    # Go module checksums
ā”œā”€ā”€ Makefile                  # Build and development tasks
└── README.md                 # This file

Supported Resources

Core Resources

  • Pods: Container orchestration and management
  • Services: Network service abstraction
  • Ingresses: External traffic routing and load balancing
  • IngressClasses: Ingress controller configuration and defaults
  • Namespaces: Resource isolation and organization

Workload Resources

  • Deployments: Declarative updates for Pods and ReplicaSets
  • DaemonSets: Ensures all nodes run a copy of a Pod
  • StatefulSets: Manages stateful applications
  • ReplicaSets: Maintains a stable set of replica Pods
  • Jobs: Creates one or more Pods and tracks their completion
  • CronJobs: Creates Jobs on a time-based schedule

Configuration Resources

  • ConfigMaps: Stores non-confidential configuration data
  • Secrets: Stores sensitive configuration data
  • ResourceQuotas: Provides constraints that limit resource consumption
  • LimitRanges: Provides constraints that limit resource consumption per namespace

Advanced Resources

  • HorizontalPodAutoscalers: Automatically scales the number of Pods
  • PriorityClasses: Indicates the relative importance of Pods
  • RuntimeClasses: Selects the container runtime configuration
  • Leases: Distributed locking mechanism
  • Webhooks: Admission control and validation

API Reference

For comprehensive API documentation, see .

Quick Overview

The server provides 23 resource types with comprehensive CRUD operations:

Core Resources (5)
  • Pods: Container orchestration and management
  • Services: Network service abstraction
  • Namespaces: Resource isolation and organization
  • ConfigMaps: Non-confidential configuration data
  • Secrets: Sensitive configuration data
Workload Resources (6)
  • Deployments: Declarative updates for Pods and ReplicaSets
  • DaemonSets: Ensures all nodes run a copy of a Pod
  • StatefulSets: Manages stateful applications
  • ReplicaSets: Maintains a stable set of replica Pods
  • Jobs: Creates one or more Pods and tracks their completion
  • CronJobs: Creates Jobs on a time-based schedule
Networking Resources (4)
  • Ingresses: External traffic routing and load balancing
  • IngressClasses: Ingress controller configuration and defaults
  • Endpoints: Network endpoints for services
  • NetworkPolicies: Network traffic control
Configuration Resources (3)
  • ResourceQuotas: Provides constraints that limit resource consumption
  • LimitRanges: Provides constraints that limit resource consumption per namespace
  • HorizontalPodAutoscalers: Automatically scales the number of Pods
Advanced Resources (5)
  • PriorityClasses: Indicates the relative importance of Pods
  • RuntimeClasses: Selects the container runtime configuration
  • Leases: Distributed locking mechanism
  • Webhooks: Admission control and validation
  • ReplicationControllers: Legacy replication controller

Available Operations

Each resource supports these operations:

OperationDescriptionExample Tool Name
list_*List all resources in a namespacelist_pods
get_*Get detailed information about a specific resourceget_pod
create_*Create a new resourcecreate_pod
update_*Update an existing resourceupdate_pod
patch_*Patch a resourcepatch_pod
delete_*Delete a resourcedelete_pod
watch_*Watch for resource changeswatch_pods
scale_*Scale a resource (Deployments, StatefulSets, etc.)scale_deployment
exec_*Execute commands in podsexec_pod

Quick Example

# List all pods in the default namespace
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "list_pods",
      "arguments": {
        "namespace": "default"
      }
    }
  }'

Development

Building

# Build the server
make build

# Build with specific flags
go build -o kubernetes-mcp-server ./cmd/kubernetes-mcp-server

Testing

# Run unit tests
make test

# Run specific test scripts
./tests/scripts/test_service.sh
./tests/scripts/test_comprehensive.sh

Running Tests

The project includes comprehensive test scripts for all functionality:

# Test service functionality
./tests/scripts/test_service.sh

# Test comprehensive functionality
./tests/scripts/test_comprehensive.sh

# Test specific resources
./tests/scripts/test_lease.sh
./tests/scripts/test_webhook.sh

Configuration

Environment Variables

VariableDescriptionDefault
K8S_MCP_SERVER_HOSTServer hostlocalhost
K8S_MCP_SERVER_PORTServer port8080
K8S_MCP_SERVER_TRANSPORTTransport type (http, sse, stdio)sse
K8S_MCP_SERVER_LOG_LEVELLog level (debug, info, warn, error)info
K8S_MCP_SERVER_LOG_FORMATLog format (json, text)json
K8S_MCP_SERVER_LOG_OUTPUTLog output pathstdout
K8S_MCP_SERVER_HEALTH_ENDPOINTHealth check endpoint/health
K8S_MCP_SERVER_READY_ENDPOINTReadiness probe endpoint/ready
K8S_MCP_SERVER_LIVE_ENDPOINTLiveness probe endpoint/live
KUBECONFIGPath to kubeconfig file~/.kube/config

Command Line Flags

  • -transport: Transport protocol (http, sse, or stdio, default: sse)
  • -port: Server port (default: 8080)
  • -host: Server host (default: localhost)
  • -log-level: Logging level (default: info)
  • -log-format: Log format (default: json)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

[Add your license information here]

Documentation

For comprehensive documentation, see the directory:

Core Documentation

  • - Complete API reference with examples
  • - Project structure and architecture guide

Implementation Guides

Technical Documentation