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

  1. Inspect the wrapped pgx error text — it names the offending DSN keyword or syntax problem.
  2. Check the user/dbname/password values in your config for stray quotes, spaces, or unescaped special characters.
  3. If the password contains special characters, use IAM auth (leave user/pass empty) or URL-encode the password component.
  4. 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

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

Related errors


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