How to Add Authentication to Your MCP Server
A practical guide to securing MCP server tools with OAuth 2.1 — and how xmcp's auth plugins (Better Auth, Clerk, Auth0, WorkOS, Scalekit) make it straightforward.
An MCP server without authentication is an open door. Any client that knows your endpoint URL can call your tools, read your resources, and consume your compute. For production MCP servers — especially ones with access to real data or paid APIs — auth is not optional.
This guide explains how MCP authentication works and how xmcp handles it.
How MCP authentication works
The MCP spec delegates authentication to the transport layer. For HTTP transports (which is what most remote MCP servers use), that means OAuth 2.1. The flow looks like this:
- The MCP client (Claude, Cursor, etc.) tries to connect to your server.
- Your server responds with a
401 Unauthorizedand aWWW-Authenticateheader pointing to protected-resource metadata that identifies the authorization server. - The client initiates an OAuth flow — the user authenticates and grants access.
- The client sends subsequent requests with a Bearer token in the
Authorizationheader. - Your server validates the token on each request before executing any tool.
The critical part is step 5: your server has to validate every incoming token. That requires an authorization server — something that issues and verifies tokens. Rolling this yourself is non-trivial.
The problem with DIY auth
Building an OAuth authorization server from scratch means handling token issuance, refresh, revocation, PKCE flows, and client registration. For a side project or internal tool, that's often more work than the MCP server itself.
This is the gap xmcp's auth plugins fill.
Auth plugins in xmcp
xmcp offers provider-specific authentication integrations. Each has its own configuration and validation behavior; choose one that matches your identity system and follow its setup guide.
The available plugins are:
| Plugin | Provider |
|---|---|
@xmcp-dev/better-auth | Better Auth (self-hosted, PostgreSQL) |
@xmcp-dev/clerk | Clerk |
@xmcp-dev/auth0 | Auth0 |
@xmcp-dev/workos | WorkOS |
@xmcp-dev/scalekit | Scalekit |
You pick the one that matches your auth infrastructure. If you don't have existing auth infrastructure and want full control, Better Auth is the self-hosted option. If you want managed auth, Clerk, Auth0, WorkOS, and Scalekit are all supported.
A quick example with Better Auth
Install the provider and PostgreSQL dependencies:
Configure HTTP in xmcp.config.ts, then register the provider as middleware:
Set these environment variables and create the required database tables before starting the server. Use the Better Auth setup and runnable example for the complete configuration.
What authenticated tools look like
Once auth is configured, your tool handlers can access the authenticated user's identity from the request context. Tools that don't need the identity don't change at all — auth is enforced at the transport layer, not inside every handler.
Choosing an auth provider
- No existing auth, want self-hosted control? → Better Auth with PostgreSQL.
- Want managed auth, fast setup? → Clerk or Auth0.
- Enterprise SSO / B2B? → WorkOS or Scalekit.
Provider APIs are not interchangeable. Review the integration catalog for each provider’s middleware, environment variables, and identity helpers. Authentication establishes identity; your tools must still enforce access to the requested records.
Next steps
- Better Auth integration guide — complete setup with database and client configuration.
- Securing Your MCP Server — broader security considerations beyond auth.
- Authentication docs — authentication approaches and provider setup.
