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, the pool config is parsed with pgxpool.ParseConfig. If the DSN string is malformed (bad key=value pairs, unescaped characters in user/dbname, or a broken options clause), parsing fails and the source cannot be created. Notably, a password containing spaces or special characters without escaping will break this parser.

Source

Thrown at internal/sources/cloudsqlpg/cloud_sql_pg.go:200

	}

	return dsn, useIAM, nil
}

func initCloudSQLPgConnectionPool(ctx context.Context, tracer trace.Tracer, name, project, region, 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()

	// Configure the driver to connect to the database
	dsn, useIAM, err := getConnectionConfig(ctx, user, pass, dbname, readOnly)
	if err != nil {
		return nil, fmt.Errorf("unable to get Cloud SQL 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 := sources.GetCloudSQLOpts(ipType, userAgent, useIAM)
	if err != nil {
		return nil, err
	}
	d, err := cloudsqlconn.NewDialer(ctx, opts...)
	if err != nil {
		return nil, fmt.Errorf("unable to parse connection uri: %w", err)
	}

	// Tell the driver to use the Cloud SQL Go Connector to create connections
	i := fmt.Sprintf("%s:%s:%s", project, region, instance)

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Escape or avoid special characters (spaces, quotes, '=') in the user and password; prefer URL-encoded or simple credentials.
  2. Regenerate a password without characters that break DSN key=value parsing.
  3. If the error persists, enable debug logging of the DSN (with secrets redacted) to find the malformed token.

Example fix

// before: password: p@ss word'123  (breaks DSN parsing)
// after: password: pAssW0rd123  or URL-escape special characters
Defensive patterns

Strategy: validation

Validate before calling

if strings.ContainsAny(pass, " '=") {
    return errors.New("password contains characters unsafe for DSN; regenerate or escape it")
}

Prevention

When it happens

Trigger: Initialize where the generated DSN is invalid — typically a password or user containing spaces, quotes, or '=' characters interpolated verbatim into the key=value DSN, or a corrupted readOnly options clause.

Common situations: Passwords with special characters pasted from a secrets manager; usernames with spaces (IAM emails are usually fine); older config with a password containing single quotes.

Understand the failure class

Related errors


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