googleapis/mcp-toolbox · error

error getting instance: %w

Error message

error getting instance: %w

What it means

Thrown in GetInstance when the sqladmin Instances.Get call (projects.instances.get) fails. The lookup of a Cloud SQL instance's details returned an API error.

Source

Thrown at internal/sources/cloudsqladmin/cloud_sql_admin.go:236

	}

	resp, err := service.Users.Insert(project, instance, &user).Do()
	if err != nil {
		return nil, fmt.Errorf("error creating user: %w", err)
	}

	return resp, nil
}

func (s *Source) GetInstance(ctx context.Context, projectId, instanceId, accessToken string) (any, error) {
	service, err := s.GetService(ctx, accessToken)
	if err != nil {
		return nil, err
	}

	resp, err := service.Instances.Get(projectId, instanceId).Do()
	if err != nil {
		return nil, fmt.Errorf("error getting instance: %w", err)
	}
	return resp, nil
}

func (s *Source) ListDatabase(ctx context.Context, project, instance, accessToken string) (any, error) {
	service, err := s.GetService(ctx, accessToken)
	if err != nil {
		return nil, err
	}

	resp, err := service.Databases.List(project, instance).Do()
	if err != nil {
		return nil, fmt.Errorf("error listing databases: %w", err)
	}

	if resp.Items == nil {
		return []any{}, nil
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Verify the instance ID with the list_instances tool or `gcloud sql instances list --project <project>`.
  2. Correct the project ID and re-run.
  3. Grant roles/cloudsql.viewer (or admin) to the calling principal.
  4. Enable sqladmin.googleapis.com in the project if not enabled.
  5. Inspect the wrapped googleapi.Error: 404 = not found, 403 = permission.

Example fix

// before
GetInstance(ctx, "my-proj", "my-instnce", token) // typo
// after
GetInstance(ctx, "my-proj", "my-instance", token)
Defensive patterns

Strategy: type-guard

Validate before calling

if projectId == "" || instanceId == "" {
    return fmt.Errorf("projectId and instanceId are required")
}
if err := ensureAPIEnabled(ctx, projectId, "sqladmin.googleapis.com"); err != nil {
    return err
}

Type guard

func isNotFoundErr(err error) bool {
    var gerr *googleapi.Error
    return errors.As(err, &gerr) && gerr.Code == http.StatusNotFound
}

Try / catch

out, err := src.GetInstance(ctx, projectId, instanceId, token)
if err != nil {
    if isNotFoundErr(err) {
        return fmt.Errorf("instance %q not found in project %q; check names with list_instances", instanceId, projectId)
    }
    return fmt.Errorf("get instance failed: %w", err)
}

Prevention

When it happens

Trigger: Calling the get_instance tool with an instance ID that does not exist in the project, a misspelled project ID, or when the caller lacks cloudsql.instances.get permission.

Common situations: Wrong project selected (instance lives in another project); typo in instance name; workload identity/service account missing Cloud SQL Viewer role; API disabled in the project.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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