fix(mcp): fix generate_chart crashing with "Working outside of request context"#38868
fix(mcp): fix generate_chart crashing with "Working outside of request context"#38868congmnguyen wants to merge 1 commit intoapache:masterfrom
Conversation
…t context"
MCPCreateFormDataCommand.run() inherited CreateFormDataCommand.run() which
calls flask.session.get("_id") on line 50. Flask's session proxy requires an
active HTTP request context, but MCP tools execute in an app context only
(no HTTP request), so every call to generate_chart / generate_explore_link
raised RuntimeError: "Working outside of request context".
The existing _get_session_id() override on MCPCreateFormDataCommand was a
no-op because the base class run() never calls self._get_session_id() — it
accesses flask.session directly.
Fix: override run() in MCPCreateFormDataCommand and substitute get_user_id()
for the session ID. The session ID is used purely as part of a Redis cache
key for contextual form-data deduplication; using the user ID preserves
that behaviour without requiring a request context.
Code Review Agent Run #30b880Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Sequence DiagramThis PR updates the MCP create form data command to derive cache keys from the current user instead of the HTTP session and to surface a clearer error when temporary form data caching fails. sequenceDiagram
participant MCPClient as MCP client
participant Command as MCP create form data
participant Auth as Access control
participant Cache as Explore form data cache
MCPClient->>Command: Request chart or explore link key
Command->>Auth: Check access for datasource and chart
Auth-->>Command: Access allowed
Command->>Command: Build contextual key using user id
Command->>Cache: Get or create key for contextual form data
Command->>Cache: Save form data state and contextual key
Cache-->>Command: Form data cached
Command-->>MCPClient: Return form data cache key
alt Cache write fails
Command-->>MCPClient: Temporary cache create failed error
end
Generated by CodeAnt AI |
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
User description
Problem
generate_chartandgenerate_explore_linkcrash with:This happens because
MCPCreateFormDataCommandinheritsCreateFormDataCommand.run(), which callsflask.session.get("_id")directly. Flask'ssessionproxy requires an active HTTP request context, but MCP tools run in an app context only — there is no HTTP request.Root cause
MCPCreateFormDataCommandalready had a_get_session_id()override intended to fix this, but it was never called. The base classrun()accessesflask.sessiondirectly rather than going throughself._get_session_id(), making the override a no-op.Fix
Override
run()inMCPCreateFormDataCommandand replacesession.get("_id")withget_user_id(). The session ID is only used as part of a Redis cache key for contextual form-data deduplication — substituting the user ID preserves that behaviour without requiring a request context.Test plan
generate_chartvia MCP — confirm it no longer raisesWorking outside of request contextgenerate_explore_linkvia MCP — confirm explore URL is returned successfullyCodeAnt-AI Description
Keep MCP chart and explore link generation working without a request context
What Changed
Impact
✅ Fewer MCP request-context crashes✅ Reliable chart and explore link generation✅ Clearer temporary cache failure errors💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.