Skip to content

Commit 59a898c

Browse files
tadasantclaude
andcommitted
fix: Handle port-position template variables in URL validation
When validating URLs with template variables in the port position (e.g., http://localhost:{--port}/mcp), the validator was incorrectly replacing the template variable with the string "placeholder", which caused URL parsing to fail since ports must be numeric. This commit fixes the issue by: - Detecting when a template variable appears in a port position (after a colon and before a slash or end of string) - Replacing port-position template variables with a numeric placeholder "8080" instead of "placeholder" - Adding a new error constant ErrInvalidPackageTransportURL to distinguish package transport errors from remote transport errors - Updating validatePackageTransport to use the correct error constant The fix allows named arguments like "--port" to be properly referenced in package transport URLs, enabling examples like: ```json { "transport": { "type": "streamable-http", "url": "http://localhost:{--port}/mcp" }, "packageArguments": [ { "type": "named", "name": "--port", "default": "3000" } ] } ``` Fixes validation of Example 15 in generic-server-json.md (line 732) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 15cee49 commit 59a898c

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

internal/validators/constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ var (
1313
ErrReservedVersionString = errors.New("version string 'latest' is reserved and cannot be used")
1414
ErrVersionLooksLikeRange = errors.New("version must be a specific version, not a range")
1515

16-
// Remote validation errors
17-
ErrInvalidRemoteURL = errors.New("invalid remote URL")
16+
// Transport validation errors
17+
ErrInvalidPackageTransportURL = errors.New("invalid package transport URL")
18+
ErrInvalidRemoteURL = errors.New("invalid remote URL")
1819

1920
// Registry validation errors
2021
ErrUnsupportedRegistryBaseURL = errors.New("unsupported registry base URL")

internal/validators/utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ func replaceTemplateVariables(rawURL string) string {
6161
result = strings.ReplaceAll(result, placeholder, replacement)
6262
}
6363

64-
// Handle any remaining {variable} patterns with generic placeholder
64+
// Handle any remaining {variable} patterns with context-appropriate placeholders
65+
// If the variable is in a port position (after a colon in the host), use a numeric placeholder
66+
// Pattern: :/{variable} or :{variable}/ or :{variable} at end
67+
portRe := regexp.MustCompile(`:(\{[^}]+\})(/|$)`)
68+
result = portRe.ReplaceAllString(result, ":8080$2")
69+
70+
// Replace any other remaining {variable} patterns with generic placeholder
6571
re := regexp.MustCompile(`\{[^}]+\}`)
6672
result = re.ReplaceAllString(result, "placeholder")
6773

internal/validators/validators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ func validatePackageTransport(transport *model.Transport, availableVariables []s
409409
templateVars := extractTemplateVariables(transport.URL)
410410
if len(templateVars) > 0 {
411411
return fmt.Errorf("%w: template variables in URL %s reference undefined variables. Available variables: %v",
412-
ErrInvalidRemoteURL, transport.URL, availableVariables)
412+
ErrInvalidPackageTransportURL, transport.URL, availableVariables)
413413
}
414-
return fmt.Errorf("%w: %s", ErrInvalidRemoteURL, transport.URL)
414+
return fmt.Errorf("%w: %s", ErrInvalidPackageTransportURL, transport.URL)
415415
}
416416
return nil
417417
default:

0 commit comments

Comments
 (0)