vm-count-by-tenant
Count virtual machines across Azure tenants and subscriptions using Azure Resource Graph queries to track VM inventory in multi-tenant environments.
Instructions
Count virtual machines per configured tenant (uses all subscriptions when available).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tenant_names | No | Optional subset of tenant names to include | |
| use_all_subscriptions | No | Try to include all subscriptions in each tenant (fallbacks to default) |
Implementation Reference
- Handler logic for the 'vm-count-by-tenant' tool within the call_tool function. Loops over tenants, scopes subscriptions or management group, executes vm_count_kql query, aggregates and formats VM counts.if name == "vm-count-by-tenant": # Optional: restrict to certain tenants wanted = set(arguments.get("tenant_names", []) or []) try_all = bool(arguments.get("use_all_subscriptions", True)) tenants = AZURE_CONFIG.get_tenants() if wanted: tenants = [t for t in tenants if t.get("name") in wanted] if not tenants: return [types.TextContent(type="text", text="No matching tenants found.")] rows: List[str] = [] total = 0 for t in tenants: tname = t.get("name") or t.get("id") cred, default_subs = AZURE_CONFIG.get_credentials(tname) mg = AZURE_CONFIG.get_management_group_id(tname) subs = list(default_subs) if try_all and mg: subs = [] # force MG usage below elif try_all: discovered = _enumerate_subscriptions_for_credential(cred) if discovered: subs = discovered if not subs: rows.append(f"- {tname}: 0 (no subscriptions)") continue kql = vm_count_kql() if try_all and mg and not subs: result = execute_kql(cred, None, kql, top=1, management_groups=[mg]) else: result = execute_kql(cred, subs, kql, top=1) if result["status"] != "success" or not result.get("results"): rows.append(f"- {tname}: error ({result.get('error','query failed')})") continue count = int(result["results"][0].get("VMCount", 0)) total += count rows.append(f"- {tname}: {count}") body = [ "VM counts by tenant:", *rows, "", f"Total VMs across tenants: {total}", ] return [types.TextContent(type="text", text="\n".join(body))]
- src/azure_assistant_mcp/server.py:153-164 (registration)Tool registration in @server.list_tools() decorator, defining name, description, and input schema for 'vm-count-by-tenant'.types.Tool( name="vm-count-by-tenant", description="Count virtual machines per configured tenant (uses all subscriptions when available).", inputSchema={ "type": "object", "properties": { "tenant_names": {"type": "array", "items": {"type": "string"}, "description": "Optional subset of tenant names to include"}, "use_all_subscriptions": {"type": "boolean", "description": "Try to include all subscriptions in each tenant (fallbacks to default)", "default": True} }, "required": [], }, ),
- Input schema definition for the 'vm-count-by-tenant' tool, specifying optional tenant_names array and use_all_subscriptions boolean.inputSchema={ "type": "object", "properties": { "tenant_names": {"type": "array", "items": {"type": "string"}, "description": "Optional subset of tenant names to include"}, "use_all_subscriptions": {"type": "boolean", "description": "Try to include all subscriptions in each tenant (fallbacks to default)", "default": True} }, "required": [],
- Helper function that loads and returns the KQL template for counting virtual machines (via kql_loader.load_kql_template('vm_count')).def vm_count_kql() -> str: return _tmpl("vm_count")