Skip to content

fix(mcp): fix generate_chart crashing with "Working outside of request context"#38868

Open
congmnguyen wants to merge 1 commit intoapache:masterfrom
congmnguyen:fix/mcp-create-form-data-request-context
Open

fix(mcp): fix generate_chart crashing with "Working outside of request context"#38868
congmnguyen wants to merge 1 commit intoapache:masterfrom
congmnguyen:fix/mcp-create-form-data-request-context

Conversation

@congmnguyen
Copy link

@congmnguyen congmnguyen commented Mar 26, 2026

User description

Problem

generate_chart and generate_explore_link crash with:

RuntimeError: Working outside of request context.

This happens because MCPCreateFormDataCommand inherits CreateFormDataCommand.run(), which calls flask.session.get("_id") directly. Flask's session proxy requires an active HTTP request context, but MCP tools run in an app context only — there is no HTTP request.

Root cause

MCPCreateFormDataCommand already had a _get_session_id() override intended to fix this, but it was never called. The base class run() accesses flask.session directly rather than going through self._get_session_id(), making the override a no-op.

Fix

Override run() in MCPCreateFormDataCommand and replace session.get("_id") with get_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

  • Call generate_chart via MCP — confirm it no longer raises Working outside of request context
  • Call generate_explore_link via MCP — confirm explore URL is returned successfully
  • Existing unit tests pass

CodeAnt-AI Description

Keep MCP chart and explore link generation working without a request context

What Changed

  • MCP chart and explore link generation no longer crash when there is no browser request
  • The command now uses the current user instead of session data to build and reuse cached form data
  • If saving temporary form data fails, the command now returns a clear cache-creation failure instead of a lower-level database error

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:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

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:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

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.

…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.
@bito-code-review
Copy link
Contributor

bito-code-review bot commented Mar 26, 2026

Code Review Agent Run #30b880

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: a992b69..a992b69
    • superset/mcp_service/commands/create_form_data.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@dosubot dosubot bot added the change:backend Requires changing the backend label Mar 26, 2026
@codeant-ai-for-open-source codeant-ai-for-open-source bot added the size:M This PR changes 30-99 lines, ignoring generated files label Mar 26, 2026
@codeant-ai-for-open-source
Copy link
Contributor

Sequence Diagram

This 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
Loading

Generated by CodeAnt AI

@netlify
Copy link

netlify bot commented Mar 26, 2026

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit a992b69
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/69c4aaa12a1ef400088219c0
😎 Deploy Preview https://deploy-preview-38868--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/M size:M This PR changes 30-99 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant