# ContextDB Authentication Guide (`auth.md`)

Last reviewed: 2026-11-28

ContextDB supports three authentication modes depending on where your agent runs.
Never place any ContextDB credential in browser code, mobile bundles, client logs,
or prompt transcripts.

## 1. OAuth 2.1 + PKCE for Interactive MCP Clients (Hosted Alpha)

Use this mode for Cursor, Claude Code, Claude, ChatGPT, OpenAI Codex CLI,
VS Code / GitHub Copilot, Grok, and other user-facing MCP clients.

- **MCP endpoint:** `https://api.contextdb.ai/mcp`
- **Authorization Server Metadata (RFC 8414):** `https://api.contextdb.ai/.well-known/oauth-authorization-server`
- **Protected Resource Metadata (RFC 9728):** `https://api.contextdb.ai/.well-known/oauth-protected-resource`
- **MCP-scoped Protected Resource Metadata:** `https://api.contextdb.ai/.well-known/oauth-protected-resource/mcp`
- **Dynamic Client Registration (RFC 7591):** `POST https://api.contextdb.ai/oauth/register`
- **Authorization endpoint:** `https://app.contextdb.ai/oauth/authorize`
- **Token endpoint:** `POST https://api.contextdb.ai/oauth/token`
- **Revocation endpoint:** `POST https://api.contextdb.ai/oauth/revoke`
- **Supported grant types:** `authorization_code` (PKCE `S256` required), `refresh_token`
- **Supported scopes:** `memory:read`, `memory:write`
- **Token lifecycle:** Access tokens (`cmo_`) are valid for 60 minutes. Rotating refresh tokens (`cmr_`) are issued automatically on authorization-code exchange (valid 30 days) so connected clients stay signed in across sessions.
- **Partition binding:** OAuth binds one account, one project, and one server-derived `user_id` partition. Do not pass `user_id` in OAuth MCP tool calls.

## 2. Project API Key (`CONTEXTDB_API_KEY`) for Backend Services & SDKs

Use this mode for server-side Python (`contextdb-cloud-client`), TypeScript
(`@contextdb/cloud`), or direct HTTP calls to `https://api.contextdb.ai/v1/*`
(and server-to-server MCP calls managing explicit per-customer `user_id` partitions).

- **Create a key:** Sign in at `https://app.contextdb.ai/keys` (100 active profiles free, no card required).
- **Header:** `Authorization: Bearer $CONTEXTDB_API_KEY`
- **Prefix:** `cdb_` (shown once at creation, stored hashed).
- **Scope:** Bound to one organization and one project. Supply `user_id` explicitly on every request to isolate each end customer's partition.
- **Idempotency:** Pass a stable `Idempotency-Key` header on `POST /v1/evolve`, `POST /v1/formation/jobs`, `POST /v1/receipts`, `POST /v1/forget`, and retryable writes.

## 3. Memory CI Evaluation Token (`CONTEXTDB_EVAL_TOKEN`)

Use this mode in CI/CD pipelines running `contextdb-memory-ci` or `POST /evals/v1/*`.

- **Header:** `Authorization: Bearer $CONTEXTDB_EVAL_TOKEN`
- **Prefix:** `cbe_` (scoped strictly to `/evals/v1/*`; cannot read or mutate customer memory routes).

## 4. Local / Offline Mode (No Cloud Account Required)

If your workflow requires zero cloud dependencies, local-only storage, or air-gapped execution:

```bash
pip install pycontextdb
```

Embedded SQLite requires no API key, no account, and makes zero outbound network calls.
To run a local HTTP/MCP server on localhost:

```bash
pip install "pycontextdb[serve]"
contextdb serve --http --host 127.0.0.1 --port 8080 --token "$CONTEXTDB_SERVE_TOKEN"
```

## Credential Safety Rules

- Store `CONTEXTDB_API_KEY` and `CONTEXTDB_EVAL_TOKEN` in server secret managers only.
- Never commit `cdb_`, `cbe_`, `cmo_`, or `cmr_` tokens to source control, fixtures, or public logs.
- Rotate compromised keys immediately in `https://app.contextdb.ai/keys` or revoke OAuth grants via `POST https://api.contextdb.ai/oauth/revoke`.
