Run FrontMCP inside NestJS

Covers FrontMCP 1.15.2 as of

@frontmcp/nestjs mounts a FrontMCP server beside your Nest controllers. Nest keeps the HTTP listener. FrontMCP receives the MCP and OAuth requests ahead of Nest's body parser so it can read their request bodies itself.

import { FrontMcpModule } from "@frontmcp/nestjs";

FrontMcpModule.forRoot({ server, paths });
FrontMcpModule.forRootAsync({ imports, inject, useFactory });

Reference

FrontMcpModule.forRoot(options)

OptionTypeDefaultDescription
serverFrontMcpConfigInputrequiredThe config you would pass to @FrontMcp, with an MCP endpoint such as http.entryPath: "/mcp".
pathsstring[]MCP entry path, /.well-known, /oauth, and the literal prefix of each http.routes pathWhich path prefixes Nest passes to FrontMCP. A prefix matches itself and its subpaths. Supply the full list when overriding it.

FrontMcpModule.forRootAsync(options)

OptionTypeDefaultDescription
importsModuleMetadata["imports"]noneNest modules that export dependencies of the factory.
injectFactoryProvider["inject"]noneNest provider tokens passed as factory arguments.
useFactory(...args: any[]) => FrontMcpModuleOptions | Promise<FrontMcpModuleOptions>requiredBuilds the same options as forRoot(), from Nest providers.

Both methods return a Nest DynamicModule and export the listener under the FRONTMCP_REQUEST_LISTENER injection token. app.close() calls the listener's close() before Nest closes HTTP. The package exports frontMcpPaths(server, paths?) to resolve the prefixes and createFrontMcpMiddleware(listener, prefixes) to make the middleware that forwards matching requests and calls next() for other requests.

Caveats

Use Nest 11 or 12 on its Express or Fastify platform. FrontMCP runs its own auth, authority rules and hooks for the requests it receives. Nest controller guards, pipes and interceptors continue to apply on the Nest routes they decorate.

The default prefixes leave /health, /healthz, /readyz and /metrics outside this mount when the MCP endpoint is at /mcp. Add the paths you want FrontMCP to serve to the full paths override.

Usage

Mounting at /mcp

npm install @frontmcp/nestjs@1.15.2 @frontmcp/sdk@1.15.2
app.module.ts
import { Module } from "@nestjs/common";
import { FrontMcpModule } from "@frontmcp/nestjs";
import { OrdersApp } from "./orders.app";

@Module({
  imports: [
    FrontMcpModule.forRoot({
      server: {
        info: { name: "orders", version: "1.0.0" },
        apps: [OrdersApp],
        http: { entryPath: "/mcp" },
      },
    }),
  ],
})
export class AppModule {}

Start the application with NestFactory.create(AppModule) and app.listen(3000). Point the MCP client at http://localhost:3000/mcp.

Sharing a Nest service with tools

Move your tool schema and handler into a FrontMCP tool and register it on a FrontMCP app. The same approach applies when moving from @nestjs/mcp or @rekog/mcp-nest. Keep the Nest service that implements the operation. Pass its instance as a value provider, then call it from the tool with this.get(OrdersService).

app.module.ts
FrontMcpModule.forRootAsync({
  imports: [OrdersModule],
  inject: [OrdersService],
  useFactory: (orders: OrdersService) => ({
    server: {
      info: { name: "orders", version: "1.0.0" },
      apps: [OrdersApp],
      http: { entryPath: "/mcp" },
      providers: [{ provide: OrdersService, useValue: orders }],
    },
  }),
});

If the old tool relied on a Nest guard or pipe, apply the equivalent check in FrontMCP's auth policy, input schema or hooks before moving its endpoint.

Troubleshooting

@frontmcp/nestjs: a FrontMCP path of "/" would take every request of the Nest application.

Use an explicit http.entryPath such as "/mcp". Start each custom route with a literal segment, or set paths to the prefixes FrontMCP should serve.

FrontMcpModule serves HTTP: create the application with NestFactory.create().

Create an HTTP application with NestFactory.create() so the module has an HTTP adapter to mount on.

Report a docs issue · Report a security concern privately