# MCP Grants and Call Quotas

URL: https://www.elchi.io/docs/ai-gateway/mcp/grants-and-quotas

> Serve MCP servers on a gateway, grant consumers a server's tools, set MCP call quotas, and read what a refused call returns.

Three things decide whether an agent may call a tool. All three must hold:

1. **The gateway serves the server.** (Gateway → **MCP servers**.)
2. **The server's tool selector exposes the tool.** (MCP server → **Tools**, see [MCP servers](https://www.elchi.io/docs/ai-gateway/mcp/servers#the-tool-selector).)
3. **The consumer may use the gateway and holds a grant for the tool.** (Consumer → **Access → Gateways** and **MCP tools**.)

The edge decides this in one place (`AllowsTool`), and the same grants are rendered into the extproc's MCP authorization, so what `tools/list` shows and what `tools/call` accepts always agree.

## Serve servers on a gateway

**UI:** **AI Gateway → Gateways →** open the gateway (or **New gateway**) → section **MCP** → **MCP servers**: pick one or more servers → **Save and deploy**.

- Picking a server turns **Require a key** on and locks it (*"On while it serves MCP: each consumer is granted tools on its own page, so every call needs a consumer's key."*). Tools are granted per consumer, so an MCP gateway without a key is refused by validation: `serves MCP, so it must require a key (requireKey)`.
- Turning **Require a key** on applies to the gateway's LLM requests too: from then on every request to the gateway needs a consumer key (401 without one). Plan this before adding MCP to a gateway that serves keyless LLM traffic.
- A gateway serving MCP may list no model at all: it can be an MCP-only gateway.
- Agents reach the servers at `/mcp` on the gateway's hosts, on the AI listeners bound to the gateway.

**API:** the gateway spec carries `mcp.servers` and `requireKey: true`:

```bash
curl -s -X PUT "$ELCHI/api/v3/ai-gateway/gateways/agents" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
        "project": "'$PROJECT'",
        "expected_version": 0,
        "gateway": {
          "name": "agents",
          "hosts": ["agents.example.com"],
          "requireKey": true,
          "models": [],
          "mcp": { "servers": ["github", "jira"] }
        }
      }'
```

Validation refuses an empty `mcp.servers`, a server listed twice, and a server the project does not have (`gateway "agents": unknown MCP server "x"`).

## Grant tools to a consumer

