# MCP Servers

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

> Add, edit and delete the MCP servers a project's gateways may serve: URL, shared credential, users' own tokens, the tool selector and tool discovery.

An MCP server is a project-wide record: where the server is, the credential Envoy sends it, whether users may send their own token, and which of its tools anyone may see. Adding a server serves nothing yet: a gateway must serve it and a consumer must be granted its tools ([Grants and quotas](https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas)).

![The MCP servers tab](https://www.elchi.io/docs/img/docs/aigw-mcp-servers-light.webp)

## What is stored where

| Part | Where it lives | Who sees it |
|---|---|---|
| **Name**, **Users' own tokens** mode, their token's header, the **Tools** selector | The project's AI gateway configuration; delivered to every edge's `conf.d` | Edges, the UI |
| **URL** and **Shared credential** | Sealed together in the control plane (encrypted with the provider key); generated into Envoy: the server's cluster, its egress route, a GenericSecret over SDS and a credential injector | The control plane and Envoy only. Never written to `conf.d`, never shown to agents, the credential's value never returned by the API |

Because URL and credential are sealed together, a stored credential is only ever sent to the URL it was entered for. Change the URL and you must enter the credential again.

## Add a server (UI)

1. Open **Infrastructure → AI Gateway → MCP servers** and click **New MCP server**. (project Owners and Admins only. The button is disabled until the project has a provider: "Add a provider first".)
2. **Server** section:
   - **Name**: lower-case letters, digits and hyphens, at most 63 characters, not `new`. Agents see the server's tools as `<name>__<tool>`. The name cannot be changed later: create another server and delete this one.
   - **URL**: the server's Streamable HTTP endpoint, for example `https://api.githubcopilot.com/mcp/`. The proxy calls it from each edge; agents never see it.
3. **Credentials** section (details below): choose **Users' own tokens** (**Shared only**, **Own, else shared**, **Own only**), and turn **Shared credential** on with **Header**, **Prefix** and **Value** if the server needs one.
4. **Tools** section (optional): **Only these tools**, **Or tools matching**, **Never these tools**, **Never tools matching**.
5. Click **Create and deploy**. The server's page opens at `/ai-gateway/mcp-servers/<name>`.

### URL rules

The UI and the backend refuse:

- anything but `https://` or `http://` (use `http://` only for a server on a trusted network);
- a URL without a host, with a user or password (`https://user:pass@…`: give the credential instead), or with a query or fragment (a token in a URL is logged: give it as the credential);
- a port out of range;
- a URL to the edge's own ports on a loopback or unspecified host (`localhost`, `*.localhost`, `127.x`, `127.1`, `0.0.0.0`, `::1`): the MCP egress `10088`, `9001`, `9002`, `9003`, `9064`, `6060`, and the proxy admin ports `30000`–`39999`.

Private network addresses are allowed: the edges call the server.

### Credentials

| **Users' own tokens** | API value | What the server receives |
|---|---|---|
| **Shared only** | `off` (default) | Every call carries the shared credential, if one is set. Users' own tokens are not forwarded. Without a shared credential, calls reach the server with no credential of the project: for a server that needs none. |
| **Own, else shared** | `optional` | The user's own token when the agent sends one, otherwise the shared credential. |
| **Own only** | `required` | Only the user's own token. The server has no shared credential (the backend refuses one). A user who sends no token gets no tools of this server: it is left out of their session. |

With **Own, else shared** or **Own only**, **Their token's header** names the header the user's token reaches the server in, as it was sent: `Authorization` (default; the user sends `Bearer ghp_…`) or the server's own (for example `X-API-Key`; the user sends the bare key). How users send it: [Personal tokens](https://www.elchi.io/docs/ai-gateway/mcp/personal-tokens).

**Shared credential** (a switch; off for a new server):

- **Header**: default `Authorization`. Not `host`, `connection`, `content-length`, `transfer-encoding`, `te`, `upgrade`, `keep-alive`, `proxy-connection` or a pseudo-header.
- **Prefix**: written before the value; default `Bearer ` with `Authorization`. Change the header to anything else and the UI empties the prefix (an `X-API-Key` server given `Bearer <key>` answers 401).
- **Value**: the secret. At most 8 KiB with the prefix, no line breaks. After saving it shows as **Stored**: leave it empty to keep it (same URL only).

With **Own, else shared** and a shared credential, both must use the **same header** (the user's token then takes the shared one's place). The backend refuses two different headers: both would reach the server and the server would choose which identity wins.

### The tool selector

The selector narrows the server's tools **for everyone**; each consumer is then granted tools on its own page.

| Field | API field | Rule |
|---|---|---|
| **Only these tools** | `tools.include` | Exact names. Empty: every tool. |
| **Or tools matching** | `tools.includeRegex` | Regular expressions. Not together with **Only these tools**: the names win and the patterns would be ignored, so a save with both is refused. |
| **Never these tools** | `tools.exclude` | Exact names. An exclusion always wins. |
| **Never tools matching** | `tools.excludeRegex` | Regular expressions. |

- Write tool names as the server lists them (`delete_repo`), **never** with the server prefix (`github__delete_repo`): that name matches nothing, so it is refused. Names: `A–Z a–z 0–9 _ . / -`, at most 128 characters.
- Patterns match **anywhere** in the name: anchor them with `^` and `$`. An `includeRegex` that is not anchored gets a warning; an `excludeRegex` that matches the empty string matches every name, and gets the warning that the server serves none.

## Discover the server's tools

On a saved server's page, the section **The server's tools** has a **List its tools** button.

- The **control plane** asks the server, the way the edges will: `initialize`, `notifications/initialized`, then `tools/list` page by page (at most 20 pages). It sends the shared credential, or, for a server that takes users' own tokens, the token typed in the field above the button (in the server's token header, as typed; used for this one listing and never stored).
- Each tool row has **Only these** and **Never** links that add the tool to the selector. Click **Save and deploy** to apply.
- Limits: 10 s per request, 30 s for the whole listing, 4 MiB per answer, redirects are not followed.
- **SSRF guard**: the request is dialled only after every resolved address is checked; loopback, link-local (cloud metadata services), unspecified and multicast addresses and the other known metadata addresses (also behind NAT64) are refused.
- If the control plane cannot reach the server (it may sit on a network only the edges reach), the listing fails with *Could not list its tools*: type the tool names by hand. The edges are not affected.

