---
title: "Solving the MCP Authentication Headache with Vercel & Better Auth"
description: "Remote MCP servers can authenticate users with OAuth instead of plaintext bearer tokens or UUIDs stored in a client's mcp.json, using Better Auth's MCP plugin together with Vercel's @vercel/mcp-adapter in a Next.js app."
url: "https://swe.quest/articles/solving-the-mcp-authentication-headache-with-vercel-better-auth-749640ed1439e99c"
canonical: "https://neon.com/blog/solving-mcp-with-vercel-and-better-auth"
source: "Neon"
published: "2025-05-23"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["identity-auth","ai-agents","typescript"]
---

# Solving the MCP Authentication Headache with Vercel & Better Auth

A post on [Neon](https://swe.quest/sources/neon.md), published 2025-05-23, 5 min read, How-to. Original: [https://neon.com/blog/solving-mcp-with-vercel-and-better-auth](https://neon.com/blog/solving-mcp-with-vercel-and-better-auth)

## Summary

Remote MCP servers can authenticate users with OAuth instead of plaintext bearer tokens or UUIDs stored in a client's mcp.json, using Better Auth's MCP plugin together with Vercel's @vercel/mcp-adapter in a Next.js app. Setup touches three files: add the mcp plugin with a login page to the Better Auth config, expose the .well-known/oauth-authorization-server discovery route, and wrap the MCP handler so it resolves the user's session via getMCPSession and returns 401 when none exists. The session's user ID can then scope tool access to that user's own data. Clients such as Cursor, connecting through mcp-remote, open a login popup on first use.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Identity & auth](https://swe.quest/tags/identity-auth.md) (Security)
- [AI agents](https://swe.quest/tags/ai-agents.md) (AI & ML)
- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)

## Prerequisites

- Next.js route handlers ([easy posts on React](https://swe.quest/tags/react.md?difficulty=easy))
- OAuth basics ([easy posts on Identity & auth](https://swe.quest/tags/identity-auth.md?difficulty=easy))
- Model Context Protocol

## Keywords

Model Context Protocol, Better Auth, @vercel/mcp-adapter, Next.js, OAuth, mcp-remote, Cursor

## Related posts

- [Bringing MCP to the Cloud](https://swe.quest/articles/bringing-mcp-to-the-cloud-de1944b558e43ff0.md): Neon · 2025-04-11 · Medium · 8 min read · AI agents, Identity & auth, API design, JavaScript
  Neon moved its MCP server from a locally spawned stdio process authenticated by a long-lived API key to a hosted server at mcp.neon.tech, using SSE for server-to-client messages and HTTP POST to a /messages endpoint routed by an in-memory session map.
- [Every app needs an MCP server, and you can build it with Neon](https://swe.quest/articles/every-app-needs-an-mcp-server-and-you-can-build-it-with-neon-1a5ce4eab0583eca.md): Neon · 2026-11-25 · Easy · 11 min read · AI agents, Identity & auth, Serverless, PostgreSQL
  An MCP server lets users drive an app from AI assistants like ChatGPT, Claude and Cursor through tools the app defines, which keeps authorization and validation in app code instead of exposing the UI or raw database access.
- [Managed Better Auth is now an OAuth provider, with MCP support](https://swe.quest/articles/managed-better-auth-is-now-an-oauth-provider-with-mcp-support-613f00a7521407ac.md): Neon · 2026-11-02 · Medium · 5 min read · Identity & auth, AI agents, PostgreSQL
  Neon's Managed Better Auth now supports the OAuth Provider plugin, turning an auth instance into an OAuth 2.1 authorization server that both companion apps and MCP clients can use to sign users in.
- [MCP or CLI? Yes.](https://swe.quest/articles/mcp-or-cli-yes-bc20ce304cab3de5.md): Sentry · 2026-11-10 · Medium · 6 min read · AI agents, Developer tools, Identity & auth
  Sentry now ships its MCP server launchable from the CLI (\`sentry mcp\`), running stdio mode with a shim that reuses CLI credentials via a separate \`Sentry-Bearer\` token header that bypasses the OAuth proxy's CIMD/DCR flow.
- [Better Rails for Agents: A New Remote MCP and Railway Agent in the CLI](https://swe.quest/articles/better-rails-for-agents-a-new-remote-mcp-and-railway-agent-in-the-cli-4b9abaf62a02f3cd.md): Railway · 2026-04-17 · Medium · 13 min read · AI agents, API design, Developer tools, TypeScript
  Railway shipped a remote MCP server at mcp.railway.com, which connects to MCP clients over OAuth with no local install, and a new \`railway agent\` CLI command for interactive, scripted or CI use with JSON output.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
