---
name: mcpproxy-least-privilege-token
description: Create a least-privilege MCPProxy credential for an AI agent. Use when the user wants an agent, script, CI job or MCP client to reach only some upstream servers or only read tools through MCPProxy, asks for a scoped, read-only or revocable API key or agent token, or wants to stop using the MCPProxy admin API key in a client config. Builds a profile, mints a token pinned to it, writes the client config with an X-API-Key header, verifies the scope and explains how to revoke.
---

# MCPProxy least-privilege token

You are acting as the operator of a running MCPProxy. The goal is one credential
that reaches only the upstream servers and tool tiers a task needs.

The scope lives in a **profile** (servers, a tier cap, tool rules). A **token**
pinned to the profile carries it to the agent. Work through the steps in order
and stop at the first one that fails. Written against MCPProxy v0.76.0; step 1
checks that the installed version has what the steps need.

## Rules

- Never print, echo or paste a token or the admin API key into the conversation,
  a commit, a log or a URL. Tokens are minted with `scripts/mint_token.py`,
  which writes the secret to a mode-600 file and prints only its prefix.
  Do not run `mcpproxy token create`, `mcpproxy token regenerate`,
  `mcpproxy client add`, `mcpproxy client rotate`, `mcpproxy status --show-key`,
  `--web-url` or `--reset-key` yourself: they print secrets. When one of them
  is needed, the user runs it in their own terminal.
- Never put a credential in a URL (`?apikey=`). Use the `X-API-Key` header.
- When you show the user an existing client config entry, show key names only:
  the server name, the URL's scheme, host and path (never its userinfo, query
  string or fragment) and the header names. Never show header values, env
  values or args: they may hold the admin key.
- Ask the user before any of these: editing `mcp_config.json`, turning on
  `require_mcp_auth`, changing `mcpproxy profile anonymous`, changing a profile
  that already has clients or tokens, granting a `destructive` tool, or
  overwriting a client config entry.
- Do not approve tools, unquarantine servers or weaken any other gate to make
  a tool callable. If a needed tool is held, report it and stop.
- Do not run a write or destructive upstream tool to "test" a denial. Step 6
  verifies without calling any upstream tool.
- The `mcpproxy` CLI talks to the local daemon as an administrator. Use it only
  for the steps below.

## 1. Check the proxy

```bash
mcpproxy version
mcpproxy status -o json
mcpproxy upstream list
mcpproxy token create --help
```

Read `listen_addr` from the status output and tell the user which proxy you are
talking to. The MCP endpoint is `http://<listen_addr>/mcp`; if the proxy listens
on anything other than a loopback address, use its `https` URL instead and stop
if it has none. Call the result `<endpoint>`: every later step uses exactly that
URL.

That is the address as seen from this machine. If the agent will run somewhere
else (a container, a CI runner, another host), `127.0.0.1` there is not the
proxy. Ask the user for the URL the agent will use, which must be `https`
unless it is loopback from the agent's side, put that URL in the client config
in step 5, and say in the report that the checks in step 6 ran from here. If the `token create` help does not list `--profile`, the
version is too old (profiles with a tier cap need v0.70.0 or later): tell the
user to upgrade and stop.

## 2. Find what the task needs

Ask the user what the agent has to do if it is not clear. Then map the task to
servers and tools:

```bash
mcpproxy tools list --server <server>
mcpproxy tools list --server <server> -o json   # each tool has a "tier"
```

Tiers are `read`, `write` and `destructive`, taken from each tool's MCP
annotations; a tool with no annotations is `unannotated`. Write down:

- the servers the task needs (nothing else goes in the profile);
- the tier cap: the highest tier among the tools it needs. Prefer `read`. Use
  `write` only when the task changes something. Use `destructive` only when the
  user confirms it;
- tools inside the cap that the task must **not** use: these become deny rules.

If the task needs one tool above the cap (for example one `write` tool in an
otherwise read-only job), keep the low cap and add an allow rule for that one
tool, by its exact `server:tool` id. An allow rule hides nothing, and it admits
its tool past the cap and past the unannotated rule, so check that tool's
annotations and keep the list short.

A profile scopes servers and tools, not arguments. It cannot limit which paths,
repositories or records a tool touches. So find out what each server in the
scope can itself reach. Ask the user (which folder, which account, which
database); do not dump a server's launch arguments, environment or config into
this conversation to find out, because they can hold secrets. A read-only tool
on a filesystem server rooted at the home
directory can read MCPProxy's own `mcp_config.json`, admin key included, and
every other secret there. If a server reaches more than the task needs, stop
and tell the user: the fix is in that server's configuration (a narrower root,
a narrower account), not in the profile.

