Build an MCP server so Claude can use your app's data
Give Claude, Cursor or any AI assistant tools that read and write your own app's data, served from a Next.js route with the official MCP SDK and locked behind a token. You'll call it from a script first, then connect Claude Code to it. Every file is shown in full, and every step was run on a fresh project before this was published.
Tested step by step on with Next.js 16.4.6, the MCP TypeScript SDK 1.32.2 and Claude Code 2.1.330 (Claude Code)
By the end of this guide your Next.js app will have an MCP server at /api/mcp with three tools: search your notes, read one note, and add a note. You'll call those tools from a small script to see exactly what an AI assistant sees, and then connect Claude Code to them so you can ask, in plain English, “what did I write about invoices?” and get an answer from your own data.
MCP, the Model Context Protocol, is the standard way to give an AI assistant tools. You describe each tool once (its name, what it does, what it takes) and any assistant that speaks MCP can use it: Claude Code, Claude Desktop, Cursor, VS Code. The assistant reads your descriptions, decides when a tool helps, and calls it. Your server does the real work and answers.
Every file is shown whole, so you can copy it as it is. The guide was run from start to finish on a fresh create-next-app project and on a fresh shadcn init project, with Next.js 16.4, the MCP TypeScript SDK 1.32, Zod 4.6 and Claude Code.
Before you start
- Node.js 20.9 or newer. Check with
node -v. - A Next.js 16 app. A new one is fine:
npx create-next-app@latest my-app --yes. - Claude Code, for the last step only. Everything before it works without it.
Run every command from your project’s folder, the one with package.json in it.
1. Install the SDK
The official TypeScript SDK does the protocol for you: the handshake, listing tools, checking inputs, formatting answers. Zod describes what each tool takes.
2. The data your tools will use
An MCP tool is just a function your server runs, so it can read anything your app can: a database, an API, files. To keep this guide about MCP, the notes live in memory in one small module. Create lib/notes.ts:
3. Serve the tools from a route
Now the server itself. It’s one route handler. Each request builds a small MCP server with three tools, hands the request to the SDK’s transport, and returns the transport’s answer. Create app/api/mcp/route.ts:
Here’s what each part does:
registerToolgives a tool its name, a title, a description and its inputs. The description is what the assistant reads to decide when to use the tool, so write it like you’d explain the tool to a new teammate: what it does and what it gives back.inputSchemais a Zod shape. The SDK checks every call against it before your function runs, soqueryreally is a string of 1 to 200 characters, andidreally is a positive whole number. A call that doesn’t match never reaches your code.isError: truetells the assistant the tool ran but couldn’t do what it asked, so it can try something else, like searching again.WebStandardStreamableHTTPServerTransportspeaks MCP over plain HTTP with web-standardRequestandResponseobjects, which is exactly what a Next.js route handler has.sessionIdGenerator: undefinedmakes it stateless: every request stands alone, which is what serverless hosting needs.enableJsonResponse: trueanswers with plain JSON instead of opening a stream, which is all these quick tools need.- The last line uses the same function for
GET,POSTandDELETE, the three methods MCP clients send.
4. Protect it with a token
These tools read and write your data, so they must never be open to everyone. authorized compares the request’s Authorization: Bearer … header with a secret you choose, using timingSafeEqual so the comparison takes the same time whatever the guess. With no token set, every request is refused.
Make a long random token and save it in .env.local:
.env.local is already in .gitignore, so the token never reaches your repository. Print it once, you’ll need it in step 6:
openssl, open .env.local in your editor and add a line MCP_TOKEN= followed by any long random string, at least 32 characters.5. Call your tools from a script
Before you connect an assistant, see your server the way an assistant does. The SDK has a client too. Create scripts/try-mcp.mjs:
Build the app and start it:
Leave it running. In a second terminal, in the same folder, run the script. --env-file hands it your token:
You should see:
That’s the whole conversation an assistant has with your server: it asks for the list of tools, then calls the ones it needs with the arguments it chose. Now check the lock. A request without the token gets nothing:
It prints 401.
6. Connect Claude Code
With the app still running, add the server to Claude Code. Replace YOUR_TOKEN with the value from step 4:
Then check that Claude Code can reach it:
The notes line ends with ✔ Connected. Start claude in any folder and ask something like “Search my notes for invoices and tell me what the template says.” Claude calls search_notes, then get_note with the id it found, and answers from your note. It asks your permission the first time it uses each tool.
claude mcp add stores the server in your own Claude Code settings for this folder, token included, not in your project. To remove it later, run claude mcp remove notes.7. Put it online
The route is an ordinary Next.js route, so it deploys with the rest of your app. Two things to do when you do:
- Set
MCP_TOKENon your host (on Vercel: Settings → Environment Variables), with a new random value. Never reuse your local one. - Swap the in-memory notes for your database. On a serverless host each request can land on a fresh copy of your app, so notes added in memory disappear. The Next.js 16 + Prisma 7 guide shows how to put them in Postgres; only
lib/notes.tschanges. - Then point Claude Code at the new address:
claude mcp add --transport http notes https://your-app.com/api/mcp --header "Authorization: Bearer …".
One token is right for tools only you use. If you want other people to connect their own assistants to their own data, each person needs their own access, which MCP does with OAuth sign-in. That’s a bigger job and its own guide.
If something goes wrong
401from the script. The token wasn’t sent or doesn’t match. Run the script with--env-file=.env.local, and restartnpm run startafter changing.env.local, because the server reads it once when it starts.ECONNREFUSEDfrom the script or✘ Failed to connectinclaude mcp list. The app isn’t running, or it’s on another port. Check the terminal runningnpm run start.Cannot find module '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js'. Your SDK is older than this guide. Runnpm install @modelcontextprotocol/sdk@latest.- A tool call fails with “Invalid arguments”. The assistant sent something your Zod shape refuses, like text where you asked for a number. That’s the schema doing its job; make the description say clearly what each input is.
- The note you added is gone. The notes are in memory and reset when the server restarts. That’s expected here; step 7 moves them to a database.
Questions people ask
What is an MCP server?
An MCP server is a program that offers tools to AI assistants through the Model Context Protocol. Each tool has a name, a description and typed inputs. An assistant like Claude Code reads the list, decides when a tool helps, and calls it, and the server runs your own code and sends the answer back.
Can I build an MCP server with Next.js?
Yes. The official MCP TypeScript SDK has a web-standard transport that takes a Request and returns a Response, which is exactly what a Next.js route handler works with. Build an McpServer, register your tools, connect the transport and return transport.handleRequest(request) from the route.
How do I secure an MCP server?
Never leave the endpoint open. Check an Authorization: Bearer token on every request, compare it with timingSafeEqual, and refuse everything when no token is set. Keep the token in an environment variable, and let Zod schemas check every tool input before your code runs.
Does an MCP server work with Cursor and Claude Desktop too?
Yes. MCP is a shared protocol, so the same server works with any client that supports remote servers over HTTP, including Claude Code, Claude Desktop, Cursor and VS Code. Each client has its own place to add the address and the Authorization header, but the server doesn’t change.
Where to go next
- Move the notes into Postgres with the Next.js 16 + Prisma 7 guide, so they survive restarts and deploys.
- Add an assistant inside your own app with Add an AI assistant to your Next.js app, with limits that protect your bill.
- Before you give tools write access to real data, read 8 security holes in a Next.js app.
Get the next guide in your inbox
One email when a new guide or free build is out, every step tested before it’s published. No spam, unsubscribe anytime.