mastra-ai/mastra · error

page value too large

Error message

page value too large

What it means

Also in `list` of the in-memory MCP clients domain: thrown when `page * perPage` would exceed `Number.MAX_SAFE_INTEGER / 2`, i.e., the computed offset is so large the slice would be meaningless or unsafe. It guards against absurd page numbers rather than memory blowup.

Source

Thrown at packages/core/src/storage/domains/mcp-clients/inmemory.ts:132

    // Also delete all versions for this client
    await this.deleteVersionsByParentId(id);
  }

  async list(args?: StorageListMCPClientsInput): Promise<StorageListMCPClientsOutput> {
    const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status } = args || {};
    const { field, direction } = this.parseOrderBy(orderBy);

    // Normalize perPage for query (false → MAX_SAFE_INTEGER, 0 → 0, undefined → 100)
    const perPage = normalizePerPage(perPageInput, 100);

    if (page < 0) {
      throw new Error('page must be >= 0');
    }

    // Prevent unreasonably large page values
    const maxOffset = Number.MAX_SAFE_INTEGER / 2;
    if (page * perPage > maxOffset) {
      throw new Error('page value too large');
    }

    // Get all MCP clients and apply filters
    let configs = Array.from(this.db.mcpClients.values());

    // Filter by status
    if (status) {
      configs = configs.filter(config => config.status === status);
    }

    // Filter by authorId if provided
    if (authorId !== undefined) {
      configs = configs.filter(config => config.authorId === authorId);
    }

    // Filter by metadata if provided (AND logic)
    if (metadata && Object.keys(metadata).length > 0) {
      configs = configs.filter(config => {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Cap the page value to a sane maximum before calling.
  2. Sanitize numeric inputs from external sources with bounds checking.
  3. Debug why the page counter grew that large (infinite pagination loop, counter overflow).

Example fix

// before
await mcpClients.list({ page: Number(req.query.page) });
// after
const page = Math.min(Math.max(0, Number(req.query.page) || 0), 1_000_000);
await mcpClients.list({ page });
Defensive patterns

Strategy: validation

Validate before calling

const MAX_PAGE = 1_000_000;
const safePage = Number.isFinite(page) ? Math.min(Math.max(0, Math.floor(page)), MAX_PAGE) : 0;
await mcpClients.list({ page: safePage, perPage });

Type guard

const isSafePage = (page: unknown): page is number =>
  typeof page === 'number' && Number.isFinite(page) && page >= 0 && page * 100 <= Number.MAX_SAFE_INTEGER / 2;

Try / catch

try {
  return await mcpClients.list({ page, perPage });
} catch (err) {
  if (err instanceof Error && err.message === 'page value too large') {
    return mcpClients.list({ page: 0, perPage });
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling `list({ page: 1e300 })` or any huge page value; unvalidated user input flowing straight into the page parameter; multiplication with normalized perPage (default 100, or MAX_SAFE_INTEGER when perPage=false).

Common situations: Passing raw unsanitized query params; arithmetic bugs producing enormous page counters; passing `false` for perPage (fetch-all) with a large page.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/dc777d012ee900d4. Report an issue: GitHub.