The first thing I got wrong was the grant type. Of course it was.
I had a Model Context Protocol server running in Go, the protocol Anthropic shipped so that Claude and friends can reach out and touch your actual data instead of hallucinating it. Mine exposed two tools: one that read customer records, one that read invoices. An agent connects, asks for context, gets JSON back. Lovely. Completely unauthenticated, which is how roughly every MCP demo on the internet ships, and how none of them should ship.
So I reached for the pattern my hands know: client credentials. Machine talking to machine, give it a client ID and secret, done. I had it working in about ten minutes and it was wrong in every way that counts.
Here’s the thing client credentials gets you: the application’s identity. The agent authenticates as itself and sees everything the agent is allowed to see, which, for a tool that reads customer records, is all of them. There is no user in that token. So when Dana from support asks the agent a question, the agent happily pulls records Dana would never be allowed to open in your actual app. You’ve built an exfiltration appliance with a chat interface.
The agent isn’t the subject. It’s the delegate. The whole point is that it acts on behalf of a specific human, bounded by what that human can do.
That means authorization code flow with PKCE. OAuth 2.1 makes this easy to reason about because it already threw out the bad options, implicit flow is gone, the password grant is gone, and PKCE is mandatory for every client, not just the public ones. The agent is a public client that can’t keep a secret, so PKCE is doing the heavy lifting: it proves the thing redeeming the code is the same thing that started the flow.
The flow, once I stopped fighting it, is unremarkable. The agent generates a verifier, hashes it, and sends the challenge:
User logs in, consents, you get a code, and you redeem it with the original verifier, no client secret in sight:
Back comes an access token whose sub is the user, not the app. That distinction is the entire security model.
Now the part people skip. Scopes are not permissions. A scope is the agent asking to read customers; it says nothing about whether this user may. If you stop at scope checks you’re back to the client-credentials problem wearing a nicer coat. You need RBAC behind it.
In Auth0 that meant defining permissions on the API (read:customers, read:invoices), bundling them into roles, assigning roles to users, and, the setting everyone misses, toggling RBAC and Add Permissions in the Access Token on the API settings so the token carries a permissions claim reflecting what that user actually holds. An hour in, I realized my token had the scope but not the permission, because I’d skipped that toggle. The agent asked for everything; the user was granted nothing; the token told the truth.
Validation in Go is boring, which is the goal. Verify the signature against the JWKS endpoint, pin the issuer and audience, then gate the handler on the permission, not the scope:
Wrap each MCP tool in that and the agent is now exactly as powerful as the person behind it. Dana gets Dana’s records.
Two things I’d tell anyone starting this. First: validate the audience. A token minted for your SPA is not a token for your MCP server, and a reused access token is how a tidy demo becomes a breach. Second, and I mean this plainly, none of this touches prompt injection. OAuth bounds what the agent can fetch. It does nothing about a cleverly-worded message convincing the agent to fetch the wrong thing within those bounds. That’s a different fight.
We relearn delegation every time a new client type shows up. Browsers, mobile, SPAs, now agents. The answer hasn’t changed: give the delegate the user’s authority, not its own.
