Skip to content

Commit f06d1ed

Browse files
authored
Added /validate endpoint and modified mcp-publish so that all validation happens there (#896)
## Motivation and Context Per discussion with @rdimitrov - all validation now happens in a new `/validate` endpoint. For details, see below. ## Breaking Changes None. Validation on /publish only validates schema version and semantic checks (manual checks) as before. We can add schema validation by changing a flag in the called to `ValidateJSONSchema` when we're ready. ## Types of changes - [X] New feature (non-breaking change which adds functionality) ## Checklist - [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 ## API Changes ### 1. New `/validate` Endpoint A new public endpoint was added for validating `server.json` files without publishing them. **Details:** - **Paths:** `/v0/validate` and `/v0.1/validate` - **Method:** `POST` - **Authentication:** None required (public endpoint) - **Request:** JSON body containing `ServerJSON` - **Response:** `200 OK` with `ValidationResult` containing: - `valid` (boolean) - `issues` (array of validation issues with type, path, message, severity, reference) **Implementation:** - New handler file: `internal/api/handlers/v0/validate.go` - Performs comprehensive validation using `ValidationAll` option: - Schema version validation - Full schema validation - Semantic validation - Always returns `200 OK` - validity is indicated in `result.Valid` field - Registered in router for both `/v0` and `/v0.1` prefixes ### 2. `/publish` Endpoint Validation Flow Changes The publish endpoint now validates earlier in the request flow and provides clearer error responses. **Before:** Validation was mixed into `ValidatePublishRequest()` (which also handled publisher extensions and registry ownership). **After:** 1. Validates `ServerJSON` structure first using `ValidateServerJSON()` (schema version + semantic validation) 2. Returns `422 Unprocessable Entity` with message: "Failed to publish server, invalid schema: call /validate for details" 3. Only proceeds to publisher extensions/registry validation if schema validation passes 4. No longer calls `ValidatePublishRequest()` internally for schema validation **Key Changes:** - Validation happens before `CreateServer()` call - Returns `422` on schema/semantic validation failures - Error message directs users to `/validate` endpoint for details - `ValidatePublishRequest()` no longer performs schema validation (added note in comments) ### 3. `/edit` Endpoint Validation Flow Changes Similar validation changes were applied to the edit endpoint. **Changes:** - Added `ValidateServerJSON()` call before server update - Returns `422` with same error message format on validation failure - Matches the `/publish` endpoint behavior ### 4. Validator Functions Refactoring Updated validator functions to reflect that schema validation is now done separately. **Changes to `ValidatePublishRequest()`:** - Removed internal `ValidateServerJSON()` call - Added note: "ValidateServerJSON should be called separately before this function" - Now only validates publisher extensions and registry ownership **Changes to `ValidateUpdateRequest()`:** - Same change (removed schema validation, added note) - Now only validates registry ownership ## CLI Changes ### 1. `validate` Command: Now Uses API Endpoint **Before:** Performed local validation using `validators.ValidateServerJSON()`. **After:** Calls the `/v0/validate` endpoint on the registry. **Details:** - Reads `server.json` and sends it to the registry API - Displays validation results returned by the server - Uses the same error formatting as before - Registry URL comes from token file (or default) - No authentication required (public endpoint) **Implementation:** - Added `validateViaAPI()` function that POSTs to `/v0/validate` - Parses `ValidationResult` response from server - Uses shared `printValidationIssues()` for formatting ### 2. `publish` Command: Validation Only on 422 Errors **Before:** Performed upfront local schema validation before publishing. **After:** Attempts publish first, then validates only if server returns 422. **Details:** - Attempts publish immediately (no upfront validation) - If publish returns 422 (Unprocessable Entity): - Calls `/v0/validate` endpoint to get detailed validation errors - Formats and displays them using same logic as `validate` command - Returns error with migration links - For non-422 errors (401, 403, etc.), returns original error (no validation call) **Implementation Changes:** - Removed upfront `runValidationAndPrintIssues()` call - Modified `publishToRegistry()` to return HTTP status code alongside response/error - Added 422 detection logic that calls `validateViaAPI()` ### 3. Shared Validation Error Formatting **Before:** `runValidationAndPrintIssues()` function handled both validation and printing together. **After:** Refactored to separate printing logic. **Details:** - Created `printValidationIssues()` function - just prints validation issues (no validation logic) - Removed `runValidationAndPrintIssues()` function (no longer used) - Both `validate` and `publish` commands use `printValidationIssues()` for consistent output **Behavior:** - Prints schema validation errors with migration guidance - Prints all other validation issues with formatting - Returns formatted error message string for schema issues ## Summary Both CLI commands now use the `/validate` API endpoint for validation instead of local validation. The `publish` command validates only after receiving a 422 error response, and both commands share the same error formatting via `printValidationIssues()`. ### Benefits - **Centralized validation logic** - All validation happens on the server - **Always up-to-date** - CLI always uses current server validation rules - **Consistent experience** - Same validation behavior whether using CLI or API directly - **Better separation of concerns** - Schema validation separated from publisher-specific validation
1 parent dc73689 commit f06d1ed

File tree

13 files changed

+986
-132
lines changed

13 files changed

+986
-132
lines changed

CHANGES.md

Whitespace-only changes.

cmd/publisher/commands/publish.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15-
"github.com/modelcontextprotocol/registry/internal/validators"
1615
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
1716
)
1817

