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

   ## Summary
   
   Fixes MCP Service authentication context error that was causing production 
failures.
   
   **Problem**: The `@mcp_auth_hook` decorator was accessing 
`current_app.config` before the Flask application context was established by 
middleware, resulting in "Working outside of application context" errors.
   
   **Error seen in production**:
   ```
   Error calling tool 'get_instance_info'
   ValueError: Username not configured
   ```
   
   ## Changes
   
   ### Before (Broken Flow)
   1. MCP tool called with `@mcp_auth_hook` decorator
   2. Decorator immediately tries to access 
`current_app.config.get("MCP_DEV_USERNAME")`
   3. ❌ ERROR: No Flask context exists yet
   4. Middleware runs AFTER
   
   ### After (Fixed Flow)
   1. Middleware (`WorkspaceContextMiddleware`) runs FIRST
      - Establishes Flask application context
      - Extracts username from JWT token
      - Loads user and sets `g.user`
   2. Decorator runs SECOND
      - Uses already-set `g.user` from middleware
      - Validates user is authenticated
      - No premature app context access
   
   ## Technical Details
   
   ### Modified: `superset/mcp_service/auth.py`
   
   **`get_user_from_request()` changes:**
   - ❌ Removed: `current_app.config.get("MCP_DEV_USERNAME")` access
   - ✅ Added: Check for `g.user` set by middleware
   - ✅ Added: Clear error message for missing JWT authentication
   
   **`mcp_auth_hook()` decorator:**
   - No longer tries to load user (middleware already did this)
   - Validates user context exists
   - Maintains session cleanup logic
   
   ## Testing
   
   Tested with:
   - ✅ Production environment with JWT authentication
   - ✅ Tools work correctly with authenticated users
   - ✅ Clear error message when authentication missing
   - ✅ No more "Working outside of application context" errors
   
   ## Related
   
   - Fixes production error in workspace: ws--8e4e5389--main
   - Works with existing WorkspaceContextMiddleware 
(preset/mcp/workspace_context_middleware.py)


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