mongodb-chatgpt-sdk-demo is a Next.js 15 starter that shows how to pair MongoDB Atlas, Vercel, and the ChatGPT Apps SDK to build a collaborative to-do assistant. The same Atlas-backed APIs drive both the traditional web UI and the Model Context Protocol (MCP) tools that make the experience available directly inside ChatGPT.
- Shared task lists.
todo_listsandtodo_itemscollections live in Atlas, complete with optimistic UI updates, sharing links, and live refresh via SWR. - Zero-friction local dev.
lib/mongodb.tsdrops into an in-memory store whenMONGODB_URIis unset, so you can demo without a network connection and switch to Atlas by setting one env variable. - ChatGPT-native tooling.
/api/mcp/[transport]/route.tsregisters widget HTML plus tools for creating, listing, updating, and deleting todos so ChatGPT can talk to the exact same backend. - One deployment path. Vercel hosts the app, serves the widget template, and exposes the MCP endpoint you plug into ChatGPT’s “Develop your own” flow.
app/
api/
mcp/[transport]/route.ts # MCP handler (JSON/SSE transports)
todos/[listId]/route.ts # REST API for CRUD actions
todos/ # Pages + widgets for list display
components/
todo-list-widget.tsx # Main React UI with optimistic updates
lib/
mongodb.ts # Atlas client + in-memory fallback
openai.ts # Widget helper (window.openai bridge)
- Node.js 20+
- Package manager (pnpm recommended)
- MongoDB Atlas cluster (M0 free tier works) or a local MongoDB for testing
- Optional: Ngrok if you want to test the MCP endpoint from ChatGPT while running locally (
pnpm ngrok)
-
Install dependencies:
pnpm install
-
Copy
.env.example(or use the snippet below) into.envand fill in your MongoDB connection string:MONGODB_URI="mongodb+srv://<user>:<password>@cluster.mongodb.net/chatgpt_todos" -
(Optional) Seed the database:
use chatgpt_todos db.todo_lists.insertOne({ _id: "launch-list", title: "Launch checklist", shareToken: UUID(), createdAt: new Date() })
pnpm dev- App: http://localhost:3000
- Todo API: http://localhost:3000/api/todos/:listId
- MCP endpoint (JSON transport): http://localhost:3000/api/mcp/json
Because lib/mongodb.ts falls back to an in-memory store when MONGODB_URI is absent, you can explore the UI and MCP tools even without a live database.
The MCP route (app/mcp/route.ts) performs three jobs:
- Renders the
/todos/widgetpage once and registers it as a widget resource so ChatGPT can embed the exact same UI. - Exposes tools (
create_todo_list,add_todo_item,complete_todo_item,delete_todo_item,get_todo_list,list_todo_lists,upsert_todo_items) withzod-validated inputs. - Returns both human-readable text and
structuredContentso the widget stays in sync after each tool call.
To try it inside ChatGPT:
- Run the dev server (or deploy to Vercel).
- In ChatGPT, open Build a GPT → Add Actions → Model Context Protocol.
- Paste
https://<your-domain>/api/mcp/json(or your ngrok URL). - ChatGPT detects the tools automatically; click Save and start prompting with phrases like “Create a project launch list and add three tasks.”
- Push the repo to GitHub and import it into Vercel.
- Set environment variables (
MONGODB_URI, plus optionalPUBLIC_BASE_URLif you need a custom domain). - Deploy.
baseUrl.tsauto-detects the correct origin for widget CSP metadata, so the MCP registration works out of the box. - Update your ChatGPT MCP configuration to use the production URL.
| Script | Description |
|---|---|
pnpm dev |
Run Next.js with Turbopack |
pnpm build |
Create the production bundle |
pnpm start |
Launch the production server locally |
pnpm ngrok |
Expose port 3000 via ngrok for ChatGPT MCP testing |
- In-memory fallback instead of Atlas? Ensure
MONGODB_URIis defined in the environment for both local dev and Vercel. - Widget not loading in ChatGPT? Verify the CSP metadata in
app/mcp/route.tsreferences the correct domain—settingPUBLIC_BASE_URLfixes custom setups. - Tool calls stuck? Check Vercel function logs or
pnpm devoutput; look for[todos]or[v0]logs from the API routes.
This project is provided as an educational sample. Adapt it freely for demos, blog posts, or your own ChatGPT-enabled apps.
