Overview
The NestJS adapter brings xmcp into your NestJS applications following NestJS patterns and conventions. It provides a module, controller, and service structure with full support for dependency injection, guards, and exception filters.Installation
Install xmcp and NestJS dependencies:Quick Start
1. Configure xmcp
Createxmcp.config.ts in your project root:
xmcp.config.ts
2. Create xmcp Module
Create a module that imports the xmcp components:src/xmcp/xmcp.module.ts
3. Create Controller
Extend the base xmcp controller:src/xmcp/xmcp.controller.ts
4. Create Exception Filter
Handle xmcp-specific exceptions:src/xmcp/xmcp.filter.ts
5. Import in App Module
Add XmcpModule to your main application module:src/app.module.ts
6. Bootstrap Application
src/main.ts
7. Create Tools
Create tools that can access NestJS services:src/tools/list-users.ts
Development
Run both xmcp and NestJS in development:package.json:
package.json
Authentication with Guards
The NestJS adapter provides a powerful guard system for OAuth authentication.Basic Auth Guard
Create an authentication guard:src/xmcp/auth.guard.ts
Apply Guard to Controller
Use the guard on your controller:src/xmcp/xmcp.controller.ts
Required Authentication
Make authentication mandatory:OAuth Module
Implement OAuth Protected Resource Metadata using the built-in OAuth module.Set Up OAuth Module
Import and configure the OAuth module:src/app.module.ts
/.well-known/oauth-protected-resource endpoint.
OAuth Service API
Inject and use the OAuth service:src/some/service.ts
Service Architecture
The adapter provides aXmcpService that handles MCP requests:
src/custom/custom.service.ts
Controller Reference
The baseXmcpController provides three methods:
src/xmcp/xmcp.controller.ts
Integration with Existing Services
Use NestJS services in your tools:src/tools/create-user.ts
API Reference
XmcpService
Injectable service for handling MCP requests.
XmcpController
Base controller with MCP endpoint handlers.
createMcpAuthGuard
Factory function for creating authentication guards.
OAuthModule
Dynamic module for OAuth Protected Resource Metadata.
OAuthService
Service for OAuth metadata.
Example Project Structure
Best Practices
Use dependency injection
Use dependency injection
Leverage NestJS dependency injection in your modules:
Apply guards at controller level
Apply guards at controller level
Use guards for authentication and authorization:
Use exception filters
Use exception filters
Handle errors consistently with exception filters:
Enable logging
Enable logging
Use NestJS logger for debugging:
Troubleshooting
Module not found errors
Module not found errors
Ensure XmcpModule is imported in your AppModule:
Guards not executing
Guards not executing
Check guard order and ensure they’re applied to the controller:
Exception filter not catching errors
Exception filter not catching errors
Apply exception filters at the controller level:
Tools not loading
Tools not loading
Run
xmcp build and verify paths in xmcp.config.ts.Next Steps
Building Tools
Learn how to create powerful tools
Next.js Adapter
Check out the Next.js adapter