How we turned our Next.js App Router API into an MCP Server (Model Context Protocol) for local and remote LLM orchestration
Hey r/nextjs,
We recently refactored our Next.js + Supabase CMS AI pipeline away from in-browser chat generation into a Model Context Protocol (MCP) Server.
The goal was to let developers control database schemas and JSONB layouts directly from external tools like Claude Code, Cursor, or ChatGPT without needing a custom UI wrapper.
We exposed a Route Handler at /api/mcp using Streamable HTTP/SSE transport following the JSON-RPC 2.0 MCP specification.
Here is the general execution loop:
[Claude Code / Cursor / ChatGPT]
│
│ Streamable HTTP/SSE (Bearer Token / OAuth)
▼
┌──────────────────────────────────────────────┐
│ Next.js 16 Route Handler (/api/mcp) │
├──────────────────────────────────────────────┤
│ MCP Tool Registry │
│ - get_database_schema │
│ - generate_jsonb_layout │
│ - query_site_analytics │
│ - update_navigation │
└──────────────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────────────┐
│ Supabase / PostgreSQL │
└──────────────────────────────────────────────┘
NextBlockCMS shared their experience refactoring a Next.js and Supabase CMS AI pipeline into a Model Context Protocol (MCP) Server. This setup allows developers to manage database schemas and JSONB layouts using external tools like Claude Code and ChatGPT. They detailed their architecture, including the use of a Route Handler at /api/mcp and the challenges faced with HTML parsing and SSE streaming.
When running locally, you can connect tools like VS Code / Claude Code natively by pointing .claude/settings.json directly to the local dev server:
JSON
{
"mcpServers": {
"local-cms": {
"url": "http://localhost:3000/api/mcp",
"transport": "sse"
}
}
}
This lets an external LLM query database tables, generate structured JSONB nodes directly into PostgreSQL, or update site configurations without touching the browser.
Curious if anyone else here is exposing MCP servers directly inside Next.js App Router routes? How are you handling authentication and session management for remote SSE connections?