The getUsers tool retrieves user account records from the Firebase/Firestore database with support for pagination and sorting.
tools/getUsers.js
Retrieves users for the authenticated account with cursor-based pagination support. Users represent account members with specific roles and permissions. Users are retrieved from the Firestore path: /Accounts/{accountId}/Users.
{
limit?: number; // Number of records to retrieve (1-100, default: 50)
cursor?: string; // Document ID for cursor-based pagination
orderBy?: string; // Field to order by (default: "email")
orderDirection?: "asc" | "desc"; // Sort direction (default: "asc")
cursorDirection?: "next" | "previous"; // Direction for pagination (default: "next")
includeTotal?: boolean; // Include total count of all users (default: false)
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
limit |
number | No | 50 | Maximum number of users to return (1-100) |
cursor |
string | No | - | Document ID to start pagination from |
orderBy |
string | No | “email” | Field name to sort by |
orderDirection |
enum | No | “asc” | Sort direction: “asc” or “desc” |
cursorDirection |
enum | No | “next” | Pagination direction: “next” or “previous” |
includeTotal |
boolean | No | false | Whether to include total count (expensive) |
{
users: User[];
pagination: PaginationMetadata;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
email: string; // User email address
name: string | null; // User display name
permissions: string[]; // Array of permission strings
readonly: boolean; // Whether user has read-only access (default: false)
role: string; // User role identifier
uid: string; // Firebase Authentication UID
}
{
limit: number; // Applied limit
hasMore: boolean; // Whether more results exist
nextCursor: string | null; // Cursor for next page
previousCursor: string | null; // Cursor for previous page
total?: number | null; // Total count (if includeTotal was true)
}
// Get first 10 users
const result = await getUsers(context, {
limit: 10
});
console.log(result.users); // Array of 10 users
console.log(result.pagination); // Pagination metadata
// First page
const page1 = await getUsers(context, {
limit: 20,
orderBy: "email",
orderDirection: "asc"
});
// Next page
const page2 = await getUsers(context, {
limit: 20,
cursor: page1.pagination.nextCursor,
cursorDirection: "next"
});
// Get users sorted by name
const result = await getUsers(context, {
limit: 10,
orderBy: "name",
orderDirection: "asc"
});
// Get all users and filter by role
const result = await getUsers(context, { limit: 100 });
const admins = result.users.filter(user => user.role === "admin");
const readOnlyUsers = result.users.filter(user => user.readonly);
const result = await getUsers(context, {
limit: 10,
includeTotal: true
});
console.log(`Account has ${result.pagination.total} total users`);
The tool transforms Firestore documents by:
id and full pathconst transformUser = (doc) => ({
id: doc.id,
path: doc.ref.path,
...doc.data(),
});
By default, users are ordered by email in ascending order for consistent alphabetical listing.
The permissions field contains an array of permission strings that define what actions the user can perform. Common permissions include:
| Error | Cause | Solution |
|---|---|---|
| “Error fetching users: [message]” | Firestore query failed | Check Firebase connection and permissions |
| “Failed to fetch users.” | Query error | Verify authentication and account access |
| Validation error | Invalid input parameters | Ensure parameters match the input schema |
Errors are logged and a generic error message is thrown:
catch (error) {
console.error("Error fetching users:", error);
throw new Error("Failed to fetch users.");
}
includeTotal: true requires an additional query/Accounts/{accountId}/Users/{userId}
- email: string
- name: string | null
- permissions: string[]
- readonly: boolean
- role: string
- uid: string