Allow for vendor extensions on servers (#298)

<!-- Provide a brief summary of your changes -->
This PR serves as a placeholder and brings together all of the changes
related to supporting vendor extensions passed through the API. The idea
is to have a single place to track and review the work as it comes
together (even if it’s still a bit of a work in progress).

**The changes in this PR cover:**
* Updates on the data model
* Updates on both databases (in-memory and postgresql), added a new
table to PSQL for tracking the extensions leaving the server table only
for the clean server.json properties
* Updates the seed.json file (thus the big line change)
* Adds a seed-migrate tool to help migrating an existing seed file to
the new format
* Updates the seed import mechanism to support this
* Updates the existing tests - unit, integration and validation tests
* Adds the publish handler to the OpenAPI spec (it was missing)
* Fixes linting errors
* Updates on the API side on (publish, list, get) to support the new
format of `ServerResponse` -
```
{
  "server": {},
  "x-io.modelcontextprotocol.registry": {}, /*not available for publish since this gets populated by the registry*/
  "x-publisher": {}
}
```

## Motivation and Context
<!-- Why is this change needed? What problem does it solve? -->
Motivated by the discussions in:
https://github.com/modelcontextprotocol/registry/discussions/284

## How Has This Been Tested?
<!-- Have you tested this in a real application? Which scenarios were
tested? -->
Locally and by running all of the tests

## Breaking Changes
<!-- Will users need to update their code or configurations? -->
Yes, it changes both the API and the format of the seed file
 
## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

---

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Co-authored-by: adam jones <domdomegg+git@gmail.com>
This commit is contained in:
Radoslav Dimitrov 2025-08-23 11:59:03 +03:00 • committed by GitHub
commit 8d7e471fd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,11 @@
package model
import (
"encoding/json"
"fmt"
"time"
)
// AuthMethod represents the authentication method used
type AuthMethod string
@ -26,10 +32,6 @@ const (
ServerStatusDeprecated ServerStatus = "deprecated"
)
// PublishRequest represents a request to publish a server to the registry
type PublishRequest struct {
ServerDetail `json:",inline"`
}
// Repository represents a source code repository as defined in the spec
type Repository struct {
@ -38,12 +40,6 @@ type Repository struct {
ID string `json:"id,omitempty" bson:"id,omitempty"`
}
// ServerList represents the response for listing servers as defined in the spec
type ServerList struct {
Servers []Server `json:"servers" bson:"servers"`
Next string `json:"next,omitempty" bson:"next,omitempty"`
TotalCount int `json:"total_count" bson:"total_count"`
}
// create an enum for Format
type Format string
@ -107,26 +103,163 @@ type Remote struct {
Headers []KeyValueInput `json:"headers,omitempty" bson:"headers,omitempty"`
}
// VersionDetail represents the version details of a server
// VersionDetail represents the version details of a server (pure MCP spec, no registry metadata)
type VersionDetail struct {
Version string `json:"version" bson:"version"`
ReleaseDate string `json:"release_date,omitempty" bson:"release_date"`
IsLatest bool `json:"is_latest,omitempty" bson:"is_latest"`
Version string `json:"version" bson:"version"`
}
// Server represents a basic server information as defined in the spec
type Server struct {
ID string `json:"id,omitempty" bson:"id"`
// ServerDetail represents complete server information as defined in the MCP spec (pure, no registry metadata)
type ServerDetail struct {
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Status ServerStatus `json:"status,omitempty" bson:"status,omitempty"`
Repository Repository `json:"repository,omitempty" bson:"repository"`
VersionDetail VersionDetail `json:"version_detail" bson:"version_detail"`
Packages []Package `json:"packages,omitempty" bson:"packages,omitempty"`
Remotes []Remote `json:"remotes,omitempty" bson:"remotes,omitempty"`
}
// ServerDetail represents detailed server information as defined in the spec
type ServerDetail struct {
Server `json:",inline" bson:",inline"`
Packages []Package `json:"packages,omitempty" bson:"packages,omitempty"`
Remotes []Remote `json:"remotes,omitempty" bson:"remotes,omitempty"`
// RegistryMetadata represents registry-generated metadata
type RegistryMetadata struct {
ID string `json:"id" bson:"_id"`
PublishedAt time.Time `json:"published_at" bson:"published_at"`
UpdatedAt time.Time `json:"updated_at,omitempty" bson:"updated_at,omitempty"`
IsLatest bool `json:"is_latest" bson:"is_latest"`
ReleaseDate string `json:"release_date" bson:"release_date"`
}
// ServerRecord represents the complete storage model that separates server.json from registry metadata
type ServerRecord struct {
ServerJSON ServerDetail `json:"server" bson:"server"` // Pure MCP server.json
RegistryMetadata RegistryMetadata `json:"registry_metadata" bson:"registry_metadata"` // Registry-generated data
PublisherExtensions map[string]interface{} `json:"publisher_extensions" bson:"publisher_extensions"` // x-publisher extensions
}
// ServerResponse represents the API response format with wrapper and extensions
type ServerResponse struct {
Server ServerDetail `json:"server"`
XIOModelContextProtocolRegistry interface{} `json:"x-io.modelcontextprotocol.registry,omitempty"`
XPublisher interface{} `json:"x-publisher,omitempty"`
}
// ServerListResponse represents the paginated server list response
type ServerListResponse struct {
Servers []ServerResponse `json:"servers"`
Metadata *Metadata `json:"metadata,omitempty"`
}
// PublishRequest represents the API request format for publishing servers
type PublishRequest struct {
Server ServerDetail `json:"server"`
XPublisher interface{} `json:"x-publisher,omitempty"`
}
// Metadata represents pagination metadata
type Metadata struct {
NextCursor string `json:"next_cursor,omitempty"`
Count int `json:"count,omitempty"`
Total int `json:"total,omitempty"`
}
// Helper functions
// ValidatePublisherExtensions validates that publisher extensions are within size limits
func ValidatePublisherExtensions(req PublishRequest) error {
const maxExtensionSize = 4 * 1024 // 4KB limit
// Check size limit for x-publisher extension
if req.XPublisher != nil {
extensionsJSON, err := json.Marshal(req.XPublisher)
if err != nil {
return fmt.Errorf("failed to marshal x-publisher extension: %w", err)
}
if len(extensionsJSON) > maxExtensionSize {
return fmt.Errorf("x-publisher extension exceeds 4KB limit (%d bytes)", len(extensionsJSON))
}
}
return nil
}
// ValidatePublishRequestExtensions validates that only allowed extension fields are present
func ValidatePublishRequestExtensions(requestData []byte) error {
// Parse the raw JSON to check for unknown fields
var rawRequest map[string]interface{}
if err := json.Unmarshal(requestData, &rawRequest); err != nil {
return fmt.Errorf("failed to parse request JSON: %w", err)
}
// Define allowed top-level fields
allowedFields := map[string]bool{
"server": true,
"x-publisher": true,
}
// Check for any disallowed fields
var invalidFields []string
for field := range rawRequest {
if !allowedFields[field] {
invalidFields = append(invalidFields, field)
}
}
if len(invalidFields) > 0 {
return fmt.Errorf("invalid extension fields: %v. Only 'server' and 'x-publisher' fields are allowed", invalidFields)
}
return nil
}
// ExtractPublisherExtensions extracts publisher extensions from a PublishRequest
func ExtractPublisherExtensions(req PublishRequest) map[string]interface{} {
publisherExtensions := make(map[string]interface{})
if req.XPublisher != nil {
// Cast to map and copy fields directly, avoiding double nesting
if publisherMap, ok := req.XPublisher.(map[string]interface{}); ok {
for k, v := range publisherMap {
publisherExtensions[k] = v
}
}
}
return publisherExtensions
}
// CreateRegistryExtensions generates the x-io.modelcontextprotocol.registry extension from registry metadata
func (rm *RegistryMetadata) CreateRegistryExtensions() map[string]interface{} {
return map[string]interface{}{
"x-io.modelcontextprotocol.registry": map[string]interface{}{
"id": rm.ID,
"published_at": rm.PublishedAt,
"updated_at": rm.UpdatedAt,
"is_latest": rm.IsLatest,
"release_date": rm.ReleaseDate,
},
}
}
// ParseServerName extracts the server name from a ServerDetail for validation purposes
func ParseServerName(serverDetail ServerDetail) (string, error) {
name := serverDetail.Name
if name == "" {
return "", fmt.Errorf("server name is required and must be a string")
}
return name, nil
}
// ToServerResponse converts a ServerRecord to API response format
func (sr *ServerRecord) ToServerResponse() ServerResponse {
response := ServerResponse{
Server: sr.ServerJSON,
}
// Add registry metadata extension
response.XIOModelContextProtocolRegistry = sr.RegistryMetadata.CreateRegistryExtensions()["x-io.modelcontextprotocol.registry"]
// Add publisher extensions directly
if len(sr.PublisherExtensions) > 0 {
response.XPublisher = sr.PublisherExtensions
}
return response
}