Skip to main content
Glama
clsung

Taiwan Stock Agent

by clsung

invalidate_cache

Clear cached Taiwan stock market data to retrieve updated information for accurate analysis and decision-making.

Instructions

Invalidate resource cache for fresh data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
resource_patternNo

Implementation Reference

  • MCP tool handler for 'invalidate_cache' that delegates to resource_manager.invalidate_cache and formats the response with cache statistics.
    @mcp.tool(name="invalidate_cache",
              description="Invalidate resource cache for fresh data")
    async def invalidate_cache_tool(resource_pattern: Optional[str] = None) -> Dict[str, Any]:
        """Invalidate resource cache."""
        try:
            invalidated_count = resource_manager.invalidate_cache(resource_pattern)
            cache_stats = resource_manager.get_cache_stats()
            
            return {
                "pattern": resource_pattern or "all",
                "invalidated_entries": invalidated_count,
                "cache_statistics": cache_stats,
                "_metadata": {
                    "source": "tw-stock-agent",
                    "timestamp": datetime.now().isoformat(),
                    "data_type": "cache_management"
                }
            }
        except Exception as e:
            logger.error(f"Failed to invalidate cache: {e}")
            return {
                "pattern": resource_pattern or "all",
                "invalidated_entries": 0,
                "error": str(e),
                "_metadata": {
                    "source": "tw-stock-agent",
                    "timestamp": datetime.now().isoformat(),
                    "data_type": "cache_management",
                    "has_error": True
                }
            }
  • Core implementation of cache invalidation in ResourceManager class, clearing all or pattern-matched cache entries.
    def invalidate_cache(self, resource_pattern: Optional[str] = None) -> int:
        """
        Invalidate cached resources.
        
        Args:
            resource_pattern: Optional pattern to match specific resources
            
        Returns:
            Number of cache entries invalidated
        """
        if resource_pattern is None:
            # Clear entire cache
            count = len(self.cache)
            self.cache.clear()
            logger.info(f"Invalidated entire resource cache ({count} entries)")
            return count
        else:
            # Clear matching entries
            keys_to_remove = [
                key for key in self.cache.keys()
                if resource_pattern in key
            ]
            for key in keys_to_remove:
                del self.cache[key]
            logger.info(f"Invalidated {len(keys_to_remove)} cache entries matching '{resource_pattern}'")
            return len(keys_to_remove)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/clsung/tw-stock-agent'

If you have feedback or need assistance with the MCP directory API, please join our Discord server