MACP Control Plane (NestJS)

NestJS service for run lifecycle, streaming, and replay

A scenario-agnostic, observer-only control plane for the MACP runtime.

This service is the backend that a Next.js UI talks to for run lifecycle, live stream projection, replay, traces, metrics, and artifacts.

Role

The control plane is an observer. It never calls Send on the runtime.

  • UI: browse runs, launch, render graphs and traces.
  • Scenario layer (e.g. macp-playground): compile scenarios → produce a generic RunDescriptor for this service + per-agent bootstrap for the initiator + participant agents.
  • Agents: authenticate to the runtime directly with their own Bearer tokens and emit their own envelopes (SessionStart, kickoff, Proposal / Evaluation / Vote / etc.) via macp-sdk-python or macp-sdk-typescript.
  • Control plane: allocates sessionId, polls GetSession(sessionId) until the initiator agent opens it, then subscribes read-only to StreamSession(sessionId). Projects canonical events for the UI.
  • Runtime: authoritative orchestrator of MACP envelopes and modes.

Invariants (see ../macp-ui-console/plans/direct-agent-auth.md §Invariants)

  1. The control-plane runtime identity is least-privilege: can_start_sessions: false, is_observer: true — either encoded in a minted short-lived JWT (preferred) or in a static entry in the runtime's MACP_AUTH_TOKENS_JSON.
  2. The control-plane never calls Send — enforced by an invariant lint test (src/runtime/observer-invariant.spec.ts).
  3. POST /runs accepts only a scenario-agnostic RunDescriptor. Fields like kickoff[], participants[].role, policyHints, commitments[], initiatorParticipantId are rejected (forbidNonWhitelisted: true).
  4. sessionId ownership: allocated by the control-plane (UUID v4) at POST /runs and returned to the caller, which distributes it to agents via bootstrap.
  5. Cancellation authority stays with the initiator agent unless the scenario's policy explicitly delegates to the control-plane (see metadata.cancellationDelegated).
  6. The observer StreamSession writes exactly one passive-subscribe frame ({subscribeSessionId, afterSequence}) per RFC-MACP-0006 §3.2 and then keeps the write side open — half-closing would signal "client is done" and stop live-envelope broadcast.

Endpoints

Runs

  • POST /runs — accepts a RunDescriptor; returns {runId, sessionId, status, traceId}
  • GET /runs/:id — run record
  • GET /runs/:id/state — projected UI state
  • GET /runs/:id/events — canonical events
  • GET /runs/:id/stream — SSE of live events
  • POST /runs/:id/cancel — UI cancel (Option A: proxies to initiator agent's cancelCallback; Option B: calls runtime.CancelSession when policy-delegated)
  • POST /runs/:id/suspend / POST /runs/:id/resume — non-terminal pause/resume (macp-proto 0.1.3; TTL banked while suspended)
  • POST /runs/validate — preflight validation
  • POST /runs/:id/clone — clone with optional tag overrides (session context overrides rejected)
  • POST /runs/:id/replay — replay descriptor
  • POST /runs/batch/{cancel,export,archive,delete} + POST /runs/compare — batch operations
  • GET/POST/PATCH/DELETE /webhooks — webhook subscriptions (HMAC-SHA256 signed, outbox + retry)

Full endpoint reference: docs/API.md. Machine-readable: openapi.json (OpenAPI 3.0, generated by npm run openapi:export, checked in CI).

Removed (direct-agent-auth CP-5/6/7)

These endpoints return 410 Gone. Agents emit envelopes via the SDKs directly:

  • POST /runs/:id/messages
  • POST /runs/:id/signal
  • POST /runs/:id/context

Administration

  • GET /audit, POST /admin/circuit-breaker/reset, GET /admin/circuit-breaker/history
  • GET /admin/runtime/sessions — read-only drift detection: runtime session list vs locally tracked runs

Runtime discovery

  • GET /runtime/manifest, /runtime/modes, /runtime/roots, /runtime/health
  • GET /runtime/policies, POST /runtime/policies (rules validated against the spec's closed-set schemas; omitted schemaVersion defaults to 3), DELETE /runtime/policies/:id

Observability

  • GET /runs/:id/traces, /runs/:id/artifacts, /runs/:id/metrics
  • GET /dashboard/overview, /dashboard/agents/metrics
  • GET /healthz, /readyz (no auth), /metrics (API key), /docs (dev only)

Request shape

{
  "mode": "live",
  "runtime": { "kind": "rust" },
  "session": {
    "sessionId": "optional — UUID v4/v7 or base64url 22+",
    "modeName": "macp.mode.decision.v1",
    "modeVersion": "1.0.0",
    "configurationVersion": "config.default",
    "policyVersion": "policy.default",
    "ttlMs": 600000,
    "participants": [
      { "id": "fraud-agent" },
      { "id": "risk-agent" },
      { "id": "growth-agent" }
    ],
    "metadata": {
      "source": "macp-playground",
      "sourceRef": "fraud/high-value-new-device@1.0.0",
      "environment": "production",
      "cancelCallback": {
        "url": "http://initiator.internal/agent/cancel",
        "bearer": "opt-in-shared-secret"
      }
    }
  },
  "execution": {
    "idempotencyKey": "fraud-high-value-new-device-demo-1",
    "tags": ["demo", "fraud"],
    "requester": { "actorId": "coordinator", "actorType": "service" }
  }
}

Response: { "runId": "<uuid>", "sessionId": "<uuid>", "status": "queued", "traceId": "..." }

Local development

Node 20 (.nvmrc) and npm 10+ (see engines in package.json).

npm install
doppler setup --project macp-control-plane --config dev   # once per checkout
doppler run -- npm run drizzle:migrate
doppler run -- npm run start:dev

Doppler is the source of truth for configuration (see Secrets and configuration). Without Doppler access, cp .env.example .env, fill it in, and load it into your shell (set -a; . ./.env; set +a) before the same commands without doppler run --.

Secrets and configuration (Doppler)

The Doppler project is macp-control-plane:

ConfigHolds
devLocal development values (the .env keys)
dev_personalYour own overrides of dev
stgprd's non-secret config only; there is no Railway staging environment yet
prdThe hosted control plane's Railway variables (project macp-scenario-runner, service macp-control-plane)

To change the hosted service, edit Doppler macp-control-plane/prd, then:

deploy/doppler-sync.sh           # dry run: key names, changed/unchanged
deploy/doppler-sync.sh --apply   # push the changed keys, then redeploy

The script manages only the keys in deploy/doppler-keys.txt. It never deletes a Railway variable and never touches a Railway reference (${{service.VAR}}) or a RAILWAY_* variable. It pipes values from Doppler to Railway without printing them, and refuses to run on a dirty tree. Railway holds no Doppler token.

Docker

The Dockerfile consumes the private @multiagentcoordinationprotocol/proto package via a BuildKit secret (never a --build-arg, which would leak into image history):

docker build --secret id=npm_token,env=GITHUB_TOKEN -t macp-control-plane .
# or, with GITHUB_TOKEN exported (PAT with read:packages):
docker compose up --build

For MACP org members

Agents working in this repo can query the MACP knowledge base through the macp-kb MCP server. Setup instructions are in the private macp-docs repo (README, "MCP server"). Answers cite the source repo and commit, so check freshness with kb_status before relying on one. Outside contributors can ignore this section; the server is limited to org members.

Testing

npm test                   # unit tests (Jest, colocated *.spec.ts)
npm run test:cov           # with coverage
docker compose -f docker-compose.test.yml up -d postgres-test
npm run test:integration   # integration tests (real NestJS app + PostgreSQL on 5433, scripted mock runtime)
                           # truncates TEST_DATABASE_URL only (default: the 5433 test DB); never DATABASE_URL

See docs/INTEGRATION.md § Running Integration Tests for runtime modes (mock / docker / remote).

CI/CD & deployment

  • CI (.github/workflows/ci.yml): lint, typecheck, unit + integration tests (with coverage summary), npm audit gate, secret scan, CLAUDE.md convention sweeps, build, and a Docker build on every PR (pushed to GHCR + Trivy-scanned on main).
  • Release (.github/workflows/release.yml): pushing a v*.*.* tag builds the image, gates it with a Trivy CRITICAL scan before pushing multi-arch (linux/amd64 + linux/arm64) semver tags (vX.Y.Z, vX.Y, X.Y.Z, X.Y, latest) to GHCR, and creates a GitHub Release with generated notes.
  • CodeQL (.github/workflows/codeql.yml) and Dependabot (.github/dependabot.yml) run on a weekly schedule.
  • Deployment: single-VM Docker Compose under deploy/ — ./deploy.sh deploy <tag> pulls the pinned GHCR image, runs migrations, starts the stack, and verifies /healthz; an optional SSH deploy workflow (.github/workflows/deploy.yml) activates once DEPLOY_SSH_* secrets exist.

See docs/CICD.md for the full pipeline reference, and docs/SELF_HOSTING.md for the image/env/health contract used by the one-command self-host stack in macp-runtime/deploy/.

Runtime compatibility (currently absorbed through macp-runtime v0.14.0, tested range >=0.8.0 <0.15.0) is tracked in docs/INTEGRATION.md; wire/spec details live in the runtime docs and RFCs, not here.

Make sure the runtime is running at RUNTIME_ADDRESS. Runtime v0.5.0 removed the x-macp-agent-id dev header and refuses to start without an explicit MACP_ALLOW_INSECURE=1 (the published image no longer bakes it in). For dev auth against the reference runtime profile, start the runtime with MACP_ALLOW_INSECURE=1 only (see macp-runtime/docs/getting-started.md#authentication-configuration → Development mode) and set on the control-plane:

RUNTIME_ALLOW_INSECURE=true
RUNTIME_USE_DEV_HEADER=true            # deprecated flag; now sends a dev BEARER, not a header
RUNTIME_DEV_AGENT_ID=macp-control-plane # value used as the dev bearer / sender identity

In dev-mode the runtime accepts any bearer and treats its value as the sender, so the control-plane keeps its macp-control-plane identity. RUNTIME_USE_DEV_HEADER is deprecated and rejected in production — configure RUNTIME_BEARER_TOKEN or MACP_AUTH_SERVICE_URL there.

Runtime auth (observer identity)

The control-plane has exactly one runtime identity with fixed scope is_observer: true, can_start_sessions: false. RuntimeCredentialResolverService resolves credentials per gRPC call using a three-step fallback chain:

ModeTriggerControl-plane env
JWT mint (preferred)MACP_AUTH_SERVICE_URL setMACP_AUTH_SERVICE_URL, MACP_AUTH_SERVICE_API_KEY (the auth-service's token-issuer key, sent as Authorization: Bearer; required against a production auth-service), MACP_AUTH_SERVICE_TIMEOUT_MS, MACP_AUTH_TOKEN_TTL_SECONDS, MACP_AUTH_TOKEN_SENDER
Static BearerJWT disabled or mint failsRUNTIME_BEARER_TOKEN (must match an entry in the runtime's MACP_AUTH_TOKENS_JSON with can_start_sessions: false and is_observer: true — the latter is required for GetSession to succeed on sessions the control-plane did not start; see docs/INTEGRATION.md § Observer authorization contract)
Dev bearer (deprecated)RUNTIME_USE_DEV_HEADER=true, local onlyRUNTIME_DEV_AGENT_ID — sent as Authorization: Bearer <id>; runtime v0.5.0 dropped the x-macp-agent-id header, so the fallback now uses a bearer that a dev-mode runtime accepts as the sender

For the runtime-side token configuration, TLS, and the full production auth story, see:

Per-agent tokens are not held by the control-plane — the scenario layer distributes them to agents via bootstrap. See ../macp-ui-console/plans/direct-agent-auth.md for the onboarding flow.

Migration from pre-2026-04 control-plane

If you're upgrading from a control-plane version that had POST /runs/:id/{messages,signal,context}, those endpoints now return 410 Gone. Agents must migrate to macp-sdk-python or macp-sdk-typescript and authenticate directly to the runtime. RUNTIME_AGENT_TOKENS_JSON is removed; its entries move to the runtime's MACP_AUTH_TOKENS_JSON (one per agent) and to the scenario layer's per-agent bootstrap.

Database tables

  • runs (with runtime_session_id populated at creation)
  • runtime_sessions
  • run_events_raw, run_events_canonical
  • run_projections, run_artifacts, run_metrics
  • run_outbound_messages, audit_log, webhooks, webhook_deliveries

Repo layout

src/
  controllers/        # NestJS controllers
  runs/               # run manager, observer executor, stream consumer
  runtime/            # observer-only runtime provider, proto decoder, credential resolver
  events/             # canonical event normalizer + SSE hub
  projection/         # UI read models
  replay/             # deterministic replay endpoints
  metrics/            # metrics aggregation
  artifacts/          # artifact registration/listing
  storage/            # Drizzle repositories
  db/                 # Drizzle schema + database service
  telemetry/          # OpenTelemetry bootstrap and manual spans
  dto/                # request/response schemas for OpenAPI
  contracts/          # TypeScript interfaces (RunDescriptor, RuntimeProvider, ...)