API (Owners and Admins):

```bash
curl -s -X POST "$ELCHI/api/v3/ai-gateway/mcp-servers/github/discover" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"project": "'$PROJECT'"}'
# with a user's own token for a server that takes one:
#   -d '{"project": "'$PROJECT'", "personal_token": "Bearer ghp_..."}'
```

Answer: `{"data": [{"name": "search_code", "description": "..."}, ...]}`. A server that refuses or does not answer: HTTP 502 with the server's reason in `message` (for example `initialize: the server answered 401: ...`).

## Add or edit a server (API)

```bash
curl -s -X PUT "$ELCHI/api/v3/ai-gateway/mcp-servers/github" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
        "project": "'$PROJECT'",
        "create": true,
        "server": {
          "name": "github",
          "tools": { "exclude": ["delete_repo"] }
        },
        "url": "https://api.githubcopilot.com/mcp/",
        "credential": { "header": "Authorization", "prefix": "Bearer ", "value": "ghp_..." }
      }'
```

| Body field | Meaning |
|---|---|
| `server.name` | Must equal the name in the path. |
| `server.personalToken` | `off` (default), `optional`, `required`. |
| `server.personalTokenHeader` | Only with `optional`/`required`; default `Authorization`. |
| `server.tools` | `include`, `includeRegex`, `exclude`, `excludeRegex`. |
| `url` | Required. |
| `credential` | Omit for no shared credential. `header` defaults to `Authorization` (then `prefix` to `Bearer `). Sent without `value` on an update: the stored value is kept, for the same URL only. |
| `create: true` | Create only: 409 if the name exists. |
| `expected_version` | Update only that version: 409 if it changed meanwhile. `0` creates or updates without a check. |

The answer carries the stored server (`credential` shows `header`, `prefix` and `set: true`, never the value), the deploy it queued, and the configuration's warnings. The whole project configuration is validated with the change applied: an invalid change is 400 naming every problem.

Read: `GET /api/v3/ai-gateway/mcp-servers?project=` (all) and `GET /api/v3/ai-gateway/mcp-servers/<name>?project=` (one); project members may read.

## Edit

Open the server's page, change fields, **Save and deploy**. Each save bumps the server's **Version**. A change goes to every edge of the project (selector, token mode) and into Envoy (URL, credential); a grant change reaches open sessions without reconnecting.

If someone saved the server since you opened it, the save is refused (*Not saved*, HTTP 409) and **Load the stored version** discards your edits and loads theirs.

**"Enter the URL and the credential again"**: shown when the server was sealed by another installation (a backup restored here). Until you enter both again, the edges cannot reach this server; every other server keeps serving.

## Delete

Click the delete icon in the list, or **Delete** on the server's page, and confirm *Delete MCP server `<name>`?* (*Its tools leave every gateway; its call history stays.*).

A server that a gateway serves or a consumer is granted cannot be deleted: the UI disables the button with *Used by gateway …, consumer …*, and the API answers **409** naming them. Take it off those gateways and consumers first.

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

## TLS to the server

For an `https://` URL Envoy on each edge opens TLS to the server:

- SNI = the URL's host name; for an IP address no SNI is sent.
- The server's certificate must chain to the edge's system CA bundle (`/etc/ssl/certs/ca-certificates.crt`) and carry a subject alternative name equal to the URL's host (DNS name, or IP address for an IP host).
- `Host` is rewritten to the URL's host (with the port when it is not the scheme's default), and the path to the URL's path. Some MCP servers refuse an unexpected `Host` (403), so the rewrite matters.

A server with a private CA or a certificate that does not name its host fails these checks: the call log shows the call as `unreachable`.

## Verify

- The list shows the server, its host, its credential mode (`shared`, `own, else shared`, `own only`; `· no shared credential` when none is set) and **Served by** (the gateways serving it; `no gateway` until one does).
- **List its tools** returns the tools you expect.
- After a gateway serves it and a consumer is granted tools, an agent's `tools/list` shows `<name>__<tool>` entries ([Connecting agents](https://www.elchi.io/docs/ai-gateway/mcp/connecting-agents)).
- In **MCP calls**, an `auth_error` outcome (*refused the credential*) means the server rejected the credential; `unreachable` means no connection (DNS, refused, TLS).
