[BUG][PLUGINS]: Plugin context not shared between HTTP_PRE_REQUEST and TOOL_PRE_INVOKE on /mcp endpoints
🐞 Bug Summary
Plugin context is not shared between HTTP_PRE_REQUEST and TOOL_PRE_INVOKE for /mcp endpoints.
🧩 Affected Component
Select the area of the project impacted:
- [ ]
mcpgateway- API - [ ]
mcpgateway- UI (admin panel) - [ ]
mcpgateway.wrapper- stdio wrapper - [x] Federation or Transports
- [ ] CLI, Makefiles, or shell scripts
- [ ] Container setup (Docker/Podman/Compose)
- [x] Other (Plugins)
Tried to write a custom authorization plugin for /mcp endpoints passing state between hooks. Found that context inside tool_pre_invoke is always empty.
What was discovered during debug session:
- At streamablehttp_transport.py#L1138
plugin_context_tableandplugin_global_contextare not passed (and never fetched from request.state). - At tool_service.py#L3823,
local_contexts=None
Similar issue 1495; probably should have been fixed in corresponding pull 1514 but no units for "/mcp"
🧩 Additional Context (optional)
MCP_REQUIRE_AUTH=true
Thanks @katichev — confirmed. The mcpgateway/transports/ directory has zero references to plugin_context_table or plugin_global_context. Auth middleware sets these on request.state (in auth.py:1055-1058) and tool_service.py accepts them as parameters, but the streamable HTTP transport never extracts and forwards them. PR #1514 (for #1495) only covered the direct API paths, not the /mcp transport path. Tagging as SHOULD for Release 1.1.0.
Still reproducible on main (eaf295e64): mcpgateway/transports/ contains zero references to plugin_context_table / plugin_global_context, so call_tool() reaches invoke_tool() without them and the hook manager gets local_contexts=None.
Opened #6140 with a fix and three regression tests. It reads both contexts from the ASGI scope (which is where request.state writes them) rather than from a ContextVar, mirroring path 2 of _get_request_context_or_default().
One note for @araujof / @jonpspri, since #3915 was closed in favour of the CPEX gated-extensions approach: main has kept standardising on plugin_context_table in the meantime — it is threaded through ~13 REST call sites in main.py, and middleware/rbac.py now uses it to share context with HTTP_AUTH_CHECK_PERMISSION — leaving /mcp as the only path where cross-hook state is dropped. #6140 closes that gap with the mechanism already in use, but I'm happy to redo it as a gated extension if that is still the preferred direction.
get_prompt / read_resource on the same transport have the same gap; I left them out to keep the PR to one concern and can file a separate issue.