DEV Community

Cover image for Per-agent tokens for MCP servers: what operators told me, and the design that came out of it

Per-agent tokens for MCP servers: what operators told me, and the design that came out of it

Zach Benson on November 09, 2026

A few weeks ago I set out to give every AI agent its own cryptographic identity. Then I asked the people who run MCP servers what they actually nee...
Collapse
 
heyvaldemar profile image
Vladimir Mikhalev •

When the server is the issuer, most of the revocation problem shrinks to token lifetime. In one MCP server I run on Kubernetes, an agent's only credential is a projected ServiceAccount token with the server's audience and a ten-minute life, and the server asks TokenReview about it before it answers. A stolen token is worth ten minutes. The API server refuses it outright.

The piece I'd borrow for your scopes is how an ungranted tool answers. It's missing from tools/list, and a call to it gets the same error as a tool that doesn't exist, so a looping agent can't probe what it might be allowed. The destructive delete_record exists on the server and is granted to nobody; a static check fails the build if it ever is.

On Kevin's spend limit, one caveat from the same setup: the rate limit lives in the process. Several replicas would each allow their own ten calls.

Collapse
 
zachbenson profile image
Zach Benson •

This is great, thank you. Three things I'm taking: ungranted tools disappear from tools/list and return the same error as a tool that doesn't exist, so a loop can't probe; destructive tools are granted to nobody by default, with a build check; and Kevin's cap has to be a shared counter, since an in-process limit multiplies with replicas. Your setup covers agents inside the cluster really cleanly. Do agents from outside it ever call that server? That's the case I'd most like to test, observe-only, if you're up for it.

Collapse
 
heyvaldemar profile image
Vladimir Mikhalev •

No. Its only callers are agent pods in the same cluster, and it accepts nothing but a projected token issued for its own audience; anything else gets a 401. An observe-only test from outside would see that 401 and little more.

That is also why it skips the spec's OAuth flow. Inside one cluster the kubelet mints a token bound to the pod and the audience, and TokenReview checks it, so the authorization server's job is already done. A desktop agent or another cluster's workload has no kubelet behind it.

Opening the door to them means the server becomes an OAuth 2.1 resource server and publishes Protected Resource Metadata (RFC 9728). That's where your issuer question comes back, with a real issuer to answer it.

Collapse
 
agentwareshq profile image
agentwares •

Kevin's point about the spend limit is the one I'd underline too, and it ties back to your revocation caching. A max-per-run carried in the token only helps if the server meters against it live: if the spend check happens at token issue and then the token is good for 15 minutes, a looping agent can still burn the whole budget inside that window. The claim has to be paired with a server-side rolling counter keyed on agent_id + sub, checked before each spend-bearing call, not just at verification time.

Same logic applies to the 60-second revocation cache for reads: fine when the cost of being wrong is one more cheap read, but for anything metered the cache window is exactly the window a runaway loop exploits. I'd treat spend-bearing tools like writes here — live check, no cache — even if the scope is read-only in name.

One question on the design: with the token's 15-minute expiry, does the agent re-mint on expiry mid-run, and does the per-run cap reset with the new token? If the cap is per-token rather than per-run, a long job that refreshes tokens gets a fresh budget each time, which quietly defeats the limit.

Collapse
 
zachbenson profile image
Zach Benson •

This is a real flaw, thank you. A cap carried in the token resets every time the agent re-mints, so a long run gets a fresh budget every 15 minutes. The fix I'm taking: the token only identifies the agent (agent_id plus sub, optionally a run_id), and the budget lives on the server as a rolling counter keyed on those, checked before every spend-bearing call. And agreed: anything that costs money gets a live check, whatever its scope says.

To answer directly: yes, the agent re-mints on expiry mid-run, which is exactly why the budget can't live in the token. If you run a metered server, I'd value you testing exactly this: observe-only, and it would show where a cap would have stopped a loop on your traffic.

Collapse
 
kolmopdf profile image
KolmoPDF •

