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
xmcpServiceandxmcpController
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:
Before using the @xmcp/adapter import, you need to:
- Run
npx xmcp buildto generate the.xmcpfolder - Update your
tsconfig.jsonto include the path mapping and the.xmcpfolder:
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:
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:
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:
To change the route, simply modify the @Controller argument:
Module
The module configures the controller and providers:
Configuration
Configure the NestJS adapter in your 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 initializedwhen the module initializes - Shutdown: Logs
[xmcpService] XMCP service shutting downwhen 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:
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:
Enable Authentication
To enable authentication, add the guard to your controller:
And add it to your module providers:
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
verifyToken | (token: string) => Promise<AuthInfo> | AuthInfo | Required | Verify the token and return auth info |
required | boolean | false | If 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:
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:
-
Ensure
tsconfig.jsonhas the path mapping: -
Ensure
.xmcpis included in theincludearray: