Control access to components using callable-based authorization checks that filter visibility and enforce permissions.
New in version 3.0.0Authorization controls what authenticated users can do with your FastMCP server. While authentication verifies identity (who you are), authorization determines access (what you can do). FastMCP provides a callable-based authorization system that works at both the component level and globally via middleware.The authorization model centers on a simple concept: callable functions that receive context about the current request and return True to allow access or False to deny it. Multiple checks combine with AND logic, meaning all checks must pass for access to be granted.
Authorization relies on OAuth tokens which are only available with HTTP transports (SSE, Streamable HTTP). In STDIO mode, there’s no OAuth mechanism, so get_access_token() returns None and all auth checks are skipped.
When an AuthProvider is configured, all requests to the MCP endpoint must carry a valid token—unauthenticated requests are rejected at the transport level before any auth checks run. Authorization checks therefore differentiate between authenticated users based on their scopes and claims, not between authenticated and unauthenticated users.
An auth check is any callable that accepts an AuthContext and returns a boolean. Auth checks can be synchronous or asynchronous, so checks that need to perform async operations (like reading server state or calling external services) work naturally.
Copy
from fastmcp.server.auth import AuthContextdef my_custom_check(ctx: AuthContext) -> bool: # ctx.token is AccessToken | None # ctx.component is the Tool, Resource, or Prompt being accessed return ctx.token is not None and "special" in ctx.token.scopes
FastMCP provides two built-in auth checks that cover common authorization patterns.
Scope-based authorization checks that the token contains all specified OAuth scopes. When multiple scopes are provided, all must be present (AND logic).
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import require_scopesmcp = FastMCP("Scoped Server")@mcp.tool(auth=require_scopes("admin"))def admin_operation() -> str: """Requires the 'admin' scope.""" return "Admin action completed"@mcp.tool(auth=require_scopes("read", "write"))def read_write_operation() -> str: """Requires both 'read' AND 'write' scopes.""" return "Read/write action completed"
Tag-based restrictions apply scope requirements conditionally. If a component has the specified tag, the token must have the required scopes. Components without the tag are unaffected.
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import restrict_tagfrom fastmcp.server.middleware import AuthMiddlewaremcp = FastMCP( "Tagged Server", middleware=[ AuthMiddleware(auth=restrict_tag("admin", scopes=["admin"])) ])@mcp.tool(tags={"admin"})def admin_tool() -> str: """Tagged 'admin', so requires 'admin' scope.""" return "Admin only"@mcp.tool(tags={"public"})def public_tool() -> str: """Not tagged 'admin', so no scope required by the restriction.""" return "Anyone can access"
Any callable that accepts AuthContext and returns bool can serve as an auth check. This enables authorization logic based on token claims, component metadata, or external systems.
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import AuthContextmcp = FastMCP("Custom Auth Server")def require_premium_user(ctx: AuthContext) -> bool: """Check for premium user status in token claims.""" if ctx.token is None: return False return ctx.token.claims.get("premium", False) is Truedef require_access_level(minimum_level: int): """Factory function for level-based authorization.""" def check(ctx: AuthContext) -> bool: if ctx.token is None: return False user_level = ctx.token.claims.get("level", 0) return user_level >= minimum_level return check@mcp.tool(auth=require_premium_user)def premium_feature() -> str: """Only for premium users.""" return "Premium content"@mcp.tool(auth=require_access_level(5))def advanced_feature() -> str: """Requires access level 5 or higher.""" return "Advanced feature"
Auth checks can be async functions, which is useful when the authorization decision depends on asynchronous operations like reading server state or querying external services.
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import AuthContextmcp = FastMCP("Async Auth Server")async def check_user_permissions(ctx: AuthContext) -> bool: """Async auth check that reads server state.""" if ctx.token is None: return False user_id = ctx.token.claims.get("sub") # Async operations work naturally in auth checks permissions = await fetch_user_permissions(user_id) return "admin" in permissions@mcp.tool(auth=check_user_permissions)def admin_tool() -> str: return "Admin action completed"
Sync and async checks can be freely combined in a list — each check is handled according to its type.
The auth parameter on decorators controls visibility and access for individual components. When auth checks fail for the current request, the component is hidden from list responses and direct access returns not-found.
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import require_scopesmcp = FastMCP("Component Auth Server")@mcp.tool(auth=require_scopes("write"))def write_tool() -> str: """Only visible to users with 'write' scope.""" return "Written"@mcp.resource("secret://data", auth=require_scopes("read"))def secret_resource() -> str: """Only visible to users with 'read' scope.""" return "Secret data"@mcp.prompt(auth=require_scopes("admin"))def admin_prompt() -> str: """Only visible to users with 'admin' scope.""" return "Admin prompt content"
Component-level auth controls both visibility (list filtering) and access (direct lookups return not-found for unauthorized requests). Additionally use AuthMiddleware to apply server-wide authorization rules and get explicit AuthorizationError responses on unauthorized execution attempts.
For server-wide authorization enforcement, use AuthMiddleware. This middleware applies auth checks globally to all components—filtering list responses and blocking unauthorized execution with explicit AuthorizationError responses.
Copy
from fastmcp import FastMCPfrom fastmcp.server.auth import require_scopesfrom fastmcp.server.middleware import AuthMiddlewaremcp = FastMCP( "Enforced Auth Server", middleware=[AuthMiddleware(auth=require_scopes("api"))])@mcp.tooldef any_tool() -> str: """Requires 'api' scope to see AND call.""" return "Protected"
Component-level auth and AuthMiddleware work together as complementary layers. The middleware applies server-wide rules to all components, while component-level auth adds per-component requirements. Both layers are checked—all checks must pass.
Tools can access the current authentication token using get_access_token() from fastmcp.server.dependencies. This enables tools to make decisions based on user identity or permissions beyond simple authorization checks.
Copy
from fastmcp import FastMCPfrom fastmcp.server.dependencies import get_access_tokenmcp = FastMCP("Token Access Server")@mcp.tooldef personalized_greeting() -> str: """Greet the user based on their token claims.""" token = get_access_token() if token is None: return "Hello, guest!" name = token.claims.get("name", "user") return f"Hello, {name}!"@mcp.tooldef user_dashboard() -> dict: """Return user-specific data based on token.""" token = get_access_token() if token is None: return {"error": "Not authenticated"} return { "client_id": token.client_id, "scopes": token.scopes, "claims": token.claims, }
The AuthContext dataclass is passed to all auth check functions.
Property
Type
Description
token
AccessToken | None
Current access token, or None if unauthenticated
component
Tool | Resource | Prompt
The component being accessed
Access to the component object enables authorization decisions based on metadata like tags, name, or custom properties.
Copy
from fastmcp.server.auth import AuthContextdef require_matching_tag(ctx: AuthContext) -> bool: """Require a scope matching each of the component's tags.""" if ctx.token is None: return False user_scopes = set(ctx.token.scopes) return ctx.component.tags.issubset(user_scopes)