greet tool at /api/mcp in your existing Next.js app. Next.js serves your application and the MCP endpoint together.
Prerequisites
- A working Next.js app using the App Router and TypeScript.
Add the integration
Install mcp-use and Zod in your Next.js project:mcp-server.ts at the root of your Next.js project, alongside next.config.ts. Add the MCP route inside your existing app directory:
src/app, the route goes inside that directory instead. Keep mcp-server.ts and next.config.ts at the project root.
Define the server
Createmcp-server.ts at the project root. Register a tool that accepts a name and returns a greeting:
mcp-server.ts
Configure the adapter
Wrap your existing Next.js configuration withwithMcpUse. Keep your application’s current options inside nextConfig:
next.config.ts
Mount the endpoint
Create an optional catch-all Route Handler atapp/api/mcp/[[...path]]/route.ts:
app/api/mcp/[[...path]]/route.ts
src/app, put the route at src/app/api/mcp/[[...path]]/route.ts and use ../../../../../mcp-server to import the server from the project root.
Try it
Start your Next.js app with its usual command:Hello, Ada!.
Use your app’s port if it differs from 3000. Restart next dev after editing mcp-server.ts so the adapter’s startup build runs again.
See the complete Next.js example for a runnable application, or the Next.js guide for shared UI views and standalone servers.