Mount FrontMCP in a web framework

Checked against FrontMCP 1.15.2 on

If your web application already serves HTTP, forward its MCP requests to FrontMcpInstance.createFetchHandler(). The framework owns the listener and routes. FrontMCP receives the original web Request and returns a Response.

import { FrontMcpInstance } from "@frontmcp/sdk";

const handle = await FrontMcpInstance.createFetchHandler(config);
const response = await handle(request);

Reference

Paths and sessions

RequestRoute to mount
MCP at /mcpPass the original URL and set http.entryPath: "/mcp".
A web-standard http.routes endpointForward the full path, such as /mcp/status, and use it in the route config.
Protected resource metadata for /mcpAlso forward /.well-known/oauth-protected-resource/mcp when using auth.
Local or remote OAuthForward the root /oauth and /.well-known routes FrontMCP serves, including OAuth metadata and callbacks.
Health probesForward /healthz and /readyz, or put health.healthzPath and health.readyzPath under the mounted prefix.

A stateless fetch handler serves requests independently. Clients on older Streamable HTTP revisions get no Mcp-Session-Id and cannot open a standalone GET stream. Set MCP_SESSION_SECRET in production if those clients connect. If they need sessions, use the Node host or a Worker with Durable Object sessions.

Usage

Sharing one handler across routes

Open
import { app, FrontMcpInstance, tool, type FetchHandlerCtx, type WebFetchHandler } from "@frontmcp/sdk";

export const status = tool({ name: "status", description: "Read service status", inputSchema: {} })(
  () => ({ status: "ok" }),
);

let handler: Promise<WebFetchHandler> | undefined;

export async function handleMcpRequest(request: Request, context?: FetchHandlerCtx): Promise<Response> {
  handler ??= FrontMcpInstance.createFetchHandler({
    info: { name: "reports", version: "1.0.0" },
    apps: [app({ name: "Reports", tools: [status] })],
    http: {
      entryPath: "/mcp",
      routes: [{ method: "GET", path: "/mcp/status", fetch: () => Response.json({ status: "ok" }) }],
    },
  }).catch((error: unknown) => {
    handler = undefined;
    throw error;
  });
  return (await handler)(request, context);
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Build the handler on the first request so the framework can load the route file at build time without starting FrontMCP. Forward the request before other middleware reads its body. Pass plain configuration to the factory. Importing a class decorated with @FrontMcp can start a separate listener.

Next.js App Router

In app/mcp/[[...path]]/route.ts, use the optional catch-all to match both /mcp and paths below it. Keep the Node runtime. Export OPTIONS to let FrontMCP answer configured CORS preflights.

import { handleMcpRequest } from "@/lib/mcp";

export const GET = (request: Request) => handleMcpRequest(request);
export const POST = GET;
export const DELETE = GET;
export const OPTIONS = GET;

With auth, export GET and OPTIONS from the same handler in app/.well-known/oauth-protected-resource/mcp/route.ts.

Hono

import { Hono } from "hono";
import { handleMcpRequest } from "./mcp";

const app = new Hono();
app.all("/mcp/*", (ctx) => handleMcpRequest(ctx.req.raw));
app.all("/.well-known/oauth-protected-resource/mcp", (ctx) => handleMcpRequest(ctx.req.raw));
export default app;

On Workers, pass ctx.executionCtx as the second argument of handleMcpRequest() so FrontMCP can keep streamed responses alive. The Node adapter provides no Worker execution context.

Nuxt with Nitro and h3 1.x

Define the handler in server/utils/mcp-handler.ts and export it as the default from server/routes/mcp/index.ts and server/routes/mcp/[...].ts. The catch-all serves subpaths. With auth, export it from server/routes/.well-known/oauth-protected-resource/mcp.ts too.

import { defineEventHandler, toWebRequest } from "h3";
import { handleMcpRequest } from "./mcp";

export const mcpEventHandler = defineEventHandler((event) => handleMcpRequest(toWebRequest(event)));

SvelteKit

Use src/routes/mcp/[...path]/+server.ts. The rest parameter also matches /mcp. Keep this route free of a +page so an HTML navigation can reach the handler. With auth, define the same fallback in src/routes/.well-known/oauth-protected-resource/mcp/+server.ts.

import { handleMcpRequest } from "$lib/server/mcp";
import type { RequestHandler } from "./$types";

export const fallback: RequestHandler = ({ request }) => handleMcpRequest(request);

Troubleshooting

{"error":"Not Found","entryPaths":["/mcp"]}

Compare the path in the request URL with http.entryPath and the configured custom routes. Preserve /mcp in the URL when the framework forwards the request.

Report a docs issue · Report a security concern privately