All framework guides

PHP

Get an OpenAPI spec from Symfony

Install NelmioApiDocBundle and run nelmio:apidoc:dump. API Platform projects already have a spec.

Tool
NelmioApiDocBundle
Time
About 10 minutes
You get
openapi.json (OpenAPI 3)

How it works

Symfony has no built-in spec. NelmioApiDocBundle builds one from your app: it reads your routes, Symfony attributes such as #[MapRequestPayload] and #[MapQueryString], your entity and DTO classes with their validator constraints, and any #[OA\...] attributes you add for the parts it cannot work out on its own.

By default it documents only routes under /api. The same bundle serves Swagger UI at /api/doc, so one install gives you browsable Swagger docs for your team and a file for MCPEasy.

If your project uses API Platform, skip the bundle: API Platform writes the OpenAPI document itself. See the second route below.

Step by step

  1. Step 1: Install NelmioApiDocBundle

    In your Symfony project folder. With Symfony Flex (the default) this also registers the bundle and creates config/packages/nelmio_api_doc.yaml.

    Terminal
    composer require nelmio/api-doc-bundle
  2. Step 2: Set the title and which routes to document

    Open config/packages/nelmio_api_doc.yaml. Put your API's name under documentation.info, and keep path_patterns matching your API prefix. The pattern below documents everything under /api except the docs page itself, which keeps routes like /_profiler out.

    config/packages/nelmio_api_doc.yaml
    nelmio_api_doc:
        documentation:
            info:
                title: Bookings API
                description: Create and manage bookings
                version: 1.0.0
        areas:
            path_patterns:
                - ^/api(?!/doc$)

    If your API is not under /api, change the pattern, for example ^/v1, or ^/ for every route.

  3. Step 3: Export the file

    This prints the whole spec. Save it to a file and upload that file to MCPEasy.

    Terminal
    php bin/console nelmio:apidoc:dump --format=json > openapi.json
    Terminal (YAML instead)
    php bin/console nelmio:apidoc:dump --format=yaml > openapi.yaml
  4. Step 4: Optional: Swagger UI and a live link

    To browse the docs at /api/doc and serve the spec at /api/doc.json, check config/routes/nelmio_api_doc.yaml. If these two routes are missing or commented out, add them. Swagger UI also needs the Twig and Asset packages.

    Terminal
    composer require twig asset
    config/routes/nelmio_api_doc.yaml
    app.swagger_ui:
        path: /api/doc
        methods: GET
        defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
    
    app.swagger:
        path: /api/doc.json
        methods: GET
        defaults: { _controller: nelmio_api_doc.controller.swagger }

    With /api/doc.json live, you can paste https://your-site.com/api/doc.json into MCPEasy instead of uploading a file, and the spec stays current as you add routes.

  5. Step 5: Fill the gaps with attributes

    Where a response or parameter comes out thin, describe it on the controller method. Model points the response at your DTO or entity class, so its fields and types appear in the spec.

    src/Controller/BookingController.php
    use App\Dto\BookingDto;
    use Nelmio\ApiDocBundle\Attribute\Model;
    use OpenApi\Attributes as OA;
    use Symfony\Component\HttpFoundation\JsonResponse;
    use Symfony\Component\Routing\Attribute\Route;
    
    #[Route('/api/bookings', methods: ['GET'])]
    #[OA\Response(
        response: 200,
        description: 'Bookings on the given date',
        content: new OA\JsonContent(type: 'array', items: new OA\Items(ref: new Model(type: BookingDto::class)))
    )]
    #[OA\Parameter(name: 'date', in: 'query', description: 'Day to list, YYYY-MM-DD', schema: new OA\Schema(type: 'string'))]
    #[OA\Tag(name: 'bookings')]
    public function list(): JsonResponse

    The descriptions become each tool's description in MCPEasy, which is what the AI reads to decide when to call it. One plain sentence per endpoint is enough.

Check that it worked

  • Open openapi.json. Each route under /api should be listed under "paths" with its method, and your DTO or entity classes should appear under "components" > "schemas".
  • If you added the routes in step 4, /api/doc shows the same endpoints in Swagger UI.

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.

Using API Platform?

API Platform generates the OpenAPI document from your resources, so there is nothing to install.

  1. Step 1: Export it as OpenAPI 3.0

    Recent API Platform versions write OpenAPI 3.2 by default. MCPEasy reads OpenAPI 3.0 and 3.1, so ask for 3.0. The same document is served live at /api/docs.jsonopenapi; add ?spec_version=3.0.0 to that link.

    Terminal
    php bin/console api:openapi:export --spec-version=3.0.0 --output=openapi.json

    For YAML add --yaml (it needs symfony/yaml installed).

If something goes wrong

"paths" is empty, or only shows /api/doc.
Your routes do not match path_patterns. Change the pattern to your prefix (step 2) and run the dump again.
Routes like /_profiler or /_error appear.
path_patterns is too wide. Keep it to your API prefix, such as ^/api(?!/doc$).
There are no commands defined in the "nelmio:apidoc" namespace.
The bundle is not registered. Without Flex, add Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true] to config/bundles.php.
/api/doc fails with an error about Twig or assets.
Swagger UI needs both packages: composer require twig asset.
A response shows no fields.
The bundle cannot see what you return from a plain JsonResponse. Add an #[OA\Response] with a Model of your DTO (step 5).
MCPEasy says the API Platform file is not a supported format.
It was written as OpenAPI 3.2. Export again with --spec-version=3.0.0.

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