rembrembdocs

Model Context Protocol (MCP) Authentication


The Model Context Protocol (MCP) is an open standard for connecting AI agents and LLM tools to data sources and services. While Supabase doesn't provide MCP server functionality, you can build your own MCP servers that connect to your Supabase project and leverage Supabase Auth's OAuth 2.1 capabilities to authenticate AI agents using your existing user base.

Why use Supabase Auth for MCP?#

When building MCP servers that connect to your Supabase project, you can leverage your existing Supabase Auth infrastructure to authenticate AI agents:

How MCP authentication works#

When you build an MCP server that connects to your Supabase project, authentication flows through Supabase Auth:

  1. Discovery: The MCP client fetches your OAuth configuration from Supabase's discovery endpoint
  2. Registration (optional): The client registers itself as an OAuth client in your Supabase project
  3. Authorization: User is redirected to your authorization endpoint to approve the AI tool's access
  4. Token exchange: Supabase issues access and refresh tokens for the authenticated user
  5. Authenticated access: The MCP server can now make requests to your Supabase APIs on behalf of the user

By leveraging Supabase Auth, your MCP server can authenticate AI agents using your existing user accounts without building a separate authentication system.

Prerequisites#

Before setting up MCP authentication:

Setting up your MCP server#

Configure your MCP server to use your Supabase Auth server:

1https://<project-ref>.supabase.co/auth/v1

Replace <project-ref> with your project reference ID from the Supabase dashboard.

MCP clients will automatically discover your OAuth configuration from:

1https://<project-ref>.supabase.co/.well-known/oauth-authorization-server/auth/v1

OAuth client setup#

Depending on your MCP server implementation, you have two options:

Dynamic registration allows any MCP client to register with your project. Consider:

Building an MCP server with Supabase Auth#

When building your own MCP server, integrate with Supabase Auth to authenticate AI agents as your existing users and leverage your RLS policies.

Looking for an easier way to build MCP servers?

FastMCP provides a streamlined way to build MCP servers with built-in Supabase Auth integration. FastMCP handles OAuth configuration, token management, and authentication flows automatically, letting you focus on building your AI agent's functionality. Check out their Supabase integration guide to get started quickly.

Handling MCP tokens in your application#

When your MCP server makes requests to your Supabase APIs on behalf of authenticated users, it will send access tokens issued by Supabase Auth, just like any other OAuth client.

Validating MCP tokens#

Use the same token validation as other OAuth clients.

See Token Security & RLS for more examples.

Security considerations#

User approval#

Always require explicit user approval for MCP clients:

Troubleshooting#

MCP client can't discover OAuth configuration#

Problem: Client shows "OAuth discovery failed" or similar error.

Solutions:

Dynamic registration fails#

Problem: Client receives 403 or 404 on registration endpoint.

Solutions:

Token exchange fails#

Problem: Client receives "invalid_grant" error.

Solutions:

RLS policies block MCP access#

Problem: MCP client can't access data despite valid token.

Solutions:

Next steps#