googleapis/mcp-toolbox · error

sql.Open: %w

Error message

sql.Open: %w

What it means

This error is returned by `initMssqlConnection` when `sql.Open("sqlserver", url)` fails. With the go-mssqldb driver, sql.Open mostly validates the DSN/URL — so this indicates the connection URL built from the source config is malformed (bad scheme, invalid query parameters, unparseable auth).

Source

Thrown at internal/sources/mssql/mssql.go:189

	// Create dsn
	query := url.Values{}
	query.Add("app name", userAgent)
	query.Add("database", dbname)
	if encrypt != "" {
		query.Add("encrypt", encrypt)
	}

	url := &url.URL{
		Scheme:   "sqlserver",
		User:     url.UserPassword(user, pass),
		Host:     fmt.Sprintf("%s:%s", host, port),
		RawQuery: query.Encode(),
	}

	// Open database connection
	db, err := sql.Open("sqlserver", url.String())
	if err != nil {
		return nil, fmt.Errorf("sql.Open: %w", err)
	}
	return db, nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Unwrap the error to see the exact DSN parse complaint
  2. Verify the built URL starts with sqlserver:// and has valid query params
  3. URL-encode user and password
  4. Check Encrypt/port values are valid per the go-mssqldb driver

Example fix

// before
u := url.URL{Scheme: "sqlserver", Host: host} // missing port formatting when host has IPv6
// after
u := url.URL{Scheme: "sqlserver", Host: net.JoinHostPort(host, strconv.Itoa(port))}
Defensive patterns

Strategy: validation

Validate before calling

func validateMssqlDsnParams(host string, port int) error {
    if host == "" {
        return errors.New("host is required")
    }
    if port < 1 || port > 65535 {
        return errors.New("port out of range")
    }
    return nil
}

Try / catch

src, err := cfg.Initialize(ctx, tracer)
if err != nil && strings.Contains(err.Error(), "sql.Open") {
    return fmt.Errorf("assembled sqlserver:// DSN is invalid; check config: %w", err)
}

Prevention

When it happens

Trigger: Calling Initialize when the assembled sqlserver:// URL is invalid: wrong scheme, invalid query parameter names/values (e.g. bad connection timeout syntax), or characters in user/password that break URL parsing before escaping.

Common situations: Hand-edited connection parameters injecting bad values; Encrypt value not matching accepted driver values; special characters in password corrupting the URL; port set to a non-numeric value.

Related errors


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