Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149---title: Supabase 🤝 FastMCPsidebarTitle: Supabasedescription: Secure your FastMCP server with Supabase Authicon: shield-checktag: NEW---
import { VersionBadge } from "/snippets/version-badge.mdx"
<VersionBadge version="2.13.0" />
This guide shows you how to secure your FastMCP server using **Supabase Auth**. This integration uses the [**Remote OAuth**](/v2/servers/auth/remote-oauth) pattern, where Supabase handles user authentication and your FastMCP server validates the tokens.
## Configuration
### Prerequisites
Before you begin, you will need:1. A **[Supabase Account](https://supabase.com/)** with a project or a self-hosted **Supabase Auth** instance2. Your FastMCP server's URL (can be localhost for development, e.g., `http://localhost:8000`)
### Step 1: Get Supabase Project URL
In your Supabase Dashboard:1. Go to **Project Settings**2. Copy your **Project URL** (e.g., `https://abc123.supabase.co`)
### Step 2: FastMCP Configuration
Create your FastMCP server using the `SupabaseProvider`:
```python server.pyfrom fastmcp import FastMCPfrom fastmcp.server.auth.providers.supabase import SupabaseProvider
# Configure Supabase Authauth = SupabaseProvider( project_url="https://abc123.supabase.co", base_url="http://localhost:8000", auth_route="/my/auth/route" # if self-hosting and using custom routes)
mcp = FastMCP("Supabase Protected Server", auth=auth)
@mcp.tooldef protected_tool(message: str) -> str: """This tool requires authentication.""" return f"Authenticated user says: {message}"
if __name__ == "__main__": mcp.run(transport="http", port=8000)```
## Testing
### Running the Server
Start your FastMCP server with HTTP transport to enable OAuth flows:
```bashfastmcp run server.py --transport http --port 8000```
Your server is now running and protected by Supabase authentication.
### Testing with a Client
Create a test client that authenticates with your Supabase-protected server:
```python client.pyfrom fastmcp import Clientimport asyncio
async def main(): # The client will automatically handle Supabase OAuth async with Client("http://localhost:8000/mcp", auth="oauth") as client: # First-time connection will open Supabase login in your browser print("✓ Authenticated with Supabase!")
# Test the protected tool result = await client.call_tool("protected_tool", {"message": "Hello!"}) print(result)
if __name__ == "__main__": asyncio.run(main())```
When you run the client for the first time:1. Your browser will open to Supabase's authorization page2. After you authorize, you'll be redirected back3. The client receives the token and can make authenticated requests
## Environment Variables
For production deployments, use environment variables instead of hardcoding credentials.
### Provider Selection
Setting this environment variable allows the Supabase provider to be used automatically without explicitly instantiating it in code.
<Card><ParamField path="FASTMCP_SERVER_AUTH" default="Not set">Set to `fastmcp.server.auth.providers.supabase.SupabaseProvider` to use Supabase authentication.</ParamField></Card>
### Supabase-Specific Configuration
These environment variables provide default values for the Supabase provider, whether it's instantiated manually or configured via `FASTMCP_SERVER_AUTH`.
<Card><ParamField path="FASTMCP_SERVER_AUTH_SUPABASE_PROJECT_URL" required>Your Supabase project URL (e.g., `https://abc123.supabase.co`)</ParamField>
<ParamField path="FASTMCP_SERVER_AUTH_SUPABASE_BASE_URL" required>Public URL of your FastMCP server (e.g., `https://your-server.com` or `http://localhost:8000` for development)</ParamField>
<ParamField path="FASTMCP_SERVER_AUTH_SUPABASE_AUTH_ROUTE" default="/auth/v1">Your Supabase auth route (e.g., `/auth/v1`)</ParamField>
<ParamField path="FASTMCP_SERVER_AUTH_SUPABASE_REQUIRED_SCOPES" default="[]">Comma-, space-, or JSON-separated list of required OAuth scopes (e.g., `openid email` or `["openid", "email"]`)</ParamField></Card>
Example `.env` file:```bash# Use the Supabase providerFASTMCP_SERVER_AUTH=fastmcp.server.auth.providers.supabase.SupabaseProvider
# Supabase configurationFASTMCP_SERVER_AUTH_SUPABASE_PROJECT_URL=https://abc123.supabase.coFASTMCP_SERVER_AUTH_SUPABASE_BASE_URL=https://your-server.comFASTMCP_SERVER_AUTH_SUPABASE_REQUIRED_SCOPES=openid,email```
With environment variables set, your server code simplifies to:
```python server.pyfrom fastmcp import FastMCP
# Authentication is automatically configured from environmentmcp = FastMCP(name="Supabase Protected Server")```