Table of contents
← Back to blog

Integrating Better Auth with xmcp

Learn how to add robust authentication to your MCP server using Better Auth, the most comprehensive authentication framework for TypeScript.

This guide shows how to add authentication using Better Auth and a PostgreSQL database. PostgreSQL is currently the only supported database provider for this plugin.

What You'll Build

By the end of this guide, you'll have:

  • A fully functional authentication system with email/password and OAuth
  • Session management integrated into your xmcp tools
  • A login/signup page

Prerequisites

Before starting, make sure you have:

  • An existing xmcp project
  • Node.js 20+ installed
  • Access to a PostgreSQL database (we recommend Neon for easy setup)

Install Dependencies

Start by installing the Better Auth plugin and PostgreSQL dependencies:

You'll also need the PostgreSQL types as a development dependency:

Set Up Your Database

Create a PostgreSQL database with the required schema. Better Auth requires specific tables for user management, sessions, and OAuth applications.

For a seamless experience, we recommend setting up your database on Neon using Vercel's storage integration.

Run the following SQL script to create the necessary tables:

The schema generation through Better Auth's CLI is not supported yet, so you'll need to run this SQL manually.

Configure Environment Variables

Create a .env file in your xmcp app root directory with the following variables:

Make sure to generate a strong, random secret for BETTER_AUTH_SECRET. This is used to sign JWT tokens and should be kept secure.

Add the Auth Middleware

Create a middleware.ts file in your xmcp app root directory and configure the Better Auth provider:

Configuration Options:

  • database: Must be a valid PostgreSQL Pool instance
  • baseURL: Your app's base URL for generating OAuth callback URLs
  • secret: Secret key for signing JWT tokens
  • providers: Configuration for authentication providers

Configure Auth Providers

Better Auth supports multiple authentication methods. You can enable email/password authentication, OAuth providers, or both.

Email and Password Authentication

To enable email and password authentication:

Google OAuth

To enable Google OAuth, you'll need to:

  1. Go to the Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google+ API
  4. Create OAuth 2.0 credentials
  5. Set the authorized redirect URI to: http://localhost:3001/auth/callback/google (for development)

For production, make sure to update the redirect URI to match your domain: https://yourdomain.com/auth/callback/google

Combined Providers

You can enable both authentication methods:

Access Sessions in Your Tools

Use the getBetterAuthSession function to access the current user session in your xmcp tools:

The getBetterAuthSession function will throw an error if called outside of a betterAuthProvider middleware.

Access the Login Page

The login page will be available at http://host:port/auth/sign-in and is automatically generated by the config you passed to the provider function. It is also the same page for signing up.

Conclusion

You've now added authentication to your MCP server! Next time you establish a connection to the MCP server, you'll be prompted to authenticate.

One framework to rule them all

    Integrating Better Auth with xmcp | xmcp Blog