feat: add configurable token cache for customer flow#133
Open
vitalykumov wants to merge 2 commits into
Open
Conversation
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.
Description
Add in-process token cache for customer agent flow. Previously every
list_mcp_tools()/call_mcp_tool()call fetched fresh IAS token via mTLS - unnecessary latency in agentic loops.Changes:
_token_cache.py(new):_TokenCache- TTL + LRU eviction for system tokens (key:app_tid) and user tokens (key:sha256(user_jwt+"|"+app_tid)[:16]). Expiry is fromexpires_in,id_tokenexp claim, or fallback TTL._customer.py:get_system_token_mtls/exchange_user_tokento consult/populate cache. 401 response from MCP server → invalidate stale token + retry once.agw_client.py:AgentGatewayClientowns_TokenCache. Exposesclear_token_cache()for forced refresh (revoked creds, tenant change).config.py: 4 newClientConfigfields added -token_expiry_buffer_seconds(60 s),max_system_token_cache_size(10 s),max_user_token_cache_size(10 s),fallback_token_ttl_seconds(300 s).LoB flow unaffected - delegates to BTP Destination Service.
Type of Change
Please check the relevant option:
How to Test
Describe how reviewers can test your changes:
Checklist
Before submitting your PR, please review and check the following:
Breaking Changes
None. Cache internal to
AgentGatewayClient- existingcreate_client()calls get caching automatically.ClientConfignew fields all have defaults.Additional Notes
Thread safety: GIL makes individual
OrderedDictops atomic, but check-then-set is not. Concurrent coroutines on same key may both miss and both fetch - redundant requests, not corruption. Acceptable for agentic loop use case.401 retry: Both
get_mcp_tools_customerandcall_mcp_tool_customerinvalidate + retry once on 401, handling server-side revocation before token expiry.