## 3. Create the profile

Run `mcpproxy profile list`. Reuse a profile only if `mcpproxy profile show
<slug>` shows exactly the scope from step 2: same servers, same cap, same rules,
`Unannotated: deny`, code execution off, management tools off. Otherwise create
a new one. The name is a lowercase slug.

Use the same values in the preview and in the create command. Preview first
(nothing is saved):

```bash
mcpproxy profile try <slug> --query "<a few words from the task>" \
  --set servers=<a,b> --set max_tier=<cap> --set unannotated=deny \
  --set code_execution=false --set management_tools=false \
  --set tools.allow=<server:tool,...> --set tools.deny=<server:tool,...>
```

Leave out a `tools.*` line you have no rules for.

Create it, adding `--allow <server:tool>` and `--deny <server:tool>` for the
rules from step 2:

```bash
mcpproxy profile create <slug> \
  --title "<Short title>" \
  --servers <a,b> \
  --max-tier <cap> \
  --unannotated deny \
  --code-execution off \
  --management-tools off
```

Setting the last three explicitly matters: with a `destructive` cap the
defaults would treat unannotated tools as read and inherit code execution.

Review everything the profile admits, not only the tools you need:

```bash
mcpproxy profile show <slug> --effective
```

Show the user the summary line and every `callable` tool. Each tool the task
needs must read `callable`. If one reads `hidden`, the `REASON` column says
which profile setting to change. If one reads `held`, a later gate (tool
approval, server state) is holding it: report that and stop, do not change the
gate. If a tool the task does not need is `callable` or `held`, add a deny rule
for it: a held tool becomes callable the day someone approves it.

## 4. Mint the token

Know what a token is before minting one. An agent token is also accepted on the
REST API, scoped to the same servers: its holder can read those servers'
launch command and arguments, their logs and their activity records
(`GET /api/v1/servers`, `/api/v1/servers/<name>/logs`, `/api/v1/activity`).
`--management-tools off` hides the MCP tools, not these REST routes. The
activity records include calls that other callers made to those servers. If an
in-scope server's arguments, logs or recorded tool calls can contain secrets
and the agent only
needs MCP, tell the user and use the per-client credential instead: REST
refuses it. That is the `mcpproxy connect` route in step 5 for a supported
client. For a script, the user runs `mcpproxy client add <id> --profile <slug>
--expires-in <duration>` in their own terminal (it prints the secret once) and
stores it themselves, for example in their secret manager or CI secret store.
Choose the duration as for a token, below: without the flag the credential
lasts 365 days. In step 5 the script then sends that stored `mcp_cli_…` value
as the `X-API-Key` header; there is no token file on this route.
Either way it is a client credential: skip the rest of this step and use the
`--client` forms of the checks, audit, rotation and revocation given at the
end of step 5, with `<id>` as the client. The `token` commands in steps 6 and
7 do not apply to it.

Pick a name that identifies the agent (one token per agent, never shared) and
the shortest expiry that fits: hours for a one-off task, `7d` to `30d` for a
standing agent. A duration can be at most `365d`; do not use `never` for an agent.

```bash
python3 scripts/mint_token.py --name <agent-name> --profile <slug> --expires 7d \
  --out ~/.config/mcpproxy/tokens/<agent-name>
```

(`scripts/` is next to this file.) The script refuses to overwrite an existing
file, writes the secret with mode 600, prints only the prefix, and revokes the
token again if it could not be saved. Then confirm the record:

```bash
mcpproxy token show <agent-name>
```

`Servers: *` and all three permissions in that output are expected for a
profile-pinned token: the profile is the bound.

## 5. Write the client config

Use the header form for the client the user named, with the endpoint from
step 1. Keep the secret out of any file that is committed.

Claude Code, project `.mcp.json`. Claude Code expands `${VAR}` here:

```json
{
  "mcpServers": {
    "mcpproxy": {
      "type": "http",
      "url": "http://127.0.0.1:8080/mcp",
      "headers": { "X-API-Key": "${MCPPROXY_TOKEN}" }
    }
  }
}
```

Codex, `~/.codex/config.toml`:

```toml
[mcp_servers.mcpproxy]
url = "http://127.0.0.1:8080/mcp"
env_http_headers = { "X-API-Key" = "MCPPROXY_TOKEN" }
```

For both, tell the user to export the variable in the shell that starts the
client. Not in a traced shell (`set -x`) or a CI step that echoes commands: the
expanded value would be logged. In CI, use the runner's secret store.

```bash
export MCPPROXY_TOKEN="$(cat ~/.config/mcpproxy/tokens/<agent-name>)"
```

