fix(amazon-bedrock-agentcore-mcp-server): expire cached browser clients to pick up refreshed credentials - #4369
Draft
kevin-orellana wants to merge 1 commit into
Draft
Conversation
…ts to pick up refreshed credentials `get_browser_client` cached a `BrowserClient` per region at module level and never invalidated it. The wrapped boto3 clients resolve credentials at construction time, so a long-lived MCP server process kept using the credentials captured on first use. After `ada credentials update`, an IAM role rotation, or an expired STS session, the stale client kept being returned and calls failed (or used stale tokens) until the process restarted. Add a per-entry TTL (default 300s, overridable via AGENTCORE_BROWSER_CLIENT_TTL_SECONDS; 0 disables caching): once an entry expires it is dropped and rebuilt on the next call, forcing the underlying boto3 clients to re-resolve credentials. This mirrors the existing credential-rotation-aware cache in sagemaker-ai-mcp-server. Add unit tests covering TTL expiry/reuse, caching disabled, and env-var parsing, and clear the new timestamp map in test setup. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4369 +/- ##
==========================================
- Coverage 93.05% 93.04% -0.01%
==========================================
Files 1031 1031
Lines 86905 86928 +23
Branches 14019 14022 +3
==========================================
+ Hits 80866 80886 +20
Misses 3658 3658
- Partials 2381 2384 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes stale-credential behavior in the AgentCore browser MCP server.
Summary
Changes
get_browser_clientcached aBrowserClientper region at module level and never invalidated it. The wrapped boto3 clients resolve credentials at construction time, so a long-lived MCP server kept using the credentials captured at first use — after rotation (ada credentials update, IAM role rotation, expired STS session) every browser call failed until the process was restarted.This change adds TTL-based cache expiry:
AGENTCORE_BROWSER_CLIENT_TTL_SECONDS; a non-positive value disables caching entirelyUser experience
Before: after credential rotation, browser tools return auth errors until the MCP server process is restarted.
After: within one TTL window (≤5 min by default) the client is rebuilt with fresh credentials automatically; no restart needed.
Checklist
tests/browser/test_unit_browser_client.py: 11 passed, including the regression guardtest_recreates_client_after_ttl_expiry(monotonic clock mocked past TTL asserts a second construction) and invalid-TTL fallback casesIs this a breaking change? (N)
RFC issue number: N/A
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.