aminghadersohi opened a new pull request, #38827: URL: https://github.com/apache/superset/pull/38827
### SUMMARY The `generate_dashboard` MCP tool used `CreateDashboardCommand` which has a `@transaction` decorator that operates in a different SQLAlchemy scoped session than the one where `g.user` and chart ORM objects were loaded by the MCP middleware. This caused `InvalidRequestError: Object '<User ...>' is already attached to session 'X' (this is 'Y')` errors when creating dashboards via MCP. **Root cause**: In MCP context, the middleware loads `g.user` inside a Flask `app_context` (bound to SQLAlchemy session N). When the tool function runs, `db.session` resolves to a different scoped session (session M). `CreateDashboardCommand`'s `@transaction` decorator calls `populate_owners()` which returns `[g.user]` from the old session, and sets `dashboard.slices = chart_objects` also from the old session — both trigger cross-session attachment errors. **Fix**: Bypass `CreateDashboardCommand` and create the Dashboard directly via `db.session`, re-querying the user and charts fresh in the tool's own session. Also narrowed exception catches from broad `Exception` to specific `SQLAlchemyError` and added `exc_info=True` to the outer error log for better debugging. ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF **Before**: `generate_dashboard` tool always fails with: - Localhost: `Object '<User at 0x...>' is already attached to session '7' (this is '8')` - Remote: `Can't reconnect until invalid transaction is rolled back` **After**: Dashboard creation works correctly, tested with 2-chart and 4-chart configurations. ### TESTING INSTRUCTIONS 1. Start MCP server: `flask preset-mcp run --debug` 2. Connect an MCP client (Claude Desktop, Claude Code, etc.) 3. Create some charts first via `generate_chart` 4. Call `generate_dashboard` with those chart IDs 5. Verify the dashboard is created and accessible at the returned URL ### ADDITIONAL INFORMATION - [ ] Has associated issue: - [ ] Required feature flags: - [ ] Changes UI - [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351)) - [ ] Migration is atomic, supports rollback & is backwards-compatible - [ ] Confirm DB migration upgrade and downgrade tested - [ ] Runtime estimates and downtime expectations provided - [ ] Introduces new feature or API - [ ] Removes existing feature or API -- 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]
