# Personal Tokens for MCP Servers

URL: https://www.elchi.io/docs/ai-gateway/mcp/personal-tokens

> Let each user reach an MCP server with their own token through the AI gateway: the x-elchi-mcp-token headers, the server's token header, and what happens when the token is missing.

Some MCP servers should see the person behind the agent, not a shared project identity: a GitHub server acting with the user's own permissions, a ticketing server writing under the user's name. For those servers the AI gateway forwards a token the user sends, while the consumer key still decides what the user may call.

Two identities are involved, and they are separate:

| | Header the agent sends | Checked by | Reaches the MCP server? |
|---|---|---|---|
| **Consumer key** (`sk-elchi-...`) | `Authorization: Bearer sk-elchi-...` | The AI gateway: grants, quotas, call log | Never. The key phase removes it. |
| **Personal token** | `x-elchi-mcp-token-<server>` or `x-elchi-mcp-token` | The MCP server | Yes, in the server's token header, exactly as sent. Only to servers that take personal tokens. |

## Turn it on for a server

On the server's page (**AI Gateway → MCP servers →** server → **Credentials**), set **Users' own tokens**:

| Option | API `personalToken` | Without a personal token | With a personal token |
|---|---|---|---|
| **Shared only** | `off` (default) | The shared credential (if any). | Not forwarded: the server still gets the shared credential. |
| **Own, else shared** | `optional` | The shared credential (if any). | The user's token, in place of the shared one. |
| **Own only** | `required` | No credential reaches the server; the server is left out of the user's session (below). | The user's token. |

Then set **Their token's header** (API `personalTokenHeader`), the header the server takes the token in:

- `Authorization` (default): users send the value the server expects there, usually `Bearer <token>`.
- The server's own header, for example `X-API-Key`: users send the bare key.

The header must be a valid header name of at most 64 characters, and not one the connection, the MCP session or the gateway uses: `host`, `connection`, `keep-alive`, `proxy-connection`, `transfer-encoding`, `te`, `trailer`, `upgrade`, `content-length`, `content-type`, `content-encoding`, `accept`, `accept-encoding`, `expect`, `mcp-session-id`, `mcp-protocol-version`, `last-event-id`, `cookie`, `forwarded`, or anything starting with `x-elchi-`, `x-ai-eg-`, `x-envoy-`, `x-forwarded-`, `proxy-`. Setting it on a **Shared only** server is refused.

With **Own, else shared** and a shared credential, the shared credential must use the same header (the backend refuses two headers). Envoy injects the shared credential with `overwrite: false`, so when the user's token is already in that header, the user's token is what the server sees.

## How users send the token

Two headers carry a personal token:

| Header | Goes to |
|---|---|
| `x-elchi-mcp-token-<server>` (for example `x-elchi-mcp-token-github`) | That server only. Wins over the next one. |
| `x-elchi-mcp-token` | Every server of the gateway that takes personal tokens. |

A user with tokens for several servers sends one `x-elchi-mcp-token-<server>` per server. `x-elchi-mcp-token` suits a gateway where one token is valid for all such servers. Servers set to **Shared only** never receive either header.

**Send it on every request, from `initialize` on.** The extproc opens its session to each server when the agent sends `initialize`, with that request's headers. A token added only after `initialize` comes too late: the server's session was already opened (or refused) without it. MCP clients that take a fixed header list (Claude Code's `--header`, Cursor's `headers`) send it on every request, which is what is needed.

The **Connect an agent** section of an AI listener's page builds the line for you. For a gateway with a server that takes personal tokens it shows a second line, after *"A server of `<gateway>` takes users' own tokens: send yours on every request, from the first."*:

```bash
claude mcp add --transport http agents https://agents.example.com/mcp \
  --header "Authorization: Bearer <consumer key>" \
  --header "x-elchi-mcp-token-github: Bearer <your github token>"
```

For a server whose token header is not `Authorization`, the UI writes `<your <server> key>` (the bare key) instead of `Bearer <your <server> token>`. (The UI prints the command on one line; it is split here for reading.)

The same with curl, for the first request of a session:

```bash
curl -si https://agents.example.com/mcp \
  -H "Authorization: Bearer sk-elchi-..." \
  -H "x-elchi-mcp-token-github: Bearer ghp_..." \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl","version":"0"}}}'
```

Repeat the `x-elchi-mcp-token-github` header on every later request of the session. The full sequence: [Connecting agents](https://www.elchi.io/docs/ai-gateway/mcp/connecting-agents#test-with-curl).

## When the token is missing or wrong

- **Own only, no token sent:** the server's `initialize` reaches it with no credential. A server that requires authentication refuses it, and the extproc leaves that server out of the whole session: its tools are not in `tools/list`, and a call to one of them fails. The user's other servers work normally. Fix: send the token from `initialize` on and start a new session.
- **Own, else shared, no token sent:** the shared credential is used. Without a shared credential, the server gets no credential.
- **A token the server rejects:** it takes the shared credential's place in the header, so the shared credential is not sent instead. The server refuses `initialize` and is left out of the session, as above.

The **MCP calls** tab shows what the servers answered: `refused the credential` (`auth_error`, HTTP 401 or 403) points at the token.

## What Elchi keeps of a personal token

Nothing. The token travels from the agent through Envoy and the extproc to the server. It is not stored in the control plane, not delivered to `conf.d`, and not written to the call log (which records consumer, gateway, server, method, tool, status and duration only). When an admin lists a server's tools with their own token (**List its tools**), the token is used for that one listing and not stored.

## Verify

1. Connect with the personal token header set and run `tools/list`: the server's tools are listed.
2. Connect without it: for an **Own only** server they are missing.
3. In **MCP calls**, filter by the server: the calls show `200`, not `refused the credential`.