@@ -38,22 +37,6 @@ func PublishCommand(args []string) error {
3837
return fmt.Errorf("invalid server.json: %w", err)
3938
}
4039

41-
// Validate schema version (non-empty schema, valid schema, and current schema)
42-
// This performs schema version checks without full schema validation
43-
// Note: When we enable full validation, use validators.ValidationAll instead
44-
result, formattedErrorMsg := runValidationAndPrintIssues(&serverJSON, validators.ValidationSchemaVersionOnly)
45-
if !result.Valid {
46-
// Return error after printing (all errors already printed by validateServerJSON)
47-
// Prefer formatted error message for schema validation errors, otherwise use first error
48-
if formattedErrorMsg != "" {
49-
return fmt.Errorf("%s", formattedErrorMsg)
50-
}
51-
if firstErr := result.FirstError(); firstErr != nil {
52-
return firstErr
53-
}
54-
return fmt.Errorf("validation failed")
55-
}
56-
5740
// Load saved token
5841
homeDir, err := os.UserHomeDir()
5942
if err != nil {
@@ -82,8 +65,33 @@ func PublishCommand(args []string) error {
8265

8366
// Publish to registry
8467
_, _ = fmt.Fprintf(os.Stdout, "Publishing to %s...\n", registryURL)
85-
response, err := publishToRegistry(registryURL, serverData, token)
68+
response, statusCode, err := publishToRegistry(registryURL, serverData, token)
8669
if err != nil {
70+
// If publish failed with 422, call validate endpoint to show detailed errors
71+
if statusCode == http.StatusUnprocessableEntity {
72+
_, _ = fmt.Fprintln(os.Stdout, "Validation failed. Checking detailed validation errors...")
73+
_, _ = fmt.Fprintln(os.Stdout)
74+
75+
// Call validate endpoint (same as validate command does)
76+
result, validateErr := validateViaAPI(registryURL, serverData)
77+
if validateErr != nil {
78+
// If validate also fails, return original publish error
79+
return fmt.Errorf("publish failed: %w", err)
80+
}
81+
82+
// Print validation results using shared formatting logic
83+
formattedErrorMsg := printValidationIssues(result, &serverJSON)
84+
85+
if !result.Valid {
86+
// Return error with formatted message if available
87+
if formattedErrorMsg != "" {
88+
return fmt.Errorf("%s", formattedErrorMsg)
89+
}
90+
return fmt.Errorf("validation failed")
91+
}
92+
}
93+
94+
// For non-422 errors, return the original error
8795
return fmt.Errorf("publish failed: %w", err)
8896
}
8997

@@ -93,18 +101,18 @@ func PublishCommand(args []string) error {
93101
return nil
94102
}
95103

96-
func publishToRegistry(registryURL string, serverData []byte, token string) (*apiv0.ServerResponse, error) {
104+
func publishToRegistry(registryURL string, serverData []byte, token string) (*apiv0.ServerResponse, int, error) {
97105
// Parse the server JSON data
98106
var serverJSON apiv0.ServerJSON
99107
err := json.Unmarshal(serverData, &serverJSON)
100108
if err != nil {
101-
return nil, fmt.Errorf("error parsing server.json file: %w", err)
109+
return nil, 0, fmt.Errorf("error parsing server.json file: %w", err)
102110
}
103111

104112
// Convert to JSON
105113
jsonData, err := json.Marshal(serverJSON)
106114
if err != nil {
107-
return nil, fmt.Errorf("error serializing request: %w", err)
115+
return nil, 0, fmt.Errorf("error serializing request: %w", err)
108116
}
109117

110118
// Ensure URL ends with the publish endpoint
@@ -116,32 +124,32 @@ func publishToRegistry(registryURL string, serverData []byte, token string) (*ap
116124
// Create and send request
117125
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, publishURL, bytes.NewBuffer(jsonData))
118126
if err != nil {
119-
return nil, fmt.Errorf("error creating request: %w", err)
127+
return nil, 0, fmt.Errorf("error creating request: %w", err)
120128
}
121129
req.Header.Set("Content-Type", "application/json")
122130
req.Header.Set("Authorization", "Bearer "+token)
123131

124132
client := &http.Client{}
125133
resp, err := client.Do(req)
126134
if err != nil {
127-
return nil, fmt.Errorf("error sending request: %w", err)
135+
return nil, 0, fmt.Errorf("error sending request: %w", err)
128136
}
129137
defer resp.Body.Close()
130138

131139
// Read response
132140
body, err := io.ReadAll(resp.Body)
133141
if err != nil {
134-
return nil, fmt.Errorf("error reading response: %w", err)
142+
return nil, resp.StatusCode, fmt.Errorf("error reading response: %w", err)
135143
}
136144

137145
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
138-
return nil, fmt.Errorf("server returned status %d: %s", resp.StatusCode, body)
146+
return nil, resp.StatusCode, fmt.Errorf("server returned status %d: %s", resp.StatusCode, body)
139147
}
140148

141149
var serverResponse apiv0.ServerResponse
142150
if err := json.Unmarshal(body, &serverResponse); err != nil {
143-
return nil, err
151+
return nil, resp.StatusCode, err
144152
}
145153

146-
return &serverResponse, nil
154+
return &serverResponse, resp.StatusCode, nil
147155
}

0 commit comments

Comments
 (0)