list_vms
Lists virtual machines on a standalone ESXi host or groups them by host within a vCenter datacenter.
Instructions
List VMs on a target.
Standalone ESXi: lists all VMs on the host.
vCenter: groups VMs by host within the specified datacenter (defaults to the target's configured datacenter).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target | No | ||
| datacenter | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/vcenter_mcp/tools/vm_list.py:8-25 (handler)Main handler function for the 'list_vms' tool. Accepts optional 'target' and 'datacenter' parameters. Loads config, resolves the target, connects to vCenter/ESXi, and delegates to _list_esxi or _list_vcenter depending on target type.
def list_vms(target: str | None = None, datacenter: str | None = None) -> str: """ List VMs on a target. - Standalone ESXi: lists all VMs on the host. - vCenter: groups VMs by host within the specified datacenter (defaults to the target's configured datacenter). """ try: cfg = load_config() target_cfg = resolve_target(cfg, target) with vcenter_connection(target_cfg) as si: content = si.RetrieveContent() if target_cfg["type"] == "esxi": return _list_esxi(content) dc_name = datacenter or target_cfg.get("datacenter") return _list_vcenter(content, dc_name) except Exception as e: return f"Error: {e}" - src/vcenter_mcp/tools/vm_list.py:28-38 (handler)Helper handler for listing VMs on a standalone ESXi host. Queries all VirtualMachine objects via ContainerView and formats name, managed object ID, power state, and guest ID.
def _list_esxi(content) -> str: container = content.viewManager.CreateContainerView( content.rootFolder, [vim.VirtualMachine], True ) lines = ["VMs:"] for vm in container.view: state = vm.runtime.powerState guest = vm.config.guestId if vm.config else "unknown" lines.append(f" {vm.name} ({vm._moId}) — {state} — {guest}") container.Destroy() return "\n".join(lines) if len(lines) > 1 else "No VMs found" - src/vcenter_mcp/tools/vm_list.py:41-57 (handler)Helper handler for listing VMs in a vCenter datacenter. Groups VMs by host and formats name, managed object ID, power state, and guest ID for each VM.
def _list_vcenter(content, dc_name: str) -> str: dc = get_obj(content, [vim.Datacenter], dc_name) if not dc: return f"Error: Datacenter '{dc_name}' not found" host_view = content.viewManager.CreateContainerView( dc, [vim.HostSystem], True ) lines = [] for host in host_view.view: lines.append(f"Host: {host.name}") for vm in host.vm: state = vm.runtime.powerState guest = vm.config.guestId if vm.config else "unknown" lines.append(f" {vm.name} ({vm._moId}) — {state} — {guest}") host_view.Destroy() return "\n".join(lines) if lines else "No VMs found" - src/vcenter_mcp/tools/vm_list.py:6-7 (registration)Registration function that registers the 'list_vms' tool with the MCP server via the @mcp.tool() decorator.
def register_list_tools(mcp) -> None: @mcp.tool() - src/vcenter_mcp/server.py:13-21 (registration)Import and invocation of register_list_tools in the main server module, which wires up the tool at server startup.
from vcenter_mcp.tools.vm_list import register_list_tools from vcenter_mcp.tools.vm_create import register_create_tools from vcenter_mcp.tools.vm_power import register_power_tools from vcenter_mcp.tools.vm_delete import register_delete_tools register_list_tools(mcp) register_create_tools(mcp) register_power_tools(mcp) register_delete_tools(mcp)