Enables management of PostgreSQL clusters through the CloudNativePG operator, providing tools for cluster creation, scaling, status monitoring, and health checking within Kubernetes environments
Manages PostgreSQL database clusters using CloudNativePG operator, offering high-level workflow tools for cluster lifecycle management, scaling operations, and monitoring cluster health and status
CloudNativePG MCP Server
An MCP (Model Context Protocol) server for managing PostgreSQL clusters using the CloudNativePG operator in Kubernetes.
Overview
This MCP server enables LLMs to interact with PostgreSQL clusters managed by the CloudNativePG operator. It provides high-level workflow tools for:
š Listing and discovering PostgreSQL clusters
š Getting detailed cluster status and health information
š Creating new PostgreSQL clusters with best practices
š Scaling clusters up or down
š Managing backups and restores (TODO)
š Monitoring cluster health and logs (TODO)
Prerequisites
Kubernetes Cluster with CloudNativePG operator installed:
kubectl apply -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.22/releases/cnpg-1.22.0.yamlPython 3.9+ installed
kubectl configured to access your cluster
Appropriate RBAC permissions for the service account (see RBAC Setup below)
Installation
Clone or download this repository
Install Python dependencies:
pip install -r requirements.txtVerify Kubernetes connectivity:
kubectl get nodes
RBAC Setup
The MCP server needs permissions to interact with CloudNativePG resources. The CloudNativePG helm chart automatically creates ClusterRoles (cnpg-cloudnative-pg-edit, cnpg-cloudnative-pg-view), so you only need to create a ServiceAccount and bind it to these existing roles:
This creates:
A
cnpg-mcp-serverServiceAccountClusterRoleBinding to
cnpg-cloudnative-pg-edit(for managing clusters)ClusterRoleBinding to
view(for reading pods, events, logs)
Verify the setup:
For read-only access: Change cnpg-cloudnative-pg-edit to cnpg-cloudnative-pg-view in rbac.yaml
Configuration
Transport Modes
The server supports two transport modes (currently only stdio is implemented):
1. stdio Transport (Default)
Communication over stdin/stdout. Best for local development and Claude Desktop integration.
Characteristics:
ā Simple setup, no network configuration
ā Automatic process management
ā Secure (no network exposure)
ā Single client per server instance
ā Client and server must be on same machine
Use cases: Claude Desktop, local CLI tools, personal development
2. HTTP/SSE Transport (Future)
HTTP server with Server-Sent Events for remote access. Best for team environments and production deployments.
When implemented, will provide:
ā Multiple clients can connect
ā Remote access capability
ā Independent server lifecycle
ā Better for team/production use
ā ļø Requires authentication/TLS setup
Use cases: Team-shared server, production deployments, Kubernetes services
The codebase is structured to easily add HTTP transport when needed. See the run_http_transport() function for implementation guidelines.
Kubernetes Configuration
The server uses your kubeconfig for authentication:
Local development: Uses
~/.kube/configIn-cluster: Automatically uses service account tokens
You can also set the KUBECONFIG environment variable:
Running the Server
Command-Line Options
Standalone Mode (for testing)
Note: The server runs as a long-running process waiting for MCP requests. In stdio mode, it won't exit until interrupted. This is expected behavior.
With Claude Desktop
Add to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
With Docker/Kubernetes Deployment
For production deployments, you can containerize the server:
Deploy as a Kubernetes service that can be accessed by your LLM application.
Available Tools
1. list_postgres_clusters
List all PostgreSQL clusters in the Kubernetes cluster.
Parameters:
namespace(optional): Filter by namespace, or omit for all namespacesdetail_level: "concise" (default) or "detailed"
Example:
2. get_cluster_status
Get detailed status for a specific cluster.
Parameters:
namespace(required): Namespace of the clustername(required): Name of the clusterdetail_level: "concise" (default) or "detailed"
Example:
3. create_postgres_cluster
Create a new PostgreSQL cluster with high availability.
Parameters:
namespace(required): Target namespacename(required): Cluster nameinstances(default: 3): Number of PostgreSQL instancesstorage_size(default: "10Gi"): Storage per instancepostgres_version(default: "16"): PostgreSQL versionstorage_class(optional): Kubernetes storage class
Example:
4. scale_postgres_cluster
Scale a cluster by changing the number of instances.
Parameters:
namespace(required): Namespace of the clustername(required): Cluster nameinstances(required): New number of instances (1-10)
Example:
Architecture
Design Principles
This MCP server follows agent-centric design principles:
Workflow-based tools: Each tool completes a meaningful workflow, not just a single API call
Optimized for context: Responses are concise by default, with detailed mode available
Actionable errors: Error messages suggest next steps
Natural naming: Tool names reflect user intent, not just API endpoints
Transport Layer Architecture
The server is designed with transport-agnostic core logic, making it easy to add new transport modes without rewriting tool implementations:
Why this matters:
All tool functions (decorated with
@mcp.tool()) work with any transportAdding HTTP transport only requires implementing
run_http_transport()No changes needed to business logic when switching transports
Can run both transports simultaneously if needed
To add HTTP/SSE transport later:
Uncomment HTTP dependencies in
requirements.txtInstall:
pip install mcp[sse] starlette uvicornImplement the
run_http_transport()function (skeleton already provided)Add authentication/authorization middleware
Configure TLS for production
Components
Kubernetes Client: Uses
kubernetesPython client for API accessCloudNativePG CRDs: Interacts with Custom Resource Definitions
Async operations: All I/O is async for better performance
Error handling: Comprehensive error formatting with suggestions
Development
Adding New Tools
To add a new tool:
Create a Pydantic model for input validation
Implement the tool function with
@mcp.tool()decoratorAdd comprehensive docstring following the format in existing tools
Implement error handling with actionable messages
Test thoroughly
Example skeleton:
Testing
Run syntax check:
Test with a real Kubernetes cluster:
TODO: Upcoming Features
Delete cluster tool
Backup management (list, create, restore)
Log retrieval from pods
SQL query execution (with safety guardrails)
Database and user management
Connection information retrieval
Monitoring and metrics integration
Certificate and secret management
Troubleshooting
"Permission denied" errors
Ensure your service account has the necessary RBAC permissions. Check:
"Connection refused" or "Cluster unreachable"
Verify kubectl connectivity:
"No module named 'mcp'"
Install dependencies:
Server hangs
This is expected behavior - the server waits for MCP requests over stdio. Run in background or use process manager.
Security Considerations
RBAC: Apply principle of least privilege - only grant necessary permissions
Secrets: Never log or expose database credentials
Input validation: All inputs are validated with Pydantic models
Namespace isolation: Consider restricting to specific namespaces
Audit logging: Enable Kubernetes audit logs for compliance
Resources
License
[Your License Here]
Contributing
Contributions are welcome! Please:
Follow the existing code style
Add comprehensive docstrings
Include error handling
Test with real Kubernetes clusters
Update README with new features
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Enables LLMs to manage PostgreSQL clusters in Kubernetes using the CloudNativePG operator. Supports creating, scaling, monitoring clusters and retrieving detailed status information through natural language commands.