Skip to main content

Request more scopes with step-up authorization

When a client connects to your MCP server, the user approves a set of scopes. With step-up authorization, that first grant stays small, and the server asks for more scopes only when the agent calls a tool that needs them:

  1. The agent calls a tool, such as get_primary_email, with an access token that lacks the tool's scope.
  2. Before the tool runs, your server answers 403 Forbidden with a challenge: a WWW-Authenticate: Bearer error="insufficient_scope", scope="..." header that names the scopes to request.
  3. The MCP client sends the user through Clerk's OAuth consent screen again, this time for the scopes in the challenge.
  4. The client retries the tool call with the new access token.

The MCP TypeScript SDK (@modelcontextprotocol/server) sends this challenge for you when you set the scopeChallenge option of registerTool(). The requireAdditionalScopes() helper from @clerk/mcp-tools builds that option from the scopes a tool needs.

This guide assumes that you've completed Build an MCP server. That server requires only the profile scope, the baseline every request needs. Its whoami tool works with profile alone. This guide adds a get_primary_email tool, which returns the user's primary email address. email becomes the step-up scope: the server asks clients for it the first time they call get_primary_email.

Clients handle the challenge differently, so check your client's documentation. For example, Claude Code reports the missing scopes and asks the user to run /mcp, which opens the consent screen for those scopes. Clients without step-up support report a failed request and reconnect with the same baseline scopes. Their users can use the tool only if the client requests the step-up scope when it connects. To have clients request it up front, add it to challengeScopes, as described in Keep step-up scopes out of the baseline.

Install the required versions

This guide requires @modelcontextprotocol/server 2.3.0 or later and @clerk/mcp-tools 1.0.0 or later. The SDK added the scopeChallenge option in 2.1, but this guide needs 2.3.0 or later for its security fixes. SDK 1.x (@modelcontextprotocol/sdk) and 2.0 don't have the option: TypeScript rejects it, and plain JavaScript ignores it.

Install or update the packages.

npm install @clerk/mcp-tools@latest @modelcontextprotocol/server @modelcontextprotocol/node
pnpm add @clerk/mcp-tools@latest @modelcontextprotocol/server @modelcontextprotocol/node
yarn add @clerk/mcp-tools@latest @modelcontextprotocol/server @modelcontextprotocol/node
bun add @clerk/mcp-tools@latest @modelcontextprotocol/server @modelcontextprotocol/node

The Express server in the build guide uses streamableHttpHandler() from @clerk/mcp-tools/express, which runs on SDK 1.x and can't send the challenge. This guide switches the /mcp route to createMcpHandler() from SDK 2 and adapts it to Express with toNodeHandler() from @modelcontextprotocol/node. @modelcontextprotocol/node 2.1.2 requires @modelcontextprotocol/server 2.4 or later, so on Express this guide needs 2.4 or later. After the switch, the route no longer uses @modelcontextprotocol/sdk. Uninstall it unless other code in your app imports it.

@clerk/mcp-tools 1.0.0 requires Express 5 or later.

Allow clients to request the step-up scope

A client can receive only the scopes your Clerk instance lets it request. If it asks for any other scope, Clerk rejects the authorization request with invalid_scope, and step-up fails. email is a built-in scope. For a scope of your own, such as projects:write, create a custom scope. Assign it to the pre-registered OAuth applications that can request it, and add it to the allowed scopes below for dynamic clients.

MCP clients usually connect as dynamic clients, either through or Dynamic Client Registration (DCR). By default, each dynamic client can request only the scopes it registered or first connected with. Clients that send no scope get Clerk's default scopes, profile and email, which would grant email up front. To let dynamic clients step up instead:

  1. In the Clerk Dashboard, navigate to the OAuth applications page and select the Settings tab.
  2. Under Client onboarding, set Default scopes for dynamic clients to profile only.
  3. In Allowed scopes for dynamic clients, add profile, email, and any custom scopes your tools need, and save your changes. The allowed scopes must include every default scope.

These settings don't change the scopes you set on a pre-registered CIMD client. In the Clerk Dashboard, on the OAuth applications page, select the Applications tab, select the client, and edit its scopes to include the step-up scope.

Add the tool's data

Keep requiredScopes in mcpAuthOptions set to ['profile'] so clients don't request email up front. To learn how clients choose their first scopes, see Keep step-up scopes out of the baseline.

In index.ts, add getPrimaryEmail(), which reads the user's primary email address with clerkClient from @clerk/express.

index.ts
import { clerkClient } from '@clerk/express'
// ...

async function getPrimaryEmail(userId: string) {
  const user = await clerkClient.users.getUser(userId)
  const email = user.emailAddresses.find((address) => address.id === user.primaryEmailAddressId)
  return email?.emailAddress ?? 'No primary email address'
}

// ...

mcpAuth() sets resourceMetadataUrl on the verified token, so the SDK's challenge points clients at your protected resource metadata.

Register the tool with a scope challenge

Pass requireAdditionalScopes() the scopes the tool needs, as separate arguments, and use the result as the tool's scopeChallenge option.

Update index.ts. On SDK 2, tool callbacks read the token from ctx.http?.authInfo. toNodeHandler() passes the SDK the req.auth that mcpAuth() sets. It also needs req.body, because express.json() has already read the request body.

index.ts
import { clerkClient } from '@clerk/express'
import {
  authorizationServerMetadataHandler,
  isClerkAuthInfo,
  mcpAuth,
  type McpAuthOptions,
  protectedResourceMetadataHandler,
  requireAdditionalScopes,
  streamableHttpHandler,
} from '@clerk/mcp-tools/express'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'
import { toNodeHandler } from '@modelcontextprotocol/node'
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'
import express from 'express'
import cors from 'cors'

