NestJS

Plug xmcp into your existing NestJS application with automatic tool discovery and customizable module configuration.

Overview

The NestJS adapter allows you to integrate xmcp into your existing NestJS application. It provides:

  • Automatic tool discovery from your src/tools/ directory
  • Scaffolded module with customizable controller, filter, and route configuration
  • NestJS integration with xmcpService and xmcpController

Installation

xmcp can work on top of your existing NestJS project. To get started, run the following command in your project directory:

On initialization, you'll see the following prompts:

? Tools directory path: (tools)

The package manager and framework will be detected automatically.

After initialization, xmcp generates a src/xmcp/ folder with customizable module files:

After setting up the project, update your package.json scripts:

package.json

Before using the @xmcp/adapter import, you need to:

  1. Run npx xmcp build to generate the .xmcp folder
  2. Update your tsconfig.json to include the path mapping and the .xmcp folder:
tsconfig.json

After these steps, TypeScript errors will be resolved.

Project Structure

After initialization, your project structure will look like this:

Generated Files

src/xmcp/ - Contains the scaffolded module with controller, filter, and module configuration. These files are yours to customize - change the route path, add middleware, modify error handling, or extend functionality as needed.

.xmcp/ - Contains the compiled adapter and auto-generated TypeScript definitions. This directory is created by xmcp build and should be added to .gitignore. It includes xmcpService, xmcpController, and all type definitions needed to integrate with NestJS.

xmcp-env.d.ts - Provides TypeScript type declarations for xmcp imports like @xmcp/adapter. This file is auto-generated and should not be edited manually. It ensures TypeScript can resolve the path alias configured in tsconfig.json.

Basic Usage

Import and add the XmcpModule to your application module:

src/app.module.ts

This registers a /mcp endpoint that handles MCP requests via POST.

Generated Module Files

Exception Filter

The exception filter provides JSON-RPC error handling for MCP endpoints:

src/xmcp/xmcp.filter.ts

This filter is scaffolded into your project, so you can customize it to handle specific error types or modify the error response format.

Controller

The controller extends xmcpController and uses NestJS decorators:

src/xmcp/xmcp.controller.ts

To change the route, simply modify the @Controller argument:

Module

The module configures the controller and providers:

src/xmcp/xmcp.module.ts

Configuration

Configure the NestJS adapter in your xmcp.config.ts:

xmcp.config.ts

The NestJS adapter uses HTTP transport and integrates with NestJS's module system, allowing you to use the xmcpService in your custom controllers.

NestJS Integration Features

The adapter provides full NestJS integration with proper lifecycle management and error handling:

Lifecycle Hooks

xmcpService implements OnModuleInit and OnModuleDestroy for proper initialization and shutdown logging:

  • Startup: Logs [xmcpService] XMCP service initialized when the module initializes
  • Shutdown: Logs [xmcpService] XMCP service shutting down when the application stops

Structured Logging

All xmcp internal logs use the NestJS Logger class, automatically inheriting your application's logging configuration.

Adding Tools

Tools are automatically discovered from your src/tools/ directory. Create a new file and export a default handler function:

src/tools/greet.ts

When you run xmcp dev or xmcp build, xmcp automatically discovers this file and registers it as an MCP tool. No additional configuration needed.

For more details on creating tools, schemas, and metadata, see the Tools documentation.

Authentication

The NestJS adapter provides a createMcpAuthGuard factory function for JWT authentication. You provide the verification logic, and the adapter handles token extraction, error responses, and attaching auth info to requests.

Setup

First, install the JWT library:

Create an auth guard configuration file:

src/xmcp/xmcp.auth.ts

Enable Authentication

To enable authentication, add the guard to your controller:

src/xmcp/xmcp.controller.ts

And add it to your module providers:

src/xmcp/xmcp.module.ts

Configuration Options

OptionTypeDefaultDescription
verifyToken(token: string) => Promise<AuthInfo> | AuthInfoRequiredVerify the token and return auth info
requiredbooleanfalseIf true, requests without tokens are rejected

The verifyToken function receives the Bearer token (without the "Bearer " prefix) and should return:

If verification fails, throw an error with a descriptive message.

Accessing Auth Info in Tools

Auth info is available in tools via the extra argument:

src/tools/whoami.ts

Testing with curl

Troubleshooting

Cannot find module '@xmcp/adapter'

This error occurs when the .xmcp directory hasn't been generated yet.

Solution: Run npx xmcp build before starting your NestJS application.

TypeScript path resolution errors

If TypeScript can't resolve @xmcp/* imports:

  1. Ensure tsconfig.json has the path mapping:

  2. Ensure .xmcp is included in the include array:

Next Steps

On this page

One framework to rule them all