{"record":{"id":"163a6b28c9998a09","repo":"googleapis/mcp-toolbox","slug":"unable-to-create-db-connection-w-163a6b","errorCode":null,"errorMessage":"unable to create db connection: %w","messagePattern":"unable to create db connection: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/mssql/mssql.go","lineNumber":71,"sourceCode":"\tType     string `yaml:\"type\" validate:\"required\"`\n\tHost     string `yaml:\"host\" validate:\"required\"`\n\tPort     string `yaml:\"port\" validate:\"required\"`\n\tUser     string `yaml:\"user\" validate:\"required\"`\n\tPassword string `yaml:\"password\" validate:\"required\"`\n\tDatabase string `yaml:\"database\" validate:\"required\"`\n\tEncrypt  string `yaml:\"encrypt\"`\n}\n\nfunc (r Config) SourceConfigType() string {\n\t// Returns Cloud SQL MSSQL source type\n\treturn SourceType\n}\n\nfunc (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {\n\t// Initializes a MSSQL source\n\tdb, err := initMssqlConnection(ctx, tracer, r.Name, r.Host, r.Port, r.User, r.Password, r.Database, r.Encrypt)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to create db connection: %w\", err)\n\t}\n\n\t// Verify db connection\n\terr = db.PingContext(ctx)\n\tif err != nil {\n\t\tdb.Close()\n\t\treturn nil, fmt.Errorf(\"unable to connect successfully: %w\", err)\n\t}\n\n\ts := &Source{\n\t\tConfig: r,\n\t\tDb:     db,\n\t}\n\treturn s, nil\n}\n\nvar _ sources.Source = &Source{}\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/mssql/mssql.go#L53-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"Password containing special characters (@, /, :) that must be URL-encoded; empty host or database; misconfigured Encrypt value; SQL Server driver not linked/registered.","solutions":["Check host, port, user, password and database values in the source config for invalid/empty values","URL-encode special characters in credentials","Verify the go-mssqldb driver is imported/registered in the build","Unwrap the error to see whether it came from URL construction or sql.Open"],"exampleFix":"// before (password with special chars in config)\npassword: p@ss/word\n// after (URL-encoded in the built URL)\nurl.UserPassword(user, url.QueryEscape(password)) // or use the driver's Query/Escape helpers","handlingStrategy":"validation","validationCode":"func validateMssqlConfig(host string, port int, user, pass, db string) error {\n    if host == \"\" || port == 0 || user == \"\" || db == \"\" {\n        return errors.New(\"host, port, user and database are required\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"src, err := cfg.Initialize(ctx, tracer)\nif err != nil && strings.Contains(err.Error(), \"unable to create db connection\") {\n    return fmt.Errorf(\"check mssql source config fields: %w\", err)\n}","preventionTips":["Validate config fields before Initialize","URL-encode credentials with special characters","Pin the go-mssqldb driver version","Fail fast with a Ping after initialization"],"tags":["mssql","configuration","connection","client-init"],"backgroundTag":"invalid-connection-uri","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}