AI agents are good at talking. But they can’t do much inside your product unless you give them a door to walk through. A next js mcp server is that door. It lets an AI agent like Claude run real actions in your app. It can look up an order, find a product, or save a note for your support team.
The good news is that you do not need a new server or a big setup. If you already use Next.js, you can add MCP with one route file. In this guide, we will build it step by step. I will keep the words simple and the code short. You can follow along even if MCP is brand new to you.
By the end, you will have a working server, a way to test it, and a clear plan to keep it safe.
What Is MCP? (In Simple Words)
MCP stands for Model Context Protocol. That sounds big, but the idea is small. MCP is a shared set of rules. It tells AI agents how to talk to apps and tools.
Think about a USB port. You can plug in a mouse, a keyboard, or a phone charger. You do not need a special cable for each one. MCP does the same thing for AI. There is one standard way to connect, and many tools can use it.
An MCP server can share three things:
• Tools. These are actions the agent can run, like “search products.”
• Resources. These are things the agent can read, like a help page or a file.
• Prompts. These are ready-made instructions the agent can reuse.
Most teams start with tools. So that is what we will build today.
So why use Next.js for this? Many teams already run their website or app backend in Next.js. You already have API routes, TypeScript, and an easy way to deploy. Adding MCP means one more route in a project you know well. You do not need to learn a new framework or pay for another server.
One quick note. Next.js 16 and newer also has an MCP tool made for coding agents. It helps an agent look at your dev server, your routes, and your errors. That is handy, but it is a different thing. In this guide, we are building a server for your product, so that outside AI agents can use your features.
What You Need Before You Start
Here is a short list:
• Node.js version 20 or newer
• A basic idea of how the Next.js App Router works
• A code editor, like VS Code
• Claude Desktop or the free MCP Inspector for testing
• A Vercel account if you want to put it online (optional)
You do not need to know anything about AI models. You are not training anything. You are just building a small API that AI agents already know how to use.
Plan Your Tools First: A Simple Rule from Numan Dev
Before you write any code, stop and think. This step saves a lot of time later.
Software engineer Numan builds mobile apps and AI systems. On his site, he shares a simple rule: start with the product need, then pick the tech. That rule fits MCP very well. Do not ask, “What can I show the agent?” Ask, “What do I want an agent to do for my users?”
Pick three to five actions. For a small online store, your list might look like this:
• find_product: look up a product by name
• get_order_status: check where an order is
• add_support_note: save a note on a support ticket
Here are some rules to follow while you plan:
• Keep each tool small. One tool should do one job.
• Use clear names. find_product is better than doStuff.
• Write a plain description. The agent reads it to decide when to use the tool.
• Ask for as little input as you can. Fewer inputs means fewer mistakes.
• Start with read-only tools. Add tools that change data later, when you trust your setup.
This planning takes ten minutes with a pen and paper. It will save you hours of fixing messy tools later.
Step 1: Create the App and Install the Packages
Open your terminal and run these lines:
npx create-next-app@latest mcp-demo –typescript –app
cd mcp-demo
npm install mcp-handler @modelcontextprotocol/sdk zod
Here is what each package does:
• mcp-handler is a helper from Vercel. It handles the MCP rules so you don’t have to.
• @modelcontextprotocol/sdk is the official MCP kit. The helper uses it behind the scenes.
• zod checks the data that comes in. It makes sure the inputs look right before your code runs.
If you already have a Next.js project, skip the first two lines. Just install the three packages inside your project.
Before we write the route, here is the big picture of what we are building. The agent sends a request to your route. Your route runs a tool. The tool talks to your data and sends back an answer.

