# Keep your API keys out of your AI agent: a credential pattern for MCP servers

> Source: <https://dev.to/rojaneerdev/keep-your-api-keys-out-of-your-ai-agent-a-credential-pattern-for-mcp-servers-4cmo>
> Published: 2026-09-27 11:08:31+00:00

If you've wired an MCP server into an agent, you've probably done something like this:

```
{
  "mcpServers": {
    "billing": {
      "command": "npx",
      "args": ["billing-mcp"],
      "env": { "BILLING_API_KEY": "sk-live-..." }
    }
  }
}
```

It works. It's also handing your live billing key to the least trustworthy process in the system.

An agent is a program that decides what to do at runtime based on text it was given — some of which comes from the outside world (a webpage it read, a document it summarized, a tool result). That's the whole point, and it's also why the agent process is the wrong place to keep a secret.

Two things go wrong with the config above:

**The credential lives in the agent's environment.** If the agent is compromised — prompt injection, a poisoned dependency, a tool that returns a malicious payload — the attacker is now one `os.environ` read away from your billing key. The blast radius of "the agent did something dumb" includes "the agent's keys are gone."

**The agent can call everything, and you can't prove what it did.** The MCP server exposes a set of tools; the agent can call any of them. When something goes wrong, your evidence is scattered across logs that the agent itself could have influenced.

You can't fix this by making the agent more careful. The agent is the untrusted part. You fix it by moving the trust boundary.

Instead of letting the agent talk to the MCP server directly, put a small trusted process — a gateway — in the middle:

```
agent  ──►  gateway  ──►  MCP server
           (holds the       (needs the
            credential)      credential)
```

The agent is *starved* of credentials. It can make tool calls, but it never possesses the secret that authorizes them. Compromise the agent and you get a revocable, per-run token — not the billing key.

This is more than a reverse proxy, because the gateway is a policy decision point. Since every call goes through it, it can also:

Here's the pattern implemented with [Agenthof](https://github.com/agenthof/agenthof), an open-source (Apache-2.0, Go) governance gateway. The config is the useful part; the tool is just one way to run it.

**1. Register the MCP server with the gateway — this is where the secret lives:**

```
tools:
  ticket-search:
    kind: mcp
    url: https://tickets.internal/mcp
    credential_source: static_env
    token_env: TICKETS_MCP_TOKEN
```

`TICKETS_MCP_TOKEN` is read from the *gateway's* environment. The agent process never sees it.

**2. Grant each agent only the tools it needs — default-deny:**

```
name: legacy-triage
execution: fronted
endpoint: https://legacy.internal/agents/triage
tools:
  - resource: ticket-search
    mode: all                            # every tool ticket-search exposes
  - resource: billing-mcp
    tools: [get_invoice, list_invoices]  # only these two of billing-mcp
```

An agent gets access to a resource only if it's named here, and to the *tools* it names (or an explicit `mode: all`). Leaving the list off isn't "allow everything" — it's rejected at config load. Widest access is always something you typed on purpose, never something you got by omission.

Want read-only? Say so, and the operator's classification decides what counts:

```
tools:
  - resource: billing-mcp
    mode: read-only        # only the tools billing-mcp is declared to expose read-only
```

**3. For OAuth-protected servers, even the client secret stays out of the agent.** The gateway's broker mints a token from your identity provider per call:

```
tools:
  billing-mcp:
    kind: mcp
    url: https://billing.internal/mcp
    credential_source: static_env
    grant_type: client_credentials
    issuer: https://idp.example.com/
    token_endpoint: https://idp.example.com/oauth2/token
    client_id_env: BILLING_MCP_CLIENT_ID
    client_secret_env: BILLING_MCP_CLIENT_SECRET
    scope: billing.read
    read_only_tools: [get_invoice, list_invoices]
```

The client id and secret are read from the gateway's environment; the agent gets the minted access token's *effects*, never the token, and never the secret that mints it.

**4. Because everything flows through one point, every tool call and every refusal lands in a hash-chained, tamper-evident audit ledger** — so "what did this agent actually do?" has a single, ordered answer.

Being honest about the boundary is the whole point, so:

Those aren't weaknesses to paper over; they're where the boundary actually sits, and knowing that is how you deploy the pattern correctly (gateway on a trusted host, agent in a sandbox, ledger shipped somewhere append-only).

Regardless of which tool you use, the pattern is portable:

The agent stays the flexible, fallible thing it's supposed to be — and it stops being the thing that holds your keys.

A working implementation of all of the above is at [github.com/agenthof/agenthof](https://github.com/agenthof/agenthof). If you're doing this differently — a sidecar, a service mesh, provider-side scoping — I'd genuinely like to hear how it's holding up.
