Topic 2 of 4 · 5 min read
Keeping credentials outside the model
Goal: Refactor a tool so the model never sees or chooses the credential it uses.
The model should ask for an action, and a trusted layer around it should supply the credential. If the model never holds the key, it cannot leak it, repeat it or be talked into sending it elsewhere.
Check yourself · 1 of 3
Which tool signature keeps the credential away from the model?
Why it matters
Hiding secrets from files is not enough if the tool design hands them back. A tool like call_api(url, token) asks the model to carry the token. A tool like get_invoice(id) does not. This is often called brokering: the code that runs the tool (your server, an MCP server or a gateway) holds the credential and attaches it to the request. OWASP's guidance on secrets management points the same way: central storage, narrow access and short life.
How to do it
- Attach credentials server-side. The tool function reads the secret from a vault or its own environment and adds the
Authorizationheader. The model only fills in business arguments. - Use handles, not values. If the model must refer to a credential, give it a name such as
crm_readonly. The broker looks up the real secret. A handle that leaks is useless outside your system. - Never let the model choose the target of a credential. If the model can pass any URL, it can send your token to a host an attacker controls. Allow-list hosts and paths inside the tool.
- Scope each credential to the tool. A read-only token for a read-only tool. Use short expiry where the provider supports it.
- Redact on the way back. Strip headers and error details that echo the secret.
Before and after
Weak:
// the model supplies both url and token
async function callApi(url: string, token: string) {
return fetch(url, { headers: { Authorization: `Bearer ${token}` } });
}
Better:
// the model supplies only an invoice id
async function getInvoice(id: string) {
if (!/^inv_[a-z0-9]+$/.test(id)) throw new Error("bad invoice id");
const token = await vault.get("billing_readonly");
const res = await fetch(`https://billing.example.com/invoices/${id}`, {
headers: { Authorization: `Bearer ${token}` },
});
return pick(await res.json(), ["id", "status", "total"]);
}
What goes wrong
- The tool takes a free-form URL, so an injected instruction can aim the credential at an attacker's server.
- One powerful "service" token is shared by every tool, so any single bug exposes everything.
- The tool returns the raw response, which includes a token the API echoed back.
Examples
Moving the token out of the arguments
A tool schema asks for api_key as a string argument. The agent fills it from earlier context. Change the schema to only customer_id, and have the server add the key. The tool call in the transcript now shows no secret, and an injected page cannot ask for it.
Allow-listing the destination
const ALLOWED = new Set(["billing.example.com"]);
function assertAllowed(u: string) {
if (!ALLOWED.has(new URL(u).hostname)) throw new Error("host not allowed");
}
Even if the model is tricked into passing another URL, the credential is never attached to it.
Key points
- The model asks for an action and the tool layer supplies the credential.
- Pass handles or names, not raw keys, when the model must refer to a credential.
- Never let the model pick the host, URL or token a credential is sent to; allow-list targets in the tool.
- Give each tool its own narrowly scoped credential rather than one shared master key.
- Filter tool results so tokens and headers echoed by an API do not return to the model.
Common mistakes
- Adding a
tokenorheadersparameter to a tool for convenience. - Relying on the system prompt saying "never reveal the key" instead of keeping the key out of reach.
- Sharing one admin credential across every tool and agent.
- Returning full upstream responses, including echoed credentials, to the model.
Check yourself · 2 of 3
What does a credential handle such as crm_readonly give you?
Check yourself · 3 of 3
A tool takes any URL and adds your API token to the request. What is the risk?
Practice items, not counted towards your mastery.
Start here
- Secrets Management Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Central storage, short-lived secrets, rotation and auditing: the baseline every agent setup should meet.
- MCP specification: Authorization (opens in a new tab)StandardFree
How MCP servers and clients must handle OAuth, audience-bound tokens and token passthrough.
- MCP security best practices (opens in a new tab)DocsFree
Names the confused deputy and token passthrough attacks and what servers must do instead.