Skip to content

Conversation

bgaidioz
Copy link
Contributor

Description

Brief description of the changes in this PR.

Type of Change

  • πŸ› Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • πŸ’₯ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • πŸ“ Documentation update
  • πŸ”§ Refactoring (no functional changes, no api changes)
  • ⚑ Performance improvement
  • πŸ§ͺ Test improvement
  • πŸ”’ Security fix

Testing

  • Tests pass locally with uv run pytest
  • Linting passes with uv run ruff check .
  • Code formatting passes with uv run black --check .
  • Type checking passes with uv run mypy .
  • Added tests for new functionality (if applicable)
  • Updated documentation (if applicable)

Security Considerations

  • This change does not introduce security vulnerabilities
  • Sensitive data handling reviewed (if applicable)
  • Policy enforcement implications considered (if applicable)

Breaking Changes

If this is a breaking change, describe what users need to do to migrate:

Additional Notes

Any additional context or screenshots.

- Add thread-safe caching to prevent repeated provider API calls
- Cache user context with configurable TTL (default 5 minutes)
- Implement automatic cleanup of expired cache entries
- Add comprehensive logging for cache hits/misses/cleanup
- Maintain backward compatibility with existing middleware interface

This addresses the performance issue where every MCP tool execution
was making HTTP requests to OAuth providers (Keycloak, Google, etc.)
to fetch the same user information repeatedly.

Performance improvement: ~95% reduction in provider API calls for
typical usage patterns within the cache TTL window.
- Move cache HIT messages to DEBUG level (frequent, low importance)
- Move cache STORE messages to DEBUG level (internal operation)
- Keep cache MISS messages at INFO level (important for monitoring API usage)
- Keeps logs cleaner while preserving visibility into provider API calls
- Add cache_ttl field to UserAuthConfig and AuthConfig types
- Update translate_auth_config to pass cache_ttl from user config to SDK
- Modify middleware initialization to use configured cache_ttl value
- Maintain backward compatibility with default TTL when not specified

Users can now configure OAuth cache TTL in their auth config:

This allows environment-specific tuning of cache behavior while
maintaining the default 5-minute TTL for existing configurations.
This prevents cache entries from becoming orphaned when external tokens
are refreshed. The MCP token remains stable throughout the session,
making it a more reliable cache key.
Adds per-MCP-token locks to prevent multiple concurrent requests from
triggering parallel refresh attempts for the same token. Includes
optimization to check if another request already completed the refresh
before proceeding.
Replaces fragile hasattr() checks with explicit HTTPException type
checking for 401 errors. Adds separate handling for non-HTTP exceptions
with proper logging for better debugging.
- DEBUG: Normal operations (cache hits/stores, token validation, client lookups)
- INFO: Important events (token refresh attempts/success, errors)
- Reduces log noise in production while keeping essential information
@bgaidioz bgaidioz force-pushed the feature/oauth-refresh-tokens branch from 0013dbe to 5d42b9c Compare September 11, 2025 13:15
@bgaidioz bgaidioz changed the title Feature/oauth refresh tokens Implement refresh tokens + caching of user_info Sep 11, 2025
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.

1 participant