aminghadersohi opened a new pull request, #36078:
URL: https://github.com/apache/superset/pull/36078
## Summary
**[PROOF OF CONCEPT]**
This PR implements FastMCP 2.13.0's new response caching middleware to solve
scalability issues for the Superset MCP service. The caching layer dramatically
improves performance for repeated queries and reduces database load.
**Note:** FastMCP 2.13.0 is already available in apache/superset master
(pyproject.toml line 143). This PR builds on that upgrade to add caching
capabilities.
## Key Features
### 1. Response Caching Middleware
- ✅ TTL-based caching for tools, resources, and prompts
- ✅ In-memory cache by default (pluggable backends supported)
- ✅ Configurable cache durations per operation type
- ✅ Tool-level granularity for cache control
### 2. Smart Caching Defaults
**List Operations** (5 minute TTL):
- `list_tools`, `list_resources`, `list_prompts`
- `list_dashboards`, `list_charts`, `list_datasets`
**Individual Items** (1 hour TTL):
- `get_dashboard_info`, `get_chart_info`, `get_chart_preview`
- `get_dataset_info`, `read_resource`, `get_prompt`
**Excluded from Cache** (mutable/expensive operations):
- `execute_sql` - SQL execution results vary
- `generate_dashboard`, `generate_chart` - AI-generated content
- `update_chart`, `update_chart_preview` - Data modification
- `add_chart_to_existing_dashboard` - Data modification
### 3. Configuration
Users can customize caching behavior in `superset_config.py`:
```python
MCP_CACHE_CONFIG = {
"enabled": True, # Enable/disable caching
"max_item_size": 1024 * 1024, # 1MB max cached item size
# TTLs in seconds
"list_tools_ttl": 300, # 5 minutes
"call_tool_ttl": 3600, # 1 hour
# Tool-specific control
"excluded_tools": ["execute_sql", "generate_chart"],
"included_tools": None, # If set, only cache these tools
}
```
## Changes
### app.py
- Removed deprecated `stateless_http=True` parameter (deprecated in FastMCP
2.13+)
- Added `middleware` parameter to `create_mcp_app()`
- Added middleware support to MCP instance factory
- Updated default mcp instance creation to use server-level lifespan
management
### mcp_config.py
- Added `MCP_CACHE_CONFIG` with sensible defaults
- Implemented `create_response_caching_middleware()` factory
- Supports configurable TTLs and tool filters
- Graceful fallback for older FastMCP versions
### server.py
- Integrated caching middleware into server initialization
- Middleware automatically enabled in default mode
- Removed unused `init_fastmcp_server` import (ruff fix)
- Created middleware list with response caching
## Benefits
🚀 **Performance:**
- Instant response for cached queries (no database hit)
- Reduced latency for repeated list operations
- Scales better with multiple concurrent clients
💾 **Database Load:**
- Fewer queries to Superset database
- Reduced load on metadata tables
- More efficient resource utilization
⚙️ **Flexibility:**
- Configurable per-operation TTLs
- Tool-level caching control
- Pluggable cache backends (memory, Redis, disk, etc.)
## Testing
✅ MCP instance creates successfully with middleware
✅ ResponseCachingMiddleware attaches properly
✅ Configuration factory works correctly
✅ Pre-commit hooks pass (mypy, ruff, pylint)
## Before/After
**Without caching:**
```
list_dashboards() → Database query every time
list_dashboards() → Database query every time
list_dashboards() → Database query every time
```
**With caching:**
```
list_dashboards() → Database query (cached for 5 min)
list_dashboards() → Cache hit (instant)
list_dashboards() → Cache hit (instant)
```
## Migration Path
This is **backward compatible** - no changes required for existing
deployments:
- Caching is enabled by default with safe settings
- Expensive/mutable operations are excluded
- Users can disable via `MCP_CACHE_CONFIG["enabled"] = False`
- Gracefully falls back if FastMCP 2.13+ not available
## Next Steps
For production deployment, consider:
1. **Persistent cache backend** - Replace in-memory cache with Redis/disk
storage
2. **Cache statistics** - Monitor hit rates via `middleware.statistics()`
3. **Cache invalidation** - Implement webhook-based cache clearing on data
changes
4. **TTL tuning** - Adjust based on actual usage patterns
## Related
- FastMCP 2.13.0 already in master: `pyproject.toml` line 143
- FastMCP Release: https://github.com/jlowin/fastmcp/releases/tag/v2.13.0
---
**Note:** This is a proof of concept to demonstrate the caching
capabilities. Feedback welcome!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]