# Auth — Fonara Search MCP

Three ways in, one gate. The response payload is identical in every mode — only the auth path differs, and a developer can move from self-host to hosted without touching their integration.

## Hosted: three auth modes

### 1. Keyless — try before you commit

`initialize`, `tools/list`, and `tools/call search` answer with **no credential**. An agent can discover the server, read the tool set, and run searches before it has a key.

```
POST /mcp
{ "jsonrpc": "2.0", "id": 1, "method": "tools/call",
  "params": { "name": "search", "arguments": { "query": "example query" } } }
```

`tools/call` for `extract` or `similar` without a credential returns `429` with `reason: 'identity'`:

```
HTTP/1.1 429 Too Many Requests
Retry-After: 0
Content-Type: application/json

{ "error": "extract and similar require an API key: keyless callers may use search and status", "retry_after": 0, "upgrade": "https://fonaralabs.dev/searchmcp/pricing", "reason": "identity" }
```

Keyless allowance: `search` and `status` free; 200 credits/day shared across anonymous callers, 10 runs/min; responses carry `snapshot_url: null` — the answer without the record. `extract` and `similar` require an API key for attribution.

### 2. Bearer — the standard path

```
POST /mcp
Authorization: Bearer <fonara-key>
Content-Type: application/json

{ "jsonrpc": "2.0", "id": 2, "method": "tools/call",
  "params": { "name": "search", "arguments": { "query": "example query" } } }
```

A malformed `Authorization` header (anything not `Bearer <token>`) is a failed authentication, not a keyless request — 401 even on `tools/list`.

### 3. Key in path — for clients that cannot set headers

```
POST /v1/<fonara-key>/mcp
```

Same validation, same tiers, same payloads as the bearer header. If both a header key and a path key are present they must agree, or the request is 401. Treat these URLs as credentials: the key rides the path, so do not log or share request URLs.

The versioned namespace has exactly one valid shape. Anything else — `/V1/...`, `/v2/...`, a trailing slash, a missing `/mcp` — is `404` in every mode, before a credential is even read, because such a path may carry a key inline.

## The 401 shape

```
{ "error": "missing or invalid API key" }
```

with the `WWW-Authenticate: Bearer` challenge header. The body carries no part of the attempted credential.

## Creating a hosted key

`POST https://searchmcp.fonaralabs.dev/v1/key-request` — unauthenticated, one call:

```json
{ "agent_name": "your-agent", "contact": "you@example.com", "intent": "one line, optional" }
```

`agent_name` (1–128 characters) and `contact` (an email address or an `http(s)` URL) are required; `intent` is one optional line. A valid request answers `200`:

```json
{
  "request_id": "kr_…",
  "status": "review",
  "next": "https://searchmcp.fonaralabs.dev/auth.md#creating-a-hosted-key"
}
```

`status` is what happened to the request:

- `review` — the request is queued (namespace `KEYS_REQUEST`) for a human to mint the key. Keep `request_id`; the address in `contact` is how you are reached, or an operator mints from the queue with the runbook below. The public deployment ships this mode: the route itself never mints a key.
- `issued` — the deployment runs with `KEY_ISSUANCE_MODE = 'auto'`; the response then also carries `key` and `tier: "free"`, usable immediately.

The route is capped per IP at `3 requests/hour` and `10 requests/day`; over a cap it returns the same 429 shape as every other limit (`error`, `retry_after`, `upgrade`) — there is no second denial format. `agent_name` and `contact` never appear in a response body, and an address is stored only as a salted SHA-256 (`ip_hash`), never raw.

### Runbook — minting from the queue (operator)

The queue is a KV namespace the Worker writes and no route reads. A human mints out of band:

```bash
# 1. list pending requests (oldest first is up to your shell)
npx wrangler kv key list --namespace-id "$KEYS_REQUEST_NS"

# 2. read one: agent_name, contact, intent, ip_hash, created_at, status
npx wrangler kv key get "<kr_…>" --namespace-id "$KEYS_REQUEST_NS"

# 3. mint a free-tier key (or {"tier":"paid"} once a tier is agreed)
npx wrangler kv key put "<fonara-key>" '{"tier":"free"}' --namespace-id "$KEYS_NS"

# 4. deliver it to the request's contact out of band; delete the request record
npx wrangler kv key delete "<kr_…>" --namespace-id "$KEYS_REQUEST_NS"
```

The same key works in the `Authorization` header and in the `/v1/<key>/mcp` path. Tiers: free (100 runs + 20 extracts per month, no card) and paid (monthly run allowance, higher concurrency, permanent receipts). Neither namespace id is served, returned, or logged by any route.

### Revocation — one step

Delete the key from `KEYS`:

```bash
npx wrangler kv key delete "<fonara-key>" --namespace-id "$KEYS_NS"
```

The next call with that key is `401 {"error":"missing or invalid API key"}` — including reads of receipts it used to own. The request record in `KEYS_REQUEST` is history, not authorization; deleting it too leaves nothing behind.

## Hosted vs self-hosted — the consequence

The public deployment runs hosted: Fonara holds the provider keys (Brave, Exa, Tavily) inside the Worker, and every call is metered — keyed calls against a key's allowance, keyless `search`/`status` against the 200-credits/day cap shared across anonymous callers. Turning hosted mode off removes the auth gate and the meter entirely: anonymous traffic then reaches our provider keys with no cap and no identity, and bills our accounts without limit. BYOK is the self-host mode: the operator's own keys do the work on their own infrastructure, so flipping hosted mode off there only changes whose keys are billed.

## Self-hosted: BYOK env vars

The public repo runs on your own provider keys — no Fonara key exists in that mode, and no key custody passes through us:

| Variable               | Required for       | Notes                                                          |
| ---------------------- | ------------------ | -------------------------------------------------------------- |
| `BRAVE_SEARCH_API_KEY` | the brave backend  | missing/empty ⇒ backend reported disabled, with the reason     |
| `EXA_API_KEY`          | the exa backend    | also powers `similar` and the exa fallback in `extract`        |
| `TAVILY_API_KEY`       | the tavily backend | the third slot; gate with `ENABLE_TAVILY` (default on)         |
| `FIRECRAWL_API_KEY`    | optional           | extract tries firecrawl first, then falls back to exa contents |

A backend without its key is reported as disabled by `status` — never silently dropped, never pretending to work. With no backends enabled, `search` fails with the per-backend reasons.

## The identical-payload promise

You are billed in units, not dollars. One `search` consumes one credit from your tier; a cache hit, a failure and `status` consume nothing. `provider_cost_usd` reports what the search engines charged for the call — we show it so you can audit the router, not because it is your bill. A cache hit returns the original run's payload unchanged — including its credits field — and consumes nothing new.

Hosted (one Fonara key) and self-hosted (BYOK) return byte-identical response payloads for the same tool call. Only the auth path differs. Cache keys, merge order, agreement arithmetic and the schema are the same code in both modes. The only keyed-vs-keyless difference inside a hosted deployment: keyless runs carry `snapshot_url: null` and do not write a receipt.

## Transport notes

Streamable HTTP, stateless, POST-only: every request is a standalone JSON-RPC 2.0 message; there is no session to resume. Receipts (`GET /r/<run_id>`) are the only GET route, and they are private to the credential that ran them — an unknown run and someone else's run are the same `404`, so existence is never disclosed.

Full request/response typing: [openapi.json](/docs/openapi.json). Operating instructions: [SKILL.md](/SKILL.md). The measured case for the server: [agentNote.md](/docs/agentNote.md).
