Skip to content

Commit 6431f23

Browse files
authored
feat: add admin edit endpoint for server moderation (#315)
## Summary Implements `PUT /v0/servers/{id}` admin edit endpoint to address part of issue #182, providing an API-based solution for server moderation without requiring direct database access. - **New endpoint**: `PUT /v0/servers/{id}` for editing existing servers - **Security**: Uses existing JWT auth with `PermissionActionEdit` permissions - **Anti-undelete protection**: Added `ServerStatusDeleted` status that cannot be reverted - **Full implementation**: Database, service, API, and comprehensive test coverage ## Key Use Cases - Mark malicious servers as `deleted` for takedown - Update server metadata for moderation actions Related to: #182 (but that is not _done_ yet because we need admins to be able to login (probably with #283), and need to add docs on how to do the takedown, and probably guidelines on what we will/won't takedown)
1 parent 36fcdff commit 6431f23

12 files changed

Lines changed: 681 additions & 23 deletions

File tree

‎docs/server-json/server.schema.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
},
6767
"status": {
6868
"type": "string",
69-
"enum": ["active", "deprecated"],
69+
"enum": ["active", "deprecated", "deleted"],
7070
"default": "active",
71-
"description": "Server lifecycle status. 'deprecated' indicates the server is no longer recommended for new usage."
71+
"description": "Server lifecycle status. 'deprecated' indicates the server is no longer recommended for new usage. 'deleted' indicates the server should never be installed and existing installations should be uninstalled - this is rare, and usually indicates malware or a legal takedown."
7272
},
7373
"repository": {
7474
"$ref": "#/$defs/Repository"

‎internal/api/handlers/v0/edit.go‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)