All framework guides

Go

Get an OpenAPI spec from Go (gin, echo, chi, net/http)

swag builds the spec from comments above your handlers. swag init writes docs/swagger.json.

Tool
swag
Time
A few minutes per endpoint
You get
docs/swagger.json and docs/swagger.yaml (Swagger 2.0)

How it works

Go has no standard way to describe an API. swag reads special comments above main() and above each handler, and writes the spec. Only handlers with comments appear.

The output is Swagger 2.0, which MCPEasy reads fine. If you have many handlers without comments, From code (GitHub) in MCPEasy is faster.

Step by step

  1. Step 1: Install the swag command

    Make sure your Go bin folder (go env GOPATH, then /bin) is on your PATH so the swag command is found.

    Terminal
    go install github.com/swaggo/swag/cmd/swag@latest
  2. Step 2: Describe the API above main()

    These comments go directly above func main().

    main.go
    // @title           My API
    // @version         1.0
    // @description     What this API does, in one sentence.
    // @host            api.example.com
    // @BasePath        /api/v1
    // @securityDefinitions.apikey BearerAuth
    // @in header
    // @name Authorization
    func main() {
        // ...
    }
  3. Step 3: Describe each handler

    One block per handler. @Router is the path and method; @Param body points at the request struct, so its fields appear.

    bookings.go
    type CreateBookingRequest struct {
        ServiceID string `json:"service_id" binding:"required"`
        StartTime string `json:"start_time" binding:"required"`
    }
    
    // CreateBooking godoc
    // @Summary      Create a booking
    // @Description  Books an appointment in a free slot.
    // @Tags         bookings
    // @Accept       json
    // @Produce      json
    // @Param        booking  body      CreateBookingRequest  true  "Booking details"
    // @Success      201      {object}  Booking
    // @Security     BearerAuth
    // @Router       /bookings [post]
    func CreateBooking(c *gin.Context) {
        // ...
    }
  4. Step 4: Generate the files

    Run in the folder with main.go. If main.go is elsewhere, point at it with -g.

    Terminal
    swag init
    Terminal (main.go in another folder)
    swag init -g cmd/api/main.go

    This writes docs/swagger.json, docs/swagger.yaml and docs/docs.go. Upload docs/swagger.json to MCPEasy.

  5. Step 5: Optional: serve it (gin)

    With gin-swagger, the spec is served at /swagger/doc.json. Replace yourmodule with your module name from go.mod.

    Terminal
    go get github.com/swaggo/gin-swagger github.com/swaggo/files
    main.go
    import (
        swaggerFiles "github.com/swaggo/files"
        ginSwagger "github.com/swaggo/gin-swagger"
    
        _ "yourmodule/docs"
    )
    
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

    For echo use github.com/swaggo/echo-swagger; for chi and net/http use github.com/swaggo/http-swagger.

Check that it worked

  • docs/swagger.json lists each handler you commented under "paths".

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

swag: command not found.
Your Go bin folder is not on PATH. Add $(go env GOPATH)/bin to PATH and open a new terminal.
A handler is missing.
It has no @Router comment, or the comment block is not directly above the function.
cannot find type definition.
swag could not find the struct named in @Param or @Success. Use the package name, for example models.Booking, or run swag init --parseDependency.

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