// ...

function createServer() {
  const server = new McpServer({ name: 'profile-server', version: '1.0.0' })
  // `authInfo.token` is the bearer token, so return only the user ID that `mcpAuth()` puts in `extra`
  server.registerTool(
    'whoami',
    { description: 'Gets the ID of the Clerk user who authorized this request' },
    async ({ authInfo }) => {
      if (!isClerkAuthInfo(authInfo)) {
        return { content: [{ type: 'text', text: 'Not signed in with Clerk.' }], isError: true }
      }
      return { content: [{ type: 'text', text: authInfo.extra.userId }] }
    },
    async (ctx) => {
      const authInfo = ctx.http?.authInfo
      if (!isClerkAuthInfo(authInfo)) {
        return { content: [{ type: 'text', text: 'Not signed in with Clerk.' }], isError: true }
      }
      return { content: [{ type: 'text', text: authInfo.extra.userId }] }
    },
  )
  server.registerTool(
    'get_primary_email',
    {
      description: 'Gets the authorized user’s primary email address. Needs the email scope.',
      scopeChallenge: requireAdditionalScopes('email'),
    },
    async (ctx) => {
      const authInfo = ctx.http?.authInfo
      if (!isClerkAuthInfo(authInfo)) throw new Error('Missing authenticated user')
      return {
        content: [{ type: 'text', text: await getPrimaryEmail(authInfo.extra.userId) }],
      }
    },
  )
  return server
}

const mcpHandler = toNodeHandler(createMcpHandler(createServer))

app.all(
  '/mcp',
  mcpAuth(mcpAuthOptions),
  streamableHttpHandler(createServer, StreamableHTTPServerTransport),
  (req, res) => mcpHandler(req, res, req.body),
)

// The rest of your code...

Test step-up

Restart your server, and then:

  1. Clear your MCP client's stored token for this server, and reconnect to get a new token. An older token may already include email, which lets get_primary_email run without step-up. If your client requests the baseline scopes, the consent screen lists your basic profile information but not your email address.
  2. Ask the agent "What's my email address?" so it calls get_primary_email.
  3. If your client supports step-up, the consent screen opens again for email, and the call succeeds.

Claude Code doesn't open the consent screen on its own. Run /mcp, re-authenticate the server, and then ask again.

If the consent screen doesn't open again, check the 403 response's WWW-Authenticate header. Confirm that its scope lists the token's granted scopes followed by email, such as profile offline_access email, and that its resource_metadata is your protected resource metadata URL. If Clerk then answers the authorization request with invalid_scope, allow clients to request the step-up scope.

Best practices

Follow these rules for every tool that uses scopeChallenge.

Challenge for the full scope set

The scope in the challenge is the complete set the client should request, including the scopes the token already has. Some clients request exactly the challenged scopes, and the new token replaces the old one. A challenge for only email produces a token without profile, and every other tool then fails or challenges again.

requireAdditionalScopes() requires the tool's scopes in addition to the ones the token already has, so it challenges for the granted scopes plus the missing ones. The SDK's own requireScopes() helper challenges for only the scopes you list, so if you use it, list every scope the token should keep.

Keep step-up scopes out of the baseline

Add the step-up scope only to the tool's scopeChallenge, unless you want clients to request it up front. Keep it out of route checks. The 401 challenge and your metadata also shape what clients ask for at first consent:

  • Route checks, such as the requiredScopes in mcpAuthOptions, run on every request, including initialize and tools/list. A step-up scope there rejects every token without it.
  • The 401 challenge names the scopes clients should request when they first connect, according to MCP 2025-11-25's scope selection strategy. The authentication on /mcp lists the requiredScopes there, followed by the challengeScopes. A token doesn't need the challengeScopes. Add a step-up scope to challengeScopes only if you want clients to request it up front, for example to let clients without step-up support use the tool.
  • scopes_supported in your protected resource metadata guides what MCP clients request when they first connect if the 401 challenge names no scopes. protectedResourceMetadataHandler() lists only the requiredScopes and challengeScopes from the options you pass it. If you set scopes_supported yourself in its properties, include every required and challenge scope. Otherwise, the handler throws at startup. scopes_supported doesn't limit what a token can get, so a client can still request step-up scopes up front.

Limitations

scopeChallenge applies only to certain requests, so check these limits before you rely on it.

When the SDK sends the challenge

The SDK sends the challenge only when:

  • The server runs @modelcontextprotocol/server 2.1 or later. This guide requires 2.3.0 or later for its security fixes. On MCP SDK 1.x (@modelcontextprotocol/sdk), tools must check their scopes themselves.
  • The request comes over HTTP, through createMcpHandler() or the Streamable HTTP transport. Over stdio (local servers), the tool runs without the challenge.
  • The request carries auth info. requireAdditionalScopes() and requireScopes() leave unauthenticated requests to your authentication check.

How the challenge behaves

  • The SDK checks tools/call, resources/read, and prompts/get requests for enabled tools, resources, and prompts. registerResource() and registerPrompt() accept the same scopeChallenge option.
  • The challenge runs before the SDK validates the tool's arguments.
  • tools/list still includes the tool for tokens that lack its scope. Mention the step-up scope in the tool's description so agents know to expect another consent screen.
  • If a scopeChallenge function throws, the SDK answers 500 Internal Server Error. requireAdditionalScopes() validates its scopes when you call it. It throws a Clerk error: at startup if you pass no scopes, an empty string, or a scope with a space, quote, backslash, control character, or non-ASCII character.

Feedback

What did you think of this content?

Last updated on