# MCP execution trail

**Last updated:** 2026-11-22T07:10:00-03:00 (UTC-3)

The execution trail keeps one durable PostgreSQL record for every authorized MCP operation: each tool listing and each tool call, through the [REST tool facade](/docs/features/mcp-execution/) or the [MCP ingress](/docs/features/mcp-ingress/). A record is written before anything is sent and closed after the answer, so an operator can always tell whether a call left the gateway and how it ended. Logs and spans already describe each operation; the trail adds a record that survives restarts and that a reviewer can page and filter.

| Flag | Default | What it turns on |
| --- | --- | --- |
| `mcp_execution_audit` | `false` | The trail, the `Idempotency-Key` refusal and `GET /api/v1/admin/mcp/executions`; an operator control under plan entitlements; requires `mcp_execution` |
| `mcp_execution_chain` | `false` | The [tamper-evident chain](#tamper-evidence) of closed records, verified with the audit chain; an operator control; requires `mcp_execution_audit` and `audit_tamper_evidence` |

## What a record holds

A record holds identifiers and outcome metadata only. Tool arguments, tool results, credentials, upstream MCP session IDs and the `Idempotency-Key` value itself are never stored; the key is kept as its SHA-256.

| Field | Meaning |
| --- | --- |
| `id` | Execution ID, `mcx_` plus 24 hex characters; also returned in the `X-MCP-Execution-Id` response header |
| `request_id`, `trace_id` | The request's `X-Request-Id` and W3C trace ID |
| `surface` | `rest` (the REST facade) or `mcp` (the MCP ingress) |
| `session_id` | The ingress session that carried the call; empty for the REST facade |
| `protocol_version` | The MCP revision the ingress client spoke (`2025-11-25` or `2026-07-28`); empty for the REST facade ([revision 2026-07-28](/docs/features/mcp-protocol-2026-07-28/)) |
| `upstream_protocol_version` | The revision negotiated with the server; empty when no session was opened |
| `operation` | `tools/list` or `tools/call` |
| `server_id`, `server_kind` | The server and where its trust comes from: `boot` (YAML), `managed` (console-approved) or `openapi` (an approved OpenAPI source) |
| `tool_name`, `schema_sha256` | For a call, the tool and the hash of the input schema the arguments were validated against |
| `key_id`, `team_id`, `user_id` | The calling key, its team and, for a user-owned key, the console user |
| `subject_issuer`, `subject` | For a [token exchange](/docs/features/mcp-token-exchange/) call, the end user it acted for |
| `credential_source` | The upstream credential class used: `none`, `boot`, `team`, `user_oauth`, `user_oauth_reused` ([credential reuse](/docs/features/mcp-credential-reuse/)), `token_exchange` or `user_static` ([per-user static credential](/docs/features/mcp-user-credentials/)) |
| `idempotent` | The call carried an `Idempotency-Key` |
| `arguments_bytes`, `result_bytes` | Sizes of the call's arguments (0 for a listing) and of the response returned |
| `state`, `sent` | See the states below; `sent` is true once the tool call left the gateway |
| `http_status`, `error_code`, `tool_error` | The answer's HTTP status, problem code, and the result's `isError` flag |
| `started_at`, `ended_at`, `duration_ms` | Database-clock times |

Only authorized operations are recorded. A request refused before authorization (missing or revoked key, missing scope, a tool the key may not use) leaves no record; its log line and span remain.

## States

| State | Meaning |
| --- | --- |
| `started` | Authorized; nothing sent yet |
| `dispatched` | The call is being sent; written immediately before the upstream request |
| `succeeded` | The upstream answered; a tool-level failure is `succeeded` with `tool_error: true` |
| `failed` | A listing failed, or a sent call was refused by the upstream credential check (HTTP 401/403), which proves the tool did not run |
| `refused` | The call was not sent: invalid arguments, a missing credential, a quarantined tool, a busy gateway |
| `uncertain` | The call was sent and no outcome was proven: a timeout, a client that disconnected, a broken connection or a protocol violation |

A record that was never closed, because its process stopped, is reported with its effective state once it is a minute old: `uncertain` when it was `dispatched`, `refused` for an unsent call and `failed` for a listing; the state filter matches what is reported. The gateway never resends an uncertain call; neither the REST facade nor the ingress retries a tool call.

## Write failures

With `require_record: true` (the default) a call whose record cannot be written is refused with `503 mcp_audit_unavailable` and nothing is sent. With `require_record: false` such a call proceeds without a record and the gateway logs `mcp_audit_record_skipped`, except a call carrying an `Idempotency-Key`, which is always refused: only its record proves the key was not used before. Once a record exists, a `dispatched` mark that cannot be written always refuses the call unsent, whatever `require_record` says, since a record left `started` would otherwise describe a call that was sent. A record that cannot be closed after the answer stays `dispatched`, reads as uncertain, and the gateway logs `mcp_audit_finish_failed`.

## Idempotency

A client may send one `Idempotency-Key` header (1-128 visible ASCII characters) on `POST /v1/mcp/servers/{id}/tools/call`. A second call by the same key with the same value is refused with `409 mcp_call_already_attempted`, whatever the first outcome was, and `X-MCP-Execution-Id` names the earlier record. Nothing is sent again, so a retry after an uncertain outcome cannot run the tool twice. Values are scoped to the calling key and kept as long as the record: a key value is consumed by the first attempt even when that attempt was refused unsent, and becomes usable again only after the record is deleted by retention. Use a new value for a deliberate new attempt. While the trail is off the header is ignored and not validated.

## Configuration

Boot configuration under `mcp_execution.audit`, read-only in the API (`GET /api/v1/admin/config`, field `mcp_execution.audit` with `retention_days` and `require_record`; it was a top-level `mcp_audit`) and shown in the execution trail panel. A restart applies a change.

```yaml
features:
  mcp_execution: true
  mcp_execution_audit: true
mcp_execution:
  protocol_version: "2025-11-25"
  audit:
    retention_days: 30    # 1-3650; default 30
    require_record: true  # default true
```

| Field | Default | Bounds | Meaning |
| --- | --- | --- | --- |
| `retention_days` | `30` | 1-3650 | Records older than this are deleted, at most 1000 per batch and one batch per process every ten minutes, after an operation answered |
| `require_record` | `true` | | Refuse a call whose record cannot be written |

## Reading the trail

`GET /api/v1/admin/mcp/executions` returns records newest first. It requires the `mcp_audit:read` console permission, which the `admin`, `viewer` and `operator` roles hold; team-scoped roles do not.

| Query | Meaning |
| --- | --- |
| `server_id`, `team_id`, `key_id`, `tool`, `request_id`, `session_id` | Exact match, at most 128 characters |
| `state` | One state; `uncertain` also matches stale `dispatched` records |
| `surface` | `rest` or `mcp` |
| `from`, `to` | RFC 3339 times on `started_at` |
| `limit` | 1-200, default 50 |
| `cursor` | `next_cursor` of the previous page |

```json
{
  "data": [
    {
      "id": "mcx_3f9a0c21d4e5b6a7980c1d2e",
      "request_id": "req-7b1c",
      "surface": "rest",
      "operation": "tools/call",
      "server_id": "weather",
      "server_kind": "boot",
      "tool_name": "forecast",
      "schema_sha256": "9c1e…",
      "key_id": "key-ops",
      "team_id": "platform",
      "credential_source": "boot",
      "idempotent": true,
      "arguments_bytes": 18,
      "result_bytes": 214,
      "state": "succeeded",
      "sent": true,
      "http_status": 200,
      "tool_error": false,
      "started_at": "2026-11-14T15:02:11.204Z",
      "ended_at": "2026-11-14T15:02:11.388Z",
      "duration_ms": 184
    }
  ],
  "next_cursor": "1794063731204000.mcx_3f9a0c21d4e5b6a7980c1d2e",
  "retention_days": 30
}
```

In the console, **MCP registry → Execution trail** filters by server and state and pages with **Load more**.

## Tamper evidence

With `mcp_execution_chain`, closed records join the [audit tamper evidence](/docs/features/data-governance/) design in a chain of their own, because the trail has its own lifecycle and retention:

```text
row_hash   = SHA-256("aigw.mcp.execution.v1" || every stored field of the record, length-prefixed)
chain_hash = SHA-256("aigw.mcp.chain.v1"     || previous chain_hash || seq || row_hash)
```

- **What is covered.** Every field of a closed record (identifiers, surface, operation, server, tool, schema hash, key, team, user, credential class, session, subject, idempotency hash, sizes, state, sent, status, error code, tool error, times and protocol revisions). The record never holds arguments, results or credentials, and the chain adds only hashes, so neither does the chain.
- **When.** The retention worker seals closed records about a minute after they close, in close order, at most 1000 per batch, one sealer at a time across replicas; each batch logs the new head as `mcp_execution_chain_sealed` (`head_seq`, `head_hash`). Turning the flag on later seals the closed history first. An open record (`started` or `dispatched`) is sealed once it closes; until then it is counted as `open`, not verified.
- **Verification.** `POST /api/v1/admin/audit-chain/verify` (console **Data governance → Audit tamper evidence → Verify now**) and `gateway audit-verify` verify it with the audit chain, whenever the chain exists (also with the flag off, or when the flag is set only in PostgreSQL), and add `mcp_executions` to their answer: `status` (`ok`, `tampered`, `not_started`), `verified_rows`, `head_seq`, `head_hash`, `anchor_seq`, `anchor_hash`, `unsealed`, `open`, `failure`. A deleted record is `missing`, an edited one `altered` (its ID is named), a re-inserted one `duplicate` or `beyond_head`. `gateway audit-verify` exits with the tampered status when either chain is tampered; the audit chain's own `status` is not changed by the MCP part.
- **Retention.** With the flag on, the retention prune deletes only a verified, contiguous prefix of sealed records older than `mcp_execution.audit.retention_days`, records a checkpoint that becomes the new anchor and attests it with an `mcp_execution_chain.checkpoint` row in the identity audit (sealed by the audit chain); moving the anchor without that attestation is reported as `anchor_unattested`. Once the audit chain's own retention has purged history, the attestation row may be gone with it; a missing attestation is then not reported, while a present one must still match. The prune runs the same anchor checks as verification and also verifies the first record it keeps, so neither a forged anchor nor a record edited to look young is purged away; a prefix that does not verify is never purged (`mcp_execution_chain_tampered` is logged). Open records that never closed are deleted after retention. With the flag off, the plain prune never deletes a sealed record, so turning the chain off does not purge its evidence.
- **Limits.** As with the audit chain, seals carry no secret key: a writer with database access who rewrites every later seal and the head is caught only against a head kept outside the database (the logged `head_hash`). A record deleted while still open, or between closing and its seal, is not covered.

## Upgrade

Migration 191 adds `chain_seq` and `chain_hash` (empty) to `gateway_mcp_executions` and creates the MCP chain head and checkpoints; nothing is sealed until `mcp_execution_chain` is on. An older binary rejects that flag name in YAML; its plain prune would delete sealed records, so turn the flag off and accept that, or keep the newer binary, before rolling back.

Migration 150 creates `gateway_mcp_executions`. It adds no column to an existing table and records nothing until the flag is turned on. Replicas that run an older release while the flag is on simply do not write records for the operations they serve.
