googleapis/mcp-toolbox · error

unable to create db connection: %w

Error message

unable to create db connection: %w

What it means

This error is returned by the MSSQL source's `Config.Initialize` when `initMssqlConnection` fails, wrapping any failure from building the connection URL or `sql.Open`. It means a *sql.DB handle could not be created for the given host/port/user/password/database/encrypt settings.

Source

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

	Type     string `yaml:"type" validate:"required"`
	Host     string `yaml:"host" validate:"required"`
	Port     string `yaml:"port" validate:"required"`
	User     string `yaml:"user" validate:"required"`
	Password string `yaml:"password" validate:"required"`
	Database string `yaml:"database" validate:"required"`
	Encrypt  string `yaml:"encrypt"`
}

func (r Config) SourceConfigType() string {
	// Returns Cloud SQL MSSQL source type
	return SourceType
}

func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {
	// Initializes a MSSQL source
	db, err := initMssqlConnection(ctx, tracer, r.Name, r.Host, r.Port, r.User, r.Password, r.Database, r.Encrypt)
	if err != nil {
		return nil, fmt.Errorf("unable to create db connection: %w", err)
	}

	// Verify db connection
	err = db.PingContext(ctx)
	if err != nil {
		db.Close()
		return nil, fmt.Errorf("unable to connect successfully: %w", err)
	}

	s := &Source{
		Config: r,
		Db:     db,
	}
	return s, nil
}

var _ sources.Source = &Source{}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Check host, port, user, password and database values in the source config for invalid/empty values
  2. URL-encode special characters in credentials
  3. Verify the go-mssqldb driver is imported/registered in the build
  4. Unwrap the error to see whether it came from URL construction or sql.Open

Example fix

// before (password with special chars in config)
password: p@ss/word
// after (URL-encoded in the built URL)
url.UserPassword(user, url.QueryEscape(password)) // or use the driver's Query/Escape helpers
Defensive patterns

Strategy: validation

Validate before calling

func validateMssqlConfig(host string, port int, user, pass, db string) error {
    if host == "" || port == 0 || user == "" || db == "" {
        return errors.New("host, port, user and database are required")
    }
    return nil
}

Try / catch

src, err := cfg.Initialize(ctx, tracer)
if err != nil && strings.Contains(err.Error(), "unable to create db connection") {
    return fmt.Errorf("check mssql source config fields: %w", err)
}

Prevention

When it happens

Trigger: Calling Initialize on an mssql source when the connection URL construction fails or sql.Open errors — typically due to invalid characters in host/user/password that break URL building, or an unknown driver registration state.

Common situations: Password containing special characters (@, /, :) that must be URL-encoded; empty host or database; misconfigured Encrypt value; SQL Server driver not linked/registered.

Related errors


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