#!/usr/bin/env python3 """Prepare the individual-agents Discovery run: the nine logical MCP servers, and each agent's gateway key. Shows what it would do, and changes nothing without --apply. With --apply it: 1. registers each missing logical name in the MCP gateway, against the sample server, and discovers its tools; 2. for each agent with no -secrets, creates a gateway key and stores it in that Secret; 3. restarts the agents it gave a key. A key is shown only once, so it goes from the API's reply straight to kubectl on stdin. It is never written to a file, an argument or the output. Agent keys belong to the tenant they're created in, so this must run in the servers' tenant. KAB tokens last five minutes, so refresh the token just before running. The token comes from $KAB_TOKEN or ~/.kab_token. --apply also needs $MCP_REG_KEY, the sample server's MCP_REGISTRATION_API_KEY, when a name is missing. Usage (from the repository root): python3 scripts/kab_bootstrap_discovery.py # plan only python3 scripts/kab_bootstrap_discovery.py --apply """ from __future__ import annotations import argparse import base64 import json import os import subprocess import sys import time import urllib.error import urllib.request import uuid from pathlib import Path DEFAULT_API = "https://api.kab-dev.kyndemo.live" CP1_TENANT = "6ae37d13999438461a529409" SAMPLE_URL = "http://alz-discovery-sample.mcp-servers.svc.cluster.local:8110" LOGICAL_SERVERS = ("alz-scope", "alz-estate", "alz-cloud", "alz-cmdb", "alz-inventory", "alz-schema", "alz-dependency", "alz-network", "alz-evidence") AGENTS = ("cp1-discovery-planner-agent", "cp1-estate-scan-executor-agent", "cp1-asset-inventory-normalisation-agent", "cp1-cmdb-itsm-reconciliation-agent", "cp1-data-classification-agent", "cp1-network-discovery-assessment-agent", "cp1-dependency-mapping-agent", "cp1-discovery-critic-agent") class Gateway: def __init__(self, api: str, token: str, tenant: str) -> None: self.base = f"{api.rstrip('/')}/api/v1/mcp-gateway/api/v1/admin" self.headers = {"Authorization": f"Bearer {token}", "X-Tenant-ID": tenant, "Content-Type": "application/json"} def call(self, method: str, path: str, body: dict | None = None): data = json.dumps(body).encode() if body is not None else None request = urllib.request.Request(f"{self.base}{path}", data=data, headers=self.headers, method=method) try: with urllib.request.urlopen(request, timeout=60) as response: text = response.read() except urllib.error.HTTPError as error: # The reply to a key request could only name an error here, but keep it short regardless. raise SystemExit(f"{method} {path}: HTTP {error.code}: {error.read()[:300].decode(errors='replace')}") return json.loads(text) if text else {} def items(self, path: str) -> list: data = self.call("GET", path) return data if isinstance(data, list) else next((v for v in data.values() if isinstance(v, list)), []) def token_seconds_left(token: str) -> int | None: """How long a KAB token lasts, read from its own expiry; None when that can't be read.""" try: payload = token.split(".")[1] return int(json.loads(base64.urlsafe_b64decode(payload + "=" * (-len(payload) % 4)))["exp"] - time.time()) except (IndexError, KeyError, TypeError, ValueError): return None def kubectl(context: str, *arguments: str, stdin: bytes | None = None) -> subprocess.CompletedProcess: return subprocess.run(["kubectl", "--context", context, *arguments], input=stdin, capture_output=True, timeout=60) def secret_exists(context: str, name: str) -> bool: return kubectl(context, "-n", "agents", "get", "secret", name, "-o", "name").returncode == 0 def store_key(context: str, agent: str, key: str) -> bool: manifest = {"apiVersion": "v1", "kind": "Secret", "metadata": {"name": f"{agent}-secrets", "namespace": "agents"}, "type": "Opaque", "data": {"MCP_AGENT_API_KEY": base64.b64encode(key.encode()).decode()}} return kubectl(context, "apply", "-f", "-", stdin=json.dumps(manifest).encode()).returncode == 0 def main(argv=None) -> int: parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--apply", action="store_true", help="make the changes; without it, only show them") parser.add_argument("--api", default=os.getenv("KAB_API", DEFAULT_API)) parser.add_argument("--tenant", default=CP1_TENANT, help="the KAB tenant the MCP servers are registered in") parser.add_argument("--context", default="crucible-kab-dev-aks") parser.add_argument("--expires-days", type=int, default=30, help="how long each agent key lasts (default 30)") parser.add_argument("--agent", action="append", help="only these agents (default: the eight individual agents)") args = parser.parse_args(argv) path = Path.home() / ".kab_token" token = os.getenv("KAB_TOKEN") or (path.read_text().strip() if path.exists() else "") if not token: raise SystemExit("No token: set KAB_TOKEN or write it to ~/.kab_token") left = token_seconds_left(token) if left is not None: if left <= 0: raise SystemExit(f"The token expired {-left} s ago. Reload KAB, copy a fresh access_token, and run this again.") print(f"Token: {left} s left.") gateway = Gateway(args.api, token, args.tenant) registered = {server.get("name") for server in gateway.items("/servers?page=1&page_size=100")} keys = {key.get("agent_name"): key for key in gateway.items("/agent-keys?page=1&page_size=100")} missing_names = [name for name in LOGICAL_SERVERS if name not in registered] need_key = [agent for agent in (args.agent or AGENTS) if not secret_exists(args.context, f"{agent}-secrets")] orphans = [agent for agent in need_key if agent in keys] print(f"Servers in the tenant: {len(registered)}. Logical names to register: {missing_names or 'none'}") print(f"Agents needing a key: {need_key or 'none'}") if orphans: print(f"These have a gateway key but no Secret, and a key can't be read back: {orphans}. " "Revoke them in KAB, then run this again.") if not args.apply: print("Plan only. Nothing changed. Add --apply to make these changes.") return 1 if orphans else 0 if orphans: return 1 if missing_names: registration_key = os.getenv("MCP_REG_KEY") if not registration_key: raise SystemExit("Set MCP_REG_KEY to the sample server's key.") for name in missing_names: server_id = str(uuid.uuid4()) gateway.call("POST", "/servers", {"server_id": server_id, "name": name, "url": SAMPLE_URL, "api_key": registration_key, "enabled": True, "max_connections": 10, "timeout_seconds": 30, "description": "ALZ sample, logical name"}) found = gateway.call("POST", f"/tools/servers/{server_id}/discover", {}) print(f"Registered {name}: {found.get('discovered', found.get('count', 'tools discovered'))}") given = [] for agent in need_key: created = gateway.call("POST", "/agent-keys", {"agent_name": agent, "expires_in_days": args.expires_days, "description": "CP1 Discovery, individual agents"}) if not store_key(args.context, agent, created.pop("api_key")): raise SystemExit(f"{agent}: the key was created but not stored. Revoke key id {created.get('id')} in KAB.") given.append(agent) print(f"{agent}: key {created.get('key_prefix')}... stored, expires {created.get('expires_at')}") for agent in given: restarted = kubectl(args.context, "-n", "agents", "rollout", "restart", f"deploy/{agent}") print(f"{agent}: {'restarted' if restarted.returncode == 0 else 'restart failed'}") print("Done.") return 0 if __name__ == "__main__": sys.exit(main())