Connecting Agents to MCP
An agent connects to a gateway's MCP endpoint like to any remote MCP server: one Streamable HTTP URL and one header with a consumer key.
| Value | |
|---|---|
| URL | https://<gateway host>/mcp (the listener's HTTPS port when it is not 443; http:// only on a listener without HTTPS) |
| Header | Authorization: Bearer sk-elchi-... (a key of a consumer granted tools on that gateway) |
| Optional headers | x-elchi-mcp-token-<server>: ... for servers that take users' own tokens (Personal tokens) |
Before you start
- The gateway serves at least one MCP server and is bound to an AI listener (Grants and quotas, Listeners).
- A consumer with a grant on that gateway, and one of its keys. A key is shown once, when it is created on the consumer's page (Consumers and keys).
- The gateway's host resolves to the edge (or your load balancer in front of it), and the agent can reach the listener's port.
- MCP is served in the project: the MCP servers tab shows no "MCP is not served yet" warning (Overview).
Get the exact command from the UI
AI Gateway → Listeners → open the listener → section Connect an agent ("Its gateways that serve MCP, as an agent adds them (Claude Code shown; any MCP client takes the same URL and header). Use a consumer's key; the host must reach this listener."). It shows one line per gateway of the listener that serves MCP, with a copy button.
How the UI builds the URL:
| Part | Rule |
|---|---|
| Scheme and port | https and the HTTPS port when the listener has HTTPS; else http and the HTTP port, unless the plain HTTP entry only redirects (then nothing is shown). The port is left out when it is 443 for https or 80 for http. |
| Host | The gateway's first exact host. If it has only wildcard hosts, the first one with * replaced by <name> (fill in a name the wildcard matches). If it has no host and is the listener's default gateway, the listener's first address (an IPv6 address in brackets). |
| Path | /mcp |
A gateway with no host that is not the listener's default cannot be reached at /mcp: the UI says "<gateway> has no host and is not this listener's default: give it a host to reach it at /mcp." Envoy selects the gateway by Host, so a listener's address reaches only its default gateway.
Claude Code
The UI line, with your consumer key in place of <consumer key>:
claude mcp add --transport http agents https://agents.example.com/mcp --header "Authorization: Bearer <consumer key>"
The server is registered under the gateway's name (agents here). For a gateway with a server that takes users' own tokens the UI shows a second line with one more --header per such server:
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>"
Check it with claude mcp list. Inside a session the tools appear as <server>__<tool> of the server agents.
Cursor
Add the gateway to ~/.cursor/mcp.json (every project) or .cursor/mcp.json (one project):
{
"mcpServers": {
"agents": {
"url": "https://agents.example.com/mcp",
"headers": {
"Authorization": "Bearer sk-elchi-...",
"x-elchi-mcp-token-github": "Bearer ghp_..."
}
}
}
}
Leave out the x-elchi-mcp-token-* line when no server of the gateway takes personal tokens. Cursor's ${env:NAME} interpolation keeps the key out of the file: "Authorization": "Bearer ${env:ELCHI_MCP_KEY}".
Any other MCP client
Choose the Streamable HTTP transport (some clients call it http), give the URL, and add the Authorization header (and any x-elchi-mcp-token-* headers) to every request, the first initialize included. The client must keep the Mcp-Session-Id the gateway returns and send it on every later request, as the MCP specification requires.
Test with curl
The four steps of a session. Replace the host and key. Answers come either as JSON or as Server-Sent Events (Content-Type: text/event-stream, the message on a data: line); both are normal.
1. initialize (and keep the session id):
GW=https://agents.example.com/mcp
KEY=sk-elchi-...
H=(-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream")
curl -si "$GW" "${H[@]}" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl","version":"0"}}}'
Expected: status 200, an Mcp-Session-Id: ... response header, and a result with the server capabilities. Copy the session id:
SID=<the Mcp-Session-Id value>
2. notifications/initialized (no id, so no JSON-RPC answer):
curl -si "$GW" "${H[@]}" -H "Mcp-Session-Id: $SID" \
-d '{"jsonrpc":"2.0","method":"notifications/initialized"}'
3. tools/list:
curl -s "$GW" "${H[@]}" -H "Mcp-Session-Id: $SID" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
Expected: result.tools[] with one entry per tool this consumer may call, named <server>__<tool>:
{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"github__search_code","description":"...","inputSchema":{...}}]}}
4. tools/call:
curl -s "$GW" "${H[@]}" -H "Mcp-Session-Id: $SID" \
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"github__search_code","arguments":{"query":"elchi"}}}'
Expected: result.content[] with the tool's answer. A tool that failed on its own side answers a result with "isError": true: that is the server's answer, not a gateway refusal.
If DNS does not point the host at the edge yet, keep the URL's host and pin the address: curl --resolve agents.example.com:443:<edge IP> ....
What goes wrong, and what you see
| You see | Cause | Fix |
|---|---|---|
HTTP 401, data.code: "invalid_api_key" | No key, a wrong key, or a disabled consumer. | Send Authorization: Bearer <consumer key>. |
HTTP 403, gateway_not_allowed | The consumer's Gateways list does not name this gateway. | Add it on the consumer's page. |
HTTP 403, tool_not_allowed | The tool is not granted, the selector hides it, or the name lacks <server>__. | Grants and quotas. |
HTTP 429, quota_exceeded | Over the consumer's tool-call quota. | Wait retry-after seconds, or raise the quota. |
HTTP 503, gateway_not_ready | This edge has not applied the configuration yet. | Retry after retry-after; check the edge's status in Operations. |
tools/list lacks a server's tools | No grant for that server; the gateway does not serve it; a Own only server and no personal token from initialize on; or the server refused the credential and was left out of the session. | Check the grant; send the personal token; look at MCP calls for refused the credential. |
A tool call answers with a JSON-RPC error sensitive_data | The sensitive-data policy blocked the arguments (HTTP 403) or the result (HTTP 200). | Sensitive data on MCP. |
| Calls fail with HTTP 500 while an edge restarts | Calls in flight on a stopping Envoy or a restarting AI gateway are cut. | Retry. |
| Tool calls reach no server; MCP servers shows "MCP is not served yet" | An edge of the project runs an AI gateway older than v0.4.0, so no listener has the MCP egress. | Upgrade the edges named in the warning. |
| A long call is cut after 15 minutes | The MCP response deadline (--mcp-write-timeout, default 15m). | Make the call shorter, or raise --mcp-write-timeout (env ELCHI_AI_GATEWAY_MCP_WRITE_TIMEOUT) on the edges. |
Every call that reaches a server is in MCP calls (Call log); refusals by the gateway are in the decision log instead.