# AI Agent Least Privilege: Credentials, Tokens and Secrets

> Credential design for tool-using AI agents: keep secrets out of the context window, use domain_secrets in sandboxes, audience-bound per-request MCP tokens, one scoped identity per tool, and approval gates on writes.

URL: https://www.matthewswong.com/en/blog/ai-agent-least-privilege-credentials/

- Author: Matthews Wong (https://www.matthewswong.com/en/about/)
- Published: 2026-10-01
- Updated: 2026-10-01
- Category: Security

> **Key takeaway:** AI agent least privilege means designing credentials on the assumption that a prompt injection will succeed. Keep every secret out of the model's context, substitute credentials outside the model with placeholders, send audience-bound OAuth tokens per request, give each tool its own narrowly scoped identity, and require human approval before any tool writes.

Picture an accounts-payable helpdesk agent wired into an ERP. It reads vendor invoices, answers status questions and drafts payments. One afternoon a vendor PDF arrives with a line of white-on-white text telling the agent to export the vendor master list to an outside address. Whether that line becomes an incident has very little to do with the prompt. It depends on what the agent's credentials allow it to reach, and whether a secret ever sat where the model could read it.

This post covers the identity and secret layer of an agent, the part that limits damage after an injection gets through. It works from the OWASP LLM06 Excessive Agency entry, the OpenAI hosted shell and remote MCP guides, and the MCP 2026-07-28 authorization specification, and ends with a review checklist you can run against any agent before it reaches production. Sandboxing and injection detection are separate topics; here the question is narrower: when the model is fooled, what can it actually do?

## AI agent least privilege starts by assuming the injection works

OWASP names the failure Excessive Agency and splits it into three root causes: excessive functionality, excessive permissions and excessive autonomy. Each one maps cleanly onto a credential decision, which is why the identity layer is where agent security is won or lost. Filters and classifiers reduce how often an injection lands; credentials decide what it costs when one does.

| OWASP root cause | What it looks like in an agent | Credential control that caps it |
| --- | --- | --- |
| Excessive functionality | A mailbox summariser that can also send mail, or an ERP reader that exposes a generic query tool | Expose only named tools; filter remote MCP servers with allowed_tools so unused tools are never imported |
| Excessive permissions | One admin API key shared by every tool the agent calls | One service account or OAuth scope per tool, read-only by default, write scopes requested only on step-up |
| Excessive autonomy | Payments posted or records deleted without anyone looking | Approval gates on every write tool, decided by code and a person rather than by the model |

The OWASP example is worth remembering because it is so ordinary: an assistant built to summarise email also had permission to send it, and an injected email told it to forward sensitive messages to the attacker. The listed fixes are all permission changes, not prompt changes. Remove the send capability, authenticate with a read-only OAuth scope, or require the user to approve each send.

## Rule one: no secret ever enters the context window

OpenAI's remote MCP guide states the problem plainly: a malicious server can exfiltrate sensitive data from anything that enters the model's context. The same is true of a poisoned web page, a hostile PDF or a confused model. So the first rule is structural. An API key written into a system prompt, a tool description or a tool argument is a key the model can repeat. The model should name an action; your executor, running outside the model, should decide which credential that action uses.

The mapping from tool name to secret reference is the most useful line in the whole design. It makes the credential a property of the tool, not of the conversation, so no amount of clever input can make read_invoice run with the drafting account. It also gives you a single file to review when someone asks what the agent can touch.

> Tool results are a leak path too. An ERP error that echoes the request headers, a debug response that includes the Authorization value or a pre-signed download URL all flow straight back into context, where the next injected instruction can find them. Redact tool output before returning it, and never let a shell tool print its own environment variables.

## Sandboxes: placeholders with domain_secrets, not raw keys

Code-executing agents make rule one harder, because the code the model writes needs to authenticate somewhere. OpenAI's hosted shell answers this with domain_secrets. Each entry holds a target domain, a friendly name and the secret value. The model and the runtime see only a placeholder such as $ERP_TOKEN; an auth-translation sidecar applies the raw value only for approved destinations, and the documentation states that raw values do not persist on API servers and do not appear in model-visible context.

The network side matters as much as the secret side. Hosted containers have no outbound network access by default. To enable it, an admin must first configure the organisation allow list in the dashboard, and the request must also set network_policy explicitly. The org list defines the full set of reachable domains, the request-level list can only narrow it, and a request that names a domain outside the org list fails rather than quietly widening access.

OpenAI also warns that allowlisting itself creates an exfiltration route: only allow domains you trust and that an attacker cannot use to receive data. A general-purpose paste site or an object store that accepts anonymous uploads defeats every other control on this page. The same guide recommends recording the hosts each session requested and the destinations it actually reached, which is the log you will want after an incident.

## MCP tokens: per request, per user, bound to one server

Remote MCP moves credentials across a trust boundary, and both ends have rules. On the OpenAI side, the Responses API does not store the value you pass in the authorization field and does not show it in the Response object, so you must send it with every request. That is a feature: the token lives in your token store, scoped to the user who is actually present, and nothing on the platform side keeps a long-lived copy.

The MCP 2026-07-28 authorization specification adds the audience rules. Clients must send the RFC 8707 resource parameter, set to the server's canonical URI, in both the authorization and token requests. Servers must validate that a token was issued specifically for them, must only accept tokens valid for their own resources, and must not accept or transit any other tokens. The security best practices page calls the forbidden pattern token passthrough: forwarding a client's token unchanged to a downstream API breaks audit trails and turns the server into a confused deputy.

The same release hardens the flow against mix-up attacks. Clients record the expected issuer before redirecting and validate the iss parameter from RFC 9207 before sending the authorization code to any token endpoint, so a code from an honest server cannot be redeemed at a hostile one. PKCE alone does not stop this, because the client would hand its code_verifier to the attacker's token endpoint. Client ID Metadata Documents replace Dynamic Client Registration as the recommended registration path, with DCR kept only for backwards compatibility.

## One identity per tool, scoped to its verb

A single agent should not be a single principal. Give each tool its own service account or OAuth scope, sized to the one verb that tool performs. In the accounts-payable example, the identity split looks like this:

| Tool | Identity | Scope | If injected, worst case |
| --- | --- | --- | --- |
| read_invoice | User's delegated token | invoices:read | Reads invoices the user could already see |
| draft_payment | User's token after step-up | payments:draft | Creates a draft that a person must still release |
| lookup_vendor | Read-only service account | vendors:read on name and status fields only | Learns vendor names, not bank details |
| post_payment | Not given to the agent | None | Nothing; posting stays a human action in the ERP |

MCP gives you the mechanism to keep the starting scope small. The specification tells servers to put the required scope in the WWW-Authenticate challenge and tells clients to treat it as authoritative. When a token lacks a scope at runtime, the server answers 403 with error insufficient_scope, and the client re-authorises with the union of what it already had and what the challenge asks for.

The best practices page lists the mistakes that undo this: publishing every scope in scopes_supported, wildcard or omnibus scopes such as all or full-access, bundling unrelated privileges to avoid future prompts, and treating the scopes claimed in a token as sufficient without server-side authorisation logic. That last one matters most for ERP work, where the real rule is usually row-level: this user may only see invoices for their own company code.

## Approval gates on every write tool

Scopes limit what is possible; approvals decide what actually happens. The Responses API exposes require_approval on an MCP tool as never, always, or an object listing the tool names that may skip approval. Pair it with allowed_tools so a server's destructive tools are never imported in the first place. Each pending call then arrives as an mcp_approval_request item, and your code answers with an mcp_approval_response carrying the request id and a boolean.

Keep the decision outside the model. A rule like every payment draft needs a person, or anything above a rupiah threshold needs a second approver, is ordinary code with an audit row behind it. Asking the model whether a call looks safe hands the decision back to the component the attacker has just compromised.

> Default to an allowlist of read tools that skip approval, not a blocklist of write tools that need it. A new tool added to the MCP server next month then starts out gated instead of silently auto-approved.

## A credential review checklist for any agent

Run this before an agent gets production credentials, and again whenever it gains a tool:

1. Search the system prompt, tool descriptions and few-shot examples for anything shaped like a key, token, password or connection string. There should be none.
2. Confirm every tool maps to exactly one credential reference in code, and that no tool can choose its own credential from its arguments.
3. Check that tool results are redacted for headers, signed URLs and token-shaped strings before they return to context.
4. For code execution, confirm secrets use domain_secrets placeholders and that every allowed domain is one an attacker cannot use as a drop box.
5. For remote MCP, confirm tokens are per user, sent per request, bound to the server with the resource parameter, and validated for audience on the server.
6. List each identity with its scopes and ask what an attacker gets if that one token leaks. Remove any scope the tool never uses.
7. Confirm every write tool requires approval, that the approval rule lives in code, and that each decision is logged with its arguments.
8. Verify you can revoke one tool's credential without stopping the whole agent, and that you know who to call when you do.

Items six and eight are the ones teams most often skip, and they are the ones that matter on the worst day. If one leaked token exposes everything, or if revoking it takes the whole helpdesk offline, the design is still a shared admin key with extra steps.

Prompt defences decide how often an agent is fooled; credentials decide what a fooled agent can do. Keep secrets where the model cannot read them, bind every token to one user and one server, give each tool the narrowest identity that lets it do its job, and keep a person between the agent and any irreversible write.

## Sources

- OWASP GenAI Security Project: LLM06 2025 Excessive Agency
- OpenAI API docs: Shell tool, network policy and domain_secrets
- OpenAI API docs: Connectors and remote MCP servers
- Model Context Protocol 2026-07-28: Authorization specification
- Model Context Protocol 2026-07-28: Security best practices

## Source links

- https://genai.owasp.org/llmrisk/llm062025-excessive-agency/
- https://developers.openai.com/api/docs/guides/tools-shell
- https://developers.openai.com/api/docs/guides/tools-connectors-mcp
- https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization
- https://modelcontextprotocol.io/docs/2026-07-28/tutorials/security/security_best_practices

## FAQ

### What does least privilege mean for an AI agent?

It means each tool the agent can call gets only the access it needs for its one job, and nothing more. OWASP's LLM06 Excessive Agency entry describes the failure as excessive functionality, excessive permissions and excessive autonomy. Least privilege caps all three, so a successful prompt injection can only do what that narrow credential allows.

### Why should API keys never be put in an agent's prompt or tool description?

Anything that enters the model's context can be repeated, summarised or exfiltrated by an injected instruction. OpenAI's remote MCP guide warns that a malicious server can exfiltrate sensitive data from anything in context. Let the model name the action and have your executor code, outside the model, attach the credential.

### How do domain_secrets work in the OpenAI hosted shell?

You list a domain, a placeholder name and the secret value inside the shell tool's network_policy. The model and the container only see the placeholder, such as $API_KEY, and a sidecar substitutes the real value only for requests to the approved domain. Outbound access also requires an org allow list in the dashboard plus an explicit network_policy on the request.

### Does OpenAI store the OAuth token I pass to a remote MCP server?

No. The Responses API does not store the value of the authorization field and does not show it in the Response object, so you must send it with every request. That lets you keep the token in your own store, scoped to the user who is present.

### What is token passthrough in MCP and why is it forbidden?

Token passthrough is when an MCP server accepts a client's token without checking it was issued for that server and forwards it unchanged to a downstream API. The MCP 2026-07-28 specification requires servers to validate the token audience and forbids accepting or transiting other tokens. Passthrough breaks audit trails and lets a stolen token use the server as a proxy.

---

- HTML version: https://www.matthewswong.com/en/blog/ai-agent-least-privilege-credentials/
- Bahasa Indonesia: https://www.matthewswong.com/id/blog/ai-agent-least-privilege-credentials/
- Site index for agents: https://www.matthewswong.com/llms.txt
- Sitemap: https://www.matthewswong.com/sitemap.xml
- API description (OpenAPI): https://www.matthewswong.com/openapi.json