The read vs write split reflects how document repositories handle delegation. For read tools, an agent querying notes or PDFs often runs in an aggressive retrieval loop. Having agent_id in the JWT lets you throttle a looping scraper without disrupting the user's interactive session.

For writes, issuing the token from the MCP server itself avoids the federated trust problem. If the server signs the token with a deterministic fingerprint of the client harness, it can enforce least-privilege mutations without maintaining external key agreements.

Collapse
 
zachbenson profile image
Zach Benson •

That's a better way to put the read case than mine: throttle the looping scraper by agent_id and leave the user's interactive session alone. And agreed on writes, the server issuing the token keeps trust where operators want it. Does KolmoPDF run an MCP server for PDFs? If so, I'd love to have you test this on your read tools: observe-only, about 30 minutes with me helping.

Collapse
 
ascendra_ventures_92c21f2 profile image
Ascendra Ventures •

Most of the first idea not surviving contact with operators is the best outcome a post like this can have. Keyless identity that costs an abuser nothing, a hosted wallet nobody wants, and revocation that was never actually painful would all have been expensive things to build.

The spend cap thread is the sharpest part. A cap carried in the token resets every time the agent re-mints, and an in-process counter multiplies with replicas. A shared rolling counter keyed on agent and run, checked before every call that costs money, is the version that holds.

At Ascendra Ventures we write these as acceptance cases before building anything for agents: revoke one agent in under a minute without signing the user out, and stop a looping agent at a number the customer chose. If the design cannot pass those two, it is not done.

Did any operator give you a target for how fast a write-tool revocation has to take effect?

Collapse
 
zachbenson profile image
Zach Benson •

No operator gave me a number for writes, but two said the same thing a different way: reads can cache revocation for about a minute, and writes need a live check on every call. So the target for writes isn't a window. The first write after a revoke fails. I'll take your two acceptance cases as the bar for the test: revoke one agent without signing the user out, and stop a looping agent at a number the customer chose. Do you build agents for clients that call outside MCP servers? That's the side I still need testers for.

Collapse
 
kevinhall profile image
Kevin Hall •

honestly I would reach for a spend limit per agent before the kill switch. on metered tools the thing that hurts is one loop going through the balance and by the time someone revokes it the money is gone, so a max per run carried in the token is what would get me to try this

Collapse
 
zachbenson profile image
Zach Benson •

That makes sense, and it changes what I'd lead with for metered tools: cap each agent's spend before worrying about the kill switch. Two corrections from this thread: the cap has to live on your server as a counter per agent, not just in the token, or a refresh resets it (agentwares). And the counter has to be shared across replicas, or each one hands out its own budget (Vladimir).

In an observe-only test I could show exactly where a per-agent cap would have stopped a loop on your real traffic, without blocking anything. Would you want to try that on a metered endpoint?

Collapse
 
wiroai profile image
Wiro AI •

We ended up close to this with plain project keys: each agent can get its own key, so cutting one off doesn't sign you out of everything else.

Collapse
 
zachbenson profile image
Zach Benson •

That's the simplest version that works. Is there anything you wish those per-agent keys did that they don't, like expiring on their own, capping spend per agent, or showing which agent did what? If any of that matters, I'd be glad to have you try this observe-only alongside your keys and see if it adds anything.

Collapse
 
wiroai profile image
Wiro AI •

Thanks! Spend is already capped per team and per member on our side, so not much is missing for now.

Collapse
 
ggle_in profile image
Jack •

Good write-up, and I appreciate that the design changed after talking to operators. Two strengths that are easy to overlook:

1) The observe-only rollout is a very practical adoption path — it gives operators per-agent visibility without breaking existing auth, which is usually the hardest part of shipping auth changes.
2) The “boring” JWT/JWKS choice is a feature, not a weakness: standard verification, no vendor lock-in, and a clear separation between identity issuance and server-side policy. That should make it easier to test and adopt.

I’d be glad to compare notes, and I can introduce a couple of colleagues who run MCP servers or security teams if that helps broaden the tester pool.

