OAuth 2.1 MCP Server

by dteam-top

Not rated
GitHub

About

A Next.js template for building MCP servers with OAuth 2.1 authentication, supporting PostgreSQL and Redis.

Details

Author
dteam-top
Categories
Developer Tools, Other, API
Tags
#postgres, #redis

Setup

Install OAuth 2.1 MCP Server in your MCP client (Claude Desktop, Cursor, Windsurf, and others).

Repository: https://github.com/dteam-top/mcp-oauth

Follow the installation instructions in the repository README, then restart your MCP client.

OAuth 2.1 MCP Server as a Next.js app on Vercel

Note: it was forked from:https://github.com/run-llama/mcp-nextjswith the following changes:

- prisma replaced with drizzle ORM
- next-auth replaced with better-auth

This is a Next.js-based application that provides an MCP (Model Context Protocol) server with OAuth 2.1 authentication support. It is intended as a model for building your own MCP server in a Next.js context. It uses the@vercel/mcp-adapterto handle the MCP protocol, in order to support both SSE and Streamable HTTP transports.

In addition to being an OAuth server, it also requires the user authenticate. This is currently configured to use Google as a provider, but you could authenticate users however you want (X, GitHub, your own user/password database etc.) without breaking the OAuth flow.

Claude currently supports only the older SSE transport, so you need to give it a different URL to all the other clients listed here.

Use the "Connect Apps" button and select "Add Integration". Provide the URL likehttps://example.com/mcp/sse(the/sseat the end is important!). Note that Claude Desktop and Web will not accept alocalhostURL.

{ "mcpServers": { "MyServer": { "name": "MCP OAuth Demo", "url": "https://example.com/mcp/mcp", "transport": "http-stream" }, } }

VSCode currentlydoesn't properly evict the client ID, so client registration fails if you accidentally delete the client (the workaround in that issue will resolve it). Otherwise, it works fine. Add this to your settings.json:

"mcp": { "servers": { "My Server": { "url": "https://example.com/mcp/mcp" } } }

If you deleted the client,you need to open the Command Palette and runAuthentication: Remove Dynamic Authentication Providersto evict the client ID from VSCode.

Tell Inspector to connect tohttps://example.com/mcp/mcp, with Streamable HTTP transport.

Note, open the link withMCP_PROXY_AUTH_TOKEN:

🔗 Open inspector with token pre-filled: http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=... (Auto-open is disabled when authentication is enabled)

You can also use the SSE transport by connecting tohttps://example.com/mcp/sseinstead.

pnpm install pnpm run db:generate pnpm run dev

The very first time you will also need to runpnpm run db:pushto create the database tables.

Required environment variables should be in.env:

DATABASE_URL="postgresql://user:pass@server/database" BETTER_AUTH_SECRET="any random string" GOOGLE_CLIENT_ID="a Google OAuth client ID" GOOGLE_CLIENT_SECRET="a Google OAuth client secret" GITHUB_CLIENT_ID=your_github_client_id GITHUB_CLIENT_SECRET=your_github_client_secret DISCORD_CLIENT_ID=your_discord_client_id DISCORD_CLIENT_SECRET=your_discord_client_secret NEXT_PUBLIC_BASE_URL=http://localhost:3000 REDIS_URL="redis://user:pass@host:6379"

DATABASE_URLis required for OAuth authentication to work, this is where sessions etc. live.

REDIS_URLis required if you need SSE transport to work (i.e. you want to support Claude Desktop and Web).

- pnpm run db:generate- Generate database client from schema
- pnpm run db:push- Push schema changes to database (development)
- pnpm run db:migrate- Generate and run migrations (production)
- pnpm run db:studio- Open Drizzle Studio to view/edit data

If you're using this as a template for your own Next.js app, the important parts are:

- /src/app/api/oauth/*- these implement oauth client registration and token exchange
- /src/app/oauth/authorize/page.tsx- this implements the oauth consent screen (it's extremely basic right now)
- /src/mcp/
[transport]/route.ts- this implements the MCP server itself. Your tools, resources, etc. should be defined here.

To handle OAuth your app needs to be able to persist clients, access tokens, etc.. To do this it's using a PostgreSQL database accessed via Drizzle ORM. You can swap this for some other database if you want (it will be easiest if it's another Drizzle-supported database).

The database schema is defined insrc/lib/db/schema.tsusing Drizzle ORM. The main tables are:

- accounts- NextAuth.js account information
- sessions- user sessions
- users- user accounts
- verificationTokens- email verification tokens
- oauthClients- registered OAuth clients
- oauthAccessTokens- issued access tokens
- oauthAuthCodes- authorization codes for the OAuth flow

- src/app/auth.ts- this implements Auth.js authentication to your app itself. It's configured to use Google as a provider, but you can change it to use any other provider supports by Auth.js. This is not required for the MCP server to work, but it's a good idea to have it in place for your own app.
- src/app/api/auth/[...nextauth]/route.ts- this plumbs in the Auth.js authentication, and is again not part of the OAuth implementation.

This app only works if deployed to Vercel currently, due to its dependence on the@vercel/mcp-adapterpackage, which in turn is required to support the old SSE transport. We didn't feel like implementing a whole extra protocol just for Claude Desktop.

Deploy as usual. You'll need to addpnpm run db:generateto your build command, and of course you'll need all the same environment variables as in the development environment.

This is a web browser that enables your coding agent, such as Claude Code, to visit websites on your behalf and assist you in identifying bugs or creating UI test cases.

A FastAPI microservice for health-related operations, featuring JWT authentication and a PostgreSQL database with Alembic migrations.

Manage Aiven cloud services like Kafka, PostgreSQL, and Redis via its API.

One shared context layer for AI agents and humans — live API specs, DB schemas, and versioned contracts across repos so every agent and teammate works from the same source of truth.

The MCP server for Bitrix24 provides AI assistants with structured access to the Bitrix24 API. It delivers up-to-date method descriptions, parameters, and valid values, allowing assistants to work with precise data instead of guesswork. This reduces code errors and accelerates Bitrix24 integration development.

Tool platform by IBM to build, test and deploy tools for any data source

One remote MCP server for 500+ production APIs — Stripe, HubSpot, Postgres, Gmail, and more. OAuth and API key auth, credential management, and a CLI.

An MCP server for interacting with the Postman API, requiring an API key.

Arbitrary code execution and tool-use platform for LLMs by Riza

A command-line tool for interacting with Shopify's Admin GraphQL API, Functions, and Polaris Web Components.

Single tool to control all 100+ API integrations, and UI components

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.