Skip to content

Fix list_dag_runs (MCP tool + af CLI) returning oldest runs first#192

Merged
kaxil merged 4 commits intoastronomer:mainfrom
JoaVirtudes19:joavirtudes19/list-dag-runs-default-order
Apr 27, 2026
Merged

Fix list_dag_runs (MCP tool + af CLI) returning oldest runs first#192
kaxil merged 4 commits intoastronomer:mainfrom
JoaVirtudes19:joavirtudes19/list-dag-runs-default-order

Conversation

@JoaVirtudes19
Copy link
Copy Markdown
Contributor

@JoaVirtudes19 JoaVirtudes19 commented Apr 27, 2026

The list_dag_runs MCP tool only accepted dag_id and called Airflow's GET /dags/{dag_id}/dagRuns with no order_by. Airflow's default sort on that endpoint is id ASC, so for any DAG with more than 100 runs the tool returned the oldest 100 runs and the recent ones were unreachable through the MCP — directly contradicting the docstring's promise of "sorted by most recent".

The af runs list CLI hit the same root cause: --order-by defaulted to None, so the API again returned oldest-first. Issue #168 reported this from the CLI side.

Changes

MCP tool (list_dag_runs)

  • Exposes limit, offset, and order_by on the tool surface
  • Defaults order_by="-start_date" so callers get newest-first

CLI (af runs list)

  • Changes --order-by default from None to -start_date, matching the MCP tool

Why -start_date

start_date is the only sort field available on both Airflow 2.x and 3.x DAG-run list endpoints. run_after is Airflow 3 only; logical_date doesn't exist in v2 (it's execution_date there). Picking -start_date keeps a single default that works across both adapters.

Callers who want a different order (e.g. id ascending to match the old Airflow default, or filter by state) can still pass --order-by explicitly on the CLI or order_by= on the MCP tool.

Closes #168

JoaVirtudes19 and others added 3 commits April 27, 2026 10:34
… pagination

The list_dag_runs MCP tool only accepted dag_id and called Airflow's
GET /dags/{dag_id}/dagRuns with no order_by. Airflow defaults to id ASC,
so for any DAG with > 100 runs the tool returned the oldest 100 runs
ever created and recent runs were unreachable through the MCP. The
docstring already promised "sorted by most recent", so this is a
contract bug.

- Expose limit, offset, order_by on the list_dag_runs MCP tool.
- Default order_by to "-start_date" so callers get newest first by
  default (start_date is a valid sort field on Airflow 2.x and 3.x,
  no adapter changes needed).
- _list_dag_runs_impl drops order_by from the kwargs when None so
  callers can still opt back into the Airflow API default if needed.
- Add unit tests in test_consolidated_tools.py covering the default
  sort, limit/offset passthrough, custom order_by override, the
  None-fallback in the internal impl, and error paths.
BaseAdapter._call already filters None values from query params, so the
extra dict + conditional in _list_dag_runs_impl was redundant. Pass
order_by straight through to the adapter; the HTTP behaviour is
identical.

Adjust the corresponding test to assert order_by=None reaches the
adapter (rather than asserting it was omitted from the call).
Mirrors the MCP-tool fix in this PR: change the `--order-by` default
from `None` (which falls through to the Airflow API default of `id`
ascending - oldest first) to `-start_date`. Users investigating recent
DAG activity see new runs at the top instead of having to scroll past
hundreds of historical runs.

Closes astronomer#168
@kaxil kaxil changed the title Fix list_dag_runs returning oldest runs and ignoring pagination Fix list_dag_runs (MCP tool + af CLI) returning oldest runs first Apr 27, 2026
@kaxil
Copy link
Copy Markdown
Contributor

kaxil commented Apr 27, 2026

Thanks for the PR @JoaVirtudes19 -- I pushed a couple more changes so I can release it today

@kaxil kaxil merged commit 3f3560c into astronomer:main Apr 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

af runs list: show most recent runs first by default

2 participants