aminghadersohi opened a new pull request, #38414:
URL: https://github.com/apache/superset/pull/38414
### SUMMARY
Add new MCP tool `save_sql_query` that allows AI assistants to save SQL
queries as named SavedQuery records in Superset. This enables the workflow of
running a query in SQL Lab via MCP and then saving it for later reuse/sharing.
**What this adds:**
- New `save_sql_query` tool: accepts `database_id`, `label`, `sql`, and
optional `schema`/`catalog`/`description`
- Validates that the target database exists and the user has access
- Creates a persistent SavedQuery via `SavedQueryDAO.create()`
- Returns saved query ID and a SQL Lab URL for reloading
(`/sqllab?savedQueryId=N`)
- Pydantic request/response schemas with input validation (empty check,
whitespace stripping, max length)
- 18 unit tests covering schemas and tool logic
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A - Backend MCP tool, no UI changes
### TESTING INSTRUCTIONS
1. Connect to the Superset MCP service via an MCP client (e.g., Claude
Desktop)
2. Call `save_sql_query` with a valid `database_id`, `label`, and `sql`:
```json
{
"database_id": 1,
"label": "Test Query",
"sql": "SELECT 1"
}
```
3. Verify the response includes an `id` and a `url` containing
`savedQueryId=`
4. Open the returned URL in a browser to confirm the query loads in SQL Lab
5. Check the Saved Queries page to verify the query appears there
6. Test error cases:
- Invalid `database_id` → should return "not found" error
- Database user has no access to → should return "Access denied" error
- Empty `sql` or `label` → should fail validation
**Run unit tests:**
```bash
pytest tests/unit_tests/mcp_service/sql_lab/tool/test_save_sql_query.py -v
```
### ADDITIONAL INFORMATION
- [x] Introduces new feature or API
- [ ] Has associated issue:
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration
- [ ] 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]