## Motivation and Context Currently there's a lot of unsafe manual JSON unmarshalling/marshalling, and we have to manually keep the openapi schema in sync. This PR migrates the API to [Huma](https://github.com/danielgtaylor/huma), which appears like a popular, well-maintained way to build APIs in Golang. I'm not a Golang expert, but seemed highly recommended by Reddit + Claude and has a decent star count. It provides automatic openapi spec generation and docs hosting, JSON parsing, as well as the ability to add auth middlewares etc. ## How Has This Been Tested? Running locally, and poking at the endpoints ## Breaking Changes Docs are served at `/docs` rather than `/v0/swagger/index.html` now ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions -->
103 lines
No EOL
3.1 KiB
Go
103 lines
No EOL
3.1 KiB
Go
package v0
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/google/uuid"
|
|
"github.com/modelcontextprotocol/registry/internal/model"
|
|
"github.com/modelcontextprotocol/registry/internal/service"
|
|
)
|
|
|
|
// Metadata contains pagination metadata
|
|
type Metadata struct {
|
|
NextCursor string `json:"next_cursor,omitempty"`
|
|
Count int `json:"count,omitempty"`
|
|
Total int `json:"total,omitempty"`
|
|
}
|
|
|
|
// ListServersInput represents the input for listing servers
|
|
type ListServersInput struct {
|
|
Cursor string `query:"cursor" doc:"Pagination cursor (UUID)" format:"uuid" required:"false"`
|
|
Limit int `query:"limit" doc:"Number of items per page" default:"30" minimum:"1" maximum:"100"`
|
|
}
|
|
|
|
// ListServersBody represents the paginated server list response body
|
|
type ListServersBody struct {
|
|
Servers []model.Server `json:"servers" doc:"List of MCP servers"`
|
|
Metadata *Metadata `json:"metadata,omitempty" doc:"Pagination metadata"`
|
|
}
|
|
|
|
// ServerDetailInput represents the input for getting server details
|
|
type ServerDetailInput struct {
|
|
ID string `path:"id" doc:"Server ID (UUID)" format:"uuid"`
|
|
}
|
|
|
|
|
|
// RegisterServersEndpoints registers all server-related endpoints
|
|
func RegisterServersEndpoints(api huma.API, registry service.RegistryService) {
|
|
// List servers endpoint
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "list-servers",
|
|
Method: http.MethodGet,
|
|
Path: "/v0/servers",
|
|
Summary: "List MCP servers",
|
|
Description: "Get a paginated list of MCP servers from the registry",
|
|
Tags: []string{"servers"},
|
|
}, func(_ context.Context, input *ListServersInput) (*Response[ListServersBody], error) {
|
|
// Validate cursor if provided
|
|
if input.Cursor != "" {
|
|
_, err := uuid.Parse(input.Cursor)
|
|
if err != nil {
|
|
return nil, huma.Error400BadRequest("Invalid cursor parameter")
|
|
}
|
|
}
|
|
|
|
// Get paginated results
|
|
servers, nextCursor, err := registry.List(input.Cursor, input.Limit)
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("Failed to get registry list", err)
|
|
}
|
|
|
|
// Build response body
|
|
body := ListServersBody{
|
|
Servers: servers,
|
|
}
|
|
|
|
// Add metadata if there's a next cursor
|
|
if nextCursor != "" {
|
|
body.Metadata = &Metadata{
|
|
NextCursor: nextCursor,
|
|
Count: len(servers),
|
|
}
|
|
}
|
|
|
|
return &Response[ListServersBody]{
|
|
Body: body,
|
|
}, nil
|
|
})
|
|
|
|
// Get server details endpoint
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "get-server",
|
|
Method: http.MethodGet,
|
|
Path: "/v0/servers/{id}",
|
|
Summary: "Get MCP server details",
|
|
Description: "Get detailed information about a specific MCP server",
|
|
Tags: []string{"servers"},
|
|
}, func(_ context.Context, input *ServerDetailInput) (*Response[model.ServerDetail], error) {
|
|
// Get the server details from the registry service
|
|
serverDetail, err := registry.GetByID(input.ID)
|
|
if err != nil {
|
|
if err.Error() == "record not found" {
|
|
return nil, huma.Error404NotFound("Server not found")
|
|
}
|
|
return nil, huma.Error500InternalServerError("Failed to get server details", err)
|
|
}
|
|
|
|
return &Response[model.ServerDetail]{
|
|
Body: *serverDetail,
|
|
}, nil
|
|
})
|
|
} |