KVM MCP Server
A powerful JSON-RPC server for managing KVM virtual machines through a simple and intuitive interface. This server provides a centralized way to control and monitor your KVM virtual machines using a standardized protocol.
Why This Project?
Managing KVM virtual machines typically requires using multiple command-line tools like virsh
, virt-install
, and qemu-system
. This project aims to:
Simplify VM Management: Provide a single, unified interface for all VM operations
Enable Remote Control: Allow remote management of VMs through JSON-RPC
Automate VM Operations: Make it easy to script and automate VM management tasks
Standardize VM Configuration: Ensure consistent VM setup across your infrastructure
Optimize Performance: Implement efficient resource management and caching strategies
Features
VM Lifecycle Management:
Create new VMs with customizable parameters
Start/stop/reboot VMs
List all available VMs with their status
Automatic state tracking and recovery
Network Management:
Configure VM networking using bridges
Support for the
brforvms
bridgeAutomatic network interface configuration
IP address tracking and management
Storage Management:
Configurable VM disk storage location
Support for various disk formats (qcow2)
Configurable disk sizes
Automatic disk cleanup and management
Display Management:
VNC support for graphical access
Automatic VNC port assignment
Tools to find and connect to VM displays
Display state tracking and recovery
Installation Support:
Network installation from ISO images
Local installation from CDROM
Support for various OS variants
Automated installation configuration
Performance Optimizations:
Connection pooling for libvirt to reduce connection overhead
VM information caching for improved responsiveness
Asynchronous processing for better concurrency
Advanced logging for diagnostics and troubleshooting
Graceful shutdown handling for proper resource cleanup
Automatic connection recovery and validation
Rate limiting for API operations
Performance metrics collection
Performance Benefits
Connection Pooling
Reduced Latency: Eliminates the overhead of repeatedly opening and closing libvirt connections
Resource Efficiency: Maintains a pool of reusable connections, reducing system resource usage
Automatic Recovery: Detects and replaces dead connections automatically
Configurable Pool Size: Adjust the number of connections based on your workload
Caching
Faster Response Times: Reduces repeated queries to libvirt for common operations
Configurable TTL: Set cache expiration based on your needs
Selective Bypass: Option to bypass cache for operations requiring fresh data
Automatic Invalidation: Cache is automatically invalidated when VM states change
Asynchronous Processing
Improved Concurrency: Handle multiple requests simultaneously
Better Resource Utilization: Efficient use of system resources
Non-blocking Operations: Long-running operations don't block the server
Graceful Shutdown: Proper cleanup of resources during shutdown
Monitoring and Diagnostics
Structured Logging: Easy-to-parse log format for analysis
Performance Metrics: Track operation timing and resource usage
Error Tracking: Detailed error logging for troubleshooting
Resource Monitoring: Track connection pool usage and cache effectiveness
Configuration
The server uses a JSON configuration file (config.json
) to store default values and paths. This makes the server more portable and easier to customize. The configuration includes:
You can modify these values to match your environment's requirements. The configuration supports environment variable overrides using the following format:
VM_DISK_PATH
fordisk_path
VM_DEFAULT_ISO
fordefault_iso
VM_DEFAULT_MASTER_IMAGE
fordefault_master_image
VM_DEFAULT_NAME
fordefault_name
VM_DEFAULT_MEMORY
fordefault_memory
VM_DEFAULT_VCPUS
fordefault_vcpus
VM_DEFAULT_DISK_SIZE
fordefault_disk_size
VM_DEFAULT_OS_VARIANT
fordefault_os_variant
VM_DEFAULT_NETWORK
fordefault_network
VM_IGNITION_DEFAULT_HOSTNAME
forignition.default_hostname
VM_IGNITION_DEFAULT_USER
forignition.default_user
VM_IGNITION_DEFAULT_SSH_KEY
forignition.default_ssh_key
VM_IGNITION_DEFAULT_TIMEZONE
forignition.default_timezone
VM_IGNITION_DEFAULT_LOCALE
forignition.default_locale
VM_IGNITION_DEFAULT_PASSWORD_HASH
forignition.default_password_hash
Performance Tuning
Connection Pool Configuration
Cache Configuration
Logging Configuration
Getting Started
Prerequisites
Python 3.6 or higher
KVM and libvirt installed on the host system
The network bridge configured (default:
brforvms
)VM storage directory created (default:
/vm/
)Sufficient system resources for your VM workload
Installation
Clone this repository:
git clone https://github.com/yourusername/kvm-mcp.git cd kvm-mcpCreate and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activateInstall dependencies:
pip install -r requirements.txtConfigure the server:
Edit
config.json
to match your environmentEnsure all required directories exist
Verify network bridge configuration
Adjust performance settings as needed
Usage
Start the server:
python3 kvm_mcp_server.pySend commands using JSON-RPC. Example scripts are provided:
create_vm.sh
: Create a new VM using default configurationget_vnc_ports.sh
: Find VNC ports for running VMs
Example Commands
Create a New VM
This will create a new VM using the default configuration from config.json
. You can override any of these defaults by providing them in the request.
Find VNC Ports
This will show all running VMs and their VNC ports, making it easy to connect to their displays.
List VMs with Cache Bypass
Monitoring and Troubleshooting
Log Files
kvm_mcp.log
: Current log filekvm_mcp.log.1
: Previous log file (rotated)Logs include timing information, connection pool status, and cache hits/misses
Performance Metrics
Connection pool usage statistics
Cache hit/miss ratios
Operation timing metrics
Resource utilization statistics
Common Issues and Solutions
Connection Pool Exhaustion
Symptom: Slow response times or connection errors
Solution: Increase
max_connections
in the connection pool configuration
Cache Invalidation Issues
Symptom: Stale VM information
Solution: Use
no_cache
parameter or reduce cache TTL
Resource Cleanup
Symptom: Resource leaks or connection issues
Solution: Ensure proper shutdown using SIGTERM or SIGINT
Project Structure
kvm_mcp_server.py
: Main server implementationconfig.json
: Configuration filerequirements.txt
: Python dependenciesExample scripts in the root directory
Test suite in the
tests/
directory
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
A JSON-RPC server that simplifies managing KVM virtual machines by providing a centralized interface for VM lifecycle, networking, storage, and display management tasks.
Related MCP Servers
- -securityAlicense-qualityA beginner-friendly MCP-inspired JSON-RPC server built with Node.js, offering basic client-server interaction through an 'initialize' capabilities handshake and an 'echo' function.Last updated -3MIT License
- -securityAlicense-qualityA VMware ESXi/vCenter management server based on MCP (Machine Control Protocol), providing simple REST API interfaces for virtual machine management.Last updated -36MIT License
- -securityFlicense-qualityA simple MCP server that allows accessing and executing shell commands on a VM machine through a web-based terminal interface, with automatic tunneling to make the VM accessible from anywhere.Last updated -1834
- -securityAlicense-qualityAn enhanced Python-based MCP server that enables complete VM lifecycle management and monitoring of Proxmox virtualization platforms through natural language, with 11 REST API endpoints for seamless integration.Last updated -12MIT License