Skip to main content
FastMCP provides helper functions to simplify authorization logic in your tools, resources, and prompts.

requireAuth()

Requires any authenticated session.
SessionAuth
required
Session authentication data (can be undefined)
boolean
Returns true if session is authenticated, false otherwise

requireScopes()

Requires session to have specific OAuth scopes.
string[]
required
One or more scopes that must be present in the session
(auth: T) => boolean
Returns a function that checks if all required scopes are present

Usage

The function checks if the session has a scopes property (array or Set) and verifies all required scopes are present:

requireRole()

Requires session to have a specific role (OR logic for multiple roles).
string[]
required
One or more roles (user needs at least one)
(auth: T) => boolean
Returns a function that checks if session has any of the allowed roles

Usage

The function checks if the session has a role property (string) matching any of the allowed roles:

requireAll()

Combines multiple authorization checks with AND logic.
Array<((auth: T) => boolean) | boolean>
required
One or more authorization check functions or boolean values
(auth: T) => boolean
Returns a function that returns true only if ALL checks pass

Usage

All checks must pass for access to be granted:

requireAny()

Combines multiple authorization checks with OR logic.
Array<((auth: T) => boolean) | boolean>
required
One or more authorization check functions or boolean values
(auth: T) => boolean
Returns a function that returns true if ANY check passes

Usage

At least one check must pass for access to be granted:

getAuthSession()

Extract and type-cast OAuth session from context. Throws if session is undefined.
SessionAuth
required
Session from context (can be undefined)
T
Typed session object (throws if undefined)

Usage

Use with provider-specific session types:

Complex Authorization Examples

Multi-tier access control

Custom authorization logic

Resource-level authorization

Prompt authorization