#!/usr/bin/env python3 """Register deployed agents in KAB's agent catalog, so a workflow that uses them validates. A workflow import or update fails with "Agent '' not found in this environment" until each of its agents is in the catalog of the tenant it runs in. Updating a workflow in place (`kab_import_workflow.py --update`) never registers agents. This calls the catalog's `POST /api/v1/agent-catalog/discover-and-register` with each agent's in-cluster URL; KAB reads the agent card there and registers it in the caller's tenant (Editor role). An agent that is already registered (HTTP 409) is reported and left as it is. Shows what it would do, and changes nothing without --apply. The token comes from $KAB_TOKEN or ~/.kab_token and lasts five minutes, so refresh it just before running. An agent with MCP tools also needs its gateway key (see docs/deployment.md, step 4); a code-only agent, or one with no tools, doesn't. Usage (from the repository root): python3 scripts/kab_register_agents.py cp1-assessment-report-agent cp1-assessment-package-agent python3 scripts/kab_register_agents.py cp1-assessment-report-agent cp1-assessment-package-agent --apply """ from __future__ import annotations import argparse import json import sys import urllib.error import urllib.request from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from kab_import_workflow import CP1_TENANT, DEFAULT_API, _token # noqa: E402 PATH = "/api/v1/agent-catalog/discover-and-register" def url_of(agent: str) -> str: return f"http://{agent}.agents.svc.cluster.local:8080" def register(api: str, token: str, agent: str) -> str: request = urllib.request.Request( f"{api.rstrip('/')}{PATH}", data=json.dumps({"url": url_of(agent)}).encode("utf-8"), method="POST", headers={"Authorization": f"Bearer {token}", "X-Tenant-ID": CP1_TENANT, "Content-Type": "application/json"}) try: with urllib.request.urlopen(request, timeout=60) as response: reply = json.loads(response.read() or b"{}") return f"registered: agent_id={reply.get('agent_id')} ({reply.get('message', '')})" except urllib.error.HTTPError as error: detail = error.read().decode("utf-8", "replace")[:400] if error.code == 409: return f"already registered ({detail})" hint = {401: "the token is missing, wrong or expired: refresh it", 403: "your KAB account lacks the Editor role"}.get(error.code, "") raise SystemExit(f"{agent}: HTTP {error.code} {hint}\n{detail}") def main(argv=None) -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("agents", nargs="+", help="agent names, as deployed (e.g. cp1-assessment-report-agent)") parser.add_argument("--api", default=DEFAULT_API) parser.add_argument("--token-file", type=Path, default=Path.home() / ".kab_token") parser.add_argument("--apply", action="store_true", help="register; without it, only show what would be done") args = parser.parse_args(argv) if not args.apply: for agent in args.agents: print(f"would register {agent} at {url_of(agent)} in tenant {CP1_TENANT}") print("add --apply to register") return 0 token = _token(args.token_file) for agent in args.agents: print(f"{agent}: {register(args.api, token, agent)}") return 0 if __name__ == "__main__": raise SystemExit(main())