fix(mcp): wrap LoggingMiddleware.on_message event_logger in try/except#38560
Conversation
LoggingMiddleware.on_message() fires on all MCP messages including the initialize handshake, but event_logger.log() requires Flask app context which isn't available during protocol-level messages. This causes the MCP server to reject the initialize request with "Working outside of application context". Wrap the event_logger.log() call with try/except RuntimeError to gracefully skip logging when no Flask context is available.
Code Review Agent Run #27be06Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Sequence DiagramThis PR updates MCP message logging so that missing Flask application context during the initialize handshake no longer breaks the flow, allowing the handshake to succeed while still logging messages when context is available. sequenceDiagram
participant Client
participant MCPServer
participant Middleware
participant EventLogger
Client->>MCPServer: Send initialize message
MCPServer->>Middleware: on_message with context
Middleware->>EventLogger: Log MCP message
alt Flask context available
EventLogger-->>Middleware: Log entry recorded
else No Flask context
EventLogger-->>Middleware: RuntimeError raised
Middleware->>Middleware: Catch error and skip logging
end
Middleware->>MCPServer: call_next and continue handling
MCPServer-->>Client: Initialize response success
Generated by CodeAnt AI |
…lask context The on_call_tool finally block runs event_logger.log() AFTER mcp_auth_hook pops the Flask app context, causing RuntimeError that destroys the successful tool result. Wrap all event_logger calls with try/except RuntimeError.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #38560 +/- ##
==========================================
- Coverage 64.85% 64.44% -0.42%
==========================================
Files 1817 2529 +712
Lines 72183 128780 +56597
Branches 22975 29677 +6702
==========================================
+ Hits 46815 82986 +36171
- Misses 25368 44351 +18983
- Partials 0 1443 +1443
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…ing-middleware-app-context
Replace exception-driven control flow with explicit Flask context check. This is cleaner because: - Explicit intent: only log when Flask context exists - Won't swallow unrelated RuntimeErrors - Avoids exception-driven control flow anti-pattern
Code Review Agent Run #4e5e81Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
apache#38560) (cherry picked from commit 6d7cfac)
SUMMARY
After registering
LoggingMiddlewarein the MCP server startup, the MCP server started failing theinitializehandshake with clients (e.g. Claude Code) with:Root cause:
LoggingMiddlewarefires on ALL MCP messages including the protocol-levelinitializehandshake and theon_call_tool()finally block (which runs aftermcp_auth_hookpops Flask context). Bothon_message()andon_call_tool()callevent_logger.log()which requires Flask app context, but:initializethere is no Flask app context (mcp_auth_hookonly pushes context for tool calls, not protocol messages)on_call_tool()'s finally block, the Flask app context has already been popped bymcp_auth_hookFix: Use
flask.has_app_context()guard check beforeevent_logger.log()calls in bothon_call_tool()andon_message(). This is explicit about intent, avoids exception-driven control flow, and won't accidentally swallow unrelatedRuntimeErrors.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A - backend fix
TESTING INSTRUCTIONS
superset mcp run --port 5008 --debug/mcp)initializehandshake completes successfully (noWorking outside of application contexterror)event_loggerwhen Flask app context is availableADDITIONAL INFORMATION