Skip to content

Conversation

@terylt
Copy link
Collaborator

@terylt terylt commented Nov 26, 2025

Closes #1495

Enable Plugin Context Sharing Across Hook Types

Summary

Implements cross-hook context sharing, allowing plugins to
maintain state across different hook types (HTTP → RBAC →
MCP hooks) within a single request.

Changes

Core Implementation:

  • Store plugin_context_table and plugin_global_context in
    Request.state (FastAPI pattern)
  • Pass contexts from HttpAuthMiddleware → RBAC decorators →
    service layer
  • Reuse existing GlobalContext across hooks for consistency
  • Properly populate GlobalContext fields (server_id,
    tenant_id, user) where available

Service Layer Updates:

  • tool_service.invoke_tool(): Added plugin context
    parameters, passes to hooks
  • resource_service.read_resource(): Added plugin context
    parameters, passes to hooks
  • prompt_service.get_prompt(): Added plugin context
    parameters, passes to hooks

Route Handler Updates:

  • Extract plugin contexts from request.state in all route
    handlers
  • Pass contexts to service methods
  • Updated RPC endpoints for tools, resources, and prompts

Testing:

  • Fixed test assertions to include new plugin context
    parameters
  • Added CrossHookContextPlugin test fixture demonstrating
    context flow
  • Added integration tests validating HTTP → Tool, HTTP →
    Resource, HTTP → Prompt context sharing

Benefits

  • Plugins can now share state across the entire request
    lifecycle
  • Enables use cases like: request tracking, risk scoring,
    audit trails, custom auth flows
  • Maintains plugin isolation (each plugin only sees its own
    context)
  • No threading/caching concerns - uses request-scoped state

Files Changed

  • mcpgateway/middleware/rbac.py
  • mcpgateway/services/{tool,resource,prompt}_service.py
  • mcpgateway/main.py
  • tests/unit/mcpgateway/test_main.py
  • tests/integration/test_cross_hook_context_sharing.py (new)
  • tests/unit/mcpgateway/plugins/fixtures/plugins/cross_hook_
    context.py (new)

@araujof araujof self-requested a review November 26, 2025 14:55
@araujof araujof added enhancement New feature or request plugins labels Nov 26, 2025
Copy link
Member

@araujof araujof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Nice job!

Summary
This PR adds support for sharing context data across different plugin hook types, enabling plugins to maintain state throughout the request
lifecycle.

Impact

  • Enables stateful plugins: Plugins can now share data across different lifecycle stages
  • Backward compatible: Falls back to creating new contexts when middleware contexts aren't available
  • Consistent context handling: Global context reused throughout request lifecycle

@araujof araujof marked this pull request as ready for review November 26, 2025 14:57
@terylt terylt marked this pull request as draft November 26, 2025 15:06
@terylt terylt marked this pull request as ready for review November 26, 2025 18:57
Teryl Taylor and others added 3 commits November 27, 2025 17:03
Signed-off-by: Teryl Taylor <terylt@ibm.com>
Signed-off-by: Teryl Taylor <terylt@ibm.com>
Update unit test assertions to use ANY for plugin_global_context
parameter since the HttpAuthMiddleware now correctly creates and
stores a GlobalContext in request.state for cross-hook context
sharing.

Also fix integration test bugs where service constructors were
incorrectly passed plugin_manager as a keyword argument (services
get the plugin manager from a global singleton, not constructor).

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai force-pushed the feat/support_context_across_hooks branch from 1b18297 to 1e03513 Compare November 27, 2025 17:21
@crivetimihai crivetimihai merged commit ccc8a26 into main Nov 27, 2025
45 checks passed
@crivetimihai crivetimihai deleted the feat/support_context_across_hooks branch November 27, 2025 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request plugins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Context set from one hook is not available in another hook

4 participants