#1220·rybbit

get_goals MCP fails: meta.total is a string (Postgres count(*)) but schema expects number

Author: raulpopadinetiCreated Sep 23, 2026Updated Sep 23, 2026

Summary

Calling the MCP tool get_goals fails Cursor/client output validation even though the underlying HTTP API succeeds. The tool response is discarded because meta.total is a string, while the MCP output schema (and the public API docs) expect a number.

Error (Cursor MCP)

MCP error -32602: Output validation error: Invalid structured content for tool get_goals:
[
  {
    "code": "invalid_type",
    "expected": "number",
    "received": "string",
    "path": ["meta", "total"],
    "message": "Expected number, received string"
  }
]

Reproduced against a live site with goals configured (e.g. site with existing event goals). create_goal, funnels, and event-count tools still work; only listing goals via MCP is broken.

Likely cause

In server/src/api/analytics/goals/getGoals.ts:

typescript
const totalGoalsResult = await db
  .select({ count: sql`count(*)` })
  .from(goals)
  .where(eq(goals.siteId, Number(siteId)));

const totalGoals = totalGoalsResult[0]?.count || 0;

Postgres count(*) via Drizzle typically returns a bigint serialized as a string. That value is passed through into:

typescript
meta: {
  total: totalGoals,  // string e.g. "3", not 3
  page: pageNumber,
  pageSize: pageSizeNumber,
  totalPages,
}

The UI may still work (JS coerces in arithmetic), but MCP clients with strict output schemas reject the entire payload.

Docs for GET /api/sites/:site/goals document meta.total as a number: https://rybbit.com/docs/api/goals/list

Suggested fix

Cast before sending (minimal):

typescript
const totalGoals = Number(totalGoalsResult[0]?.count ?? 0);

or in SQL:

typescript
.select({ count: sql<number>`count(*)::int` })

Worth grepping other analytics list endpoints for the same count(*) → meta.total pattern so MCP validation does not fail elsewhere.

Impact

Agents / automations that rely on get_goals for weekly conversion reads cannot use the goals API and have to fall back to raw event counts. Fixing the type makes MCP and the documented JSON schema match the runtime response.

Happy to send a one-line PR if useful.