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:
parent
ebaf4c9c99
commit
8d7e471fd3
1 changed files with 19980 additions and 15589 deletions
|
|
@ -20,7 +20,7 @@ import (
|
|||
|
||||
const registryURL = "http://localhost:8080"
|
||||
|
||||
var publishedIDRegex = regexp.MustCompile(`"id":\s*"([^"]+)"`)
|
||||
var publishedIDRegex = regexp.MustCompile(`"x-io\.modelcontextprotocol\.registry":\s*\{[^}]*"id":\s*"([^"]+)"`)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
|
|
@ -96,77 +96,10 @@ func run() error {
|
|||
func publish(examples []example) error {
|
||||
published := 0
|
||||
for _, example := range examples {
|
||||
log.Printf("Publishing example starting on line %d", example.line)
|
||||
|
||||
expected := map[string]any{}
|
||||
if err := json.Unmarshal(example.content, &expected); err != nil {
|
||||
log.Println(" ⛔ Example isn't valid JSON:", err)
|
||||
if err := publishExample(example); err != nil {
|
||||
log.Printf(" ⛔ %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Remove any existing namespace prefix and add anonymous prefix
|
||||
if !strings.HasPrefix(expected["name"].(string), "io.modelcontextprotocol.anonymous/") {
|
||||
parts := strings.SplitN(expected["name"].(string), "/", 2)
|
||||
serverName := parts[len(parts)-1]
|
||||
expected["name"] = "io.modelcontextprotocol.anonymous/" + serverName
|
||||
}
|
||||
example.content, _ = json.Marshal(expected)
|
||||
|
||||
p := filepath.Join("bin", fmt.Sprintf("example-line-%d.json", example.line))
|
||||
if err := os.WriteFile(p, example.content, 0600); err != nil {
|
||||
log.Printf(" ⛔ Failed to write example JSON to %s: %v\n", p, err)
|
||||
continue
|
||||
}
|
||||
defer os.Remove(p)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "./bin/publisher", "publish", "--mcp-file", p, "--registry-url", registryURL, "--auth-method", "none")
|
||||
cmd.WaitDelay = 100 * time.Millisecond
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) {
|
||||
return errors.New(" ⛔ publisher not found; did you run tests/integration/run.sh?")
|
||||
}
|
||||
output := strings.TrimSpace("publisher output:\n\t" + strings.ReplaceAll(string(out), "\n", "\n \t"))
|
||||
if err != nil {
|
||||
return errors.New(" ⛔ " + output)
|
||||
}
|
||||
log.Println(" ✅", output)
|
||||
|
||||
m := publishedIDRegex.FindStringSubmatch(output)
|
||||
if len(m) != 2 || m[1] == "" {
|
||||
return errors.New(" ⛔ Didn't find ID in publisher output")
|
||||
}
|
||||
id := m[1]
|
||||
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryURL+"/v0/servers/"+id, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf(" ⛔ %w", err)
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf(" ⛔ %w", err)
|
||||
}
|
||||
content, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf(" ⛔ failed to read registry response: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf(" ⛔ registry responded %d: %s", res.StatusCode, string(content))
|
||||
}
|
||||
|
||||
actual := map[string]any{}
|
||||
if err := json.Unmarshal(content, &actual); err != nil {
|
||||
return fmt.Errorf(" ⛔ failed to unmarshal registry response: %w", err)
|
||||
}
|
||||
if err := compare(expected, actual); err != nil {
|
||||
return fmt.Errorf(` ⛔ example "%s": %w`, expected["name"], err)
|
||||
}
|
||||
log.Print(" ✅ registry response matches example\n\n")
|
||||
published++
|
||||
}
|
||||
|
||||
|
|
@ -178,6 +111,136 @@ func publish(examples []example) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func publishExample(example example) error {
|
||||
log.Printf("Publishing example starting on line %d", example.line)
|
||||
|
||||
expected, err := parseExample(example)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := publishToRegistry(expected, example.line); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print(" ✅ registry response matches example\n\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseExample(example example) (map[string]any, error) {
|
||||
expected := map[string]any{}
|
||||
if err := json.Unmarshal(example.content, &expected); err != nil {
|
||||
return nil, fmt.Errorf("example isn't valid JSON: %w", err)
|
||||
}
|
||||
|
||||
// Handle both old ServerDetail format and new PublishRequest format
|
||||
var serverData map[string]any
|
||||
if server, exists := expected["server"]; exists {
|
||||
// New PublishRequest format
|
||||
serverData = server.(map[string]any)
|
||||
} else {
|
||||
// Old ServerDetail format (backward compatibility)
|
||||
serverData = expected
|
||||
}
|
||||
|
||||
// Remove any existing namespace prefix and add anonymous prefix
|
||||
if !strings.HasPrefix(serverData["name"].(string), "io.modelcontextprotocol.anonymous/") {
|
||||
parts := strings.SplitN(serverData["name"].(string), "/", 2)
|
||||
serverName := parts[len(parts)-1]
|
||||
serverData["name"] = "io.modelcontextprotocol.anonymous/" + serverName
|
||||
}
|
||||
|
||||
// Update the expected structure if it's PublishRequest format
|
||||
if _, exists := expected["server"]; exists {
|
||||
expected["server"] = serverData
|
||||
}
|
||||
|
||||
return expected, nil
|
||||
}
|
||||
|
||||
func publishToRegistry(expected map[string]any, line int) error {
|
||||
content, _ := json.Marshal(expected)
|
||||
p := filepath.Join("bin", fmt.Sprintf("example-line-%d.json", line))
|
||||
if err := os.WriteFile(p, content, 0600); err != nil {
|
||||
return fmt.Errorf("failed to write example JSON to %s: %w", p, err)
|
||||
}
|
||||
defer os.Remove(p)
|
||||
|
||||
id, err := runPublisher(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return verifyPublishedServer(id, expected)
|
||||
}
|
||||
|
||||
func runPublisher(filePath string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "./bin/publisher", "publish", "--mcp-file", filePath, "--registry-url", registryURL, "--auth-method", "none")
|
||||
cmd.WaitDelay = 100 * time.Millisecond
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) {
|
||||
return "", errors.New("publisher not found; did you run tests/integration/run.sh?")
|
||||
}
|
||||
output := strings.TrimSpace("publisher output:\n\t" + strings.ReplaceAll(string(out), "\n", "\n \t"))
|
||||
if err != nil {
|
||||
return "", errors.New(output)
|
||||
}
|
||||
log.Println(" ✅", output)
|
||||
|
||||
m := publishedIDRegex.FindStringSubmatch(output)
|
||||
if len(m) != 2 || m[1] == "" {
|
||||
return "", errors.New("didn't find ID in publisher output")
|
||||
}
|
||||
return m[1], nil
|
||||
}
|
||||
|
||||
func verifyPublishedServer(id string, expected map[string]any) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryURL+"/v0/servers/"+id, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read registry response: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("registry responded %d: %s", res.StatusCode, string(content))
|
||||
}
|
||||
|
||||
actual := map[string]any{}
|
||||
if err := json.Unmarshal(content, &actual); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal registry response: %w", err)
|
||||
}
|
||||
|
||||
// Both API response and expected are now in extension wrapper format
|
||||
// Compare the server portions of both
|
||||
actualServer, ok := actual["server"]
|
||||
if !ok {
|
||||
return fmt.Errorf("expected server field in registry response")
|
||||
}
|
||||
|
||||
// Extract expected server portion for comparison
|
||||
expectedServer := expected
|
||||
if server, exists := expected["server"]; exists {
|
||||
expectedServer = server.(map[string]any)
|
||||
}
|
||||
|
||||
if err := compare(expectedServer, actualServer); err != nil {
|
||||
return fmt.Errorf(`example "%s": %w`, expectedServer["name"], err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type example struct {
|
||||
content []byte
|
||||
line int
|
||||
|
|
|
|||
Loading…
Reference in a new issue