MCP Through the AI Gateway
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. |
| 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. |
| 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. |
| 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. |
The request path
Step by step, as Elchi generates it per Envoy version:
- The gateway's
/mcproute. Every gateway that serves MCP gets a route matching the path/mcpon its virtual host. Envoy picks the gateway by the request'sHost, exactly as for LLM requests (see Listeners). The route has no timeout (timeout: 0s) and a 900 s idle timeout. - Key phase. The ext_authz key phase reads the consumer key (
Authorization: Bearer,x-api-keyorapi-key), setsx-elchi-consumerand strips the key headers: no MCP server ever sees an elchi key. A client's ownx-elchi-consumeris removed before this by the gateway's header filter. On/mcpthe key phase applies no token or request quota (tools are not model requests). - 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'smaxScanByteswhen that is larger). It refuses anytools/callthe 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). - The AI ext_proc is off on
/mcp. The request goes to the clusterelchi-aigw-mcp__proxy, the extproc's MCP proxy on/run/elchi-ai-gateway/mcp.sock. The route setsx-ai-eg-mcp-route: <gateway>andx-ai-eg-mcp-subject: <consumer>(a client's own values are overwritten), so a session belongs to its consumer. - 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>-mcpon127.0.0.1:10088(reuse_port, source restricted to127.0.0.0/8). Its route per server rewritesHostand the path to the server's URL, removes the proxy's routing headers, and sends the request to the server's cluster. - Credential injection. The server's cluster runs Envoy's
credential_injectorwith the server's shared credential from a GenericSecret over SDS, withoverwrite: false: a user's own token already in that header wins. - The answer back. When the gateway's sensitive-data policy scans results, the
elchi-ai-gateway-mcp-guardext_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:
curl -s "$ELCHI/api/v3/ai-gateway/mcp-readiness?project=$PROJECT" \
-H "Authorization: Bearer $TOKEN"
{ "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); 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.