googleapis/mcp-toolbox · error

failed to create logical view: %w

Error message

failed to create logical view: %w

What it means

CreateLogicalView wraps errors from InstanceAdmin.CreateLogicalView. A logical view is a named SQL query over a Bigtable instance; creation fails if the ID already exists, the query is invalid/unsupported, permissions are missing, or the instance ID is wrong. The underlying SDK error is wrapped with %w.

Source

Thrown at internal/sources/bigtable/admin_wrappers.go:212

	return views, nil
}

func (s *Source) ListMaterializedViews(ctx context.Context, instanceId string) (any, error) {
	views, err := s.InstanceAdmin.MaterializedViews(ctx, instanceId)
	if err != nil {
		return nil, fmt.Errorf("failed to list materialized views: %w", err)
	}
	return views, nil
}

func (s *Source) CreateLogicalView(ctx context.Context, instanceId, logicalViewId, query string) (any, error) {
	conf := &bigtable.LogicalViewInfo{
		LogicalViewID: logicalViewId,
		Query:         query,
	}
	err := s.InstanceAdmin.CreateLogicalView(ctx, instanceId, conf)
	if err != nil {
		return nil, fmt.Errorf("failed to create logical view: %w", err)
	}
	return map[string]string{"status": "logical view created successfully"}, nil
}

func (s *Source) UpdateLogicalView(ctx context.Context, instanceId, logicalViewId, query string) (any, error) {
	conf := bigtable.LogicalViewInfo{ // MUST be value per bigtable SDK
		LogicalViewID: logicalViewId,
		Query:         query,
	}
	err := s.InstanceAdmin.UpdateLogicalView(ctx, instanceId, conf)
	if err != nil {
		return nil, fmt.Errorf("failed to update logical view: %w", err)
	}
	return map[string]string{"status": "logical view updated successfully"}, nil
}

func (s *Source) DeleteLogicalView(ctx context.Context, instanceId, logicalViewId string) (any, error) {
	err := s.InstanceAdmin.DeleteLogicalView(ctx, instanceId, logicalViewId)

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Check errors.Is(err, 4xx googleapi.Status) for AlreadyExists and choose a new logicalViewId or delete/update the existing view
  2. Validate the SQL query against the instance's actual tables and column families before creating
  3. Grant roles/bigtable.admin (or bigtable.views.create) to the caller's service account
  4. Confirm instanceId and project are correct with gcloud bigtable instances list

Example fix

// before: blind retry fails with 'already exists'
err := s.InstanceAdmin.CreateLogicalView(ctx, instanceId, conf)
// after: pre-check then create
existing, _ := s.InstanceAdmin.ListLogicalViews(ctx, instanceId)
if containsID(existing, logicalViewId) {
    return s.InstanceAdmin.UpdateLogicalView(ctx, instanceId, bigtable.LogicalViewInfo{LogicalViewID: logicalViewId, Query: query})
}
err := s.InstanceAdmin.CreateLogicalView(ctx, instanceId, conf)
Defensive patterns

Strategy: validation

Validate before calling

func validateLogicalViewInput(instanceId, viewId, query string) error {
    if instanceId == "" || viewId == "" || query == "" {
        return errors.New("instanceId, logicalViewId and query are required")
    }
    if !regexp.MustCompile(`^[a-zA-Z0-9._-]{1,255}$`).MatchString(viewId) {
        return fmt.Errorf("invalid logical view id %q", viewId)
    }
    return nil
}

Type guard

func isAlreadyExists(err error) bool {
    st, ok := status.FromError(errors.Unwrap(err))
    return ok && st.Code() == codes.AlreadyExists
}

Try / catch

res, err := src.CreateLogicalView(ctx, instanceId, viewId, query)
if err != nil {
    if isAlreadyExists(err) {
        return src.UpdateLogicalView(ctx, instanceId, viewId, query) // create-or-update
    }
    return err
}

Prevention

When it happens

Trigger: Calling CreateLogicalView (bigtable_create_logical_view tool) when: logicalViewId already exists (AlreadyExists), the SQL query fails validation, caller lacks bigtable.views.create permission, or the instanceId does not exist.

Common situations: Re-running an idempotent-create script against an existing view; SQL query referencing non-existent tables/column families; service account without Bigtable Admin role; typo'd instance name.

Related errors


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