Any other client: check its own documentation for how it sets an HTTP header
and whether it expands environment variables. If it does not, the header value
has to be the literal token, in a file that is mode 600 and not committed. Do
not put the literal value there yourself: an edit made through your tools
carries the secret through this conversation. Write the entry with a
placeholder and have the user paste the value from the token file.
Anything that speaks HTTP can send `X-API-Key: <token>` or
`Authorization: Bearer <token>` to the `/mcp` URL.

Before writing, find **every** entry in the client's config that points at this
proxy: any name, any spelling of the address (`127.0.0.1`, `localhost`, a
hostname), any `/mcp` path, in every config scope the client reads (user and
project). An old entry with the admin key, or with any other credential that
reaches more than this profile, keeps the client that wide no matter what the
new entry says. Do not open the config with a tool that returns its raw
content: header values would land in this conversation. Use `mcpproxy connect
--list` and `mcpproxy doctor` first, and if the file itself must be inspected,
ask the user to do it or print key names only (see Rules). Ask before replacing
or removing an entry, and do not report the client as scoped while a wider
entry remains.

Alternative for an interactive client that `mcpproxy connect --list` shows,
when `<endpoint>` is the proxy's own loopback URL (connect writes the address
the daemon listens on, not a URL you choose):
`mcpproxy connect <client> --profile <slug>` mints a per-client credential
locked to the profile and writes the config itself. Know what it does before
offering it: it writes the literal credential into the client's config file,
needs `--force` to replace an existing entry, and is refused while keyless
callers could reach more than the profile (see the `require_mcp_auth` note in
step 6). Decide on this route before step 4, because it is a client credential,
not a token, and steps 4, 6 and 7 change: no token is minted; check the policy
with `mcpproxy access explain --client <client> --tool <server:tool>`; then
have the restarted client make one `retrieve_tools` call and confirm that a
fresh record shows up in `mcpproxy activity list --client <client>` (the
client may read a different config file than the one that was written).
Revoke with `mcpproxy client forget <client>`. Rotate with `mcpproxy client
rotate <client>`, run by the user: for a supported client it rewrites the
config; for a custom one (`client add`) it prints the new secret once and
leaves both secrets valid for at most 24 hours: within that window the user
installs the new one, checks that the client works, and runs `mcpproxy client
rotate <client> --finalize`.

## 6. Verify

Admin side, no call is made. Pick real tools from the lists in step 2: one the
task needs, and at least one the token must not reach. Cover both kinds of
denial when both exist: a tool above the cap (or denied by a rule), and a tool
on a server outside the profile. If the proxy has no other server, skip that
probe and say so in the report. Take every name from `mcpproxy tools list
--server <server>`; never invent a tool name for a denial check, because a
misspelled name is "not found" too. If there is no real tool outside the scope
at all, run the script with `--no-denied-probe` instead of `--denied` and
report that denial of upstream tools was not tested.

```bash
mcpproxy access explain --token <agent-name> --tool <server:needed_tool>
mcpproxy access explain --token <agent-name> --tool <server:tool_above_the_cap>
mcpproxy access explain --token <agent-name> --tool <other_server:any_tool>
```

Expect `VERDICT: allowed` for the first, and `hidden` or `blocked` at the gate
you intended (`tier_cap`, `tool_rule`, `server_in_scope`) for the others.

Agent side, with the token itself and the same tools. The only tool the script
calls is `describe_tool`, which never reaches an upstream. It also sends the
token to `GET /api/v1/config` on the same host and port to check for a `403`,
so `<endpoint>`'s host must be MCPProxy itself, not a gateway that routes
`/api` to something else:

```bash
python3 scripts/verify_scope.py --url <endpoint> \
  --token-file ~/.config/mcpproxy/tokens/<agent-name> \
  --allowed <server:needed_tool> \
  --denied <server:tool_above_the_cap> --denied <other_server:any_tool>
```

Every check line must be `PASS` and the last line `RESULT OK`. Otherwise the
token is not verified: say so, and do not hand it over as done.

- `FAIL no credential -> accepted` means `require_mcp_auth` is off: any client
  that omits the header gets in without the token, and the probe cannot tell
  whether the proxy honoured the token at all. Explain this and let the user
  decide. The fix is `"require_mcp_auth": true` in the MCPProxy config. Every
  MCP client then needs a credential, so the user must give the other clients
  theirs first. The weaker alternative keeps keyless access and confines it:
  check the current setting with `mcpproxy profile anonymous`, and only if the
  user agrees to change it for **all** keyless callers, set it to a profile that
  is at least as narrow as this one **and** has no `switchable_to` (a keyless
  caller could otherwise switch to a wider profile; `mcpproxy profile show
  <slug>` prints it). Then re-run the script with `--allow-anonymous`. It must
  show no `FAIL`; it ends in `RESULT NOT VERIFIED` (exit code 2), because in
  this setup the probe shows what the profiles admit, not that the proxy
  recognised the token. Report it to the user in exactly those terms.