One limitation I don’t think can be argued away: the design is strongest for reads and still unresolved for writes. For send/delete/spend tools, operators want identity they issue, so an external per-agent JWT alone cannot deliver the control that matters most; without consistent server-side validation and live checks at every enforcement point, it can create a false sense of security.

A realistic next step would be to publish a small operator checklist and reference tests covering issuer/audience/expiry validation, scope mapping, live revocation for writes and metered calls, shared rolling spend counters keyed on agent_id + sub (and run_id if used), and audit records that stamp the verified agent_id at write time. That would make the design easier to evaluate and safer to adopt.

Collapse
 
eminbudak profile image
Emin •

Does a per-agent token help when two agents sit behind one client, like two Claude Code sessions? That's the case one client registration per agent can't separate.

Collapse
 
zachbenson profile image
Zach Benson •

Good question, and that's exactly the case client registration can't separate. A per-agent token can, but only if each session gets its own agent_id, which means something on the client side has to mint one per session: a small wrapper or local proxy around Claude Code, since it won't do that by itself. Is that your setup, several sessions running at once against the same server?

Collapse
 
x_kev_the_dev profile image
KevTheDev •

This lines up almost exactly with where I landed building the same thing, which is either reassuring or suspicious.

Three notes from running it in production rather than designing it:

Revocation. I went with a live check on everything rather than a 60s read cache. Your tradeoff is probably right at volume, but one cost worth knowing: the demo that makes people actually understand the product is revoke, then call again, denied, in the same breath. With a cache window that demo has an asterisk in it.

The thing I’d add to your list. Your operators told you revocation must be fast and dangerous tools need their own scopes. Both of those are about stopping an agent. Nothing in the design answers what it did before you stopped it. I hash-chain every verify decision into an append-only log, and the honest reason was compliance theater. Then I had an outage where the absence of one expected log entry located the crash immediately, and it’s turned out to be the most useful thing I built.

On “does this beat OAuth.” Per-client registration gets you most of the way for a handful of agents. It falls over when agents are spawned programmatically, because you’re doing client registration at runtime for ephemeral things. That’s where the line is for me.

One thing I’d be curious whether you probed: your three no’s were on hosted issuance, which makes sense to me, that’s a thing people can build themselves and know it. Did anyone react differently to the audit side? My read is that a log you self-host doesn’t prove much, since you control the database, and third-party custody is the only part of this genuinely worth paying someone else for. But I’ve been wrong about what people want before.

Happy to compare notes on the write-path issuer question, I haven’t solved that one either. I’ve got a working implementation of most of the rest if it’s useful to have something to poke at while you build.

Collapse
 
agentsecindex profile image
Agent Security Index •

The boring JWT design is the part I would keep. A short-lived token with sub, agent_id, aud, scope, exp, and jti gives you revocation of one agent without rotating every client the user has. Two checks from the MCP authorization work are worth making explicit. Bind the audience to the MCP server that should accept the token, and reject a token minted for a different resource. The official security best practices spend a lot of time on confused deputy and token misuse when a proxy sits between clients and a third-party API, and a missing audience check is how that starts. I would also keep writes on a live revocation check, as you already suggest, and I would not let the model request a scope upgrade on its own. A prompt injection that can talk the agent into calling a tool can often also talk it into asking for a broader scope. Scope changes should be a user or operator action, recorded with the agent_id that will receive them.

Collapse
 
izgorodin profile image
Edward Izgorodin •

For the operator in your list who runs a hosted notes server, the kill switch covers only half of what a misbehaving agent leaves behind. On a memory or notes server, what the agent did before you stopped it is not only a log entry, it is the set of notes it wrote, and every other agent on that account can read them on its next search. Revoking the agent stops new writes; it does not tell you which of the stored notes came from it.

That only works if the server stamps the verified agent_id from the token onto every record at write time, rather than trusting anything the agent puts in the payload, so that a revoke can come with a second step: list or hide everything that agent wrote since a given time. The token is already verified on every call, so the attribution costs nothing extra, and it is the part of per-agent identity that a shared key can never give you after the fact.