Skip to content

Commit c2f2606

Browse files
committed
feat: add admin edit endpoint for server moderation
Implements PUT /v0/servers/{id} endpoint to allow admins to modify server metadata for moderation purposes. Addresses part of issue #182 by providing an API-based "escape hatch" for handling malicious servers, spam, and data corruption without requiring direct database access. Key features: - Resource-level edit permissions using existing PermissionActionEdit - Added ServerStatusDeleted with anti-undelete protection - Full CRUD operations across database, service, and API layers - Comprehensive test coverage including edge cases Related to: #182 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> :house: Remote-Dev: homespace
1 parent 1df3737 commit c2f2606

12 files changed

Lines changed: 624 additions & 15 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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
// Parse the validated request body
62+
var editRequest model.PublishRequest
63+
if err := json.Unmarshal(input.RawBody, &editRequest); err != nil {
64+
return nil, huma.Error400BadRequest("Invalid JSON format", err)
65+
}
66+
67+
// Verify edit permissions for this server
68+
if !jwtManager.HasPermission(editRequest.Server.Name, auth.PermissionActionEdit, claims.Permissions) {
69+
return nil, huma.Error403Forbidden("You do not have edit permissions for this server")
70+
}
71+
72+
// Validate the server detail
73+
validator := validators.NewObjectValidator()
74+
if err := validator.Validate(&editRequest.Server); err != nil {
75+
return nil, huma.Error400BadRequest(err.Error())
76+
}
77+
78+
// Check if we're trying to undelete a server
79+
currentServer, err := registry.GetByID(input.ID)
80+
if err != nil {
81+
if errors.Is(err, database.ErrNotFound) {
82+
return nil, huma.Error404NotFound("Server not found")
83+
}
84+
return nil, huma.Error500InternalServerError("Failed to get current server", err)
85+
}
86+
87+
// Prevent undeleting servers - once deleted, they stay deleted
88+
if currentServer.Server.Status == model.ServerStatusDeleted && editRequest.Server.Status != model.ServerStatusDeleted {
89+
return nil, huma.Error400BadRequest("Cannot change status of deleted server. Deleted servers cannot be undeleted.")
90+
}
91+
92+
// Edit the server
93+
updatedServer, err := registry.EditServer(input.ID, editRequest)
94+
if err != nil {
95+
if errors.Is(err, database.ErrNotFound) {
96+
return nil, huma.Error404NotFound("Server not found")
97+
}
98+
return nil, huma.Error400BadRequest("Failed to edit server", err)
99+
}
100+
101+
return &Response[model.ServerResponse]{
102+
Body: *updatedServer,
103+
}, nil
104+
})
105+
}

0 commit comments

Comments
 (0)