Hook System
6. Hook SystemΒΆ
6.1 Hook Execution FlowΒΆ
The hook execution flow implements a priority-based pipeline that processes MCP requests through multiple plugin layers before reaching core gateway logic. This architecture ensures predictable plugin execution order while enabling comprehensive request/response transformation and policy enforcement.
sequenceDiagram
participant Client
participant Gateway
participant PM as PluginManager
participant P1 as Plugin 1 (Priority 10)
participant P2 as Plugin 2 (Priority 20)
participant Core as Core Logic
Client->>Gateway: MCP Request
Gateway->>PM: Execute Hook (e.g., tool_pre_invoke)
PM->>P1: Execute (higher priority)
P1-->>PM: Result (continue=true, modified_payload)
PM->>P2: Execute with modified payload
P2-->>PM: Result (continue=true)
PM-->>Gateway: Final Result
alt Continue Processing
Gateway->>Core: Execute Core Logic
Core-->>Gateway: Response
Gateway-->>Client: Success Response
else Block Request
Gateway-->>Client: Violation Response
end
6.1.1 Execution Flow BreakdownΒΆ
Phase 1: Request Reception & Hook Identification
- Client Request: MCP client sends request (tool invocation, prompt fetch, resource access) to the gateway
- Hook Selection: Gateway identifies the appropriate hook type based on the request (e.g.,
tool_pre_invoke
for tool calls) - Plugin Manager Invocation: Gateway delegates hook execution to the Plugin Manager with request payload
Phase 2: Priority-Based Plugin Execution
- Plugin Discovery: Plugin Manager retrieves all plugins registered for the specific hook type
- Priority Sorting: Plugins are sorted in ascending priority order (lower numbers execute first)
- Conditional Filtering: Plugins with conditions are filtered based on current request context (user, tenant, server, etc.)
Phase 3: Sequential Plugin Processing
- First Plugin Execution: Highest priority plugin (P1, priority 10) executes with original payload
- Result Evaluation: Plugin returns
PluginResult
indicating whether to continue processing - Payload Chain: If P1 modifies the payload, the modified version is passed to the next plugin
- Second Plugin Execution: Next priority plugin (P2, priority 20) executes with potentially modified payload
- Continue Chain: Process repeats for all remaining plugins in priority order
Phase 4: Flow Control Decision
- Aggregated Result: Plugin Manager combines all plugin results and determines final action
- Continue vs Block: Based on plugin results, request either continues to core logic or is blocked
Phase 5: Request Resolution
- Continue Path: If all plugins allow processing, request continues to core gateway logic
- Core logic executes the actual MCP operation (tool invocation, prompt rendering, resource fetching)
- Success response is returned to client
- Block Path: If any plugin blocks the request with a violation
- Request processing stops immediately
- Violation details are returned to client as an error response
6.1.2 Plugin Interaction PatternsΒΆ
Payload Transformation Chain:
Example Flow for Tool Pre-Invoke:
- Client calls
file_reader
tool with path argument - Gateway triggers
tool_pre_invoke
hook - Security Plugin (Priority 10): Validates file path, blocks access to
/etc/passwd
- Sanitization Plugin (Priority 20): Never executes (request blocked)
- Result: Client receives "Access Denied" error
Alternative Success Flow:
- Client calls
file_reader
tool with path./documents/report.txt
- Security Plugin (Priority 10): Validates path, allows access, normalizes path
- Sanitization Plugin (Priority 20): Adds read timeout, limits file size
- Core Logic: Executes file reading with sanitized parameters
- Result: Client receives file contents
6.1.3 Error Handling and ResilienceΒΆ
Plugin Error Isolation:
- Plugin execution errors don't crash other plugins or the gateway
- Failed plugins are logged and handled based on their execution mode:
- Enforce Mode: Plugin errors block the request
- Permissive Mode: Plugin errors are logged but request continues
- Enforce Ignore Error Mode: Plugin violations block, but technical errors are ignored
Timeout Protection:
- Each plugin execution is wrapped with configurable timeouts (default 30 seconds)
- Timed-out plugins are treated as errors according to their execution mode
- External plugins may have longer timeout allowances due to network latency
Context Preservation:
- Plugin contexts are preserved across the execution chain
- State set by early plugins is available to later plugins
- Global context maintains request-level information throughout the flow
This execution model ensures predictable behavior, comprehensive security coverage, and operational resilience while maintaining the flexibility to implement complex policy enforcement and content transformation workflows.
6.2 Plugin Execution PriorityΒΆ
- Plugins execute in ascending priority order (lower number = higher priority)
- Priority Ranges (recommended):
1-50
: Critical security plugins (authentication, PII filtering)51-100
: Content filtering and validation101-200
: Transformations and enhancements201+
: Logging and monitoring
6.3 Hook RegistrationΒΆ
# Plugin registers for specific hooks via configuration
hooks: list[HookType] = [
HookType.PROMPT_PRE_FETCH,
HookType.TOOL_PRE_INVOKE,
HookType.TOOL_POST_INVOKE
]
# Plugin Manager routes hooks to registered plugins
plugins = registry.get_plugins_for_hook(HookType.TOOL_PRE_INVOKE)
6.4 Conditional ExecutionΒΆ
Plugins can specify conditions for when they should execute:
class PluginCondition(BaseModel):
"""Conditions for plugin execution"""
server_ids: Optional[set[str]] = None # Execute only for specific servers
tenant_ids: Optional[set[str]] = None # Execute only for specific tenants
tools: Optional[set[str]] = None # Execute only for specific tools
prompts: Optional[set[str]] = None # Execute only for specific prompts
resources: Optional[set[str]] = None # Execute only for specific resources
user_patterns: Optional[list[str]] = None # Execute for users matching patterns
content_types: Optional[list[str]] = None # Execute for specific content types
6.5 Hook Reference DocumentationΒΆ
The plugin framework provides two main categories of hooks, each documented in detail in separate files:
MCP Security HooksΒΆ
For detailed information about MCP protocol security hooks including payload structures, examples, and use cases, see:
π MCP Security Hooks Reference
This document covers the eight core MCP protocol hooks:
- HTTP Pre/Post-Forwarding Hooks - Header processing and authentication
- Prompt Pre/Post-Fetch Hooks - Input validation and content filtering
- Tool Pre/Post-Invoke Hooks - Parameter validation and result processing
- Resource Pre/Post-Fetch Hooks - URI validation and content transformation
Gateway Administrative HooksΒΆ
For detailed information about gateway management and administrative hooks, see:
π Gateway Administrative Hooks Reference
This document covers administrative operation hooks:
- Server Management Hooks - Registration, updates, deletion, activation
- Gateway Federation Hooks - Peer gateway management (Future)
- A2A Agent Hooks - Agent-to-Agent integration management (Future)
- Entity Lifecycle Hooks - Tool, resource, and prompt registration (Future)