All framework guides

Node.js (JavaScript or TypeScript)

Get an OpenAPI spec from Express

swagger-autogen reads your routes and writes openapi.json. No comments needed.

Tool
swagger-autogen
Time
About 5 minutes
You get
openapi.json (OpenAPI 3.0)

How it works

Express has no built-in spec. swagger-autogen reads your route files as text, follows app.use('/prefix', router) into each router file, and writes every path and method it finds into openapi.json.

It also notices fields you read from req.body, req.query and req.params. It cannot know types or which fields are required, so those parts are thin. That is fine: MCPEasy fills the gaps, and you review every tool before it goes live.

Step by step

  1. Step 1: Install swagger-autogen

    In your API project folder, install it as a development tool. It is not needed when your API runs.

    Terminal
    npm install --save-dev swagger-autogen
  2. Step 2: Find your main file

    This is the file that calls express() and mounts your routers with app.use(...). It is usually app.js, server.js, index.js or src/app.js (src/app.ts for TypeScript).

    You only list this one file. swagger-autogen follows the app.use lines into your router files, keeping their prefixes such as /users or /api/v1.

  3. Step 3: Create generate-openapi.js in the project root

    Copy this file and change three things: the title, your API's public address under servers, and the path to your main file from step 2.

    If your package.json contains "type": "module", use the second version instead.

    generate-openapi.js (CommonJS, the default)
    const swaggerAutogen = require('swagger-autogen')({ openapi: '3.0.0' });
    
    const doc = {
      info: {
        title: 'My API',
        description: 'What this API does, in one sentence.',
      },
      servers: [{ url: 'https://api.example.com' }],
    };
    
    const outputFile = './openapi.json';
    const routes = ['./app.js']; // your main file from step 2
    
    swaggerAutogen(outputFile, routes, doc);
    generate-openapi.js (if package.json has "type": "module")
    import swaggerAutogen from 'swagger-autogen';
    
    const doc = {
      info: {
        title: 'My API',
        description: 'What this API does, in one sentence.',
      },
      servers: [{ url: 'https://api.example.com' }],
    };
    
    const outputFile = './openapi.json';
    const routes = ['./app.js']; // your main file from step 2
    
    swaggerAutogen({ openapi: '3.0.0' })(outputFile, routes, doc);

    TypeScript project? Point routes at your source file, for example ['./src/app.ts']. If paths are missing, build the project first and point it at the compiled file instead, for example ['./dist/app.js'].

  4. Step 4: Run it

    This writes openapi.json next to the script. Run it again whenever you add or change routes.

    Terminal
    node generate-openapi.js
  5. Step 5: Optional: serve the file from your API

    If your API serves the file, you can paste its link into MCPEasy instead of uploading, and MCPEasy can pick up changes when you sync.

    Add this to your main file, after const app = express():

    app.js
    const path = require('path');
    
    app.get('/openapi.json', (req, res) => {
      res.sendFile(path.join(__dirname, 'openapi.json'));
    });

    Using ES modules? __dirname does not exist there. Use res.sendFile('openapi.json', { root: process.cwd() }) instead.

  6. Step 6: Optional: better descriptions with #swagger comments

    Good descriptions give better AI tools. You can add them one route at a time, inside the handler function. Routes without comments still appear.

    routes/bookings.js
    router.post('/', (req, res) => {
      // #swagger.summary = 'Create a booking'
      // #swagger.description = 'Books an appointment in a free slot.'
      const { service_id, start_time } = req.body;
      // ...
    });

Check that it worked

  • Open openapi.json. Under "paths" you should see each of your routes, such as "/users" or "/api/v1/bookings", with methods like "get" and "post".
  • If you served it, open https://your-api.com/openapi.json in a browser and you should see the same JSON.

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.

Already using swagger-jsdoc?

swagger-jsdoc does not read your routes. It only collects @openapi comments that you write above each route. Without those comments the file has no endpoints at all ("paths": {}). Use it only if your team already writes these comments; otherwise use swagger-autogen above.

  1. Step 1: Install and configure it

    apis is the list of files that contain your @openapi comments.

    Terminal
    npm install swagger-jsdoc
    app.js
    const swaggerJsdoc = require('swagger-jsdoc');
    
    const spec = swaggerJsdoc({
      definition: {
        openapi: '3.0.0',
        info: { title: 'My API', version: '1.0.0' },
        servers: [{ url: 'https://api.example.com' }],
      },
      apis: ['./routes/*.js'],
    });
    
    app.get('/openapi.json', (req, res) => res.json(spec));
  2. Step 2: Describe every route in a comment

    Only routes with a block like this appear in the file.

    routes/bookings.js
    /**
     * @openapi
     * /bookings:
     *   post:
     *     summary: Create a booking
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             required: [service_id, start_time]
     *             properties:
     *               service_id: { type: string }
     *               start_time: { type: string, format: date-time }
     *     responses:
     *       201:
     *         description: The new booking
     */
    router.post('/bookings', createBooking);

If something goes wrong

"paths" is empty or some routes are missing.
routes must list the file that mounts your routers with app.use, not a file that only creates the server. If your routers are loaded in a loop (for example with fs.readdirSync), swagger-autogen cannot follow them: list each router file in routes, or use From code (GitHub) in MCPEasy instead.
Paths are there but without the /api prefix.
The prefix comes from the app.use line. Make sure the file with app.use('/api', ...) is the one in routes, not the router file itself.
Error: Cannot use import statement outside a module (or require is not defined).
Your project's module type does not match the script. Use the other version of generate-openapi.js from step 3.

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