aminghadersohi opened a new pull request, #37200:
URL: https://github.com/apache/superset/pull/37200

   ### SUMMARY
   
   Adds a middleware that prevents oversized MCP tool responses from 
overwhelming LLM clients like Claude Desktop. When users query large datasets 
(e.g., listing 100+ charts with all columns), the response can be so large it 
crashes the client.
   
   This PR introduces `ResponseSizeGuardMiddleware` that:
   1. Estimates the token count of tool responses using a character-based 
heuristic (~3.5 chars/token for JSON)
   2. Blocks responses exceeding the configured limit (default: 25k tokens)
   3. Returns helpful, actionable suggestions for reducing response size
   
   **Key features:**
   - **Smart suggestions** based on tool and query parameters:
     - Calculates and suggests specific `page_size`/`limit` values
     - Suggests using `select_columns` to exclude large fields
     - Suggests adding filters to narrow results
     - Tool-specific recommendations (e.g., SQL LIMIT clause for `execute_sql`)
   - **Configurable** via `MCP_RESPONSE_SIZE_CONFIG` in superset_config.py
   - **Warning logs** when approaching the limit (default: 80%)
   - **Excluded tools** for URL-returning endpoints (health_check, 
get_chart_preview, etc.)
   
   **Example error message when limit exceeded:**
   ```
   Response too large: ~75,000 tokens (limit: 25,000)
   
   This response would overwhelm the LLM context window.
   Please modify your query to reduce the response size:
   
   1. Reduce page_size/limit from 100 to 33 (need ~67% reduction)
   2. Use 'select_columns' to exclude large fields: params, query_context, 
form_data
   3. Add filters to narrow down results
   4. For list_charts, exclude 'params' and 'query_context' columns
   5. Use the 'search' parameter to find specific items
   
   Tool: list_charts
   Reduction needed: ~67%
   ```
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   N/A - Backend middleware change
   
   ### TESTING INSTRUCTIONS
   
   1. Start the MCP service
   2. Query a large dataset via MCP tool (e.g., `list_charts` with 
`page_size=100` and many charts)
   3. If response exceeds 25k tokens, verify you get a helpful error message 
with suggestions
   4. Configure a smaller limit in `superset_config.py` to test more easily:
      ```python
      MCP_RESPONSE_SIZE_CONFIG = {
          "enabled": True,
          "token_limit": 1000,  # Very low for testing
      }
      ```
   5. Run the new unit tests: `pytest 
tests/unit_tests/mcp_service/utils/test_token_utils.py 
tests/unit_tests/mcp_service/test_middleware.py -v`
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration
   - [x] Introduces new feature or API
   - [ ] Removes existing feature or API
   
   🤖 Generated with [Claude Code](https://claude.ai/code)


-- 
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]

Reply via email to