Skip to content

Commit adfbfaf

Browse files
Avish34Avish Porwal
andauthored
npm package validation: require identifier and version (#424)
<!-- Provide a brief summary of your changes --> Adding validation for version and identifier for packages. ## Motivation and Context While debugging a bug, I found out that we need exact version to be there in package to fetch from npm, otherwise we won't be able to parse the response as it returns all versions. Even if we do, we still need to go and figure out which version have the mcpname associated with while iterating over all versions. So, we should make mandatory to have identifier and version present in the given package object if we want to validate package association with registries. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> UTs. ## Breaking Changes <!-- Will users need to update their code or configurations? --> ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] 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 --> Co-authored-by: Avish Porwal <[email protected]>
1 parent 6bb62d1 commit adfbfaf

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

internal/validators/registries/npm.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ func ValidateNPM(ctx context.Context, pkg model.Package, serverName string) erro
2222
pkg.RegistryBaseURL = model.RegistryURLNPM
2323
}
2424

25+
if pkg.Identifier == "" {
26+
return fmt.Errorf("package identifier is required for NPM packages")
27+
}
28+
29+
// we need version to look up the package metadata
30+
// not providing version will return all the versions
31+
// and we won't be able to validate the mcpName field
32+
// against the server name
33+
if pkg.Version == "" {
34+
return fmt.Errorf("package version is required for NPM packages")
35+
}
36+
2537
// Validate that the registry base URL matches NPM exactly
2638
if pkg.RegistryBaseURL != model.RegistryURLNPM {
2739
return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s",

internal/validators/registries/npm_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ func TestValidateNPM_RealPackages(t *testing.T) {
2020
expectError bool
2121
errorMessage string
2222
}{
23+
{
24+
name: "empty package identifier should fail",
25+
packageName: "",
26+
version: "1.0.0",
27+
serverName: "com.example/test",
28+
expectError: true,
29+
errorMessage: "package identifier is required for NPM packages",
30+
},
31+
{
32+
name: "empty package version should fail",
33+
packageName: "test-package",
34+
version: "",
35+
serverName: "com.example/test",
36+
expectError: true,
37+
errorMessage: "package version is required for NPM packages",
38+
},
39+
{
40+
name: "both empty identifier and version should fail with identifier error first",
41+
packageName: "",
42+
version: "",
43+
serverName: "com.example/test",
44+
expectError: true,
45+
errorMessage: "package identifier is required for NPM packages",
46+
},
2347
{
2448
name: "non-existent package should fail",
2549
packageName: generateRandomPackageName(),

0 commit comments

Comments
 (0)