|
| 1 | +package v0 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/danielgtaylor/huma/v2" |
| 11 | + "github.com/modelcontextprotocol/registry/internal/auth" |
| 12 | + "github.com/modelcontextprotocol/registry/internal/config" |
| 13 | + "github.com/modelcontextprotocol/registry/internal/database" |
| 14 | + "github.com/modelcontextprotocol/registry/internal/model" |
| 15 | + "github.com/modelcontextprotocol/registry/internal/service" |
| 16 | + "github.com/modelcontextprotocol/registry/internal/validators" |
| 17 | +) |
| 18 | + |
| 19 | +// EditServerInput represents the input for editing a server |
| 20 | +type EditServerInput struct { |
| 21 | + Authorization string `header:"Authorization" doc:"Registry JWT token with edit permissions" required:"true"` |
| 22 | + ID string `path:"id" doc:"Server ID (UUID)" format:"uuid"` |
| 23 | + RawBody []byte `body:"raw"` |
| 24 | +} |
| 25 | + |
| 26 | +// RegisterEditEndpoints registers the edit endpoint |
| 27 | +func RegisterEditEndpoints(api huma.API, registry service.RegistryService, cfg *config.Config) { |
| 28 | + jwtManager := auth.NewJWTManager(cfg) |
| 29 | + |
| 30 | + // Edit server endpoint |
| 31 | + huma.Register(api, huma.Operation{ |
| 32 | + OperationID: "edit-server", |
| 33 | + Method: http.MethodPut, |
| 34 | + Path: "/v0/servers/{id}", |
| 35 | + Summary: "Edit MCP server", |
| 36 | + Description: "Update an existing MCP server (admin only)", |
| 37 | + Tags: []string{"admin"}, |
| 38 | + Security: []map[string][]string{ |
| 39 | + {"bearer": {}}, |
| 40 | + }, |
| 41 | + }, func(ctx context.Context, input *EditServerInput) (*Response[model.ServerResponse], error) { |
| 42 | + // Extract bearer token |
| 43 | + const bearerPrefix = "Bearer " |
| 44 | + authHeader := input.Authorization |
| 45 | + if len(authHeader) < len(bearerPrefix) || !strings.EqualFold(authHeader[:len(bearerPrefix)], bearerPrefix) { |
| 46 | + return nil, huma.Error401Unauthorized("Invalid Authorization header format. Expected 'Bearer <token>'") |
| 47 | + } |
| 48 | + token := authHeader[len(bearerPrefix):] |
| 49 | + |
| 50 | + // Validate Registry JWT token |
| 51 | + claims, err := jwtManager.ValidateToken(ctx, token) |
| 52 | + if err != nil { |
| 53 | + return nil, huma.Error401Unauthorized("Invalid or expired Registry JWT token", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Validate that only allowed extension fields are present |
| 57 | + if err := model.ValidatePublishRequestExtensions(input.RawBody); err != nil { |
| 58 | + return nil, huma.Error400BadRequest("Invalid request format", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Get current server to check permissions against existing name |
| 62 | + currentServer, err := registry.GetByID(input.ID) |
| 63 | + if err != nil { |
| 64 | + if errors.Is(err, database.ErrNotFound) { |
| 65 | + return nil, huma.Error404NotFound("Server not found") |
| 66 | + } |
| 67 | + return nil, huma.Error500InternalServerError("Failed to get current server", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Verify edit permissions for this server using the existing server name |
| 71 | + if !jwtManager.HasPermission(currentServer.Server.Name, auth.PermissionActionEdit, claims.Permissions) { |
| 72 | + return nil, huma.Error403Forbidden("You do not have edit permissions for this server") |
| 73 | + } |
| 74 | + |
| 75 | + // Parse the validated request body |
| 76 | + var editRequest model.PublishRequest |
| 77 | + if err := json.Unmarshal(input.RawBody, &editRequest); err != nil { |
| 78 | + return nil, huma.Error400BadRequest("Invalid JSON format", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Validate the server detail |
| 82 | + validator := validators.NewObjectValidator() |
| 83 | + if err := validator.Validate(&editRequest.Server); err != nil { |
| 84 | + return nil, huma.Error400BadRequest(err.Error()) |
| 85 | + } |
| 86 | + |
| 87 | + // Prevent renaming servers |
| 88 | + if currentServer.Server.Name != editRequest.Server.Name { |
| 89 | + return nil, huma.Error400BadRequest("Cannot rename server") |
| 90 | + } |
| 91 | + |
| 92 | + // Prevent undeleting servers - once deleted, they stay deleted |
| 93 | + if currentServer.Server.Status == model.ServerStatusDeleted && editRequest.Server.Status != model.ServerStatusDeleted { |
| 94 | + return nil, huma.Error400BadRequest("Cannot change status of deleted server. Deleted servers cannot be undeleted.") |
| 95 | + } |
| 96 | + |
| 97 | + // Edit the server |
| 98 | + updatedServer, err := registry.EditServer(input.ID, editRequest) |
| 99 | + if err != nil { |
| 100 | + if errors.Is(err, database.ErrNotFound) { |
| 101 | + return nil, huma.Error404NotFound("Server not found") |
| 102 | + } |
| 103 | + return nil, huma.Error400BadRequest("Failed to edit server", err) |
| 104 | + } |
| 105 | + |
| 106 | + return &Response[model.ServerResponse]{ |
| 107 | + Body: *updatedServer, |
| 108 | + }, nil |
| 109 | + }) |
| 110 | +} |
0 commit comments