Skip to content
Back to blog

Integrating Better Auth with xmcp

Learn how to add secure authentication to your xmcp MCP server using Better Auth with PostgreSQL — sessions, account linking, and OAuth 2.1 providers.

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

Info

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 22+ installed
  • Access to a PostgreSQL database (we recommend Neon for easy setup)

Install Dependencies

Start by installing the Better Auth plugin and PostgreSQL dependencies:

Shell

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

Shell

Set Up Your Database

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

Database Setup

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:

SQL
Info

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:

Shell
Security Note

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 src/middleware.ts file and configure the Better Auth provider:

TypeScript

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:

TypeScript

Google OAuth

To enable Google OAuth, you'll need to:

  1. Create a Web application OAuth client in the Google Cloud Console, following Better Auth’s Google setup.
  2. Configure the consent screen and any required test users.
  3. Set the authorized redirect URI to ${BETTER_AUTH_BASE_URL}/auth/callback/google, matching this xmcp plugin’s configured base URL exactly. For the configuration above, use http://127.0.0.1:3001/auth/callback/google.
TypeScript
Info

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:

TypeScript

Access Sessions in Your Tools

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

TypeScript
Warning

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.

Run the Better Auth HTTP example and see the integration reference for the current setup. Set a generated BETTER_AUTH_SECRET before starting; do not use a shared fallback secret.

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