<!-- Provide a brief summary of your changes --> ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> The following PR: * Adds a new `transport` type used in Package and Remotes * Added `docker` to the `runtime_hint` examples * Added support for templated URLs. It is available for streamable-http and sse transports for Packages. I didn't thought of reasons to have this for Remotes, but we can enable it if needed. * Added validation for templated URLs so we can ensure all referenced placeholder variables are available. * For streamable-http and SSE transport types the URL is now a required property * Updated the publisher init command to reflect these changes * Updated the seed.json file * Added unit tests for the transport type related changes * Updated the server.json schema and the external pkg structs to reflect the new transport type * Updated the examples Thanks to @kkkarthik for spotting it! 🙏 ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> Ran the tests locally ## Breaking Changes <!-- Will users need to update their code or configurations? --> Yes ## 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 ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [ ] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> --------- Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
1193 lines
33 KiB
Go
1193 lines
33 KiB
Go
package validators_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/registry/internal/config"
|
|
"github.com/modelcontextprotocol/registry/internal/validators"
|
|
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
|
|
"github.com/modelcontextprotocol/registry/pkg/model"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
serverDetail apiv0.ServerJSON
|
|
expectedError string
|
|
}{
|
|
{
|
|
name: "valid server detail with all fields",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
ID: "owner/repo",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
RegistryBaseURL: "https://registry.npmjs.org",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/remote",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "server with invalid repository source",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://bitbucket.org/owner/repo",
|
|
Source: "bitbucket", // Not in validSources
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRepositoryURL.Error(),
|
|
},
|
|
{
|
|
name: "server with invalid GitHub URL format",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner", // Missing repo name
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRepositoryURL.Error(),
|
|
},
|
|
{
|
|
name: "server with invalid GitLab URL format",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://gitlab.com", // Missing owner and repo
|
|
Source: "gitlab",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRepositoryURL.Error(),
|
|
},
|
|
{
|
|
name: "package with spaces in name",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test package with spaces",
|
|
RegistryType: "npm",
|
|
RegistryBaseURL: "https://registry.npmjs.org",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrPackageNameHasSpaces.Error(),
|
|
},
|
|
{
|
|
name: "multiple packages with one invalid",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "valid-package",
|
|
RegistryType: "npm",
|
|
RegistryBaseURL: "https://registry.npmjs.org",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
{
|
|
Identifier: "invalid package", // Has space
|
|
RegistryType: "pypi",
|
|
RegistryBaseURL: "https://pypi.org",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrPackageNameHasSpaces.Error(),
|
|
},
|
|
{
|
|
name: "remote with invalid URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "not-a-valid-url",
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRemoteURL.Error(),
|
|
},
|
|
{
|
|
name: "remote with missing scheme",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "example.com/remote",
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRemoteURL.Error(),
|
|
},
|
|
{
|
|
name: "remote with localhost url",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "http://localhost",
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRemoteURL.Error(),
|
|
},
|
|
{
|
|
name: "remote with localhost url with port",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "http://localhost:3000",
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRemoteURL.Error(),
|
|
},
|
|
{
|
|
name: "multiple remotes with one invalid",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://valid.com/remote",
|
|
},
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "invalid-url",
|
|
},
|
|
},
|
|
},
|
|
expectedError: validators.ErrInvalidRemoteURL.Error(),
|
|
},
|
|
{
|
|
name: "server detail with nil packages and remotes",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: nil,
|
|
Remotes: nil,
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "server detail with empty packages and remotes slices",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{},
|
|
Remotes: []model.Transport{},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validators.ValidateServerJSON(&tt.serverDetail)
|
|
|
|
if tt.expectedError == "" {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.expectedError)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidate_RemoteNamespaceMatch(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
serverDetail apiv0.ServerJSON
|
|
expectError bool
|
|
errorMsg string
|
|
}{
|
|
{
|
|
name: "valid match - example.com domain",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/mcp",
|
|
},
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid match - subdomain mcp.example.com",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://mcp.example.com/endpoint",
|
|
},
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid match - api subdomain",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/api-server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://api.example.com/mcp",
|
|
},
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "invalid - wrong domain",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://google.com/mcp",
|
|
},
|
|
},
|
|
},
|
|
expectError: true,
|
|
errorMsg: "remote URL host google.com does not match publisher domain example.com",
|
|
},
|
|
{
|
|
name: "invalid - different domain entirely",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.microsoft/server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://api.github.com/endpoint",
|
|
},
|
|
},
|
|
},
|
|
expectError: true,
|
|
errorMsg: "remote URL host api.github.com does not match publisher domain microsoft.com",
|
|
},
|
|
{
|
|
name: "invalid URL format",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "not-a-valid-url",
|
|
},
|
|
},
|
|
},
|
|
expectError: true,
|
|
errorMsg: "invalid remote URL",
|
|
},
|
|
{
|
|
name: "empty remotes array",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test",
|
|
Remotes: []model.Transport{},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "multiple valid remotes - different subdomains",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://api.example.com/sse",
|
|
},
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://mcp.example.com/websocket",
|
|
},
|
|
},
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "one valid, one invalid remote",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/server",
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/sse",
|
|
},
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://google.com/websocket",
|
|
},
|
|
},
|
|
},
|
|
expectError: true,
|
|
errorMsg: "remote URL host google.com does not match publisher domain example.com",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validators.ValidateServerJSON(&tt.serverDetail)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.errorMsg)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidate_ServerNameFormat(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
serverDetail apiv0.ServerJSON
|
|
expectError bool
|
|
errorMsg string
|
|
}{
|
|
{
|
|
name: "valid namespace/name format",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example.api/server",
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid complex namespace",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.microsoft.azure.service/webapp-server",
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "empty server name",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "",
|
|
},
|
|
expectError: true,
|
|
errorMsg: "server name is required",
|
|
},
|
|
{
|
|
name: "missing slash separator",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example.server",
|
|
},
|
|
expectError: true,
|
|
errorMsg: "server name must be in format 'dns-namespace/name'",
|
|
},
|
|
{
|
|
name: "empty namespace part",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "/server-name",
|
|
},
|
|
expectError: true,
|
|
errorMsg: "non-empty namespace and name parts",
|
|
},
|
|
{
|
|
name: "empty name part",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/",
|
|
},
|
|
expectError: true,
|
|
errorMsg: "non-empty namespace and name parts",
|
|
},
|
|
{
|
|
name: "multiple slashes - uses first as separator",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/server/path",
|
|
},
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validators.ValidateServerJSON(&tt.serverDetail)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.errorMsg)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateArgument_ValidNamedArguments(t *testing.T) {
|
|
validCases := []model.Argument{
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "/path/to/dir"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--directory",
|
|
},
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Default: "8080"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--port",
|
|
},
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "true"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "-v",
|
|
},
|
|
{
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "-p",
|
|
},
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "/etc/config.json"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "config",
|
|
},
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Default: "false"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "verbose",
|
|
},
|
|
// No dash prefix requirement as per modification #1
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "json"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "output-format",
|
|
},
|
|
}
|
|
|
|
for _, arg := range validCases {
|
|
t.Run("Valid_"+arg.Name, func(t *testing.T) {
|
|
server := createValidServerWithArgument(arg)
|
|
err := validators.ValidateServerJSON(&server)
|
|
assert.NoError(t, err, "Expected valid argument %+v", arg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateArgument_ValidPositionalArguments(t *testing.T) {
|
|
positionalCases := []model.Argument{
|
|
{Type: model.ArgumentTypePositional, Name: "anything with spaces"},
|
|
{Type: model.ArgumentTypePositional, Name: "anything<with>brackets"},
|
|
{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "--port 8080"}},
|
|
Type: model.ArgumentTypePositional,
|
|
}, // Can contain flags in value for positional
|
|
}
|
|
|
|
for i, arg := range positionalCases {
|
|
t.Run(fmt.Sprintf("ValidPositional_%d", i), func(t *testing.T) {
|
|
server := createValidServerWithArgument(arg)
|
|
err := validators.ValidateServerJSON(&server)
|
|
assert.NoError(t, err, "Expected valid positional argument %+v", arg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateArgument_InvalidNamedArgumentNames(t *testing.T) {
|
|
invalidNameCases := []struct {
|
|
name string
|
|
arg model.Argument
|
|
}{
|
|
{"contains_description", model.Argument{Type: model.ArgumentTypeNamed, Name: "--directory <absolute_path_to_adfin_mcp_folder>"}},
|
|
{"contains_value", model.Argument{Type: model.ArgumentTypeNamed, Name: "--port 8080"}},
|
|
{"contains_dollar", model.Argument{Type: model.ArgumentTypeNamed, Name: "--config $CONFIG_FILE"}},
|
|
{"contains_brackets", model.Argument{Type: model.ArgumentTypeNamed, Name: "--file <path>"}},
|
|
{"empty_name", model.Argument{Type: model.ArgumentTypeNamed, Name: ""}},
|
|
{"has_spaces", model.Argument{Type: model.ArgumentTypeNamed, Name: "name with spaces"}},
|
|
}
|
|
|
|
for _, tc := range invalidNameCases {
|
|
t.Run("Invalid_"+tc.name, func(t *testing.T) {
|
|
server := createValidServerWithArgument(tc.arg)
|
|
err := validators.ValidateServerJSON(&server)
|
|
assert.Error(t, err, "Expected error for invalid named argument name: %+v", tc.arg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateArgument_InvalidValueFields(t *testing.T) {
|
|
invalidValueCases := []struct {
|
|
name string
|
|
arg model.Argument
|
|
}{
|
|
{
|
|
"value_starts_with_name",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "--port 8080"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--port",
|
|
},
|
|
},
|
|
{
|
|
"default_starts_with_name",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Default: "--config /etc/app.conf"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--config",
|
|
},
|
|
},
|
|
{
|
|
"value_starts_with_name_complex",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "--with-editable $REPOSITORY_DIRECTORY"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--with-editable",
|
|
},
|
|
},
|
|
{
|
|
"default_starts_with_name_complex",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Default: "--with-editable $REPOSITORY_DIRECTORY"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--with-editable",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range invalidValueCases {
|
|
t.Run("Invalid_"+tc.name, func(t *testing.T) {
|
|
server := createValidServerWithArgument(tc.arg)
|
|
err := validators.ValidateServerJSON(&server)
|
|
assert.Error(t, err, "Expected error for argument with value starting with name: %+v", tc.arg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateArgument_ValidValueFields(t *testing.T) {
|
|
validValueCases := []struct {
|
|
name string
|
|
arg model.Argument
|
|
}{
|
|
{
|
|
"value_without_name",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "8080"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--port",
|
|
},
|
|
},
|
|
{
|
|
"default_without_name",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Default: "/etc/app.conf"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--config",
|
|
},
|
|
},
|
|
{
|
|
"value_with_var",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "$REPOSITORY_DIRECTORY"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--with-editable",
|
|
},
|
|
},
|
|
{
|
|
"absolute_path",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "/absolute/path/to/directory"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--directory",
|
|
},
|
|
},
|
|
{
|
|
"contains_but_not_starts_with_name",
|
|
model.Argument{
|
|
InputWithVariables: model.InputWithVariables{Input: model.Input{Value: "use --port for configuration"}},
|
|
Type: model.ArgumentTypeNamed,
|
|
Name: "--port",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range validValueCases {
|
|
t.Run("Valid_"+tc.name, func(t *testing.T) {
|
|
server := createValidServerWithArgument(tc.arg)
|
|
err := validators.ValidateServerJSON(&server)
|
|
assert.NoError(t, err, "Expected valid argument %+v", tc.arg)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Helper function to create a valid server with a specific argument for testing
|
|
func TestValidate_TransportValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
serverDetail apiv0.ServerJSON
|
|
expectedError string
|
|
}{
|
|
// Package transport tests - stdio (no URL required)
|
|
{
|
|
name: "package transport stdio without URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "package transport stdio with URL (should still work)",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
URL: "ignored-for-stdio",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
// Package transport tests - streamable-http (URL required)
|
|
{
|
|
name: "package transport streamable-http with valid URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/mcp",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "package transport streamable-http with templated URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "streamable-http",
|
|
URL: "http://{host}:{port}/mcp",
|
|
},
|
|
EnvironmentVariables: []model.KeyValueInput{
|
|
{Name: "host"},
|
|
{Name: "port"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "package transport streamable-http without URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "streamable-http",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "url is required for streamable-http transport type",
|
|
},
|
|
{
|
|
name: "package transport streamable-http with templated URL missing variables",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "streamable-http",
|
|
URL: "http://{host}:{port}/mcp",
|
|
},
|
|
// Missing host and port variables
|
|
},
|
|
},
|
|
},
|
|
expectedError: "template variables in URL",
|
|
},
|
|
// Package transport tests - sse (URL required)
|
|
{
|
|
name: "package transport sse with valid URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "sse",
|
|
URL: "https://example.com/events",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "package transport sse without URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "sse",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "url is required for sse transport type",
|
|
},
|
|
// Package transport tests - unsupported type
|
|
{
|
|
name: "package transport unsupported type",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "websocket",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "unsupported transport type: websocket",
|
|
},
|
|
// Remote transport tests - streamable-http
|
|
{
|
|
name: "remote transport streamable-http with valid URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/mcp",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "remote transport streamable-http without URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "url is required for streamable-http transport type",
|
|
},
|
|
// Remote transport tests - sse
|
|
{
|
|
name: "remote transport sse with valid URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "sse",
|
|
URL: "https://example.com/events",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "remote transport sse without URL",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "sse",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "url is required for sse transport type",
|
|
},
|
|
// Remote transport tests - unsupported types
|
|
{
|
|
name: "remote transport stdio not supported",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "unsupported transport type for remotes: stdio",
|
|
},
|
|
{
|
|
name: "remote transport unsupported type",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "websocket",
|
|
URL: "wss://example.com/ws",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "unsupported transport type for remotes: websocket",
|
|
},
|
|
// Localhost URL tests - packages vs remotes
|
|
{
|
|
name: "package transport allows localhost URLs",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
Transport: model.Transport{
|
|
Type: "streamable-http",
|
|
URL: "http://localhost:3000/mcp",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: "",
|
|
},
|
|
{
|
|
name: "remote transport rejects localhost URLs",
|
|
serverDetail: apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "http://localhost:3000/mcp",
|
|
},
|
|
},
|
|
},
|
|
expectedError: "invalid remote URL",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validators.ValidateServerJSON(&tt.serverDetail)
|
|
|
|
if tt.expectedError == "" {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.expectedError)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidate_RegistryTypesAndUrls(t *testing.T) {
|
|
testCases := []struct {
|
|
tcName string
|
|
name string
|
|
registryType string
|
|
baseURL string
|
|
identifier string
|
|
version string
|
|
expectError bool
|
|
}{
|
|
// Valid registry types (should pass)
|
|
{"valid_npm", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeNPM, model.RegistryURLNPM, "airtable-mcp-server", "1.7.2", false},
|
|
{"valid_npm", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeNPM, "", "airtable-mcp-server", "1.7.2", false},
|
|
{"valid_pypi", "io.github.domdomegg/time-mcp-pypi", model.RegistryTypePyPI, model.RegistryURLPyPI, "time-mcp-pypi", "1.0.1", false},
|
|
{"valid_pypi", "io.github.domdomegg/time-mcp-pypi", model.RegistryTypePyPI, "", "time-mcp-pypi", "1.0.1", false},
|
|
{"valid_oci", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeOCI, model.RegistryURLDocker, "domdomegg/airtable-mcp-server", "1.7.2", false},
|
|
{"valid_nuget", "io.github.domdomegg/time-mcp-server", model.RegistryTypeNuGet, model.RegistryURLNuGet, "TimeMcpServer", "1.0.2", false},
|
|
{"valid_nuget", "io.github.domdomegg/time-mcp-server", model.RegistryTypeNuGet, "", "TimeMcpServer", "1.0.2", false},
|
|
{"valid_mcpb_github", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeMCPB, model.RegistryURLGitHub, "https://github.com/domdomegg/airtable-mcp-server/releases/download/v1.7.2/airtable-mcp-server.mcpb", "1.7.2", false},
|
|
{"valid_mcpb_github", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeMCPB, "", "https://github.com/domdomegg/airtable-mcp-server/releases/download/v1.7.2/airtable-mcp-server.mcpb", "1.7.2", false},
|
|
{"valid_mcpb_gitlab", "io.gitlab.fforster/gitlab-mcp", model.RegistryTypeMCPB, model.RegistryURLGitLab, "https://gitlab.com/fforster/gitlab-mcp/-/releases/v1.31.0/downloads/gitlab-mcp_1.31.0_Linux_x86_64.tar.gz", "1.31.0", false}, // this is not actually a valid mcpb, but it's the closest I can get for testing for now
|
|
{"valid_mcpb_gitlab", "io.gitlab.fforster/gitlab-mcp", model.RegistryTypeMCPB, "", "https://gitlab.com/fforster/gitlab-mcp/-/releases/v1.31.0/downloads/gitlab-mcp_1.31.0_Linux_x86_64.tar.gz", "1.31.0", false}, // this is not actually a valid mcpb, but it's the closest I can get for testing for now
|
|
|
|
// Invalid registry types (should fail)
|
|
{"invalid_maven", "io.github.domdomegg/airtable-mcp-server", "maven", model.RegistryURLNPM, "airtable-mcp-server", "1.7.2", true},
|
|
{"invalid_cargo", "io.github.domdomegg/time-mcp-pypi", "cargo", model.RegistryURLPyPI, "time-mcp-pypi", "1.0.1", true},
|
|
{"invalid_gem", "io.github.domdomegg/airtable-mcp-server", "gem", model.RegistryURLDocker, "domdomegg/airtable-mcp-server", "1.7.2", true},
|
|
{"invalid_unknown", "io.github.domdomegg/time-mcp-server", "unknown", model.RegistryURLNuGet, "TimeMcpServer", "1.0.2", true},
|
|
{"invalid_blank", "io.github.domdomegg/time-mcp-server", "", model.RegistryURLNuGet, "TimeMcpServer", "1.0.2", true},
|
|
{"invalid_docker", "io.github.domdomegg/airtable-mcp-server", "docker", model.RegistryURLDocker, "domdomegg/airtable-mcp-server", "1.7.2", true}, // should be oci
|
|
{"invalid_github", "io.github.domdomegg/airtable-mcp-server", "github", model.RegistryURLGitHub, "https://github.com/domdomegg/airtable-mcp-server/releases/download/v1.7.2/airtable-mcp-server.mcpb", "1.7.2", true}, // should be mcpb
|
|
|
|
{"invalid_mix_1", "io.github.domdomegg/time-mcp-server", model.RegistryTypeNuGet, model.RegistryURLNPM, "TimeMcpServer", "1.0.2", true},
|
|
{"invalid_mix_2", "io.github.domdomegg/airtable-mcp-server", model.RegistryTypeOCI, model.RegistryURLNPM, "domdomegg/airtable-mcp-server", "1.7.2", true},
|
|
{"invalid_mix_3", "io.github.domdomegg/airtable-mcp-server", model.RegistryURLNPM, model.RegistryURLNPM, "airtable-mcp-server", "1.7.2", true},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.tcName, func(t *testing.T) {
|
|
serverJSON := apiv0.ServerJSON{
|
|
Name: tc.name,
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
ID: "owner/repo",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: tc.identifier,
|
|
RegistryType: tc.registryType,
|
|
RegistryBaseURL: tc.baseURL,
|
|
Version: tc.version,
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := validators.ValidatePublishRequest(serverJSON, &config.Config{
|
|
EnableRegistryValidation: true,
|
|
})
|
|
if tc.expectError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func createValidServerWithArgument(arg model.Argument) apiv0.ServerJSON {
|
|
return apiv0.ServerJSON{
|
|
Name: "com.example/test-server",
|
|
Description: "A test server",
|
|
Repository: model.Repository{
|
|
URL: "https://github.com/owner/repo",
|
|
Source: "github",
|
|
ID: "owner/repo",
|
|
},
|
|
VersionDetail: model.VersionDetail{
|
|
Version: "1.0.0",
|
|
},
|
|
Packages: []model.Package{
|
|
{
|
|
Identifier: "test-package",
|
|
RegistryType: "npm",
|
|
RegistryBaseURL: "https://registry.npmjs.org",
|
|
Transport: model.Transport{
|
|
Type: "stdio",
|
|
},
|
|
RuntimeArguments: []model.Argument{arg},
|
|
},
|
|
},
|
|
Remotes: []model.Transport{
|
|
{
|
|
Type: "streamable-http",
|
|
URL: "https://example.com/remote",
|
|
},
|
|
},
|
|
}
|
|
}
|