Sign agents in to your app with a one-time link, Beta
Before you start
When an agent uses a browser for your user and reaches your app's sign-in page, it usually stops there, or asks the user for a password. A create_browser_session tool on your MCP server lets the agent get a one-time link instead. The link opens your app already signed in as the user that the MCP access token belongs to.
You add the tool with @clerk/mcp-tools. It creates the link with Agent Tasks and takes the user from the verified access token, never from the tool's input. The agent can set only the path in your app and a short task description.
This guide adds the tool to the MCP server from Build an MCP server. The tool relies on the authentication that guide adds to /mcp. It checks the token's audience and marks the token as verified by Clerk. Without the audience check, an access token your instance issued to any OAuth application could create a link.
This guide requires @clerk/mcp-tools 1.0.0 or later and Express 5 or later. The tools in @clerk/mcp-tools/tools also need zod 3.25 or later. If your server runs @modelcontextprotocol/server 2.x, use zod 4.2 or later.
npm install @clerk/mcp-tools@latest zodpnpm add @clerk/mcp-tools@latest zodyarn add @clerk/mcp-tools@latest zodbun add @clerk/mcp-tools@latest zodCreate a scope for the tool
The link signs a browser in to your app as the user, so require a scope that the user approves for each MCP client. Use a scope for this tool only, not one that nearly every client gets, such as profile or email. With a common scope, every MCP client the user connects can open your app as them.
- In the Clerk Dashboard, navigate to the OAuth applications page.
- Select the Scopes tab and create a custom scope named
browser_session. - Assign the scope to the OAuth applications that may use the tool.
- If MCP clients register themselves through Client ID Metadata Documents (CIMD) or dynamic client registration, select the Settings tab and add the scope to Allowed scopes for dynamic clients. The list limits what those clients can request, so also include every default scope and any other scope they need.
Don't add the scope to the default scopes, so a client gets it only after the user approves it.
In index.ts, define the scope above mcpAuthOptions:
const BROWSER_SESSION_SCOPES = ['browser_session']Keep it out of requiredScopes. Every request to your MCP server needs those scopes, while only this tool needs BROWSER_SESSION_SCOPES.
Advertise the scope
List the scope in the 401 challenge and in the protected resource metadata so clients request it when they first connect. Clients without step-up authorization have no other way to ask for the scope. Under MCP 2025-11-25's scope selection strategy, clients should request the scope from the initial 401 challenge. If the challenge has no scope, they should fall back to scopes_supported from the protected resource metadata. Add the scope to mcpAuthOptions as challengeScopes. The authentication on /mcp then lists it after requiredScopes in the challenge. Unlike requiredScopes, a token doesn't need it.
The metadata route takes the same mcpAuthOptions, so its scopes_supported includes the scope too. It lists requiredScopes first, then challengeScopes. If either list includes a scope a client can't request, Clerk rejects the whole request with invalid_scope and the client can't connect to your MCP server. Complete the previous step first.
// ...
const mcpAuthOptions = {
// ...
challengeScopes: BROWSER_SESSION_SCOPES,
} satisfies McpAuthOptionsRegister the tool
Create the tool with createBrowserSessionTool() and pass the result to server.registerTool(). Set destinationUrl to the page of your app the link opens. It must be on a domain of the same Clerk instance as your MCP server. Keep the localhost value during local development and replace APP_URL with your HTTPS URL when you deploy.
import { browserSessionInstructions, createBrowserSessionTool } from '@clerk/mcp-tools/tools'
// ...
const APP_URL = 'http://localhost:3000/'
// Created once, so a bad configuration fails at startup instead of on every request
const browserSessionTool = createBrowserSessionTool({
secretKey: process.env.CLERK_SECRET_KEY!,
destinationUrl: APP_URL,
requiredScopes: BROWSER_SESSION_SCOPES,
})
function createServer() {
const server = new McpServer(
{ name: 'profile-server', version: '1.0.0' },
{ instructions: browserSessionInstructions(APP_URL) },
)
server.registerTool(...browserSessionTool)
// Your other tools...
return server
}
// The rest of your code...createServer() runs for each request, so create the tool outside it.
browserSessionInstructions() tells agents about the tool in your server's instructions. Some MCP clients show agents only the server instructions until the agent searches for a tool, so without them an agent may open your app and stop at the sign-in page.
Try it
Start your app and connect an MCP client that can use a browser. Ensure the agent's browser profile isn't signed in to your app. If Clerk detects another session in that browser, it refuses the link, and the link stays valid.
Ask the agent to open your app. It should call create_browser_session, either first or when it reaches the sign-in page, and open the link it gets back. If your MCP client asks, approve the browser_session scope.
If you haven't followed Request more scopes with step-up authorization, your server runs MCP SDK 1.x, which has no step-up authorization. When the access token lacks the tool's scope, the tool returns an error. The error tells the agent to ask the user to reconnect your MCP server and approve the scope. If you followed that guide, your server runs @modelcontextprotocol/server 2.3.0 or later. A token that lacks the tool's scope gets the step-up challenge instead.
What the session can do
By default, the link starts a restricted session. The session can read data and refresh its own session token. Clerk's Frontend API refuses every other write from it, including:
- Session touch requests, which update the session's last active time
- Sign-in, sign-up, and sign-out
- A switch to another session or Organization
- Changes to the user's own profile and Organizations
- Reverification, and OAuth consent and approval
The agent controls the browser, so these limits keep it from approving its own OAuth grant or changing the user's credentials.
Clerk also refuses JWT template and Firebase tokens, so getToken({ template }) fails in a restricted session.
Your app's backend sees an ordinary signed-in session, and Clerk doesn't limit what your own API lets it do. To treat agent sessions differently in your backend, see Restrict agent sessions.
If Clerk doesn't confirm the restriction, the tool tries to revoke the link, returns no link, and fails with the restricted_not_applied error code.
To start an ordinary session instead, pass restricted: false to createBrowserSessionTool().
Security and limits
- The link is a credential: anyone who has it before it's used or expires can sign in as the user. The tool never includes it in errors or logs, but the tool result contains it so that the agent can open it. Don't log tool results, and treat MCP client and gateway logs of tool results as credentials.
- Each link works once and expires after 1 hour. The session it starts lasts up to 30 minutes by default. Set
sessionMaxDurationInSecondsto change that. - The agent can pass a
path, such as/settings, with an optional query and hash. It replaces the path ofdestinationUrl, and the tool refuses any path that would leave your app. - The session starts in the user's last active Organization if they're still a member, and otherwise in their personal workspace. A restricted session can't start in the personal workspace when your instance has Membership required turned on. In that case, the tool fails with the
organization_rejectederror code. A restricted session can't switch to another Organization. - To start the session in a specific Organization, pass
organizationIdtocreateBrowserSessionTool(). It takes an Organization ID, or a function that returns one from the verified auth info. The function can returnundefinedto let Clerk choose. If the function throws or returns anything else, the tool fails with theorganization_lookup_failederror code. To fail with a different code, such asorganization_rejected, throw aBrowserSessionErrorwith that code.
Feedback
Last updated on