Context
#2677 adds orderBy to CubeQuery in wren-core, so a cube query can be ranked natively. That PR is deliberately scoped to core internals; this issue tracks the agent-facing surfaces — the wren CLI and the MCP query_cube tool — which currently cannot reach the new capability.
Why this matters: ranked top-N over MCP is not actually ranked
query_cube (core/wren/src/wren/mcp_server.py:146) exposes cube / measures / dimensions / time_dimension / filters / limit / offset / sql_only and builds its query exclusively through _build_cube_query (core/wren/src/wren/cube_cli.py:93), which never emits an ordering key. There is therefore no way for an agent to request ordering.
It does expose limit, and the bundled usage skill steers agents toward cube query for exactly this shape of question — core/wren/src/wren/skills_content/usage/SKILL.md:311 names "top customers" as a motivating example. Today such a query returns an arbitrary N rows (or, when a time dimension is present, the N earliest by that dimension) rather than the top N by the measure.
This is pre-existing behaviour, not a regression — #2677 is the enabling half of the fix, and this issue is the other half.
What to change
Both surfaces share _build_cube_query, so this is naturally one change:
-
Parser — accept an ordering spec in the existing CLI spec style. filters use dim:op[:value] and time_dimension uses name:granularity[:start,end], so member:direction fits the established grammar:
--order-by "net_spend:desc,merchant_name:asc"
-
_build_cube_query — take the parsed list and emit orderBy on the query dict.
-
CLI — add the --order-by flag to wren cube query, and document it in the option table at docs/core/reference/cli.md:365. Note that ordering is already reachable today via --from <file|->, which passes the JSON through unfiltered; the flag is for parity with the other spec-style options.
-
MCP — add an order_by: list[str] | None = None parameter to query_cube and thread it through build_sql, keeping sql_only=True on the same path.
-
Usage skill — update the cube query workflow in core/wren/src/wren/skills_content/usage/SKILL.md so agents know ordering is available, and so the "top customers" example actually produces a top-N.
Validation semantics inherited from core
Errors surface from wren-core and should be readable as-is: a member that the query does not select is rejected, a member listed twice is rejected, and the direction is a closed enum accepting only lowercase asc / desc. Omitting the option preserves the current default ordering.
Context
#2677 adds
orderBytoCubeQueryinwren-core, so a cube query can be ranked natively. That PR is deliberately scoped to core internals; this issue tracks the agent-facing surfaces — thewrenCLI and the MCPquery_cubetool — which currently cannot reach the new capability.Why this matters: ranked top-N over MCP is not actually ranked
query_cube(core/wren/src/wren/mcp_server.py:146) exposescube / measures / dimensions / time_dimension / filters / limit / offset / sql_onlyand builds its query exclusively through_build_cube_query(core/wren/src/wren/cube_cli.py:93), which never emits an ordering key. There is therefore no way for an agent to request ordering.It does expose
limit, and the bundled usage skill steers agents toward cube query for exactly this shape of question —core/wren/src/wren/skills_content/usage/SKILL.md:311names "top customers" as a motivating example. Today such a query returns an arbitrary N rows (or, when a time dimension is present, the N earliest by that dimension) rather than the top N by the measure.This is pre-existing behaviour, not a regression — #2677 is the enabling half of the fix, and this issue is the other half.
What to change
Both surfaces share
_build_cube_query, so this is naturally one change:Parser — accept an ordering spec in the existing CLI spec style.
filtersusedim:op[:value]andtime_dimensionusesname:granularity[:start,end], somember:directionfits the established grammar:_build_cube_query— take the parsed list and emitorderByon the query dict.CLI — add the
--order-byflag towren cube query, and document it in the option table atdocs/core/reference/cli.md:365. Note that ordering is already reachable today via--from <file|->, which passes the JSON through unfiltered; the flag is for parity with the other spec-style options.MCP — add an
order_by: list[str] | None = Noneparameter toquery_cubeand thread it throughbuild_sql, keepingsql_only=Trueon the same path.Usage skill — update the cube query workflow in
core/wren/src/wren/skills_content/usage/SKILL.mdso agents know ordering is available, and so the "top customers" example actually produces a top-N.Validation semantics inherited from core
Errors surface from
wren-coreand should be readable as-is: a member that the query does not select is rejected, a member listed twice is rejected, and the direction is a closed enum accepting only lowercaseasc/desc. Omitting the option preserves the current default ordering.