# MCP Through the AI Gateway

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

> How Elchi AI Gateway serves MCP servers' tools to agents at /mcp on an AI listener: one connection per agent, per-consumer grants and call quotas, credentials kept in the control plane and the proxy, a call log and a sensitive-data scan.

A gateway of Elchi AI Gateway can serve the tools of one or more **MCP (Model Context Protocol) servers**. An agent (Claude Code, Cursor, your own MCP client) connects once, to `/mcp` on the gateway's host, with a consumer key. It sees the tools of every server the gateway serves that its consumer was granted, and every call it makes is checked, counted and logged on the edge.

## What you get

| | What happens |
|---|---|
| **One connection per agent** | The agent adds a single Streamable HTTP endpoint: `https://<gateway host>/mcp`. The engine (the Agent Router extproc's MCP proxy) opens a session to each server behind it. |
| **Tool aggregation** | The tools of all the gateway's servers are listed together, each named `<server>__<tool>` (for example `github__search_code`). |
| **Per-consumer grants** | Nothing is granted by default. A consumer gets a server's tools one server at a time: every tool, or the tools you name. `tools/list` shows a consumer only what it may call; any other `tools/call` is refused with 403 `tool_not_allowed` before it reaches the extproc. See [Grants and quotas](https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas). |
| **Call quotas** | `Tool calls per minute` (counted on each edge) and `Tool calls per day` (UTC day, every edge together). Over the quota: 429 `quota_exceeded` with `retry-after`. |
| **Credentials stay out of the edge's files** | A server's URL and shared credential are stored sealed in the control plane and generated into Envoy (a cluster per server and a credential injector reading a secret over SDS). They never reach the AI gateway's `conf.d`, and agents never see them. Users can also send their own token per server: [Personal tokens](https://www.elchi.io/docs/ai-gateway/mcp/personal-tokens). |
| **Call log** | One row per request the proxy sends to a server (consumer, gateway, server, method, tool, status, outcome class, duration) in ClickHouse, shown in the **MCP calls** tab. No arguments or results are kept. See [Call log](https://www.elchi.io/docs/ai-gateway/mcp/call-log). |
| **Sensitive-data scan** | The project's sensitive-data policy also reads what agents send to the servers (tool arguments) and what the servers send back (tool results). See [Sensitive data on MCP](https://www.elchi.io/docs/ai-gateway/mcp/sensitive-data). |

## The request path

```mermaid
sequenceDiagram
    autonumber
    participant A as Agent (MCP client)
    participant E as Envoy: AI listener, gateway vhost, route /mcp
    participant Z as authz.sock (key phase, MCP phase)
    participant G as authz.sock (mcp-guard result scan)
    participant X as extproc MCP proxy (mcp.sock)
    participant L as Envoy MCP egress 127.0.0.1:10088
    participant S as MCP server
    A->>E: POST /mcp  Authorization: Bearer sk-elchi-...
    E->>Z: key phase: which consumer? (sets x-elchi-consumer, removes the key)
    E->>Z: MCP phase: the JSON-RPC body (grant, argument scan, call quota)
    E->>X: request (x-ai-eg-mcp-route = gateway, session bound to the consumer)
    X->>L: http://127.0.0.1:10088 (x-ai-eg-mcp-backend = server)
    L->>S: Host and path rewritten, TLS, shared credential injected
    S-->>L: result (JSON or SSE)
    L-->>X: result (access log to authz.sock: the call log row)
    X-->>E: merged answer
    E->>G: response streamed through the result scan (when the policy scans results)
    E-->>A: answer
```

Step by step, as Elchi generates it per Envoy version:

1. **The gateway's `/mcp` route.** Every gateway that serves MCP gets a route matching the path `/mcp` on its virtual host. Envoy picks the gateway by the request's `Host`, exactly as for LLM requests (see [Listeners](https://www.elchi.io/docs/ai-gateway/listeners)). The route has no timeout (`timeout: 0s`) and a 900 s idle timeout.
2. **Key phase.** The ext_authz key phase reads the consumer key (`Authorization: Bearer`, `x-api-key` or `api-key`), sets `x-elchi-consumer` and strips the key headers: no MCP server ever sees an elchi key. A client's own `x-elchi-consumer` is removed before this by the gateway's header filter. On `/mcp` the key phase applies no token or request quota (tools are not model requests).
3. **MCP phase.** The model-phase ext_authz runs as the MCP phase on this route (`context_extensions.aigw_phase: mcp`) with the request body (up to 4 MiB, or the sensitive-data policy's `maxScanBytes` when that is larger). It refuses any `tools/call` the consumer is not granted, scans the arguments when the policy covers them, then counts the calls against the consumer's MCP quotas. Refusals are JSON-RPC errors (see [Grants and quotas](https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas#what-a-refused-call-returns)).
4. **The AI ext_proc is off on `/mcp`.** The request goes to the cluster `elchi-aigw-mcp__proxy`, the extproc's MCP proxy on `/run/elchi-ai-gateway/mcp.sock`. The route sets `x-ai-eg-mcp-route: <gateway>` and `x-ai-eg-mcp-subject: <consumer>` (a client's own values are overwritten), so a session belongs to its consumer.
5. **The loopback egress.** The proxy calls every server through one address, `http://127.0.0.1:10088`, naming the server in a header. Every AI listener carries a last entry `<listener>-mcp` on `127.0.0.1:10088` (`reuse_port`, source restricted to `127.0.0.0/8`). Its route per server rewrites `Host` and the path to the server's URL, removes the proxy's routing headers, and sends the request to the server's cluster.
6. **Credential injection.** The server's cluster runs Envoy's `credential_injector` with the server's shared credential from a GenericSecret over SDS, with `overwrite: false`: a user's own token already in that header wins.
7. **The answer back.** When the gateway's sensitive-data policy scans results, the `elchi-ai-gateway-mcp-guard` ext_proc reads the response as it streams. The egress's access log goes to the AI gateway's authorization socket and becomes the call-log row.

The installer's nftables rules let only the AI gateway's user connect to `127.0.0.1:10088` and only Envoy's user answer from it, so no other local process can use the egress and its injected credentials. They load at boot from `elchi-ai-gateway-firewall.service`.

## Requirements

- **The AI gateway configuration exists in the project.** In the UI the **New MCP server** button is disabled until a provider exists ("Add a provider first").
- **A gateway serving MCP requires a key.** Serving an MCP server turns **Require a key** on and locks it on; the schema refuses an MCP gateway without `requireKey`.
- **The gateway is bound to an AI listener**, and the agent reaches it by one of the gateway's hosts (or by the listener's address when the gateway is that listener's default gateway).
- **Every edge of the project runs AI gateway v0.4.0 or later** (`MinMCPGatewayVersion`). The edge agent reports the sidecar's version in its registration metadata (`ai_gateway_version`). Two things follow from it:
  - An edge reporting an older version gets its configuration **without MCP** (no servers, no MCP gateways, no grants, no MCP quotas); its LLM gateways keep working.
  - While **any** client of the project (connected or not) reports a version older than v0.4.0, **no AI listener of the project gets the MCP egress entry**, so no tool call reaches a server anywhere. An older gateway has no firewall on the egress port. A client whose version is unknown (for example the gateway restarting) does not hold MCP back.
- **ClickHouse** for the call log and for the day quota across edges. Without a ClickHouse DSN on the edge nothing is written to the call log and each edge counts the day quota on its own.

### Which edges hold MCP back

The **MCP servers** tab shows a warning when MCP is not served yet: *"MCP is not served yet: an edge runs an AI gateway older than v0.4.0"*, naming the clients. The same answer from the API:

```bash
curl -s "$ELCHI/api/v3/ai-gateway/mcp-readiness?project=$PROJECT" \
  -H "Authorization: Bearer $TOKEN"
```

```json
{ "data": { "ready": false, "holding": ["e6099922..."], "min_version": "v0.4.0" } }
```

`ready: true` with an empty `holding` means every edge takes MCP. To clear a holding edge, upgrade the AI gateway on it (see [Installation](https://www.elchi.io/docs/ai-gateway/installation)); once it re-registers with the new version, the next deploy adds the egress to the listeners.

## Limits

| Limit | Value | What happens past it |
|---|---|---|
| A response's whole duration (a tool call, an open SSE stream) | 15 minutes (`--mcp-write-timeout`, env `ELCHI_AI_GATEWAY_MCP_WRITE_TIMEOUT`) | The extproc ends the response. The upstream default of 120 s cut a 150 s call (the server answered, the client got 503) and closed every SSE stream at 121 s; Elchi passes 15 minutes instead. |
| Silence on `/mcp` and on the egress | 900 s idle timeout | Envoy closes the stream. |
| A request body on `/mcp` | 4 MiB (or the policy's `maxScanBytes`, if larger) | HTTP 413; the request reaches no server. |

**Restarts.** A stop of Envoy without a drain, or a crash, fails the calls in flight on that Envoy's egress (500); the extproc does not retry them on another Envoy. The edge agent's unit stop waits for MCP calls in flight on the egress, within the drain timeout. When the result scan is on, an AI gateway restart cuts the calls in flight with a 500, whatever the failure mode.

**Sessions.** The session id is encrypted with a project-wide seed that the control plane derives (it is stored nowhere), so a session survives a change of edge. A changed JWT secret on the control plane changes the seed: open sessions then start again. A session is bound to its consumer: another consumer's key cannot use it.

## Next

1. [Add an MCP server](https://www.elchi.io/docs/ai-gateway/mcp/servers).
2. [Serve it on a gateway and grant tools to consumers](https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas).
3. [Connect an agent](https://www.elchi.io/docs/ai-gateway/mcp/connecting-agents).
