All framework guides

Node.js (TypeScript)

Get an OpenAPI spec from NestJS

Add @nestjs/swagger and its CLI plugin. The spec is served at /docs-json.

Tool
@nestjs/swagger
Time
About 5 minutes
You get
Live at /docs-json, plus an optional openapi.json file

How it works

NestJS has an official OpenAPI module. It reads your controllers and their decorators (@Get, @Post, @Body, @Param, @Query) and builds the spec while the app starts.

With the CLI plugin turned on, it also reads the fields of your DTO classes, so tools get proper inputs without extra decorators.

Step by step

  1. Step 1: Install the module

    In your NestJS project folder:

    Terminal
    npm install --save @nestjs/swagger
  2. Step 2: Build and serve the document in main.ts

    Add the lines marked NEW to src/main.ts. If you call app.setGlobalPrefix('api'), keep it before SwaggerModule.createDocument so the prefix is included.

    The writeFileSync line is optional. It saves a copy as openapi.json each time the app starts, which is handy for uploading.

    src/main.ts
    import { NestFactory } from '@nestjs/core';
    import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; // NEW
    import { writeFileSync } from 'node:fs'; // NEW (optional)
    import { AppModule } from './app.module';
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule);
    
      // NEW: describe the API and serve the spec
      const config = new DocumentBuilder()
        .setTitle('My API')
        .setDescription('What this API does, in one sentence.')
        .setVersion('1.0')
        .addBearerAuth()
        .build();
      const document = SwaggerModule.createDocument(app, config);
      SwaggerModule.setup('docs', app, document);
      writeFileSync('./openapi.json', JSON.stringify(document, null, 2)); // NEW (optional)
    
      await app.listen(process.env.PORT ?? 3000);
    }
    bootstrap();
  3. Step 3: Turn on the CLI plugin in nest-cli.json

    Without it, DTO fields only appear if you add @ApiProperty() to each one. With it, Nest reads them for you from files ending in .dto.ts or .entity.ts.

    Add the compilerOptions block (or just the plugins line if compilerOptions already exists):

    nest-cli.json
    {
      "collection": "@nestjs/schematics",
      "sourceRoot": "src",
      "compilerOptions": {
        "plugins": ["@nestjs/swagger"]
      }
    }
  4. Step 4: Start the app and get the spec

    Start it as usual. The spec is at /docs-json (and /docs-yaml), and a browsable page is at /docs.

    On your live server the same link works, for example https://api.example.com/docs-json. Paste that into MCPEasy, or download it:

    Terminal
    npm run start:dev
    Terminal (second window)
    curl http://localhost:3000/docs-json -o openapi.json
  5. Step 5: Optional: mark protected endpoints and add summaries

    These make the tools clearer: AI apps see what each endpoint does and which ones need a key.

    bookings.controller.ts
    import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
    
    @ApiBearerAuth()
    @Controller('bookings')
    export class BookingsController {
      @Post()
      @ApiOperation({ summary: 'Create a booking', description: 'Books an appointment in a free slot.' })
      create(@Body() dto: CreateBookingDto) {
        // ...
      }
    }

Check that it worked

  • Open http://localhost:3000/docs-json. You should see "paths" with every controller route.
  • Open a POST route in that JSON: its requestBody should list your DTO fields. If it is empty, see the first fix below.

Give it to MCPEasy

Have a link? Paste it (best)

In MCPEasy, create a server, choose OpenAPI URL and paste the link, for example https://api.example.com/openapi.json. Later changes to your API can be picked up with a sync.

Have a file? Upload it

Choose Upload file and drop in the .json or .yaml file (up to 5 MB). MCPEasy turns each endpoint into a tool, and you review them all before anything goes live.

If something goes wrong

Request bodies have no fields.
The CLI plugin is not running, or your DTO is a TypeScript interface or type. Use a class for each DTO, name the file *.dto.ts, and restart with npm run start:dev so the plugin runs.
/docs-json returns 404 in production.
Check that the SwaggerModule lines are not inside an if (process.env.NODE_ENV !== 'production') block. Or keep it off in production and upload the openapi.json file instead.
Paths are missing the /api prefix.
Call app.setGlobalPrefix('api') before SwaggerModule.createDocument.

Still stuck, or no time to set this up? In MCPEasy choose From code (GitHub): it reads your routes straight from the repository, read-only, and tests every tool against your live API before it goes live.

Official documentation

Other frameworks