# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Repository status

**Stale note, corrected 2026-09-26**: this section used to say the repo had no source code.
That stopped being true on 2026-09-09. There is now a working proof-of-concept under
`poc-code/` — three real modules (Node/Express platform stub, Angular SPA, a cloned-and-modified
Spring Boot PDS, a TypeScript local MCP proxy), each with its own build system, tests, and
manual-testing docs. Steps 1–8 of the build order plus 4b/5b are done and live-verified
end-to-end (a real mandate → real PDS-signed account-link assertion → real DPoP-signed MCP tool
call → correct data back); only step 9 (E2E dry run) is not marked done — `compose.yaml` (step 8) was live-tested on
2026-10-09 on the WSL2 demo machine, and step 9b (Trace Prestataire endpoint + dashboard) was
added during that rehearsal (corrected 2026-11-30; this line previously said "not yet live-tested").
Do not assume "no code" or "design only" when picking up work here — check
[`poc-code/contracts.md`](poc-code/contracts.md) §0 (the build-order table) and
[`poc-code/step-5b-checklist.md`](poc-code/step-5b-checklist.md)/
[`poc-code/step-6-checklist.md`](poc-code/step-6-checklist.md) for the current, authoritative
state before proposing new scaffolding — most of what a fresh read of `base-project-analysis.md`
would suggest building already exists.

**Consolidated documentation (added 2026-11-30):** [`documentation/`](documentation/README.md) —
English topic pages (`documentation/topics/*.md`) plus every source below converted as-is, built
into a browsable static site by `node documentation/build.mjs` (open `documentation/site/index.html`).
The topic pages summarise; the sources below stay authoritative for their own detail.

**Document layering, entry points first:**
- [`poc-code/architecture.md`](poc-code/architecture.md) → [`poc-code/contracts.md`](poc-code/contracts.md)
  (frozen cross-module wire format + build-order status) → `poc-code/<module>/spec.md` — the
  POC's own design + build docs, most current for anything already implemented.
- [`analysis/synthese/README.md`](analysis/synthese/README.md) — the living synthesis of open
  product/legal/business questions and verified facts; check here before reusing a claim from
  the raw transcript below.
- `analysis/base-project-analysis.md` — the **original** raw, ~500KB French-language Q&A
  transcript that first defined the product and technical architecture for this project
  (codenamed **"mandat" (mandate) for AI agents**). Still the deepest source for requirements
  not yet resolved into `poc-code/`'s specs or `analysis/synthese/`'s decisions — read the
  relevant section (see headers below) before proposing anything not already covered by the POC
  docs, since it contains specific, deliberate design decisions (and explicitly rejected
  alternatives, struck through with `~~...~~`) that are easy to contradict by guessing. Some of
  its claims are now known-wrong or superseded — `analysis/synthese/README.md` §6 tracks which.

## The product, in one paragraph

The sections below (architecture, security model) describe the **target product design**, not
necessarily the POC's exact implementation — the POC deliberately narrows scope (one PDS,
read-only tools only, hand-signed JWT instead of a real RFC 8693 Keycloak broker, no Integration
SDK, no real itsme) and its exact wire format is frozen separately in
[`poc-code/contracts.md`](poc-code/contracts.md), which also documents every POC-vs-production
deviation explicitly. Read this section for the *what and why*; read `contracts.md` for the
*exact bytes on the wire today*.

A platform that lets a human strongly authenticate (via **itsme**, a Belgian/EU eIDAS-qualified
identity provider) and issue a cryptographically signed, scoped **mandate** authorizing a
*specific* AI agent (identified by its own public key, not by IP address) to call a specific
third party's tools (e.g. a bank's MCP endpoints) within strict limits (allowed tools, amount
caps, expiry). The platform never sees the transaction traffic and never holds any agent's
private key — it only issues and revokes the mandate.

## Core architecture (3 pillars)

1. **Web Platform (cloud)** — the trust anchor. Users authenticate via itsme; the platform acts
   as an Identity Broker (Keycloak is the recommended implementation) performing an
   **OAuth 2.0 Token Exchange (RFC 8693)** to convert the itsme ID Token into a signed
   **Agent Delegation Token** (a JWT). Also hosts the JWKS endpoint (platform's public key) and
   the mandate revocation/introspection endpoint. This is the only component the human interacts
   with directly, and the only place mandates are created, modified, or revoked.

2. **Local MCP Server** (downloadable npm/npx package, e.g. `@veritia/mcp-server`) —
   for no-code / `.md`-driven agents (Claude Desktop, Claude Code, GitHub Copilot, etc.). Runs
   locally on the user's machine, ships two modes:
   - `init` — generates the agent's asymmetric keypair locally, writes the private key to a
     restricted-permission file on disk (never an env var, never transmitted), prints the public
     key for the user to paste into the Web Platform.
   - server/runtime (stdio-based, speaks native MCP) — a **generic, tool-agnostic proxy**: on
     startup it dynamically discovers the third party's tools via their MCP discovery endpoint
     (no hardcoded tool list, so third parties can add tools without any change on this side),
     and on every tool call it signs the payload with the local private key and injects the
     Agent Delegation Token before forwarding to the third party's API.

3. **Integration SDK** (Python + TypeScript, e.g. `pip install`/`npm install`) — same
   capabilities as the local MCP server (keypair generation, Proof-of-Possession request
   signing, JWT header injection) but as a library for developers writing custom/programmatic
   agents (LangChain, CrewAI, raw Python/TS, etc.) instead of `.md`-driven ones. The SDK and the
   local MCP server are alternatives, not layers on top of each other — a developer picks one.

Multiple third parties (e.g. two different banks) are handled by running/configuring multiple
isolated instances of the local MCP server or SDK client, each bound to its own
`BANK_API_ENDPOINT` and its own Agent Delegation Token — never one server merging tool sets.

## Security model — the concepts that recur everywhere in the doc

- **Three independent keypairs**, never sharing secrets across parties:
  1. Platform's keypair — signs Agent Delegation Tokens; public key published via **JWKS**.
  2. Agent's keypair — generated and kept locally by the agent/user; private key never leaves
     the machine; public key gets sealed into the JWT's `cnf` claim.
  3. Third party's (bank's) keypair — used for its own API/account-linking responses.
- **Proof of Possession (PoP)**: every tool-call request is signed with the agent's private key.
  The verifier (bank) extracts the agent's public key from the JWT's `cnf` claim and checks the
  request signature against it — this is what makes a stolen/copied JWT alone useless (a copied
  token without the matching private key fails signature verification), and is explicitly the
  replacement for the earlier (rejected) IP-whitelist design.
- **Agent Delegation Token (JWT, `alg: RS256`, `typ: at+jwt`)** — see the exact payload shape
  under `# **Agent Delegation Token (format JWT)**` in the doc. Key claims: `sub` (itsme human
  identity), `act` (RFC 8693 actor = the AI agent), `cnf` (agent's public JWK — PoP anchor),
  `mcp_constraints.allowed_tools`, `mcp_constraints.restrictions` (currency/amount caps/accounts),
  `mcp_constraints.transparency` (EU AI Act "this caller is an AI" attestation + audit link).
- **Revocation**: two complementary approaches described — (A) a revocation list / RFC 7662
  token introspection endpoint the bank polls or checks per-request, and (B) short-lived (~15
  min) JWTs with a refresh flow, so a revoked mandate stops being renewable.
- **Mandate uniqueness**: the rule is *1 human + 1 specific agent = 1 unique mandate*. Sharing a
  JWT across multiple agent instances is cryptographically self-defeating (each has a different
  private key, so PoP fails) and is deliberately not supported, for legal-imputability,
  blast-radius, and least-privilege reasons (see `# **5\. Pourquoi autoriser le partage de
  mandat...**`).
- **Regulatory anchors driving the design**: eIDAS 2.0 (qualified identity via itsme, sovereign
  non-repudiation) and the EU AI Act (mandatory AI-disclosure to third-party systems, transparency
  claim in the token). These are treated as first-class design constraints, not afterthoughts.

## Document conventions worth knowing when editing it

- Struck-through text (`~~like this~~`) marks an earlier design that was explicitly rejected and
  replaced — e.g. IP whitelisting → PoP, or bank-side manual mandate approval → instant local
  cryptographic validation. Don't reintroduce a struck-through approach without noting why.
- The doc is a chronological accumulation of Q&A turns (each `# **...**` top-level heading is a
  separate question/topic that was explored), not a spec written top-down — later sections
  sometimes refine or add nuance to earlier ones (e.g. mandate-ownership verification is refined
  twice: national-identifier matching vs. OAuth account-linking).