![A consumer's MCP tools section](https://www.elchi.io/docs/img/docs/aigw-consumer-detail-light.webp)

**UI:** **AI Gateway → Consumers →** open the consumer → section **MCP tools**:

1. Click **Grant a server** and pick the **MCP server** (each server once per consumer).
2. In the tools field either leave it empty (placeholder **Every tool**) or type tool names. The search icon (*List the tools of `<server>`*) asks the server for its tools and offers them as suggestions; if the control plane cannot reach the server, type the names.
3. Optionally set **Tool calls per minute** and **Tool calls per day** (below).
4. **Save and deploy**.

| Grant | What the consumer gets |
|---|---|
| No grant for a server | No tool of that server. **Nothing is granted by default.** |
| Server, no tools listed | Every tool of the server that its selector lets through, including tools the server adds later. |
| Server, tools listed | Exactly those tools (if the selector lets them through). |

Rules for tool names in a grant: as the server lists them (`search_code`), never with the prefix (`github__search_code` is refused), `A–Z a–z 0–9 _ . / -`, at most 128 characters, each once.

Prompts and resources have no per-tool rules: a consumer's session reaches only the servers it holds a grant for, and their prompts and resources follow that server grant.

**API:** the consumer spec carries `mcp` (a list of grants) and the MCP quotas:

```bash
curl -s -X PUT "$ELCHI/api/v3/ai-gateway/consumers/dev" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
        "project": "'$PROJECT'",
        "expected_version": 0,
        "consumer": {
          "name": "dev",
          "mcp": [
            { "server": "github", "tools": ["search_code", "get_issue"] },
            { "server": "jira" }
          ],
          "quota": { "mcpCallsPerMinute": 60, "mcpCallsPerDay": 5000 }
        }
      }'
```

A PUT replaces the consumer's spec: send its existing `gateways`, `providers`, `models` and other quota fields too, or they are cleared.

### What `tools/list` shows

A consumer's `tools/list` contains a tool `<server>__<tool>` only when the gateway serves the server, the selector exposes the tool, the consumer is enabled, its **Gateways** list is empty or names this gateway, and it holds a grant for the server (whole or naming the tool). A disabled consumer's key is refused before (401).

Grant changes reach open sessions: once the edge has applied the change, an agent that lists again sees the new set without reconnecting.

### Warnings you may see

The configuration's warnings appear on the gateway, consumer and server pages and in the save answer. They do not block a save, but each means a setting does nothing:

| Warning | Meaning |
|---|---|
| `MCP server "s" of gateway "g": no enabled consumer is granted it, so none of its tools is listed` | Grant it to a consumer that may use the gateway. |
| `consumer "c" may use MCP server "s", which no gateway it may use serves: the grant allows nothing` | Serve the server on a keyed gateway the consumer may use. |
| `consumer "c" lists tool "t" of MCP server "s", which the server's tool selector hides: the entry allows nothing` | Widen the selector or drop the tool from the grant. |
| `consumer "c" has an MCP call quota but may use no MCP server` | Grant a server or remove the quota. |

## MCP call quotas

| Field (UI) | API (`quota.`) | Counts | Window |
|---|---|---|---|
| **Tool calls per minute** | `mcpCallsPerMinute` | `tools/call` messages; a JSON-RPC batch counts each call in it | Sliding minute, **on each edge** (N edges allow up to N × the limit) |
| **Tool calls per day** | `mcpCallsPerDay` | the same | **UTC day, every edge together**: each edge adds the other edges' calls from the ClickHouse call log |

- Empty or `0`: no limit.
- Only `tools/call` counts. `initialize`, `tools/list`, prompts, resources and the session's own requests do not.
- A call refused before the quota (not granted, sensitive data blocked, malformed) uses none of it.
- The token, request and cost quotas of the consumer do not apply on `/mcp`, and MCP calls do not use them.
- Without a ClickHouse DSN on the edges, the day quota is counted by each edge alone and restarts with the gateway.

## What a refused call returns

Every refusal on `/mcp` is a **JSON-RPC error**, with the HTTP status of the refusal. The `id` is the refused request's id (the first `tools/call` of a batch), or `null` when no request was read (a key refusal):

```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "error": {
    "code": -32000,
    "message": "consumer dev may not call tool github__sum on gateway agents",
    "data": { "code": "tool_not_allowed", "type": "permission_error", "status": 403 }
  }
}
```

| HTTP | `data.code` | When | What to do |
|---|---|---|---|
| 401 | `invalid_api_key` | No key, an unknown key, or a disabled consumer. | Send `Authorization: Bearer sk-elchi-...` of an enabled consumer. |
| 403 | `gateway_not_allowed` | The consumer's **Gateways** list does not name this gateway. | Add the gateway to the consumer, or connect to another. |
| 403 | `tool_not_allowed` | The tool is not granted, the gateway does not serve its server, the selector hides it, or the name has no `<server>__` prefix. | Grant it; check the selector. |
| 429 | `quota_exceeded` | Over **Tool calls per minute** or **per day**. The message names the quota and when it resets (UTC, RFC 3339); the `retry-after` header gives the seconds. | Wait, or raise the quota. |
| 400 | `invalid_request` | The body is not a JSON-RPC message or batch, or could be read two ways (a key repeated or written in another case, such as `"Method"` beside `"method"`, or data after the message). | Send one well-formed JSON-RPC message or batch. |
| 413 | | The body is larger than the gateway reads (4 MiB, or the sensitive-data policy's `maxScanBytes` if larger). | Send a smaller request. |
| 503 | `gateway_not_ready` | This edge's configuration does not serve MCP on the gateway yet (Envoy is ahead of the edge's set), or a POST arrived without its body. `retry-after: 5`. | Retry; if it lasts, check the edge in [Operations](https://www.elchi.io/docs/ai-gateway/operations). |
| 403 / 413 / 503 | `sensitive_data`, `sensitive_scan_*` | The sensitive-data policy refused the arguments. | See [Sensitive data on MCP](https://www.elchi.io/docs/ai-gateway/mcp/sensitive-data). |

A refused call never reaches the server and is not in the call log. Refusals are recorded in the decision log (`elchi_ai_gateway_decisions`): the MCP phase's (`tool_not_allowed`, `quota_exceeded`, `invalid_request`, the sensitive-data codes) with phase `mcp`, the consumer, the server as `provider` and the tool as `request_model`; the key phase's (`invalid_api_key`, `gateway_not_allowed`) with phase `key`. Refusals that name no consumer (a 401) are recorded at most 5 per second per gateway and code (bursts of 20); the rest are only counted:

```bash
curl -s "$ELCHI/api/v3/ai-gateway/decisions?project=$PROJECT&phase=mcp&from=2026-12-05T00:00:00Z&to=2026-12-05T12:00:00Z" \
  -H "Authorization: Bearer $TOKEN"
```

(Owners and Admins. The **Sensitive data** tab's decision list shows only the sensitive-data decisions.)

## Verify

1. Connect as the consumer ([Connecting agents](https://www.elchi.io/docs/ai-gateway/mcp/connecting-agents)) and run `tools/list`: only the granted `<server>__<tool>` names appear.
2. Call a tool that is not granted: HTTP 403 with `tool_not_allowed`.
3. Exceed **Tool calls per minute**: HTTP 429 with `quota_exceeded` and `retry-after`.
4. **MCP calls** shows the granted calls with outcome `200`; the refused ones are not there.
