Skip to content

feat(reporting): add a run_report_task tool with sampling_level - #263

Open
Goldloli wants to merge 1 commit into
googleanalytics:mainfrom
Goldloli:feat/run-report-task-sampling-level
Open

Goldloli wants to merge 1 commit into
googleanalytics:mainfrom
Goldloli:feat/run-report-task-sampling-level

Conversation

@Goldloli

Copy link
Copy Markdown

Description

Adds a run_report_task tool that reports through the Data API report tasks
API, which is the only report surface that carries a samplingLevel. The
current run_report tool builds a RunReportRequest, and that message has no
sampling_level field, so an MCP client has no way to ask for a larger sample
or for an unsampled report.

The new tool creates a report task, waits for it to become ACTIVE, and returns
its rows. As report tasks are built asynchronously, it polls while the task is
CREATING, surfaces the API error message when the task is FAILED, and raises
a TimeoutError that points at the 72-hour query window when the task is still
building after timeout_seconds.

Behaviour

  • sampling_level accepts LOW, MEDIUM and UNSAMPLED, case-insensitively.
  • Omitting sampling_level leaves report_definition.sampling_level unset, so
    the property default applies. This mirrors the issue's request to preserve the
    API default when the field is omitted.
  • timeout_seconds defaults to 120 and is bounded to 1-300 seconds.
  • The remaining report arguments (dimension_filter, metric_filter,
    order_bys, limit, offset, currency_code) behave as they do in
    run_report, so a report can be moved between the two tools unchanged.
  • The tool is read-only, so it is registered with the read-only tools and gets
    the same read_only_hint annotations as the other reporting tools.

Testing

nox -s tests-3.11
  • 48 tests pass, including 16 new ones in tests/report_tasks_test.py.
  • nox -s lint (black, 80 columns) passes.

The new tests cover the create request payload, each sampling level, the
case-insensitive and non-string inputs, an omitted level leaving the field
unset, invalid levels being rejected, the optional filters/limits/currency
reaching the definition, the returned rows, polling a CREATING task until it
is ACTIVE, FAILED tasks surfacing the API message, the timeout path, the
timeout_seconds bounds, and the tool over the MCP tools/list and
tools/call handlers.

I also verified the tests fail when the implementation is mutated: dropping the
sampling_level assignment (8 failures), skipping the FAILED state check
(1 failure), falling back to LOW for an invalid level (3 failures), and
removing the timeout_seconds bounds (2 failures).

Not covered locally: the API call itself. The tests use a fake client, so the
real report task creation path will first run in CI and against a real property.

Notes

Refs #257

The Data API only exposes the sampling level of a report through report
tasks: RunReportRequest has no sampling_level field, so an MCP client
cannot request a larger sample or an unsampled report today.

Add a run_report_task tool that creates a report task, waits for it to
become ACTIVE, and returns its rows. The sampling_level argument accepts
LOW, MEDIUM and UNSAMPLED, case-insensitively, and stays unset when the
argument is omitted so the property default applies.

Report tasks are built asynchronously, so the tool polls the task while it
is CREATING, surfaces the API error message when it FAILED, and raises a
TimeoutError pointing at the 72-hour query window when it does not finish
within timeout_seconds.

Refs googleanalytics#257
@google-cla

google-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Goldloli

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

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.

3 participants