Both use the same V2
MCPServer, tool, and view APIs. Neither requires an MCP configuration file.
Embedded in Next.js
The Next.js adapter mounts MCP at a Route Handler.withMcpUse integrates view compilation, public assets, output tracing, and CORS with the Next.js build; createNextHandler exposes the server over HTTP. Restart next dev after changing the MCP server or a view so its startup build runs again.
1
Install mcp-use
2
Export the MCP server
mcp-server.ts
listen(). Next.js owns the HTTP listener.3
Enable the Next.js integration
next.config.ts
4
Add the Route Handler
Use an optional catch-all because mcp-use serves the MCP endpoint and nested view assets from the same subtree:If the server is at the project root and your
app/api/mcp/[[...path]]/route.ts
@/* alias only maps src/*, use the appropriate relative import or move the server under src/.package.json
pnpm dev, open the Next.js page, and connect an MCP client to http://localhost:3000/api/mcp.
The complete example at packages/server/examples/nextjs also shows a view that imports a component used by the Next.js landing page and an image from the application’s public directory.
Standalone beside Next.js
The generic standalone CLI can run inside any TypeScript project, including a Next.js monorepo. It uses the projecttsconfig.json, so the MCP server and its views can reuse project aliases, ordinary services, and browser-safe components.
1
Create an MCP source directory
2
Export the server
src/mcp/server.ts
listen(). The mcp-use CLI owns the standalone listener.3
Add separate scripts
package.json
pnpm next:dev and pnpm mcp:dev in separate terminals. The website is available at http://localhost:3000; the independent MCP endpoint is available at http://localhost:3001/mcp.
Split monorepo
When the MCP source is outside the Next.js package, select the host project explicitly:--path selects the project root used for package.json, .env, and tsconfig.json. --entry and --views-dir are resolved relative to that root. The same flags work with mcp-use build; start the generated production server with mcp-use start --path apps/web.
The complete example is at packages/server/examples/nextjs-standalone.
Sharing code safely
When the standalone CLI detects a Next.js host project, it loads the Next.js environment cascade and provides compatibility shims for common server-only imports. Shared services that importserver-only, next/cache, or next/headers can load, but the shims do not invent a website request: headers() and cookies() are empty, and cache invalidation is a no-op. Pass required identity or request data through MCP authentication and request context.
MCP App views run in a browser iframe. A view may import browser-safe components from the Next.js project, but it must not import Server Components or modules that depend on server-only, next/headers, a database client, the filesystem, or other server-only APIs. Fetch data in the tool and return it to the view as structuredContent.
CLI discovery
--mcp-dir src/mcp discovers the first supported server entry in that directory and uses src/mcp/views for views. An explicit --entry overrides entry discovery; --views-dir overrides the view directory. The project does not need mcp.config.ts.
Verification
For either topology, verify the complete contract rather than only checking that a port is open:- Build the project in production mode.
- Initialize an MCP session with
@mcp-use/client. - List tools and confirm the expected names and schemas.
- Call every example tool and assert its text and
structuredContent. - Read each view URI from tool metadata and assert that its HTML resource loads.
- Load every generated JavaScript, stylesheet, and public asset referenced by the view.
- For embedded mode, also request the Next.js landing page and verify browser CORS preflight against
/api/mcp.
pnpm verify, which performs their MCP assertions with the mcp-use client. The embedded example additionally verifies its Next.js landing page and nested public asset route; the standalone example should also pass pnpm next:build.