Skip to main content
Expose a 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:
Keep mcp-server.ts at the root of your Next.js project, alongside next.config.ts. Add the MCP route inside your existing app directory:
If your project uses src/app, the route goes inside that directory instead. Keep mcp-server.ts and next.config.ts at the project root.

Define the server

Create mcp-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 with withMcpUse. Keep your application’s current options inside nextConfig:
next.config.ts

Mount the endpoint

Create an optional catch-all Route Handler at app/api/mcp/[[...path]]/route.ts:
app/api/mcp/[[...path]]/route.ts
If your app uses 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:
In a second terminal, open the Inspector connected to your new endpoint:
Select Tools → greet and enter:
Run the tool. It returns 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.