MCPcopy Index your code
hub / github.com/modelcontextprotocol/registry / parseServerName

Function parseServerName

internal/validators/validators.go:701–741  ·  view source on GitHub ↗
(serverJSON apiv0.ServerJSON)

Source from the content-addressed store, hash-verified

699}
700
701func parseServerName(serverJSON apiv0.ServerJSON) (string, error) {
702 name := serverJSON.Name
703 if name == "" {
704 return "", fmt.Errorf("server name is required and must be a string")
705 }
706
707 // Validate format: dns-namespace/name
708 if !strings.Contains(name, "/") {
709 return "", fmt.Errorf("server name must be in format 'dns-namespace/name' (e.g., 'com.example.api/server')")
710 }
711
712 // Check for multiple slashes - reject if found
713 slashCount := strings.Count(name, "/")
714 if slashCount > 1 {
715 return "", ErrMultipleSlashesInServerName
716 }
717
718 // Split and check for empty parts
719 parts := strings.SplitN(name, "/", 2)
720 if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
721 return "", fmt.Errorf("server name must be in format 'dns-namespace/name' with non-empty namespace and name parts")
722 }
723
724 // Validate name format using regex
725 if !serverNameRegex.MatchString(name) {
726 namespace := parts[0]
727 serverName := parts[1]
728
729 // Check which part is invalid for a better error message
730 if !namespaceRegex.MatchString(namespace) {
731 return "", fmt.Errorf("%w: namespace '%s' is invalid. Namespace must start and end with alphanumeric characters, and may contain dots and hyphens in the middle", ErrInvalidServerNameFormat, namespace)
732 }
733 if !namePartRegex.MatchString(serverName) {
734 return "", fmt.Errorf("%w: name '%s' is invalid. Name must start and end with alphanumeric characters, and may contain dots, underscores, and hyphens in the middle", ErrInvalidServerNameFormat, serverName)
735 }
736 // Fallback in case both somehow pass individually but not together
737 return "", fmt.Errorf("%w: invalid format for '%s'", ErrInvalidServerNameFormat, name)
738 }
739
740 return name, nil
741}

Callers 1

ValidateServerJSONFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…