Skip to content
mentoor

Topic 3 of 5 · 5 min read

Scopes, audiences and least-privilege tokens

Goal: Design the scopes for a server's tools and decide whether a presented token must be accepted, refused or stepped up.

A token should carry only the scopes the current work needs, name your server as its audience, and expire soon; the server checks all three on every request. Scopes say what the holder may do. The audience says who may accept the token.

Check yourself · 1 of 3

A token is valid, signed by your authorization server, and was issued for the server with scope tickets:read. It is used to call delete_ticket. What should the server return?

Choose one answer

Why it matters

An MCP server often fronts real actions: close a ticket, delete a file, send a payment. If one broad scope covers everything, a leaked token or a confused agent can do all of it. Per-tool scopes shrink the damage. Audience checks stop a token issued for another service from working on yours.

How to do it

  1. Group tools by risk and name scopes by capability. For a ticketing server: tickets:read for search and get, tickets:write for create and update, tickets:delete for removal. Do not use one scope called admin.
  2. Ask for the least at first. Put the basic scope in scopes_supported and in the scope part of the first 401 challenge.
  3. Step up when needed. If a valid token lacks a scope for this call, return 403 with WWW-Authenticate: Bearer error="insufficient_scope", scope="tickets:delete", resource_metadata="...". Put every scope the operation needs in one challenge. The client unions the new scopes with those it already holds, asks the user again, and retries a few times at most.
  4. Check the audience. The token must have been issued for your server, which is the resource value the client sent. Reject anything else with 401. Never accept a token meant for another service, and never pass the client's token on to an upstream API.
  5. Keep tokens short-lived. A leaked token then stops working soon. Do not ask for offline_access in your server's challenge; refresh tokens are not a resource requirement.
  6. Honour hierarchies. If tickets:write is defined to include tickets:read, a write token may read.

Before and after

// weak: any valid token from our identity provider is enough
if (!verifySignature(token)) return unauthorized();
return runTool(name, args);

This accepts a token issued for a different application and ignores scope. The fixed check:

const claims = verify(token);
if (!claims.aud.includes("https://mcp.example.com")) return unauthorized(); // 401
if (!hasScope(claims.scope, REQUIRED[name])) return insufficientScope(REQUIRED[name]); // 403
return runTool(name, args);

What goes wrong

  • One catch-all scope, so a read-only integration holds delete rights.
  • Returning one missing scope per challenge, which sends the user through several consent screens.
  • Forwarding the client's token to a downstream API, which makes your server a confused deputy.

Examples

Scope map for a tickets server

const REQUIRED: Record<string, string> = {
  search_tickets: "tickets:read",
  get_ticket: "tickets:read",
  update_ticket: "tickets:write",
  delete_ticket: "tickets:delete",
};

A client that starts with tickets:read can search all day. The first delete_ticket call returns 403 with scope="tickets:delete", and the user sees one consent prompt for that scope.

Decide: accept, refuse or step up

Three requests reach delete_ticket. A token issued for https://billing.example.com with scope tickets:delete: refuse with 401, wrong audience. A token for this server with only tickets:read: 403 step-up asking for tickets:delete. A token for this server with tickets:delete: accept.

Key points

  • Design scopes around capabilities and risk, with separate scopes for read, write and delete.
  • Invalid, expired or wrong-audience tokens get 401; a valid token without the needed scope gets 403 with insufficient_scope.
  • A step-up challenge lists every scope the operation needs in one response, and the client unions them with scopes it already holds.
  • Servers must check that a token was issued for them, using the resource indicator, and must not accept or pass through other tokens.
  • Short-lived access tokens limit what a leak is worth.

Common mistakes

  • Granting a single broad scope because per-tool scopes feel like extra work.
  • Validating the signature but not the audience.
  • Answering 401 for a valid token with too few scopes, which makes the client restart login instead of stepping up.
  • Forwarding the client's access token to another API instead of getting a separate token for it.

Check yourself · 2 of 3

A token has the right scopes but its audience is a different company service. What must your MCP server do?

Choose one answer

Check yourself · 3 of 3

A tool needs both files:read and files:write. The client holds neither. Which challenge is best?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic