Skip to content

fix(samples): refresh the access token in the BigQuery MCP sample - #7218

Open
vishal-bulbule wants to merge 1 commit into
google:mainfrom
vishal-bulbule:fix/bigquery-mcp-sample-token-refresh
Open

vishal-bulbule wants to merge 1 commit into
google:mainfrom
vishal-bulbule:fix/bigquery-mcp-sample-token-refresh

Conversation

@vishal-bulbule

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

The bigquery_mcp sample fetches an Application Default Credentials access token once at import
and passes it as a fixed Authorization header. The token expires after about an hour and is
never refreshed, so in a long-running adk web or adk api_server process the BigQuery tools
stop working until the process restarts. Details in #7217.

Solution:

Replace the fixed header with a header_provider that refreshes the credentials when they are no
longer valid and returns the current token. It works on both the mTLS and the regular endpoint
(the session manager's own ADC refresh skips requests that already carry an Authorization
header). One file, sample only.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Sample-only change; there are no unit tests for samples.

$ pytest tests/unittests -n auto
1 failed, 15287 passed, 84 skipped, 27 xfailed, 2 xpassed

The one failure is test_import_loading, which also fails on main and is fixed by #7214.

Manual End-to-End (E2E) Tests:

Ran against the BigQuery MCP server with Application Default Credentials (user credentials), on
main at d57c84f:

  1. Same process, current sample and fixed sample, calling the list_dataset_ids tool before and
    after the token expires:

    09:12:57 before expiry, current sample: OK, list_dataset_ids returned 52 datasets
    

09:13:00 before expiry, fixed sample : OK, list_dataset_ids returned 52 datasets
10:18:17 current sample credentials valid: False
10:18:18 after expiry, current sample : FAILED (tool call rejected)
10:18:20 after expiry, fixed sample : OK, list_dataset_ids returned 52 datasets


2. Forced expiry on the fixed sample (set `credentials.expiry` in the past): the next call
refreshed the token and the tool call succeeded.

3. `adk run bigquery_mcp` with the fixed sample, asking which tables exist in the
`bigquery-public-data.ml_datasets` dataset: the agent called the MCP tool and answered with
the table list.

Note for anyone reproducing this: `tools/list` on the BigQuery MCP server does not require a
valid token, so listing tools still succeeds with an expired one. Only `tools/call` fails.

### Checklist

- [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [x] I have manually tested my changes end-to-end.
- [x] Any dependent changes have been merged and published in downstream modules.

The sample fetched an Application Default Credentials token once at import
and sent it as a fixed Authorization header, so the toolset stopped working
about an hour into a long-running adk web or adk api_server process. Use a
header_provider that refreshes the credentials when they are no longer valid.
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.

BigQuery MCP sample stops working after the access token expires

2 participants