Step 2: Build the MCP Route Handler
Now create a new file at app/api/[transport]/route.ts. Yes, the square brackets are on purpose. The [transport] part lets one file handle different ways of connecting.
Paste this in:
import { createMcpHandler } from “mcp-handler”;
import { z } from “zod”;
const handler = createMcpHandler(
(server) => {
server.tool(
“say_hello”,
“Says hello to a person by name”,
{ name: z.string() },
async ({ name }) => ({
content: [{ type: “text”, text: `Hello, ${name}!` }],
})
);
},
{},
{ basePath: “/api” }
);
export { handler as GET, handler as POST, handler as DELETE };
Let’s break it down. createMcpHandler builds your server. Inside it, server.tool adds a tool. It takes four things: a name, a short description, the input rules, and the function to run. The function must return a content list. Each item in the list holds text that the agent can read.
The basePath line tells the helper where your route lives. It must match your folder. Our file is inside app/api, so the base path is /api. If the two do not match, you will get a 404 error.
Now start the app with npm run dev. Your MCP server is live at http://localhost:3000/api/mcp. That is the web address you will give to an agent.
Step 3: Add a Tool That Does Real Work
A hello tool is fun, but it does not help a business. Let’s add something real. We will make a tool that finds a product and tells the agent its price and stock.
To keep things simple, we will use a small list. In a real app, you would swap it for a database call. Add this above createMcpHandler, and put the tool next to say_hello:
const products = [
{ id: “p1”, name: “Basmati Rice 5kg”, price: 18, stock: 40 },
{ id: “p2”, name: “Olive Oil 1L”, price: 12, stock: 0 },
];
server.tool(
“find_product”,
“Finds a product by name. Returns price and stock.”,
{ query: z.string().min(2).max(50) },
async ({ query }) => {
const found = products.filter((p) =>
p.name.toLowerCase().includes(query.toLowerCase())
);
if (found.length === 0) {
return {
content: [{ type: “text”, text: “No product found.” }],
isError: true,
};
}
return {
content: [{ type: “text”, text: JSON.stringify(found) }],
};
}
);
Let me point out a few things. The query input must be text between 2 and 50 characters. Zod will block anything else. That stops bad or strange input before it reaches your code.
The description matters more than you think. The agent reads it to decide when to use this tool. “Finds a product by name. Returns price and stock.” is clear. “Product tool” is not.
Also, keep the answer short. If your database returns 500 rows, do not send all of them. Send the top five. A short answer is faster and cheaper, and agents handle it better.
When something goes wrong, say so clearly. Notice the isError: true line. It tells the agent that the tool failed, so it can try again or let the user know.
Step 4: Test With MCP Inspector
Never skip testing. The MCP Inspector is a free tool that lets you try your tools by hand, with no AI involved.
Keep your app running. Open a second terminal and run:
npx @modelcontextprotocol/inspector@latest
A page opens in your browser. Pick the Streamable HTTP type, paste http://localhost:3000/api/mcp, and click Connect. Then open the Tools tab and click List Tools. You should see say_hello and find_product.
Try find_product with the word “rice.” You should get the rice back with its price and stock.
If you see an error, check these three things:
• Is your dev server running?
• Does the basePath match your folder?
• Does the URL end with /api/mcp?
Step 5: Connect a Real AI Agent
Now for the fun part. Let’s let an AI agent use your tool.
If you use Claude Desktop, open its settings and find the developer config file. Add this:
{
“mcpServers”: {
“my-product”: {
“command”: “npx”,
“args”: [“-y”, “mcp-remote”, “http://localhost:3000/api/mcp”]
}
}
}
This uses mcp-remote, a small helper that links Claude Desktop to your local server. Save the file and restart the app. Then ask: “Is olive oil in stock?”
Claude should call your find_product tool, read the answer, and tell you the oil is out of stock. That is your product talking to an AI agent, and you wrote less than 50 lines of code.
Once your server is online, some AI apps let you add a remote server just by pasting its web address. Check the settings of the app you use.
Keep Your MCP Server Safe
A server that agents can call is a server that anyone can call, unless you lock it. Please do not skip this part.
• Add a login. Do not leave your route open. Ask for a token on every request. You can use OAuth or an auth service so each agent proves who it is. The mcp-handler package has a helper for this.
• Check every input. You already use Zod. Keep doing it, and set tight limits.
• Give the least access. If a tool only reads data, connect it with a database user that can only read.
• Ask before risky actions. For anything that deletes data or moves money, require a clear confirm step or a human check.
• Watch for tricks. Text from users or web pages can hide commands like “ignore your rules.” Treat anything your tools return as plain data, not as orders.
• Log and limit. Keep a record of tool calls. Add rate limits so one agent can’t flood your server.
Security is not a bonus feature here. Your tools can touch real customer data, so treat them like any other public API.
Put It Online With Vercel
When your tools work on your computer, it is time to go live. Vercel makes this easy for Next.js.
• Push your project to GitHub.
• Import the project in Vercel.
• Add your secrets, like API keys and database links, as environment variables. Never put them in your code.
• Click Deploy.
Your server will now live at https://your-app.vercel.app/api/mcp. Run the Inspector again with the live address to make sure it works. Also test the login. Try a request with no token and make sure it gets blocked.
Common Mistakes to Avoid
I see the same problems again and again. Watch out for these:
1. Too many tools. If you give an agent 40 tools, it gets confused. Start with five or fewer.
2. Vague descriptions. The agent can’t guess what “data tool” means. Say exactly what the tool does.
3. Huge answers. Long replies waste time and money. Send only what the agent needs.
4. No login. An open MCP route is an open door to your data.
5. Wrong base path. If the basePath and your folder do not match, you get a 404 error.
6. Skipping tests. Always try your tools in the Inspector before you blame the AI agent.
Frequently Asked Questions
Do I need a separate server for MCP?
No. One route inside your Next.js app is enough.
Does MCP only work with Claude?
No. MCP is an open standard. Many AI apps and tools support it.
Can I use my existing database?
Yes. Call your database inside the tool function, just like you would in any API route.
Is it safe to let an AI agent change my data?
It can be, if you add a login, tight input rules, and limits. Start with read-only tools and add more later.
Final Thoughts
Building an MCP server with Next.js is much easier than it sounds. You plan a few small tools. You add one route. You test it, lock it, and put it online. That is all it takes to let AI agents work with your product.
Start small. Build one read-only tool today and test it in the Inspector. Once it works, add a second tool, then a login, then more. Small steps keep your server clean and safe.
AI agents are not going away. Products that are easy for agents to use will have an edge. With a Next.js MCP server, you can be ready for that today.