Skip to content
mentoor

Topic 3 of 4 · 6 min read

Delegated OAuth for agents acting for users

Goal: Choose a delegation flow that gives an agent user-scoped, revocable access without passing the user's token through.

When an agent acts for a user, give it its own narrow, revocable token for that user, issued for one specific service, and never forward the user's own token. The user stays in control and each service only accepts tokens meant for it.

Check yourself · 1 of 3

Why should an MCP server not forward the user's incoming token to a downstream API?

Choose one answer

Why it matters

An agent that reads a user's calendar or files needs access to their data, but not to their password or to every service the user can reach. A shared service account would let the agent see everyone's data. Passing the user's login token along is also unsafe: the token may work on services it was never meant for, and logs cannot tell the user from the agent.

Parts you should know

  • Authorization code flow with consent. The user signs in with the provider and approves named scopes such as calendar.read. The agent app receives a token for those scopes only. The user can revoke it.
  • Audience-bound tokens. A token names the service it is for (the aud claim). The client asks for it with a resource indicator (RFC 8707). A service must reject tokens issued for someone else. MCP's authorization specification requires this.
  • Token exchange (RFC 8693) and on-behalf-of. A service that holds a token for the user swaps it for a new, narrower token for a downstream API. The new token can record both the user and the acting agent.
  • No token passthrough. An MCP server must not accept a token that was issued for another service and forward it onward. The MCP security guidance calls out passthrough and the confused deputy problem, where a service uses its own power on behalf of the wrong party.

How to choose

  1. Does the agent need the user's data? Use delegated OAuth, not a shared key.
  2. Does a downstream call need a different service? Exchange the token, do not forward it.
  3. Is the access long-lived? Use refresh tokens stored by the app, short access tokens, and a revoke path.

Before and after

Weak: the MCP server receives the user's token for the app, then sends the same token to the payments API.

Better: the MCP server checks the token's audience is itself, then exchanges it for a payments token limited to payments.read for that user, valid for minutes.

What goes wrong

  • Scopes that are far broader than the task, "to be safe".
  • Skipping the audience check because the signature is valid.
  • No revoke path, so a lost laptop means access lasts until expiry.

Examples

Exchange instead of forward

// Inside the MCP server, after validating the incoming token's audience
const downstream = await oauth.tokenExchange({
  subject_token: incomingToken,
  audience: "https://payments.example.com",
  scope: "payments.read",
});
const res = await fetch(PAYMENTS_URL, {
  headers: { Authorization: `Bearer ${downstream.access_token}` },
});

The payments API sees a token meant for it, with a small scope, tied to the user.

The audience check

if (!claims.aud.includes("https://mcp.example.com")) {
  return reply.code(401).send({ error: "invalid_token" });
}

Without this line, a token stolen from a different service would work here.

Key points

  • Delegated OAuth gives the agent user-scoped, consented and revocable access instead of a shared key or the user's password.
  • Tokens should be bound to an audience, and a service must reject tokens issued for another service.
  • Token exchange swaps a token for a narrower one for the next service, and can record both user and agent.
  • MCP servers must not pass through a client's token to downstream APIs.
  • Keep access tokens short-lived and give users a way to revoke the agent.

Common mistakes

  • Forwarding the incoming bearer token to a downstream API.
  • Validating the signature but not the audience or scope.
  • Requesting every scope up front instead of the ones the task needs.
  • Using one service account for all users, which breaks per-user access control and audit.

Check yourself · 2 of 3

What does the audience of an access token say?

Choose one answer

Check yourself · 3 of 3

An agent needs to read one user's calendar. Which access is the best fit?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic