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

   ### SUMMARY
   
   `ToolResultCompatibilityMiddleware.on_call_tool` has a last-resort `except 
Exception` that turns any escaping exception into `ToolResult(is_error=True)`. 
For any non-`ToolError` exception it also called `MCP_ERROR_HOOK`, **without 
checking whether the error was user-class**. 
`GlobalErrorHandlerMiddleware._handle_error` deliberately keeps user-class 
errors (bad arguments, permission denials, missing objects) out of the hook, 
because they are normal MCP traffic and would flood an error tracker.
   
   **Root cause.** FastMCP treats the first middleware added as the outermost. 
In the default stack in `superset/mcp_service/server.py`, the compat middleware 
is outermost, so `GlobalErrorHandlerMiddleware` sees every error first, 
classifies it and re-raises it as `ToolError`, which the last-resort catch 
skips. A deployment can instead add the compat middleware (or its deprecated 
`StructuredContentStripperMiddleware` subclass) *after* 
`GlobalErrorHandlerMiddleware`, which puts it inside that handler. In that 
order, a raw FastMCP/pydantic argument `ValidationError` (for example, a tool 
with a required `request` param called with `{}`) reaches the last-resort catch 
first. The hook fires there with the `validation_message()` text, e.g. 
`Validation error in get_dataset_info: request: Field required`, and the outer 
handler never sees the exception. Every malformed agent call then becomes an 
error-level event in the error tracker.
   
   **Fix.** Apply the same classification in the last-resort catch that 
`GlobalErrorHandlerMiddleware` already uses. The hook now fires only for 
system-class errors: `_is_user_error`, refined by 
`_datasource_error_is_user_error`, so a missing table is not paged but an 
unreachable database is. A small helper, `_is_user_error_for_reporting`, is 
shared by both call sites so the decision can't drift between them. 
`GlobalErrorHandlerMiddleware` keeps its existing behavior through the helper. 
The client-facing `ToolResult(is_error=True)` and its text are unchanged.
   
   #### Tradeoffs
   
   This changes what gets reported when an error reaches the last-resort catch. 
User-class errors that reach it (`ValidationError`, FastMCP `ValidationError`, 
`ValueError`, `PermissionError`, `ObjectNotFoundError`, sub-500 
`SupersetException`s, datasource "missing object" errors, ...) **no longer fire 
`MCP_ERROR_HOOK`**, so they no longer reach Sentry or any other tracker. Before 
this change they were paged. They are still returned to the client as 
`is_error` results. System-class errors are unchanged: they still invoke the 
hook from this catch with the same context dict (`user_id`/`duration_ms` still 
`None`). `ToolError` is still never hooked here.
   
   #### Follow-ups
   
   - Some deployments register the compat middleware innermost, unlike 
`server.py` where it is outermost. This PR intentionally leaves that ordering 
alone. The middleware strips `outputSchema`/`structuredContent`, and moving it 
is a separate decision for those deployments. With this fix, both orderings 
report the same way.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   N/A (backend only). Repro: a FastMCP server with 
`GlobalErrorHandlerMiddleware` added first and 
`StructuredContentStripperMiddleware` added last, and a tool 
`get_dataset_info(request: Req)` called with `{}`:
   
   | | is_error | client text | `MCP_ERROR_HOOK` calls |
   |---|---|---|---|
   | before, compat innermost | True | `Error: Validation error in 
get_dataset_info: request: Field required` | **1** (fastmcp `ValidationError`, 
`duration_ms=None`) |
   | after, compat innermost | True | same | **0** |
   | after, `server.py` order (compat outermost) | True | same | 0 (as before) |
   
   ### TESTING INSTRUCTIONS
   
   ```
   pytest tests/unit_tests/mcp_service/test_middleware.py \
          tests/unit_tests/mcp_service/test_error_classification.py \
          tests/unit_tests/mcp_service/test_worker_metadata_pool.py
   ```
   
   New tests in `TestToolResultCompatibilityErrorHook`:
   - `test_does_not_invoke_hook_for_user_error`, parametrized over pydantic 
`ValidationError`, FastMCP `ValidationError`, `ValueError`, `PermissionError` 
and a missing-table `SupersetErrorException`. Each one: hook not called, 
`is_error=True`, and the text is identical to what the catch produced before.
   - `test_invokes_hook_for_datasource_connection_failure`: an unreachable 
database is still paged.
   - `test_invalid_arguments_do_not_page_when_registered_inside_handler`: end 
to end through a real FastMCP `Client`, with the compat middleware added after 
`GlobalErrorHandlerMiddleware`.
   - The existing `test_invokes_hook_for_exception_bypassing_error_handler` 
(`RuntimeError`) still checks that system errors invoke the hook with the full 
context contract.
   
   Results:
   - The 6 new user-error/end-to-end cases fail without the fix and pass with 
it. All 11 tests in the class pass.
   - The three files above: 230 passed.
   - Full `tests/unit_tests/mcp_service/` locally: 7450 passed. The only 
failures are 36 in `dataset/tool/test_query_dataset.py`, which fail identically 
on unmodified master because the local `freezegun` is too old for 
`real_asyncio`.
   - `pre-commit run` on the changed files passes: ruff 0.9.7, ruff-format, 
mypy, pylint, auto-walrus.
   
   ### 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
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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