OAuth 2.1 MCP Server

OAuth 2.1 MCP Server

A Next.js-based MCP server template that implements OAuth 2.1 authentication with support for both SSE and Streamable HTTP transports. Provides a reference implementation for building authenticated MCP servers that work with Claude, Cursor, VSCode, and other MCP clients.

Category
访问服务器

README

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

Note: it was forked from: https://github.com/run-llama/mcp-nextjs with 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-adapter to 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.

Using with

Claude Desktop and Claude.ai

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 like https://example.com/mcp/sse (the /sse at the end is important!). Note that Claude Desktop and Web will not accept a localhost URL.

Cursor

Edit your mcp.json to look like this:

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

VSCode

VSCode currently doesn'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 run Authentication: Remove Dynamic Authentication Providers to evict the client ID from VSCode.

MCP Inspector

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

Note, open the link with MCP_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 to https://example.com/mcp/sse instead.

Running the server

pnpm install
pnpm run db:generate
pnpm run dev

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

Environment variables

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_URL is required for OAuth authentication to work, this is where sessions etc. live.

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

Database Commands

Common Drizzle ORM commands:

  • 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

Architecture

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).

Database Schema

The database schema is defined in src/lib/db/schema.ts using 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

You'll also notice:

  • 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.

Deploying to production

This app only works if deployed to Vercel currently, due to its dependence on the @vercel/mcp-adapter package, 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 add pnpm run db:generate to your build command, and of course you'll need all the same environment variables as in the development environment.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选