Changes on the server.json format and the API (#562)

This commit is contained in:
Radoslav Dimitrov 2025-09-29 23:30:35 +03:00 • committed by GitHub
commit b21c6f564d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"github.com/danielgtaylor/huma/v2"
@ -18,8 +19,9 @@ import (
// EditServerInput represents the input for editing a server
type EditServerInput struct {
Authorization string `header:"Authorization" doc:"Registry JWT token with edit permissions" required:"true"`
ServerID string `path:"server_id" doc:"Server ID (UUID)" format:"uuid"`
Version string `query:"version" doc:"Version to edit (e.g., '1.0.0')" required:"true" example:"1.0.0"`
ServerName string `path:"serverName" doc:"URL-encoded server name" example:"com.example%2Fmy-server"`
Version string `path:"version" doc:"URL-encoded version to edit" example:"1.0.0"`
Status string `query:"status" doc:"New status for the server (active, deprecated, deleted)" required:"false" enum:"active,deprecated,deleted"`
Body apiv0.ServerJSON `body:""`
}
@ -31,14 +33,14 @@ func RegisterEditEndpoints(api huma.API, registry service.RegistryService, cfg *
huma.Register(api, huma.Operation{
OperationID: "edit-server",
Method: http.MethodPut,
Path: "/v0/servers/{server_id}",
Path: "/v0/servers/{serverName}/versions/{version}",
Summary: "Edit MCP server",
Description: "Update an existing MCP server (admin only). Requires version query parameter to specify which version to edit.",
Description: "Update a specific version of an existing MCP server (admin only).",
Tags: []string{"admin"},
Security: []map[string][]string{
{"bearer": {}},
},
}, func(ctx context.Context, input *EditServerInput) (*Response[apiv0.ServerJSON], error) {
}, func(ctx context.Context, input *EditServerInput) (*Response[apiv0.ServerResponse], error) {
// Extract bearer token
const bearerPrefix = "Bearer "
authHeader := input.Authorization
@ -53,9 +55,20 @@ func RegisterEditEndpoints(api huma.API, registry service.RegistryService, cfg *
return nil, huma.Error401Unauthorized("Invalid or expired Registry JWT token", err)
}
// Get current server to check permissions against existing name
currentServer, err := registry.GetByServerIDAndVersion(input.ServerID, input.Version)
// URL-decode the server name
serverName, err := url.PathUnescape(input.ServerName)
if err != nil {
return nil, huma.Error400BadRequest("Invalid server name encoding", err)
}
// URL-decode the version
version, err := url.PathUnescape(input.Version)
if err != nil {
return nil, huma.Error400BadRequest("Invalid version encoding", err)
}
// Get current server to check permissions against existing name
currentServer, err := registry.GetServerByNameAndVersion(ctx, serverName, version)
if err != nil {
if errors.Is(err, database.ErrNotFound) {
return nil, huma.Error404NotFound("Server not found")
@ -64,27 +77,42 @@ func RegisterEditEndpoints(api huma.API, registry service.RegistryService, cfg *
}
// Verify edit permissions for this server using the existing server name
if !jwtManager.HasPermission(currentServer.Name, auth.PermissionActionEdit, claims.Permissions) {
if !jwtManager.HasPermission(currentServer.Server.Name, auth.PermissionActionEdit, claims.Permissions) {
return nil, huma.Error403Forbidden("You do not have edit permissions for this server")
}
// Prevent renaming servers
if currentServer.Name != input.Body.Name {
if currentServer.Server.Name != input.Body.Name {
return nil, huma.Error400BadRequest("Cannot rename server")
}
// Prevent undeleting servers - once deleted, they stay deleted
if currentServer.Status == model.StatusDeleted && input.Body.Status != model.StatusDeleted {
return nil, huma.Error400BadRequest("Cannot change status of deleted server. Deleted servers cannot be undeleted.")
// Validate that the version in the body matches the URL parameter
if input.Body.Version != version {
return nil, huma.Error400BadRequest("Version in request body must match URL path parameter")
}
// Edit the server using the version_id from the current server
versionID := currentServer.GetVersionID()
if versionID == "" {
return nil, huma.Error500InternalServerError("Server version ID not found in metadata", nil)
// Handle status changes with proper permission validation
if input.Status != "" {
newStatus := model.Status(input.Status)
// Prevent undeleting servers - once deleted, they stay deleted
if currentServer.Meta.Official != nil &&
currentServer.Meta.Official.Status == model.StatusDeleted &&
newStatus != model.StatusDeleted {
return nil, huma.Error400BadRequest("Cannot change status of deleted server. Deleted servers cannot be undeleted.")
}
// For now, only allow status changes for admins
// Future: Implement logic to allow server authors to change active <-> deprecated
// but only admins can set to deleted
}
updatedServer, err := registry.EditServer(versionID, input.Body)
// Update the server using the service
var statusPtr *string
if input.Status != "" {
statusPtr = &input.Status
}
updatedServer, err := registry.UpdateServer(ctx, serverName, version, &input.Body, statusPtr)
if err != nil {
if errors.Is(err, database.ErrNotFound) {
return nil, huma.Error404NotFound("Server not found")
@ -92,7 +120,7 @@ func RegisterEditEndpoints(api huma.API, registry service.RegistryService, cfg *
return nil, huma.Error400BadRequest("Failed to edit server", err)
}
return &Response[apiv0.ServerJSON]{
return &Response[apiv0.ServerResponse]{
Body: *updatedServer,
}, nil
})