Files
cp1-agents/scripts/kab_register_agents.py
Jonathan Boniface 66c32745aa
Some checks failed
check agents / check (push) Failing after 14s
F02: Assessment report and package (ADR 0014, 0015); register agents in KAB
- Report (cp1-assessment-report-agent 0.2.0, step s7): a model writes the
  Assessment report from facts worked out in code (register, gaps, scorecard,
  rule traces, checks, and what each application is from the Discovery
  package); code sets the verdict and renders the pages. The
  disposition_signoff review point moves from the critic to the report.
- Package (cp1-assessment-package-agent 0.1.1, step s8): after sign-off, every
  step's output in one package, with a register, a gaps register, the
  sign-off record, lineage and a sha256 per part, in code. Page step s9.
- scripts/kab_register_agents.py: registers deployed agents in KAB's agent
  catalog (an in-place workflow update doesn't).
- Runs exec_1b1257380df7 (014) and exec_374d5d577077 (015) completed on
  crucible-kab-dev: critic pass, report figures all in its facts, package
  complete with every digest recomputing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:19:10 +00:00

74 lines
3.4 KiB
Python

#!/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 '<name>' 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())