googleapis/mcp-toolbox · error
unable to parse connection uri: %w
Error message
unable to parse connection uri: %w
What it means
After building the DSN, initAlloyDBPgConnectionPool calls pgxpool.ParseConfig to validate the connection string. This error indicates the constructed DSN is not a parseable pgx connection string/URI, so no pool can be created. Because the DSN is built by the source from project/region/cluster/instance plus user fields, this usually means one of those inputs is malformed (e.g. contains characters illegal in a DSN).
Source
Thrown at internal/sources/alloydbpg/alloydb_pg.go:223
dsn += " options='-c alloydb_session_read_only=locked'"
}
return dsn, useIAM, nil
}
func initAlloyDBPgConnectionPool(ctx context.Context, tracer trace.Tracer, name, project, region, cluster, instance, ipType, user, pass, dbname string, readOnly bool) (*pgxpool.Pool, error) {
//nolint:all // Reassigned ctx
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceType, name)
defer span.End()
dsn, useIAM, err := getConnectionConfig(ctx, user, pass, dbname, readOnly)
if err != nil {
return nil, fmt.Errorf("unable to get AlloyDB connection config: %w", err)
}
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("unable to parse connection uri: %w", err)
}
// Create a new dialer with options
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
return nil, err
}
opts, err := getOpts(ipType, userAgent, useIAM)
if err != nil {
return nil, err
}
d, err := alloydbconn.NewDialer(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("unable to parse connection uri: %w", err)
}
// Tell the driver to use the AlloyDB Go Connector to create connections
i := fmt.Sprintf("projects/%s/locations/%s/clusters/%s/instances/%s", project, region, cluster, instance)
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, instance string) (net.Conn, error) {View on GitHub (pinned to 8cc6e09de2)
Solutions
- Inspect the wrapped pgx error text — it names the offending DSN keyword or syntax problem.
- Check the user/dbname/password values in your config for stray quotes, spaces, or unescaped special characters.
- If the password contains special characters, use IAM auth (leave user/pass empty) or URL-encode the password component.
- Verify the project/region/cluster/instance fields are plain identifiers without extra characters.
Example fix
// before: dbname with quotes/whitespace in config dbname: "" "my db""" // invalid DSN keyword value // after dbname: mydb
Defensive patterns
Strategy: validation
Validate before calling
if strings.ContainsAny(dbname, " '\"") || strings.ContainsAny(user, " '\"") {
return errors.New("dbname/user contain DSN-illegal characters")
}
if _, err := pgxpool.ParseConfig(testDSN); err != nil {
return fmt.Errorf("invalid DSN: %w", err)
} Try / catch
if _, err := pgxpool.ParseConfig(dsn); err != nil {
return fmt.Errorf("check dbname/user values for illegal characters: %w", err)
} Prevention
- Avoid quotes, spaces, and control characters in dbname/user config values.
- Trim whitespace on all config string fields before writing them to YAML.
- URL-encode special characters in passwords if embedding them in a DSN.
- Test the DSN locally with psql or pgx ParseConfig before deploying.
When it happens
Trigger: Calling Initialize with a dbname, user, or other DSN component containing characters pgx cannot parse (spaces, unescaped quotes, invalid keyword=value syntax) or an instance name with unexpected separators.
Common situations: Database or username values pasted with surrounding whitespace or quotes into YAML; special characters in passwords interpolated into the DSN without escaping; non-ASCII or control characters in config values.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- unable to parse connection uri: %w
- failed to initialize AlloyDB source in read-only mode: 'allo
- unable to execute query: %w
- unable to parse row: %w
- invalid ipType %s
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/6acc358eb6199b66.
Report an issue: GitHub.