googleapis/mcp-toolbox · error

invalid region %q: must match %s

Error message

invalid region %q: must match %s

What it means

ValidateInstanceConnectionName checks the region segment of a connection name against ^[a-z]+-[a-z0-9-]+$ (lowercase letters, digits, hyphens). The region must look like a valid GCP region string (e.g. us-central1). This guards against malformed input being interpolated into generated connection code and shell commands.

Source

Thrown at internal/util/cloudsqlconnect/inputvalidate.go:51

	gcpRegionRe        = regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`)
	cloudSQLInstanceRe = regexp.MustCompile(`^[a-z][a-z0-9-]{0,97}[a-z0-9]$`)
	gceResourceRe      = regexp.MustCompile(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`)
	databaseNameRe     = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_-]{0,62}$`)
)

// ValidateInstanceConnectionName splits and validates project, region, and
// instance ID per the GCP naming rules. Use this in place of plain
// ParseConnectionName when the parts will flow into generated code or shell.
func ValidateInstanceConnectionName(connName string) (project, region, instance string, err error) {
	project, region, instance, err = ParseConnectionName(connName)
	if err != nil {
		return "", "", "", err
	}
	if !projectIDRe.MatchString(project) {
		return "", "", "", fmt.Errorf("invalid project ID %q: must match %s", project, projectIDRe)
	}
	if !gcpRegionRe.MatchString(region) {
		return "", "", "", fmt.Errorf("invalid region %q: must match %s", region, gcpRegionRe)
	}
	if !cloudSQLInstanceRe.MatchString(instance) {
		return "", "", "", fmt.Errorf("invalid Cloud SQL instance ID %q: must match %s", instance, cloudSQLInstanceRe)
	}
	return project, region, instance, nil
}

// ValidateGCEResourceName checks a VM name or zone name against the standard
// GCE resource-name rule (lowercase, digits, hyphen; must start with a letter
// and not end with a hyphen, max 63 chars).
func ValidateGCEResourceName(name, kind string) error {
	if !gceResourceRe.MatchString(name) {
		return fmt.Errorf("invalid %s %q: must match %s", kind, name, gceResourceRe)
	}
	return nil
}

// ValidateDatabaseName accepts the conservative subset of database identifier

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Use the exact GCP region shown by `gcloud sql instances describe <instance> --format='value(region)'`
  2. Normalize the region to lowercase with hyphens (e.g. 'us-central1', 'europe-west1')
  3. Verify the connection-name separator ':' wasn't misplaced, shifting a non-region value into the region slot

Example fix

// before
ValidateInstanceConnectionName("my-project:US-Central1:my-db")
// after
ValidateInstanceConnectionName("my-project:us-central1:my-db")
Defensive patterns

Strategy: validation

Validate before calling

var gcpRegionRe = regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`)
region := parts[1]
if !gcpRegionRe.MatchString(region) {
    return fmt.Errorf("region %q must be a lowercase GCP region like us-central1", region)
}

Type guard

func isValidGCPRegion(r string) bool {
    return regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`).MatchString(r)
}

Try / catch

project, region, instance, err := cloudsqlconnect.ValidateInstanceConnectionName(connName)
if err != nil {
    return fmt.Errorf("invalid connection name %q: %w; fetch region via 'gcloud sql instances describe'", connName, err)
}

Prevention

When it happens

Trigger: Passing a connection name whose middle segment contains uppercase letters, underscores, spaces, or is empty — e.g. 'proj:US-CENTRAL1:inst' or 'proj:my_region:inst'.

Common situations: Region copied from an Azure/AWS name (e.g. 'eastus', 'us-east-1' with digits in AWS style is fine, but 'East US' is not); using a zone instead of a region ('us-central1-a' actually matches the regex, but 'usCentral1' does not); env var with wrong value.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/9d5173b9c5cd0f60. Report an issue: GitHub.