ModelFetch

Next.js

Run flexible MCP servers with Next.js

The @modelfetch/next package lets you run MCP servers as Next.js App Router API route handlers.

Installation

Terminal
npm install @modelfetch/next

Usage

Next.js App Router

app/[[...path]]/route.ts
import handle from "@modelfetch/next";
import server from "./server"; // Import your McpServer

const handler = handle(server);

// Export as Next.js App Router API route handlers
export const GET = handler;
export const POST = handler;
export const DELETE = handler;

Use Node.js Runtime

app/[[...path]]/route.ts
import handle from "@modelfetch/next";
import server from "./server"; // Import your McpServer

const handler = handle(server);

// Export as Next.js App Router API route handlers
export const GET = handler;
export const POST = handler;
export const DELETE = handler;

// Use Node.js runtime (default)
export const runtime = "nodejs";

Use Edge Runtime

app/[[...path]]/route.ts
import handle from "@modelfetch/next";
import server from "./server"; // Import your McpServer

const handler = handle(server);

// Export as Next.js App Router API route handlers
export const GET = handler;
export const POST = handler;
export const DELETE = handler;

// Use Edge runtime
export const runtime = "edge";

API Reference

handle(server)

Creates a Next.js App Router API route handler from an McpServer instance