- `FAIL administrator tools are listed` means the credential is the admin key,
  not the token. Check which value the file or variable holds.

This probes the token, not the client. Starting the client proves nothing by
itself: many clients do not touch an MCP server until they need it. After the
user starts the client, have that client make one harmless call, a
`retrieve_tools` search on the `mcpproxy` server (it is built in and reaches no
upstream). Then check that the call was recorded under the token:

```bash
mcpproxy activity list --token <agent-name> --limit 5
```

A fresh `retrieve_tools` record with that client's name must be there. If it is
not, the client is not sending this token: do not report the client as set up.

## 7. Report and hand over

Tell the user, in a few lines:

- the profile name, its servers, tier cap and rules, and the callable/hidden
  counts; say so if the profile is shared with other clients or tokens;
- the token name, its expiry and where the secret is stored (path, not value);
- the config file you wrote and the variable to export;
- the verification result, including anything that was not verified;
- the limit of what was verified: the tools the token can and cannot see over
  MCP, and that it cannot read the admin config. `RESULT OK` says nothing
  about the REST reads of in-scope servers described in step 4. If the agent
  that will use the token can run commands or read files as the OS user that
  runs MCPProxy (a local coding agent usually can), say that it is **not**
  confined by the token, for the reason in the last section;
- how to audit, rotate and revoke:

```bash
mcpproxy activity list --token <agent-name>                    # the 50 most recent records
mcpproxy activity list --token <agent-name> --status blocked   # refused by policy
mcpproxy activity list --token <agent-name> --status error     # failed calls
python3 scripts/mint_token.py --regenerate --name <agent-name> \
  --out ~/.config/mcpproxy/tokens/<agent-name>                 # rotate: new secret, same profile and expiry
mcpproxy token revoke <agent-name>                             # stops on the next request
mcpproxy token delete <agent-name>                             # removes the record
```

After a rotation the old secret is dead at once and only the token file has
the new one. A client that reads `MCPPROXY_TOKEN` needs the variable exported
again from the file and a restart. A client whose config holds the literal
token needs that header value replaced by the user (never through your tools,
which would carry the secret through this conversation) and a restart. Then run step 6 again, including the client check.

To widen the scope later, change the profile; the token follows it without
being reissued. A saved change is live on the next request, for every client
and token on the profile, so preview first and save once:

```bash
mcpproxy profile try <slug> --query "<a few words from the task>" --set <field>=<value>
```

- **One more tool.** If a deny rule matches it, lift that rule first (deny
  beats allow): `--remove-deny <server:tool>` for an exact rule. If the match
  is a glob, replace the glob with exact deny rules for the other tools it
  covers, and tell the user the cost: exact rules do not cover a tool the
  server adds later. If that matters, keep the glob and make a separate
  profile. Then add `--add-allow <server:tool>` only if the cap still hides
  the tool.
- **A higher cap.** Prefer exact allow rules: `--max-tier write` admits every
  write tool on every server of the profile at once. If the cap must go up,
  save it together with deny rules for the tools that are not needed, in one
  `mcpproxy profile update`.
- **One more server.** `--add-server <name>` admits all of its tools that pass
  the cap and the rules, and lets a token read that server's arguments, logs
  and activity over REST (step 4). Go through steps 2 and 3 for the new server
  and save its deny rules in the same update.

Afterwards check `mcpproxy profile show <slug> --effective` before saying that
access changed.

If a client config held the admin API key: once **no** client or script uses
that key any more (`mcpproxy doctor` lists the client configs it knows that
still hold it), the user should rotate it with `mcpproxy status --reset-key`.
That command prints the new admin key, so the user runs it in their own
terminal; never run it yourself. Removing the key from one file does not
invalidate copies.

## What this does not protect against

Include this in the hand-over whenever it applies, not only when asked. The token confines
what an agent can do **through MCPProxy**. An agent that can run commands as the
OS user that runs the proxy can use the `mcpproxy` CLI over the local socket, or
read the proxy's data directory, and act as an administrator. For that case the
agent needs to run somewhere that cannot reach the socket or read the data
directory: another user account or machine, or a container that does not mount
them. A sandbox that only confines writes is not enough. Tiers come from each
server's own annotations, so a server that mislabels a tool can get it past a
tier cap.
