Caching
Cache management utilities for MCPClient objects.
This module provides functions for efficient type-annotated retrieval and caching of MCPClient querysets. It includes utilities to:
Retrieve and cache MCPClients owned by a user profile
Retrieve and cache MCPClients shared with a user profile
Retrieve and cache MCPClients available to a user profile (owned or shared)
Invalidate caches for owned, shared, and available MCPClients
Invalidate all MCPClient-related caches for a user profile
Functions:
get_cached_mcpclients_owned_by_user_profile(user_profile)
invalidate_cached_mcpclients_owned_by_user_profile(user_profile)
get_cached_mcpclients_shared_with_user_profile(user_profile)
invalidate_cached_mcpclients_shared_with_user_profile(user_profile)
get_cached_mcpclients_available_to_user_profile(user_profile)
invalidate_cached_mcpclients_available_to_user_profile(user_profile)
invalidate_all_cached_mcpclients_for_user_profile(user_profile)
get_cached_catalog(mcpclient, refresh=False)
invalidate_cached_catalog(mcpclient)
MCP server catalogs, i.e. each server’s tools, instructions and capabilities, are cached
for the MCPClient’s cache_ttl seconds, so that prompts do not connect to the server
to list its tools. The cache key includes MCPClient.fingerprint, so that changing
how the MCPClient connects, or what it allows, invalidates its catalog. A failure to
connect is cached for FAILURE_CACHE_TTL seconds, so that an unreachable server
does not delay every prompt by its timeout.
Dependencies:
Django ORM
smarter.lib.cache.cache_results
smarter.apps.account.models.user_profile.UserProfile
smarter.apps.mcpclient.models.MCPClient
smarter.apps.mcpclient.serializers.MCPClientSerializer
- smarter.apps.mcpclient.caching.FAILURE_CACHE_TTL = 60
Seconds to remember that an MCP server could not be reached.
- smarter.apps.mcpclient.caching.catalog_cache_key(mcpclient)[source]
Return the cache key of an MCPClient’s catalog.
- smarter.apps.mcpclient.caching.failure_cache_key(mcpclient)[source]
Return the cache key of an MCPClient’s most recent connection failure.
- Return type:
- smarter.apps.mcpclient.caching.get_cached_catalog(mcpclient, refresh=False)[source]
Return an MCPClient’s catalog: its MCP server’s tools, instructions and capabilities.
The catalog is cached for the MCPClient’s
cache_ttlseconds. On a cache miss, Smarter connects to the server, records the result in the MCPClient’s status fields, and sendsmcpclient_connectedormcpclient_connection_failed.- Parameters:
- Returns:
The catalog.
- Return type:
- Raises:
SmarterMCPClientConnectionError – If the server cannot be reached, now or, unless
refresh, within the lastFAILURE_CACHE_TTLseconds.SmarterMCPClientConfigurationError – If the MCPClient is misconfigured.
- smarter.apps.mcpclient.caching.get_cached_mcpclients_available_to_user_profile(user_profile)[source]
Retrieve the MCPClients available to the given UserProfile, using caching to optimize performance.
This function returns a queryset of MCPClient objects that are available to the specified user profile, which may include both owned and shared MCPClients. The results are cached to reduce database queries and improve performance. If the cache is invalidated, the queryset is fetched from the database again and re-cached.
- Parameters:
user_profile (
UserProfile) – The user profile whose available MCPClients should be retrieved.- Returns:
A Django queryset containing the MCPClient objects available to the user.
- Return type:
>>> user_profile = UserProfile.objects.get(pk=1) >>> available_mcpclients = get_cached_mcpclients_available_to_user_profile(user_profile) >>> for bot in available_mcpclients: ... print(bot.name)
See also
invalidate_cached_mcpclients_available_to_user_profile()- Invalidate the cache for available MCPClients of a user profile.
- smarter.apps.mcpclient.caching.get_cached_mcpclients_owned_by_user_profile(user_profile)[source]
Retrieve the MCPClients owned by the given UserProfile, using caching to optimize performance.
This function returns a queryset of MCPClient objects that are owned by the specified user profile. The results are cached to reduce database queries and improve performance. If the cache is invalidated, the queryset is fetched from the database again and re-cached.
- Parameters:
user_profile (
UserProfile) – The user profile whose owned MCPClients should be retrieved.- Returns:
A Django queryset containing the MCPClient objects owned by the user.
- Return type:
>>> user_profile = UserProfile.objects.get(pk=1) >>> mcpclients = get_cached_mcpclients_owned_by_user_profile(user_profile) >>> for bot in mcpclients: ... print(bot.name)
See also
invalidate_cached_mcpclients_owned_by_user_profile()- Invalidate the cache for owned MCPClients of a user profile.
Retrieve the MCPClients shared with the given UserProfile, using caching to optimize performance.
This function returns a queryset of MCPClient objects that are shared with the specified user profile. The results are cached to reduce database queries and improve performance. If the cache is invalidated, the queryset is fetched from the database again and re-cached.
- Parameters:
user_profile (
UserProfile) – The user profile whose shared MCPClients should be retrieved.- Returns:
A Django queryset containing the MCPClient objects shared with the user.
- Return type:
>>> user_profile = UserProfile.objects.get(pk=1) >>> shared_mcpclients = get_cached_mcpclients_shared_with_user_profile(user_profile) >>> for bot in shared_mcpclients: ... print(bot.name)
See also
invalidate_cached_mcpclients_shared_with_user_profile()- Invalidate the cache for shared MCPClients of a user profile.
- smarter.apps.mcpclient.caching.invalidate_all_cached_mcpclients_for_user_profile(user_profile)[source]
Invalidate all cached MCPClient querysets related to the given UserProfile.
This function invalidates the caches for all MCPClient querysets that are related to the specified user profile, including owned, shared, and available MCPClients. This is useful when a change occurs that may affect any of these querysets, ensuring that subsequent calls will fetch fresh data from the database.
- Parameters:
user_profile (
UserProfile) – The user profile for which to invalidate cached MCPClient querysets.- Returns:
None
- Return type:
>>> user_profile = UserProfile.objects.get(pk=1) >>> invalidate_all_cached_mcpclients_for_user_profile(user_profile)
See also
invalidate_cached_mcpclients_owned_by_user_profile()- Invalidate the cache for owned MCPClients of a user profile.invalidate_cached_mcpclients_shared_with_user_profile()- Invalidate the cache for shared MCPClients of a user profile.invalidate_cached_mcpclients_available_to_user_profile()- Invalidate the cache for available MCPClients of a user profile.
- smarter.apps.mcpclient.caching.invalidate_cached_catalog(mcpclient)[source]
Invalidate an MCPClient’s cached catalog, and any cached connection failure.
Catalogs cached under a previous
MCPClient.fingerprintare no longer used, and expire with theircache_ttl.
- smarter.apps.mcpclient.caching.invalidate_cached_mcpclients_available_to_user_profile(user_profile)[source]
- Return type:
- smarter.apps.mcpclient.caching.invalidate_cached_mcpclients_owned_by_user_profile(user_profile)[source]
- Return type:
- Return type:
- smarter.apps.mcpclient.caching.record_connection(mcpclient, catalog)[source]
Record a successful connection in the MCPClient’s status fields.
The fields are updated with a queryset update, which neither changes
updated_atnor sendspost_save, because this is a status change, not a configuration change.- Parameters:
mcpclient (
MCPClient) – The MCPClient.catalog (
MCPServerCatalog) – What the MCP server reported.
- Return type: